Skip to content

Commit 5def5ba

Browse files
author
Jan Kluka
committed
1.2
Implemented Currency API
1 parent b898cfa commit 5def5ba

8 files changed

Lines changed: 99 additions & 40 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>dev.drawethree.xprison</groupId>
66
<artifactId>X-PrisonAPI</artifactId>
7-
<version>1.1</version>
7+
<version>1.2-SNAPSHOT</version>
88
<packaging>jar</packaging>
99

1010
<name>X-PrisonAPI</name>

src/main/java/dev/drawethree/xprison/api/XPrisonAPI.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import dev.drawethree.xprison.api.autominer.XPrisonAutoMinerAPI;
44
import dev.drawethree.xprison.api.autosell.XPrisonAutoSellAPI;
55
import dev.drawethree.xprison.api.bombs.XPrisonBombsAPI;
6+
import dev.drawethree.xprison.api.currency.XPrisonCurrencyAPI;
67
import dev.drawethree.xprison.api.enchants.XPrisonEnchantsAPI;
78
import dev.drawethree.xprison.api.gangs.XPrisonGangsAPI;
89
import dev.drawethree.xprison.api.gems.XPrisonGemsAPI;
@@ -124,6 +125,14 @@ public interface XPrisonAPI {
124125
@NotNull
125126
XPrisonBombsAPI getBombsApi();
126127

128+
/**
129+
* Gets the currency API.
130+
*
131+
* @return the Currency API instance
132+
*/
133+
@NotNull
134+
XPrisonCurrencyAPI getCurrencyApi();
135+
127136
/**
128137
* Gets the singleton instance of the XPrisonAPI.
129138
*
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package dev.drawethree.xprison.api.blocks;
2+
3+
/**
4+
* API interface for the AutoSell feature.
5+
* Provides methods for managing selling blocks, pricing, and player earnings.
6+
*/
7+
public interface XPrisonBlocksAPI {
8+
9+
}

src/main/java/dev/drawethree/xprison/api/currency/CurrencyType.java

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package dev.drawethree.xprison.api.currency;
2+
3+
import dev.drawethree.xprison.api.shared.currency.enums.XPrisonCurrency;
4+
5+
import java.util.Collection;
6+
7+
/**
8+
* API for interacting with XPrison's currency system.
9+
* <p>
10+
* Allows external plugins to register new currencies or retrieve existing ones.
11+
*/
12+
public interface XPrisonCurrencyAPI {
13+
14+
/**
15+
* Registers a new custom currency to the system.
16+
* <p>
17+
* If a currency with the same name (case-insensitive) already exists, it will be overwritten.
18+
*
19+
* @param currency The {@link XPrisonCurrency} implementation to register.
20+
*/
21+
void registerCurrency(XPrisonCurrency currency);
22+
23+
/**
24+
* Retrieves a currency by its name.
25+
*
26+
* @param name The name of the currency (case-insensitive).
27+
* @return The matching {@link XPrisonCurrency}, or null if not found.
28+
*/
29+
XPrisonCurrency getCurrency(String name);
30+
31+
/**
32+
* Returns all registered currencies.
33+
*
34+
* @return A collection of all {@link XPrisonCurrency} objects.
35+
*/
36+
Collection<XPrisonCurrency> getAllCurrencies();
37+
}

src/main/java/dev/drawethree/xprison/api/enchants/model/XPrisonEnchantment.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dev.drawethree.xprison.api.enchants.model;
22

3-
import dev.drawethree.xprison.api.currency.CurrencyType;
3+
import dev.drawethree.xprison.api.shared.currency.enums.XPrisonCurrency;
44

55
/**
66
* Represents a custom enchantment in the XPrison system.
@@ -89,9 +89,12 @@ public interface XPrisonEnchantment {
8989
void unload();
9090

9191
/**
92-
* Gets the type of currency used to purchase this enchantment.
92+
* Gets the currency used to purchase or upgrade this enchantment.
93+
* <p>
94+
* This defines which type of currency (e.g., Tokens, Gems, Vault/Money)
95+
* will be consumed when a player interacts with this enchant.
9396
*
94-
* @return The currency type (e.g., TOKENS, GEMS, VAULT).
97+
* @return The {@link XPrisonCurrency} implementation representing the currency type.
9598
*/
96-
CurrencyType getCurrencyType();
99+
XPrisonCurrency getCurrency();
97100
}

