ANY Universal Currency System

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!
 
I've seen people try a few different things for handling points/Currencies in general. This is pretty nice. May copy this and write my own system. Appreciate the snippet :)
 
enum naming, Currencies -> Currency

This is bad for a few reasons
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
                }
            }
        });

I believe enum valueOf returns an exception if you provide an invalid type, so Optional isn't going to save you here (even if it returned null, you'd have to use Optional#ofNullable). But I'd ditch the Optional entirely, shops are defined by you, they should always have a true currency type. In fact, I'd just have Shop#getCurrency be type Currency in the first place (if it needs to be saved somewhere, convert obj during serialization/deserialization instead)

Secondarily, hasEnough seems unecessary. take is already a boolean.

So it can basically be simplified to

Code:
if(shop.getCurrency().take(player, amount)) {
...
} else {
// don't have enough message...
}
 
What Joe said, Optional does not handle null values, just directly work with the Currencies,

Curenncies currency = shop.getCurrency();

if (currency.take(player, amount)) {
//yeet
}
 
Last edited:
replace ur
Code:
 private final Function<Player, Predicate<Integer>> predicate, take, add;

with this:

Code:
private Function<Player, Consumer<BiConsumer<Integer, Function<Player, Predicate<Integer>>>>> predicate, take, add;
 

Users who are viewing this thread (total: 1, members: 0, guests: 1)

Who read this thread (total members: 12)