1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 11:15:51 +00:00

Multi tools won't reset their modes after server restart (#4122)

This commit is contained in:
Daniel Walsh 2024-06-08 06:55:33 +01:00 committed by GitHub
parent a00e4baa7f
commit 2fb89add03
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 59 additions and 15 deletions

View File

@ -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 @@ import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
/**
* 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<UUID, Integer> selectedMode = new HashMap<>();
private final List<MultiToolMode> 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 @@ public class MultiTool extends SlimefunItem implements Rechargeable {
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 @@ public class MultiTool extends SlimefunItem implements Rechargeable {
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<String> 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);
}
};
}

View File

@ -8,22 +8,43 @@ import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
class MultiToolMode {
private final int id;
private final String itemId;
private final ItemSetting<String> item;
private final ItemSetting<Boolean> 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();
}
}
}