Skip to content

Commit 2fb89ad

Browse files
authored
Multi tools won't reset their modes after server restart (#4122)
1 parent a00e4ba commit 2fb89ad

2 files changed

Lines changed: 59 additions & 15 deletions

File tree

src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/gadgets/MultiTool.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package io.github.thebusybiscuit.slimefun4.implementation.items.electric.gadgets;
22

33
import java.util.ArrayList;
4-
import java.util.HashMap;
54
import java.util.List;
6-
import java.util.Map;
7-
import java.util.UUID;
5+
import java.util.regex.Pattern;
86

97
import javax.annotation.Nonnull;
108
import javax.annotation.ParametersAreNonnullByDefault;
119

10+
import io.github.bakedlibs.dough.common.ChatColors;
11+
import io.github.bakedlibs.dough.data.persistent.PersistentDataAPI;
1212
import org.bukkit.ChatColor;
13+
import org.bukkit.NamespacedKey;
1314
import org.bukkit.entity.Player;
1415
import org.bukkit.inventory.ItemStack;
16+
import org.bukkit.inventory.meta.ItemMeta;
1517

1618
import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
1719
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
@@ -26,18 +28,20 @@
2628
/**
2729
* The {@link MultiTool} is an electric device which can mimic
2830
* the behaviour of any other {@link SlimefunItem}.
29-
*
30-
* @author TheBusyBiscuit
3131
*
32+
* @author TheBusyBiscuit
3233
*/
3334
public class MultiTool extends SlimefunItem implements Rechargeable {
3435

3536
private static final float COST = 0.3F;
3637

37-
private final Map<UUID, Integer> selectedMode = new HashMap<>();
3838
private final List<MultiToolMode> modes = new ArrayList<>();
3939
private final float capacity;
4040

41+
private static final NamespacedKey key = new NamespacedKey(Slimefun.instance(), "multitool_mode");
42+
private static final String LORE_PREFIX = ChatColors.color("&8\u21E8 &7Mode: ");
43+
private static final Pattern REGEX = Pattern.compile(ChatColors.color("(&c&o)?" + LORE_PREFIX) + "(.+)");
44+
4145
@ParametersAreNonnullByDefault
4246
public MultiTool(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, float capacity, String... items) {
4347
super(itemGroup, item, recipeType, recipe);
@@ -73,25 +77,44 @@ protected ItemUseHandler getItemUseHandler() {
7377
return e -> {
7478
Player p = e.getPlayer();
7579
ItemStack item = e.getItem();
80+
ItemMeta meta = item.getItemMeta();
7681
e.cancel();
7782

78-
int index = selectedMode.getOrDefault(p.getUniqueId(), 0);
83+
int index = PersistentDataAPI.getInt(meta, key);
84+
SlimefunItem sfItem = modes.get(index).getItem();
7985

8086
if (!p.isSneaking()) {
81-
if (removeItemCharge(item, COST)) {
82-
SlimefunItem sfItem = modes.get(index).getItem();
83-
84-
if (sfItem != null) {
85-
sfItem.callItemHandler(ItemUseHandler.class, handler -> handler.onRightClick(e));
86-
}
87+
if (sfItem != null && removeItemCharge(item, COST)) {
88+
sfItem.callItemHandler(ItemUseHandler.class, handler -> handler.onRightClick(e));
8789
}
8890
} else {
8991
index = nextIndex(index);
9092

9193
SlimefunItem selectedItem = modes.get(index).getItem();
9294
String itemName = selectedItem != null ? selectedItem.getItemName() : "Unknown";
9395
Slimefun.getLocalization().sendMessage(p, "messages.multi-tool.mode-change", true, msg -> msg.replace("%device%", "Multi Tool").replace("%mode%", ChatColor.stripColor(itemName)));
94-
selectedMode.put(p.getUniqueId(), index);
96+
97+
PersistentDataAPI.setInt(meta, key, index);
98+
99+
List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();
100+
101+
boolean regexMatchFound = false;
102+
for (int i = 0; i < lore.size(); i++) {
103+
String line = lore.get(i);
104+
105+
if (REGEX.matcher(line).matches()) {
106+
lore.set(i, LORE_PREFIX + ChatColor.stripColor(itemName));
107+
regexMatchFound = true;
108+
break;
109+
}
110+
}
111+
112+
if (!regexMatchFound) {
113+
lore.add(2, LORE_PREFIX + ChatColor.stripColor(itemName));
114+
}
115+
116+
meta.setLore(lore);
117+
item.setItemMeta(meta);
95118
}
96119
};
97120
}

src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/gadgets/MultiToolMode.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,43 @@
88

99
class MultiToolMode {
1010

11+
private final int id;
12+
private final String itemId;
1113
private final ItemSetting<String> item;
1214
private final ItemSetting<Boolean> enabled;
1315

16+
// TODO: Move "id" into some NamespacedKey
1417
MultiToolMode(@Nonnull MultiTool multiTool, int id, @Nonnull String itemId) {
18+
this.id = id;
19+
this.itemId = itemId;
1520
this.item = new ItemSetting<>(multiTool, "mode." + id + ".item", itemId);
1621
this.enabled = new ItemSetting<>(multiTool, "mode." + id + ".enabled", true);
1722

1823
multiTool.addItemSetting(item, enabled);
1924
}
2025

26+
/**
27+
* This method is deprecated and should not be used.
28+
*
29+
*
30+
* @return The ID of this mode
31+
*/
32+
@Deprecated(since = "RC-37", forRemoval = true)
33+
public int getId() {
34+
return id;
35+
}
36+
2137
@Nullable
2238
SlimefunItem getItem() {
2339
return SlimefunItem.getById(item.getValue());
2440
}
2541

42+
@Nonnull
43+
String getItemId() {
44+
return itemId;
45+
}
46+
2647
boolean isEnabled() {
2748
return enabled.getValue();
2849
}
29-
}
50+
}

0 commit comments

Comments
 (0)