src/main/java/dev/drawethree/xprison/api/enchants/model/XPrisonEnchantmentBase.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import com.google.gson.JsonObject;
44
import com.google.gson.JsonParser;
5-
import dev.drawethree.xprison.api.currency.CurrencyType;
5+
import dev.drawethree.xprison.api.XPrisonAPI;
6+
import dev.drawethree.xprison.api.shared.currency.enums.XPrisonCurrency;
67
import dev.drawethree.xprison.api.utils.JsonUtils;
78
import lombok.Getter;
89
import lombok.Setter;
@@ -34,7 +35,7 @@ public abstract class XPrisonEnchantmentBase implements XPrisonEnchantment, Refu
3435
protected long baseCost;
3536
protected long increaseCost;
3637
protected XPrisonEnchantmentGuiProperties guiProperties;
37-
protected CurrencyType currencyType;
38+
protected XPrisonCurrency currencyType;
3839
protected boolean refundEnabled;
3940
protected int refundGuiSlot;
4041
protected double refundPercentage;
@@ -77,7 +78,7 @@ private void loadBaseProperties(JsonObject config) {
7778
this.maxLevel = JsonUtils.getRequiredInt(config,"maxLevel");
7879
this.baseCost = JsonUtils.getRequiredLong(config, "initialCost");
7980
this.increaseCost = JsonUtils.getRequiredLong(config,"increaseCostBy");
80-
this.currencyType = CurrencyType.valueOf(JsonUtils.getOptionalString(config,"currency", CurrencyType.TOKENS.name()));
81+
this.currencyType = XPrisonAPI.getInstance().getCurrencyApi().getCurrency(JsonUtils.getOptionalString(config,"currency", "Tokens"));
8182

8283
JsonObject refundObject = JsonUtils.getRequiredObject(config,"refund");
8384
this.refundEnabled = JsonUtils.getRequiredBoolean(refundObject,"enabled");

src/main/java/dev/drawethree/xprison/api/shared/currency/enums/XPrisonCurrency.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,67 @@
44

55
/**
66
* Represents a generic currency system in the XPrison API.
7+
* <p>
78
* This interface abstracts common currency operations such as querying,
8-
* adding, removing, and setting amounts for players.
9-
*
10-
* Implementations could represent different currency types like Tokens, Gems, etc.
9+
* adding, removing, and setting balances for players. Implementations
10+
* could represent different currency types like Tokens, Gems, Money, etc.
11+
* <p>
12+
* Methods that modify balances return a boolean indicating whether the
13+
* operation was successful (e.g., not blocked, not insufficient funds, etc.).
1114
*/
1215
public interface XPrisonCurrency {
1316

17+
/**
18+
* Gets the name of this currency.
19+
* <p>
20+
* This is typically used for display purposes, configuration keys,
21+
* or to distinguish between multiple currency types (e.g., "Tokens", "Gems", "Money").
22+
*
23+
* @return The name of the currency.
24+
*/
25+
String getName();
26+
1427
/**
1528
* Gets the current amount of this currency for the specified player.
1629
*
1730
* @param player The player (offline or online) whose balance to retrieve.
1831
* @return The amount of currency the player currently has.
1932
*/
20-
long getAmount(OfflinePlayer player);
33+
double getBalance(OfflinePlayer player);
2134

2235
/**
2336
* Adds a specified amount of currency to the player's balance.
2437
*
2538
* @param player The player to add currency to.
2639
* @param amount The amount of currency to add.
27-
* @param cause The cause or reason for adding currency (e.g. mining, reward).
40+
* @return true if the currency was successfully added, false otherwise.
2841
*/
29-
void add(OfflinePlayer player, long amount, ReceiveCause cause);
42+
boolean addBalance(OfflinePlayer player, double amount);
3043

3144
/**
3245
* Removes a specified amount of currency from the player's balance.
3346
*
3447
* @param player The player to remove currency from.
3548
* @param amount The amount of currency to remove.
36-
* @param cause The cause or reason for removing currency (e.g. purchase, penalty).
49+
* @return true if the currency was successfully removed, false (e.g., insufficient funds) otherwise.
3750
*/
38-
void remove(OfflinePlayer player, long amount, LostCause cause);
51+
boolean removeBalance(OfflinePlayer player, double amount);
3952

4053
/**
4154
* Sets the player's currency balance to a new specified amount.
4255
*
4356
* @param player The player whose balance to set.
4457
* @param amount The new amount to set.
58+
* @return true if the balance was successfully set, false otherwise.
59+
*/
60+
boolean setBalance(OfflinePlayer player, double amount);
61+
62+
/**
63+
* Checks if the specified player has at least the given amount of this currency.
64+
*
65+
* @param player The player whose balance to check.
66+
* @param amount The minimum amount to verify.
67+
* @return true if the player has at least the specified amount, false otherwise.
4568
*/
46-
void set(OfflinePlayer player, long amount);
69+
boolean has(OfflinePlayer player, double amount);
4770
}

0 commit comments

Comments
 (0)