Skip to content

Commit 5538384

Browse files
committed
v3.2.3 - 1.14 support, more config & shop options, and new api methods
1 parent e989716 commit 5538384

13 files changed

Lines changed: 137 additions & 20 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ plugins {
1717
}
1818

1919
group 'me.realized'
20-
version '3.2.2'
20+
version '3.2.3'
2121

2222
compileJava.options.encoding = 'UTF-8'
2323
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8

src/main/java/me/realized/tokenmanager/TokenManagerPlugin.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,30 @@ public void setTokens(final Player player, final long amount) {
237237
dataManager.set(player, amount);
238238
}
239239

240+
@Override
241+
public boolean addTokens(final Player player, final long amount) {
242+
final OptionalLong balance = getTokens(player);
243+
244+
if (!balance.isPresent()) {
245+
return false;
246+
}
247+
248+
setTokens(player, balance.getAsLong() + amount);
249+
return true;
250+
}
251+
252+
@Override
253+
public boolean removeTokens(final Player player, final long amount) {
254+
final OptionalLong balance = getTokens(player);
255+
256+
if (!balance.isPresent()) {
257+
return false;
258+
}
259+
260+
setTokens(player, balance.getAsLong() - amount);
261+
return true;
262+
}
263+
240264
@Override
241265
public void setTokens(final String key, final long amount) {
242266
dataManager.set(key, ModifyType.SET, amount, amount, true, null, Log::error);

src/main/java/me/realized/tokenmanager/api/TokenManager.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
public interface TokenManager {
4040

41+
4142
/**
4243
* Get a shop by name.
4344
*
@@ -46,6 +47,7 @@ public interface TokenManager {
4647
*/
4748
Optional<Shop> getShop(final String name);
4849

50+
4951
/**
5052
* Get a shop by inventory.
5153
*
@@ -54,6 +56,7 @@ public interface TokenManager {
5456
*/
5557
Optional<Shop> getShop(final Inventory inventory);
5658

59+
5760
/**
5861
* Get worth of the material defined in worth.yml.
5962
*
@@ -63,6 +66,7 @@ public interface TokenManager {
6366
*/
6467
OptionalLong getWorth(final Material material);
6568

69+
6670
/**
6771
* Get worth of the item defined in worth.yml.
6872
*
@@ -72,6 +76,7 @@ public interface TokenManager {
7276
*/
7377
OptionalLong getWorth(final ItemStack item);
7478

79+
7580
/**
7681
* Get online player's token balance.
7782
*
@@ -80,6 +85,7 @@ public interface TokenManager {
8085
*/
8186
OptionalLong getTokens(final Player player);
8287

88+
8389
/**
8490
* Set online player's token balance.
8591
*
@@ -88,53 +94,82 @@ public interface TokenManager {
8894
*/
8995
void setTokens(final Player player, final long amount);
9096

97+
98+
/**
99+
* Add an amount of tokens to online player's token balance.
100+
*
101+
* @param player Player to add tokens
102+
* @param amount Amount of tokens to add
103+
* @return true if add was successful, Otherwise false
104+
* @since v3.2.3
105+
*/
106+
boolean addTokens(final Player player, final long amount);
107+
108+
109+
/**
110+
* Remove an amount of tokens from online player's token balance.
111+
*
112+
* @param player Player to remove tokens
113+
* @param amount Amount of tokens to remove
114+
* @return true if remove was successful, Otherwise false
115+
* @since v3.2.3
116+
*/
117+
boolean removeTokens(final Player player, final long amount);
118+
119+
91120
/**
92121
* Set player's token balance.
93-
* @since v3.1.0
94122
*
95123
* @param key {@link UUID#toString()} if server is in online mode, otherwise name of the player
96124
* @param amount Amount to replace player's token balance
125+
* @since v3.1.0
97126
*/
98127
void setTokens(final String key, final long amount);
99128

129+
100130
/**
101131
* Add tokens to player's token balance.
102-
* @since v3.1.0
103132
*
104133
* @param key {@link UUID#toString()} if server is in online mode, otherwise name of the player
105134
* @param amount Amount to add to player's token balance
106135
* @param silent true to prevent sending message if target player is online
136+
* @since v3.1.0
107137
*/
108138
void addTokens(final String key, final long amount, final boolean silent);
109139

140+
110141
/**
111142
* Works the same as {@link #addTokens(String, long, boolean)} with silent defaulting to false.
112143
*
113144
* @see #addTokens(String, long, boolean)
114145
*/
115146
void addTokens(final String key, final long amount);
116147

148+
117149
/**
118150
* Remove tokens from player's token balance.
119-
* @since v3.1.0
120151
*
121152
* @param key {@link UUID#toString()} if server is in online mode, otherwise name of the player
122153
* @param amount Amount to remove from player's token balance
123154
* @param silent true to prevent sending message if target player is online
155+
* @since v3.1.0
124156
*/
125157
void removeTokens(final String key, final long amount, final boolean silent);
126158

159+
127160
/**
128161
* Works the same as {@link #removeTokens(String, long, boolean)} with silent defaulting to false.
129162
*
130163
* @see #removeTokens(String, long, boolean)
131164
*/
132165
void removeTokens(final String key, final long amount);
133166

167+
134168
/**
135169
* Reload the modules of the plugin.
136170
*
137171
* @return true if reload was successful, otherwise false
138172
*/
139173
boolean reload();
174+
140175
}

src/main/java/me/realized/tokenmanager/config/Config.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public class Config extends AbstractConfiguration<TokenManagerPlugin> {
6666
@Getter
6767
private boolean checkInventoryFull;
6868
@Getter
69+
private boolean logPurchases;
70+
@Getter
6971
private boolean mysqlEnabled;
7072
@Getter
7173
private String mysqlUsername;
@@ -120,6 +122,7 @@ protected void loadValues(FileConfiguration configuration) throws Exception {
120122
confirmPurchaseCancel = configuration.getString("shop.confirm-purchase-gui.cancel-button", "STAINED_CLAY:14 1 name:&c&lCANCEL");
121123
clickDelay = configuration.getInt("shop.click-delay", 0);
122124
checkInventoryFull = configuration.getBoolean("shop.check-inventory-full", false);
125+
logPurchases = configuration.getBoolean("shop.log-purchases", false);
123126
mysqlEnabled = configuration.getBoolean("data.mysql.enabled", false);
124127
mysqlUsername = configuration.getString("data.mysql.username", "root");
125128
mysqlPassword = configuration.getString("data.mysql.password", "password");

src/main/java/me/realized/tokenmanager/shop/Shop.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class Shop {
4141

4242
@Getter
4343
private final String name;
44+
private final String title;
4445
@Getter(value = AccessLevel.PACKAGE)
4546
private final Inventory inventory;
4647
@Getter
@@ -72,14 +73,15 @@ public Shop(final String name, final String title, final int rows, final boolean
7273
throw new IllegalArgumentException("Shop rows must be in between 1 - 6.");
7374
}
7475

75-
this.inventory = Bukkit.createInventory(null, rows * 9, StringUtil.color(title));
76+
this.title = StringUtil.color(title);
77+
this.inventory = Bukkit.createInventory(null, rows * 9, title);
7678
this.autoClose = autoClose;
7779
this.usePermission = usePermission;
7880
this.confirmPurchase = confirmPurchase;
7981
}
8082

8183
public String getTitle() {
82-
return inventory.getTitle();
84+
return title;
8385
}
8486

8587
public int getSize() {

src/main/java/me/realized/tokenmanager/shop/ShopConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ protected void loadValues(final FileConfiguration configuration) {
134134
shop,
135135
(int) slot,
136136
slotSection.getInt("cost", 1000000),
137+
slotSection.getInt("empty-slots-required", 0),
137138
displayed,
138139
slotSection.getString("message"),
139140
slotSection.getString("subshop"),

src/main/java/me/realized/tokenmanager/shop/Slot.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public class Slot {
5252
@Getter
5353
private final int cost;
5454
@Getter
55+
private final int emptySlotsRequired;
56+
@Getter
5557
private final ItemStack displayed;
5658
@Getter
5759
private final String message;
@@ -64,19 +66,28 @@ public class Slot {
6466
@Getter
6567
private final boolean confirmPurchase;
6668

67-
public Slot(final TokenManagerPlugin plugin, final Shop shop, final int slot, final int cost, final ItemStack displayed, final String message, final String subshop,
69+
public Slot(final TokenManagerPlugin plugin, final Shop shop, final int slot, final int cost, final int emptySlotsRequired, final ItemStack displayed, final String message, final String subshop,
6870
final List<String> commands, final boolean usePermission, final boolean confirmPurchase) {
6971
this.plugin = plugin;
7072
this.shop = shop;
7173
this.slot = slot;
7274
this.cost = cost;
75+
this.emptySlotsRequired = emptySlotsRequired;
7376
this.displayed = displayed;
7477
this.message = message != null ? Placeholders.replaceLong(message, cost, "price", "cost") : null;
7578
this.subshop = subshop;
7679
this.commands = commands;
7780
this.usePermission = usePermission;
7881
this.confirmPurchase = confirmPurchase;
79-
commands.replaceAll(command -> command = Placeholders.replaceLong(command, cost, "price", "cost"));
82+
commands.replaceAll(command -> {
83+
command = Placeholders.replaceLong(command, cost, "price", "cost");
84+
85+
if (command.startsWith("/")) {
86+
command = command.substring(1);
87+
}
88+
89+
return command;
90+
});
8091
}
8192

8293
public boolean purchase(final Player player, final boolean confirmPurchase, final boolean close) {

src/main/java/me/realized/tokenmanager/shop/gui/guis/ConfirmGui.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class ConfirmGui extends BaseGui {
2121
private final int slot;
2222

2323
public ConfirmGui(final TokenManagerPlugin plugin, final Shop shop, final int slot) {
24-
super(plugin, shop, InventoryUtil.deepCopyOf(plugin.getShopConfig().getConfirmGuiSample()));
24+
super(plugin, shop, InventoryUtil.deepCopyOf(plugin.getShopConfig().getConfirmGuiSample(), plugin.getConfiguration().getConfirmPurchaseTitle()));
2525
this.dataManager = plugin.getDataManager();
2626
this.shopManager = plugin.getShopManager();
2727
this.slot = slot;

src/main/java/me/realized/tokenmanager/shop/gui/guis/ShopGui.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package me.realized.tokenmanager.shop.gui.guis;
22

33
import me.realized.tokenmanager.TokenManagerPlugin;
4+
import me.realized.tokenmanager.config.Config;
5+
import me.realized.tokenmanager.config.Lang;
46
import me.realized.tokenmanager.data.DataManager;
57
import me.realized.tokenmanager.shop.Shop;
68
import me.realized.tokenmanager.shop.Slot;
79
import me.realized.tokenmanager.shop.gui.BaseGui;
810
import me.realized.tokenmanager.util.Placeholders;
911
import me.realized.tokenmanager.util.compat.Items;
12+
import me.realized.tokenmanager.util.inventory.InventoryUtil;
1013
import me.realized.tokenmanager.util.inventory.ItemUtil;
1114
import org.bukkit.Bukkit;
1215
import org.bukkit.entity.Player;
@@ -15,10 +18,16 @@
1518

1619
public class ShopGui extends BaseGui {
1720

21+
private static final String PURCHASE_LOG = "%s (%s) purchased %s:%s for %s tokens.";
22+
23+
private final Config config;
24+
private final Lang lang;
1825
private final DataManager dataManager;
1926

2027
public ShopGui(final TokenManagerPlugin plugin, final Shop shop) {
2128
super(plugin, shop, Bukkit.createInventory(null, shop.getSize(), shop.getTitle()));
29+
this.config = plugin.getConfiguration();
30+
this.lang = plugin.getLang();
2231
this.dataManager = plugin.getDataManager();
2332
}
2433

@@ -66,12 +75,23 @@ public boolean handle(final Player player, final int slot) {
6675

6776
if (data.isUsePermission() && !player.hasPermission("tokenmanager.use." + shop.getName() + "-" + slot)) {
6877
plugin.doSync(player::closeInventory);
69-
plugin.getLang().sendMessage(player, true, "ERROR.no-permission", "permission", "tokenmanager.use." + shop.getName() + "-" + slot);
78+
lang.sendMessage(player, true, "ERROR.no-permission", "permission", "tokenmanager.use." + shop.getName() + "-" + slot);
79+
return false;
80+
}
81+
82+
if (data.getEmptySlotsRequired() > 0 && InventoryUtil.getEmptySlots(player.getInventory()) < data.getEmptySlotsRequired()) {
83+
plugin.doSync(player::closeInventory);
84+
lang.sendMessage(player, true, "ERROR.not-enough-space", "slots", data.getEmptySlotsRequired());
7085
return false;
7186
}
7287

7388
if (data.purchase(player, shop.isConfirmPurchase() || data.isConfirmPurchase(), false)) {
7489
refresh(player, false);
90+
91+
if (config.isLogPurchases()) {
92+
plugin.getLogger().info(String.format(PURCHASE_LOG, player.getUniqueId(), player.getName(), shop.getName(), slot, data.getCost()));
93+
}
94+
7595
return true;
7696
}
7797

src/main/java/me/realized/tokenmanager/util/inventory/InventoryUtil.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
package me.realized.tokenmanager.util.inventory;
22

3+
import me.realized.tokenmanager.util.StringUtil;
34
import org.bukkit.Bukkit;
45
import org.bukkit.entity.Player;
56
import org.bukkit.inventory.Inventory;
67
import org.bukkit.inventory.InventoryView;
8+
import org.bukkit.inventory.ItemStack;
79

810
public final class InventoryUtil {
911

1012
private InventoryUtil() {}
1113

12-
public static Inventory deepCopyOf(final Inventory other) {
13-
final Inventory result = Bukkit.createInventory(null, other.getSize(), other.getTitle());
14+
public static Inventory deepCopyOf(final Inventory inventory, final String title) {
15+
final Inventory result = Bukkit.createInventory(null, inventory.getSize(), StringUtil.color(title));
1416

15-
for (int i = 0; i < other.getSize(); i++) {
16-
result.setItem(i, other.getItem(i).clone());
17+
for (int i = 0; i < inventory.getSize(); i++) {
18+
result.setItem(i, inventory.getItem(i).clone());
1719
}
1820

1921
return result;
2022
}
2123

24+
public static int getEmptySlots(final Inventory inventory) {
25+
int empty = 0;
26+
27+
for (int i = 0; i < 36; i++) {
28+
final ItemStack item = inventory.getItem(i);
29+
30+
if (item == null) {
31+
empty++;
32+
}
33+
}
34+
35+
return empty;
36+
}
37+
2238
public static boolean isInventoryFull(final Player player) {
2339
return player.getInventory().firstEmpty() == -1;
2440
}
2541

2642
public static Inventory getClickedInventory(final int rawSlot, final InventoryView view) {
27-
if (view.getTopInventory() != null && rawSlot < view.getTopInventory().getSize()) {
43+
if (rawSlot < view.getTopInventory().getSize()) {
2844
return view.getTopInventory();
2945
} else {
3046
return view.getBottomInventory();

0 commit comments

Comments
 (0)