From 2fb89add036c7cc200cbfa29667a81bbbb99c769 Mon Sep 17 00:00:00 2001 From: Daniel Walsh Date: Sat, 8 Jun 2024 06:55:33 +0100 Subject: [PATCH] Multi tools won't reset their modes after server restart (#4122) --- .../items/electric/gadgets/MultiTool.java | 51 ++++++++++++++----- .../items/electric/gadgets/MultiToolMode.java | 23 ++++++++- 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/gadgets/MultiTool.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/gadgets/MultiTool.java index 56707206d4..d3087016aa 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/gadgets/MultiTool.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/gadgets/MultiTool.java @@ -1,17 +1,19 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.electric.gadgets; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.util.regex.Pattern; import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; +import io.github.bakedlibs.dough.common.ChatColors; +import io.github.bakedlibs.dough.data.persistent.PersistentDataAPI; import org.bukkit.ChatColor; +import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; @@ -26,18 +28,20 @@ /** * The {@link MultiTool} is an electric device which can mimic * the behaviour of any other {@link SlimefunItem}. - * - * @author TheBusyBiscuit * + * @author TheBusyBiscuit */ public class MultiTool extends SlimefunItem implements Rechargeable { private static final float COST = 0.3F; - private final Map selectedMode = new HashMap<>(); private final List modes = new ArrayList<>(); private final float capacity; + private static final NamespacedKey key = new NamespacedKey(Slimefun.instance(), "multitool_mode"); + private static final String LORE_PREFIX = ChatColors.color("&8\u21E8 &7Mode: "); + private static final Pattern REGEX = Pattern.compile(ChatColors.color("(&c&o)?" + LORE_PREFIX) + "(.+)"); + @ParametersAreNonnullByDefault public MultiTool(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, float capacity, String... items) { super(itemGroup, item, recipeType, recipe); @@ -73,17 +77,15 @@ protected ItemUseHandler getItemUseHandler() { return e -> { Player p = e.getPlayer(); ItemStack item = e.getItem(); + ItemMeta meta = item.getItemMeta(); e.cancel(); - int index = selectedMode.getOrDefault(p.getUniqueId(), 0); + int index = PersistentDataAPI.getInt(meta, key); + SlimefunItem sfItem = modes.get(index).getItem(); if (!p.isSneaking()) { - if (removeItemCharge(item, COST)) { - SlimefunItem sfItem = modes.get(index).getItem(); - - if (sfItem != null) { - sfItem.callItemHandler(ItemUseHandler.class, handler -> handler.onRightClick(e)); - } + if (sfItem != null && removeItemCharge(item, COST)) { + sfItem.callItemHandler(ItemUseHandler.class, handler -> handler.onRightClick(e)); } } else { index = nextIndex(index); @@ -91,7 +93,28 @@ protected ItemUseHandler getItemUseHandler() { SlimefunItem selectedItem = modes.get(index).getItem(); String itemName = selectedItem != null ? selectedItem.getItemName() : "Unknown"; Slimefun.getLocalization().sendMessage(p, "messages.multi-tool.mode-change", true, msg -> msg.replace("%device%", "Multi Tool").replace("%mode%", ChatColor.stripColor(itemName))); - selectedMode.put(p.getUniqueId(), index); + + PersistentDataAPI.setInt(meta, key, index); + + List lore = meta.hasLore() ? meta.getLore() : new ArrayList<>(); + + boolean regexMatchFound = false; + for (int i = 0; i < lore.size(); i++) { + String line = lore.get(i); + + if (REGEX.matcher(line).matches()) { + lore.set(i, LORE_PREFIX + ChatColor.stripColor(itemName)); + regexMatchFound = true; + break; + } + } + + if (!regexMatchFound) { + lore.add(2, LORE_PREFIX + ChatColor.stripColor(itemName)); + } + + meta.setLore(lore); + item.setItemMeta(meta); } }; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/gadgets/MultiToolMode.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/gadgets/MultiToolMode.java index 7bf079771b..d3963496d4 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/gadgets/MultiToolMode.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/gadgets/MultiToolMode.java @@ -8,22 +8,43 @@ class MultiToolMode { + private final int id; + private final String itemId; private final ItemSetting item; private final ItemSetting enabled; + // TODO: Move "id" into some NamespacedKey MultiToolMode(@Nonnull MultiTool multiTool, int id, @Nonnull String itemId) { + this.id = id; + this.itemId = itemId; this.item = new ItemSetting<>(multiTool, "mode." + id + ".item", itemId); this.enabled = new ItemSetting<>(multiTool, "mode." + id + ".enabled", true); multiTool.addItemSetting(item, enabled); } + /** + * This method is deprecated and should not be used. + * + * + * @return The ID of this mode + */ + @Deprecated(since = "RC-37", forRemoval = true) + public int getId() { + return id; + } + @Nullable SlimefunItem getItem() { return SlimefunItem.getById(item.getValue()); } + @Nonnull + String getItemId() { + return itemId; + } + boolean isEnabled() { return enabled.getValue(); } -} +} \ No newline at end of file