Corrupt
Everything is Corrupt
- Oct 20, 2020
- 116
- 15
- 0
A Better Currency System, universal to most systems
This is a Enum that overlaps all currencies and uses Functions and Predicates to perform whats is needed. This currency system can be an overhaul to most currencies, more centered around shops that are cluttered with currency codes. Being used in my new server, trying to help the community with a tad bit of extra help.
Code:
[MENTION=240694]alla[/MENTION]rgsConstructor
@Getter
public enum Currencies {
GOLD("Gold", player -> amount -> player.getInventory().contains(995, amount),
player -> amount -> player.getInventory().delete(995, amount),
player -> amount -> player.getInventory().add(995, amount)),
QUEST("Quest", player -> amount-> player.getWallet().hasEnough(PlayerWallet.QUEST, amount),
player -> amount -> player.getWallet().takeCoins(amount, PlayerWallet.QUEST),
player -> amount -> player.getWallet().addCoins(amount, PlayerWallet.QUEST)),
DONATOR("Donator", player -> amount -> player.getWallet().hasEnough(PlayerWallet.DONATOR, amount),
player -> amount -> player.getWallet().takeCoins(amount, PlayerWallet.DONATOR),
player -> amount -> player.getWallet().addCoins(amount, PlayerWallet.DONATOR)),
LOYALTY("Loyalty", player -> amount -> player.getWallet().hasEnough(PlayerWallet.LOYALTY, amount),
player -> amount -> player.getWallet().takeCoins(amount, PlayerWallet.LOYALTY),
player -> amount -> player.getWallet().addCoins(amount, PlayerWallet.LOYALTY))
;
private final String name;
private final Function<Player, Predicate<Integer>> predicate, take, add;
public boolean hasEnough(Player player, int amount){
return predicate.apply(player).test(amount);
}
public boolean take(Player player, int amount){
return take.apply(player).test(amount);
}
public boolean add(Player player, int amount){
return add.apply(player).test(amount);
}
}
You can use this quite simply as follows :
Code:
Currencies currency = Currencies.valueOf("GOLD"); // Change this to example : shop.getCurrency()
Currencies currency = Currencies.GOLD; // Direct usage
Than you can easily use this to check if they have enough, add or delete the amount. Example code :
Code:
Optional<Currencies> currency = Optional.of(Currencies.valueOf(shop.getCurrency()));
currency.ifPresent(c -> {
if(c.hasEnough(player, amount)){
if(c.take(player, amount)){
// do something here
}
}
});
Simple, yet effective to what it needs to do. You can also use this as a base to add/remove coins or currencies through other functions as well. Will release small systems like this as I go along with building my new server, so hopefully you will enjoy using it!