Skip to content

Commit

Permalink
Multi tools won't reset their modes after server restart (#4122)
Browse files Browse the repository at this point in the history
  • Loading branch information
WalshyDev authored Jun 8, 2024
1 parent a00e4ba commit 2fb89ad
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<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);
Expand Down Expand Up @@ -73,25 +77,44 @@ 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);

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);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,43 @@

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();
}
}
}

0 comments on commit 2fb89ad

Please sign in to comment.