Skip to content

Commit d0bad37

Browse files
committed
Refactor item identification using PersistentDataContainer
Replaces legacy item identification via display names and localized names with PersistentDataContainer and NamespacedKey for shop, prefix, and rank items. Updates inventory creation and click handling logic in ShopCommand, PrefixCommand, ShopListener, and RankShopListener to use custom IDs stored in item metadata. Also refactors RecipeLoader to use PersistentDataContainer for custom item frames and updates event handling for placing invisible item frames.
1 parent ddb34e3 commit d0bad37

File tree

9 files changed

+359
-570
lines changed

9 files changed

+359
-570
lines changed

src/main/java/de/lars/utilsManager/recipes/RecipeLoader.java

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
import org.bukkit.event.EventHandler;
1313
import org.bukkit.event.Listener;
1414
import org.bukkit.event.block.Action;
15+
import org.bukkit.event.player.PlayerInteractEntityEvent;
1516
import org.bukkit.event.player.PlayerInteractEvent;
1617
import org.bukkit.inventory.ItemStack;
1718
import org.bukkit.inventory.ShapedRecipe;
1819
import org.bukkit.inventory.meta.ItemMeta;
20+
import org.bukkit.persistence.PersistentDataType;
1921

2022
public class RecipeLoader implements Listener {
2123

@@ -25,22 +27,25 @@ public void registerRecipes() {
2527
itemMetaLight.displayName(Component.text("Light", NamedTextColor.LIGHT_PURPLE));
2628
light.setItemMeta(itemMetaLight);
2729

28-
NamespacedKey lightKey = new NamespacedKey(Main.getInstance(), "magic_light");
30+
NamespacedKey lightKey = new NamespacedKey("utils_manager", "magic_light");
2931

3032
ShapedRecipe lightRecipe = new ShapedRecipe(lightKey, light);
3133
lightRecipe.shape("GGG", "GTG", "GGG");
3234
lightRecipe.setIngredient('G', Material.GLASS);
3335
lightRecipe.setIngredient('T', Material.TORCH);
3436

35-
ItemStack invisibleItemFrame = new ItemBuilder(Material.ITEM_FRAME)
36-
.setLocalizedName(Component.text("invisible_itemframe"))
37-
.setDisplayname(Component.text("Magic Item Frame", NamedTextColor.LIGHT_PURPLE))
38-
.build();
39-
40-
41-
NamespacedKey key = new NamespacedKey(Main.getInstance(), "invisible_frame");
37+
ItemStack invisibleItemFrame = new ItemStack(Material.ITEM_FRAME);
38+
ItemMeta meta = invisibleItemFrame.getItemMeta();
39+
if (meta != null) {
40+
meta.displayName(Component.text("Magic Item Frame", NamedTextColor.LIGHT_PURPLE));
41+
meta.setCustomModelData(1);
42+
NamespacedKey key = new NamespacedKey("utils_manager", "invisible_frame");
43+
meta.getPersistentDataContainer().set(key, PersistentDataType.BYTE, (byte) 1);
44+
invisibleItemFrame.setItemMeta(meta);
45+
}
4246

43-
ShapedRecipe itemFrameRecipe = new ShapedRecipe(key, invisibleItemFrame);
47+
NamespacedKey recipeKey = new NamespacedKey("utils_manager", "invisible_itemframe");
48+
ShapedRecipe itemFrameRecipe = new ShapedRecipe(recipeKey, invisibleItemFrame);
4449
itemFrameRecipe.shape("GGG", "GSG", "GGG");
4550
itemFrameRecipe.setIngredient('G', Material.GLASS);
4651
itemFrameRecipe.setIngredient('S', Material.ITEM_FRAME);
@@ -50,29 +55,17 @@ public void registerRecipes() {
5055
}
5156

5257
@EventHandler
53-
public void onPlayerInteract(PlayerInteractEvent event) {
54-
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
55-
56-
ItemStack item = event.getItem();
57-
if (item == null || item.getType() != Material.ITEM_FRAME) return;
58+
public void onItemFramePlace(PlayerInteractEntityEvent event) {
59+
if (!(event.getRightClicked() instanceof ItemFrame frame)) return;
5860

59-
ItemMeta meta = item.getItemMeta();
60-
if (meta == null) return;
61-
if (!(meta.itemName().equals(Component.text("invisible_itemframe")))) return;
61+
ItemStack item = event.getPlayer().getInventory().getItemInMainHand();
62+
if (item.getItemMeta() == null) return;
6263

63-
event.setCancelled(true);
64-
65-
Block clicked = event.getClickedBlock();
66-
BlockFace face = event.getBlockFace();
67-
Location loc = clicked.getRelative(face).getLocation().add(0.5, 0.5, 0.5);
68-
69-
ItemFrame frame = clicked.getWorld().spawn(loc, ItemFrame.class);
70-
frame.setVisible(false);
71-
frame.setFacingDirection(face);
72-
73-
Player player = event.getPlayer();
74-
if (player.getGameMode() != GameMode.CREATIVE) {
75-
item.setAmount(item.getAmount() - 1);
64+
NamespacedKey key = new NamespacedKey("utils_manager", "invisible_frame");
65+
if (item.getItemMeta().getPersistentDataContainer().has(key, PersistentDataType.BYTE)) {
66+
frame.setVisible(false);
67+
frame.setFixed(true);
7668
}
7769
}
70+
7871
}

src/main/java/de/lars/utilsmanager/commands/admin/PerformanceCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.lars.utilsmanager.commands.admin;
22

3+
import de.lars.utilsmanager.util.Statements;
34
import io.papermc.paper.command.brigadier.BasicCommand;
45
import io.papermc.paper.command.brigadier.CommandSourceStack;
56
import me.lucko.spark.api.Spark;

src/main/java/de/lars/utilsmanager/commands/economy/ShopCommand.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import net.kyori.adventure.text.format.TextDecoration;
1010
import org.bukkit.Bukkit;
1111
import org.bukkit.Material;
12+
import org.bukkit.NamespacedKey;
1213
import org.bukkit.entity.Player;
1314
import org.bukkit.inventory.Inventory;
1415
import org.bukkit.inventory.ItemStack;
@@ -29,31 +30,33 @@ public void execute(@NotNull CommandSourceStack stack, @NotNull String[] args) {
2930
player.sendMessage(Statements.getNotAllowed(player));
3031
return;
3132
}
33+
NamespacedKey shopKey = new NamespacedKey("utilsmanager", "shop_id");
34+
3235
HashMap<Integer, ItemStack> integerItemStackHashMap = new HashMap<>();
33-
integerItemStackHashMap.put(0, new ItemBuilder(Material.COPPER_INGOT).setDisplayname(Component.text("Buy Copper", NamedTextColor.YELLOW)).setLocalizedName(Component.text("buycopper")).build());
34-
integerItemStackHashMap.put(9, new ItemBuilder(Material.OAK_SIGN).setDisplayname(Component.text("Price: 10$", NamedTextColor.GRAY)).setLocalizedName(Component.text("pricecopper")).build());
35-
integerItemStackHashMap.put(18, new ItemBuilder(Material.OAK_SIGN).setDisplayname(Component.text("Sellprice: 5$", NamedTextColor.GRAY)).setLocalizedName(Component.text("sellpricecopper")).build());
36-
integerItemStackHashMap.put(27, new ItemBuilder(Material.COPPER_INGOT).setDisplayname(Component.text("Sell Copper", NamedTextColor.YELLOW)).setLocalizedName(Component.text("sellcopper")).build());
37-
38-
integerItemStackHashMap.put(2, new ItemBuilder(Material.AMETHYST_SHARD).setDisplayname(Component.text("Buy Amethyst Shard", NamedTextColor.LIGHT_PURPLE)).setLocalizedName(Component.text("buyamethyst")).build());
39-
integerItemStackHashMap.put(11, new ItemBuilder(Material.OAK_SIGN).setDisplayname(Component.text("Price: 30$", NamedTextColor.GRAY)).setLocalizedName(Component.text("priceamethyst")).build());
40-
integerItemStackHashMap.put(20, new ItemBuilder(Material.OAK_SIGN).setDisplayname(Component.text("Sellprice: 20$", NamedTextColor.GRAY)).setLocalizedName(Component.text("sellpriceamethyst")).build());
41-
integerItemStackHashMap.put(29, new ItemBuilder(Material.AMETHYST_SHARD).setDisplayname(Component.text("Sell Amethyst Shard", NamedTextColor.LIGHT_PURPLE)).setLocalizedName(Component.text("sellamethyst")).build());
42-
43-
integerItemStackHashMap.put(4, new ItemBuilder(Material.DIAMOND).setDisplayname(Component.text("Buy Diamond", NamedTextColor.AQUA)).setLocalizedName(Component.text("buydiamond")).build());
44-
integerItemStackHashMap.put(13, new ItemBuilder(Material.OAK_SIGN).setDisplayname(Component.text("Price: 150$", NamedTextColor.GRAY)).setLocalizedName(Component.text("pricediamond")).build());
45-
integerItemStackHashMap.put(22, new ItemBuilder(Material.OAK_SIGN).setDisplayname(Component.text("Sellprice: 100$", NamedTextColor.GRAY)).setLocalizedName(Component.text("sellpricediamond")).build());
46-
integerItemStackHashMap.put(31, new ItemBuilder(Material.DIAMOND).setDisplayname(Component.text("Sell Diamond", NamedTextColor.AQUA)).setLocalizedName(Component.text("selldiamond")).build());
47-
48-
integerItemStackHashMap.put(6, new ItemBuilder(Material.NETHERITE_INGOT).setDisplayname(Component.text("Buy Netherite Ingot", NamedTextColor.RED)).setLocalizedName(Component.text("buynetherite")).build());
49-
integerItemStackHashMap.put(15, new ItemBuilder(Material.OAK_SIGN).setDisplayname(Component.text("Price: 1.250$", NamedTextColor.GRAY)).setLocalizedName(Component.text("pricenetherite")).build());
50-
integerItemStackHashMap.put(24, new ItemBuilder(Material.OAK_SIGN).setDisplayname(Component.text("Sellprice: 1.000$", NamedTextColor.GRAY)).setLocalizedName(Component.text("sellpricenetherite")).build());
51-
integerItemStackHashMap.put(33, new ItemBuilder(Material.NETHERITE_INGOT).setDisplayname(Component.text("Sell Netherite Ingot", NamedTextColor.RED)).setLocalizedName(Component.text("sellnetherite")).build());
52-
53-
integerItemStackHashMap.put(8, new ItemBuilder(Material.SPAWNER).setDisplayname(Component.text("Buy Spawner", NamedTextColor.DARK_GRAY)).setLocalizedName(Component.text("buyspawner")).build());
54-
integerItemStackHashMap.put(17, new ItemBuilder(Material.OAK_SIGN).setDisplayname(Component.text("Price: 10.000$", NamedTextColor.GRAY)).setLocalizedName(Component.text("pricespawner")).build());
55-
integerItemStackHashMap.put(26, new ItemBuilder(Material.OAK_SIGN).setDisplayname(Component.text("Sellprice: 7.500$", NamedTextColor.GRAY)).setLocalizedName(Component.text("sellpricespawner")).build());
56-
integerItemStackHashMap.put(35, new ItemBuilder(Material.SPAWNER).setDisplayname(Component.text("Sell Spawner", NamedTextColor.DARK_GRAY)).setLocalizedName(Component.text("sellspawner")).build());
36+
integerItemStackHashMap.put(0, new ItemBuilder(Material.COPPER_INGOT).setDisplayName(Component.text("Buy Copper", NamedTextColor.YELLOW)).setCustomId(shopKey,"buycopper").build());
37+
integerItemStackHashMap.put(9, new ItemBuilder(Material.OAK_SIGN).setDisplayName(Component.text("Price: 10$", NamedTextColor.GRAY)).setCustomId(shopKey,"pricecopper").build());
38+
integerItemStackHashMap.put(18, new ItemBuilder(Material.OAK_SIGN).setDisplayName(Component.text("Sellprice: 5$", NamedTextColor.GRAY)).setCustomId(shopKey,"sellpricecopper").build());
39+
integerItemStackHashMap.put(27, new ItemBuilder(Material.COPPER_INGOT).setDisplayName(Component.text("Sell Copper", NamedTextColor.YELLOW)).setCustomId(shopKey,"sellcopper").build());
40+
41+
integerItemStackHashMap.put(2, new ItemBuilder(Material.AMETHYST_SHARD).setDisplayName(Component.text("Buy Amethyst Shard", NamedTextColor.LIGHT_PURPLE)).setCustomId(shopKey,"buyamethyst").build());
42+
integerItemStackHashMap.put(11, new ItemBuilder(Material.OAK_SIGN).setDisplayName(Component.text("Price: 30$", NamedTextColor.GRAY)).setCustomId(shopKey,"priceamethyst").build());
43+
integerItemStackHashMap.put(20, new ItemBuilder(Material.OAK_SIGN).setDisplayName(Component.text("Sellprice: 20$", NamedTextColor.GRAY)).setCustomId(shopKey,"sellpriceamethyst").build());
44+
integerItemStackHashMap.put(29, new ItemBuilder(Material.AMETHYST_SHARD).setDisplayName(Component.text("Sell Amethyst Shard", NamedTextColor.LIGHT_PURPLE)).setCustomId(shopKey,"sellamethyst").build());
45+
46+
integerItemStackHashMap.put(4, new ItemBuilder(Material.DIAMOND).setDisplayName(Component.text("Buy Diamond", NamedTextColor.AQUA)).setCustomId(shopKey,"buydiamond").build());
47+
integerItemStackHashMap.put(13, new ItemBuilder(Material.OAK_SIGN).setDisplayName(Component.text("Price: 150$", NamedTextColor.GRAY)).setCustomId(shopKey,"pricediamond").build());
48+
integerItemStackHashMap.put(22, new ItemBuilder(Material.OAK_SIGN).setDisplayName(Component.text("Sellprice: 100$", NamedTextColor.GRAY)).setCustomId(shopKey,"sellpricediamond").build());
49+
integerItemStackHashMap.put(31, new ItemBuilder(Material.DIAMOND).setDisplayName(Component.text("Sell Diamond", NamedTextColor.AQUA)).setCustomId(shopKey,"selldiamond").build());
50+
51+
integerItemStackHashMap.put(6, new ItemBuilder(Material.NETHERITE_INGOT).setDisplayName(Component.text("Buy Netherite Ingot", NamedTextColor.RED)).setCustomId(shopKey,"buynetherite").build());
52+
integerItemStackHashMap.put(15, new ItemBuilder(Material.OAK_SIGN).setDisplayName(Component.text("Price: 1.250$", NamedTextColor.GRAY)).setCustomId(shopKey,"pricenetherite").build());
53+
integerItemStackHashMap.put(24, new ItemBuilder(Material.OAK_SIGN).setDisplayName(Component.text("Sellprice: 1.000$", NamedTextColor.GRAY)).setCustomId(shopKey,"sellpricenetherite").build());
54+
integerItemStackHashMap.put(33, new ItemBuilder(Material.NETHERITE_INGOT).setDisplayName(Component.text("Sell Netherite Ingot", NamedTextColor.RED)).setCustomId(shopKey,"sellnetherite").build());
55+
56+
integerItemStackHashMap.put(8, new ItemBuilder(Material.SPAWNER).setDisplayName(Component.text("Buy Spawner", NamedTextColor.DARK_GRAY)).setCustomId(shopKey,"buyspawner").build());
57+
integerItemStackHashMap.put(17, new ItemBuilder(Material.OAK_SIGN).setDisplayName(Component.text("Price: 10.000$", NamedTextColor.GRAY)).setCustomId(shopKey,"pricespawner").build());
58+
integerItemStackHashMap.put(26, new ItemBuilder(Material.OAK_SIGN).setDisplayName(Component.text("Sellprice: 7.500$", NamedTextColor.GRAY)).setCustomId(shopKey,"sellpricespawner").build());
59+
integerItemStackHashMap.put(35, new ItemBuilder(Material.SPAWNER).setDisplayName(Component.text("Sell Spawner", NamedTextColor.DARK_GRAY)).setCustomId(shopKey,"sellspawner").build());
5760
Inventory i = Bukkit.createInventory(null, 4*9, Component.text(" Shop", NamedTextColor.DARK_GREEN, TextDecoration.BOLD));
5861
for (Map.Entry<Integer, ItemStack> integerItemStackEntry : integerItemStackHashMap.entrySet()) {
5962
i.setItem(integerItemStackEntry.getKey() , integerItemStackEntry.getValue());

src/main/java/de/lars/utilsmanager/commands/player/MsgCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import de.lars.apiManager.languageAPI.LanguageAPI;
44
import de.lars.utilsmanager.util.RankStatements;
5+
import de.lars.utilsmanager.util.Statements;
56
import io.papermc.paper.command.brigadier.BasicCommand;
67
import io.papermc.paper.command.brigadier.CommandSourceStack;
78
import net.kyori.adventure.text.Component;

0 commit comments

Comments
 (0)