|
1 | 1 | package io.github.thebusybiscuit.slimefun4.implementation.items.electric.gadgets; |
2 | 2 |
|
3 | 3 | import java.util.ArrayList; |
4 | | -import java.util.HashMap; |
5 | 4 | import java.util.List; |
6 | | -import java.util.Map; |
7 | | -import java.util.UUID; |
| 5 | +import java.util.regex.Pattern; |
8 | 6 |
|
9 | 7 | import javax.annotation.Nonnull; |
10 | 8 | import javax.annotation.ParametersAreNonnullByDefault; |
11 | 9 |
|
| 10 | +import io.github.bakedlibs.dough.common.ChatColors; |
| 11 | +import io.github.bakedlibs.dough.data.persistent.PersistentDataAPI; |
12 | 12 | import org.bukkit.ChatColor; |
| 13 | +import org.bukkit.NamespacedKey; |
13 | 14 | import org.bukkit.entity.Player; |
14 | 15 | import org.bukkit.inventory.ItemStack; |
| 16 | +import org.bukkit.inventory.meta.ItemMeta; |
15 | 17 |
|
16 | 18 | import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; |
17 | 19 | import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; |
|
26 | 28 | /** |
27 | 29 | * The {@link MultiTool} is an electric device which can mimic |
28 | 30 | * the behaviour of any other {@link SlimefunItem}. |
29 | | - * |
30 | | - * @author TheBusyBiscuit |
31 | 31 | * |
| 32 | + * @author TheBusyBiscuit |
32 | 33 | */ |
33 | 34 | public class MultiTool extends SlimefunItem implements Rechargeable { |
34 | 35 |
|
35 | 36 | private static final float COST = 0.3F; |
36 | 37 |
|
37 | | - private final Map<UUID, Integer> selectedMode = new HashMap<>(); |
38 | 38 | private final List<MultiToolMode> modes = new ArrayList<>(); |
39 | 39 | private final float capacity; |
40 | 40 |
|
| 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 | + |
41 | 45 | @ParametersAreNonnullByDefault |
42 | 46 | public MultiTool(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, float capacity, String... items) { |
43 | 47 | super(itemGroup, item, recipeType, recipe); |
@@ -73,25 +77,44 @@ protected ItemUseHandler getItemUseHandler() { |
73 | 77 | return e -> { |
74 | 78 | Player p = e.getPlayer(); |
75 | 79 | ItemStack item = e.getItem(); |
| 80 | + ItemMeta meta = item.getItemMeta(); |
76 | 81 | e.cancel(); |
77 | 82 |
|
78 | | - int index = selectedMode.getOrDefault(p.getUniqueId(), 0); |
| 83 | + int index = PersistentDataAPI.getInt(meta, key); |
| 84 | + SlimefunItem sfItem = modes.get(index).getItem(); |
79 | 85 |
|
80 | 86 | 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)); |
87 | 89 | } |
88 | 90 | } else { |
89 | 91 | index = nextIndex(index); |
90 | 92 |
|
91 | 93 | SlimefunItem selectedItem = modes.get(index).getItem(); |
92 | 94 | String itemName = selectedItem != null ? selectedItem.getItemName() : "Unknown"; |
93 | 95 | 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); |
95 | 118 | } |
96 | 119 | }; |
97 | 120 | } |
|
0 commit comments