Skip to content

Commit

Permalink
Add Better Currency System
Browse files Browse the repository at this point in the history
  • Loading branch information
Oribuin committed Nov 25, 2024
1 parent b0bbd14 commit cf67461
Show file tree
Hide file tree
Showing 10 changed files with 504 additions and 29 deletions.
90 changes: 74 additions & 16 deletions src/main/java/xyz/oribuin/fishing/api/economy/Cost.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,91 @@
package xyz.oribuin.fishing.api.economy;

/**
* Represents a cost for an item
*
* @param currency The currency type
* @param amount The amount of currency
* @param symbol The symbol of the currency
*/
public record Cost(Currency currency, int amount, String symbol) {
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;

public class Cost implements Currency {

private final Currency currency;
private Number price;

/**
* Create a new cost object
* Create a new cost object with a currency and amount
*
* @param currency The currency type
* @param amount The amount of currency
* @param currency The currency to use
* @param price The amount of currency to use
*/
public Cost(@NotNull Currency currency, @NotNull Number price) {
this.currency = currency;
this.price = price;
}

/**
* @return The name of the currency
*/
public Cost(Currency currency, int amount) {
this(currency, amount, "");
@Override
public String name() {
return this.currency.name();
}

/**
* Check if the player has enough currency
* Get the amount of currency the player has
*
* @param player The player to check
*/
@Override
public @NotNull Number amount(@NotNull OfflinePlayer player) {
return this.currency.amount(player);
}

/**
* Check if the player has enough currency to purchase an item
*
* @param player The player who is purchasing the item
* @param amount The amount to check
*
* @return If the player has enough currency
*/
public boolean has(int amount) {
return this.amount >= amount;
@Override
public boolean has(@NotNull OfflinePlayer player, @NotNull Number amount) {
return this.currency.has(player, amount);
}

/**
* Give the player an amount of currency
*
* @param player The player to give the currency to
* @param amount The amount to give
*/
@Override
public void give(@NotNull OfflinePlayer player, @NotNull Number amount) {
this.currency.give(player, amount);
}

/**
* Take an amount of currency from the player
*
* @param player The player to take the currency from
* @param amount The amount to take
*/
@Override
public void take(@NotNull OfflinePlayer player, @NotNull Number amount) {
this.currency.take(player, amount);
}

/**
* @return The price of the item
*/
public Number price() {
return this.price;
}

/**
* Add a price to the item cost
*
* @param price The price to add
*/
public void price(Number price) {
this.price = price;
}

}
28 changes: 28 additions & 0 deletions src/main/java/xyz/oribuin/fishing/api/economy/Currencies.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package xyz.oribuin.fishing.api.economy;

import xyz.oribuin.fishing.api.economy.impl.EntropyCurrency;
import xyz.oribuin.fishing.api.economy.impl.FishExpCurrency;
import xyz.oribuin.fishing.api.economy.impl.PlayerExpCurrency;
import xyz.oribuin.fishing.api.economy.impl.SkillpointCurrency;
import xyz.oribuin.fishing.api.economy.impl.VaultCurrency;

import java.util.PrimitiveIterator;

public enum Currencies {
ENTROPY(new EntropyCurrency()),
FISH_EXP(new FishExpCurrency()),
PLAYER_XP(new PlayerExpCurrency()),
SKILL_POINTS(new SkillpointCurrency()),
VAULT(new VaultCurrency());

private final Currency currency;

Currencies(Currency currency) {
this.currency = currency;
}

public Currency get() {
return this.currency;
}

}
64 changes: 57 additions & 7 deletions src/main/java/xyz/oribuin/fishing/api/economy/Currency.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,59 @@
package xyz.oribuin.fishing.api.economy;

public enum Currency {
VAULT, // Vault economy plugin
ENTROPY, // Entropy
FISH_EXP, // Fish experience
PLAYER_XP, // Player experience
SKILL_POINTS, // Skill points
}
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import xyz.oribuin.fishing.FishingPlugin;
import xyz.oribuin.fishing.manager.base.DataManager;
import xyz.oribuin.fishing.storage.Fisher;

public interface Currency {

/**
* @return The name of the currency
*/
String name();

/**
* Get the amount of currency the player has
*
* @param player The player to check
*/
@NotNull
Number amount(@NotNull OfflinePlayer player);

/**
* Check if the player has enough currency to purchase an item
*
* @param player The player who is purchasing the item
* @param amount The amount to check
*
* @return If the player has enough currency
*/
boolean has(@NotNull OfflinePlayer player, @NotNull Number amount);

/**
* Give the player an amount of currency
*
* @param player The player to give the currency to
* @param amount The amount to give
*/
void give(@NotNull OfflinePlayer player, @NotNull Number amount);

/**
* Take an amount of currency from the player
*
* @param player The player to take the currency from
* @param amount The amount to take
*/
void take(@NotNull OfflinePlayer player, @NotNull Number amount);

/**
* Get the Fisher object for the player
*
* @param player The player to get the Fisher object for
*/
default Fisher fisher(@NotNull OfflinePlayer player) {
return FishingPlugin.get().getManager(DataManager.class).get(player.getUniqueId());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package xyz.oribuin.fishing.api.economy.impl;

import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import xyz.oribuin.fishing.api.economy.Currency;
import xyz.oribuin.fishing.storage.Fisher;

public class EntropyCurrency implements Currency {

/**
* @return The name of the currency
*/
@Override
public String name() {
return "entropy";
}

/**
* Get the amount of currency the player has
*
* @param player The player to check
*/
@Override
public @NotNull Number amount(@NotNull OfflinePlayer player) {
return this.fisher(player).entropy();
}

/**
* Check if the player has enough currency to purchase an item
*
* @param player The player who is purchasing the item
* @param amount The amount to check
*
* @return If the player has enough currency
*/
@Override
public boolean has(@NotNull OfflinePlayer player, @NotNull Number amount) {
return this.amount(player).intValue() >= amount.intValue();
}

/**
* Give the player an amount of currency
*
* @param player The player to give the currency to
* @param amount The amount to give
*/
@Override
public void give(@NotNull OfflinePlayer player, @NotNull Number amount) {
Fisher fisher = this.fisher(player);
fisher.entropy(fisher.entropy() + amount.intValue());
}

/**
* Take an amount of currency from the player
*
* @param player The player to take the currency from
* @param amount The amount to take
*/
@Override
public void take(@NotNull OfflinePlayer player, @NotNull Number amount) {
Fisher fisher = this.fisher(player);
fisher.entropy(fisher.entropy() - amount.intValue());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package xyz.oribuin.fishing.api.economy.impl;

import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import xyz.oribuin.fishing.api.economy.Currency;
import xyz.oribuin.fishing.storage.Fisher;

public class FishExpCurrency implements Currency {

/**
* @return The name of the currency
*/
@Override
public String name() {
return "fish_exp";
}

/**
* Get the amount of currency the player has
*
* @param player The player to check
*/
@Override
public @NotNull Number amount(@NotNull OfflinePlayer player) {
return this.fisher(player).experience();
}

/**
* Check if the player has enough currency to purchase an item
*
* @param player The player who is purchasing the item
* @param amount The amount to check
*
* @return If the player has enough currency
*/
@Override
public boolean has(@NotNull OfflinePlayer player, @NotNull Number amount) {
return this.amount(player).intValue() >= amount.intValue();
}

/**
* Give the player an amount of currency
*
* @param player The player to give the currency to
* @param amount The amount to give
*/
@Override
public void give(@NotNull OfflinePlayer player, @NotNull Number amount) {
Fisher fisher = this.fisher(player);
fisher.experience(fisher.experience() + amount.intValue());
}

/**
* Take an amount of currency from the player
*
* @param player The player to take the currency from
* @param amount The amount to take
*/
@Override
public void take(@NotNull OfflinePlayer player, @NotNull Number amount) {
Fisher fisher = this.fisher(player);
fisher.experience(fisher.experience() - amount.intValue());
}

}
Loading

0 comments on commit cf67461

Please sign in to comment.