From 3bcad7cd312379553fdc80461fbd20812aaa99da Mon Sep 17 00:00:00 2001 From: Nils Gereke Date: Sat, 23 Mar 2024 00:56:25 +0100 Subject: [PATCH] feat: add permission check when crafting items fix: missing translation keys in translation config fix: can't craft items #19 --- .../net/imprex/zip/api/ZIPBackpackType.java | 4 ++++ .../java/net/imprex/zip/api/ZIPHandler.java | 2 ++ .../java/net/imprex/zip/api/ZIPRegistry.java | 8 +++++++ .../java/net/imprex/zip/BackpackHandler.java | 21 +++++++++++++---- .../java/net/imprex/zip/BackpackListener.java | 23 +++++++++++-------- .../java/net/imprex/zip/BackpackRegistry.java | 16 +++++++++++++ .../java/net/imprex/zip/BackpackType.java | 10 ++++++++ .../imprex/zip/config/BackpackTypeConfig.java | 12 ++++++++++ .../src/main/resources/config/config-1.19.yml | 5 +++- .../src/main/resources/config/config-1.20.yml | 5 +++- zip-plugin/src/main/resources/lang/en_US.yml | 5 ++-- 11 files changed, 93 insertions(+), 18 deletions(-) diff --git a/zip-api/src/main/java/net/imprex/zip/api/ZIPBackpackType.java b/zip-api/src/main/java/net/imprex/zip/api/ZIPBackpackType.java index 2b67778..a65fa2f 100644 --- a/zip-api/src/main/java/net/imprex/zip/api/ZIPBackpackType.java +++ b/zip-api/src/main/java/net/imprex/zip/api/ZIPBackpackType.java @@ -17,4 +17,8 @@ public interface ZIPBackpackType { ItemStack getItem(); ZIPRecipe getRecipe(); + + boolean hasCraftingPermission(); + + String getCraftingPermission(); } \ No newline at end of file diff --git a/zip-api/src/main/java/net/imprex/zip/api/ZIPHandler.java b/zip-api/src/main/java/net/imprex/zip/api/ZIPHandler.java index 7b93da3..f29e5fd 100644 --- a/zip-api/src/main/java/net/imprex/zip/api/ZIPHandler.java +++ b/zip-api/src/main/java/net/imprex/zip/api/ZIPHandler.java @@ -13,5 +13,7 @@ public interface ZIPHandler { ZIPBackpack getBackpack(ItemStack item); + ZIPBackpackType getBackpackType(ItemStack item); + boolean isBackpack(ItemStack item); } diff --git a/zip-api/src/main/java/net/imprex/zip/api/ZIPRegistry.java b/zip-api/src/main/java/net/imprex/zip/api/ZIPRegistry.java index de89b3d..9353cc0 100644 --- a/zip-api/src/main/java/net/imprex/zip/api/ZIPRegistry.java +++ b/zip-api/src/main/java/net/imprex/zip/api/ZIPRegistry.java @@ -2,9 +2,17 @@ import java.util.Collection; +import org.bukkit.entity.Player; + public interface ZIPRegistry { ZIPBackpackType getTypeByName(String name); + default void discoverRecipes(Player player) { + this.discoverRecipes(player, false); + } + + void discoverRecipes(Player player, boolean force); + Collection getType(); } diff --git a/zip-plugin/src/main/java/net/imprex/zip/BackpackHandler.java b/zip-plugin/src/main/java/net/imprex/zip/BackpackHandler.java index a3d1bbb..a5aa7bd 100644 --- a/zip-plugin/src/main/java/net/imprex/zip/BackpackHandler.java +++ b/zip-plugin/src/main/java/net/imprex/zip/BackpackHandler.java @@ -21,6 +21,7 @@ import io.netty.buffer.Unpooled; import net.imprex.zip.api.ZIPBackpack; +import net.imprex.zip.api.ZIPBackpackType; import net.imprex.zip.api.ZIPHandler; import net.imprex.zip.api.ZIPUniqueId; import net.imprex.zip.common.Ingrim4Buffer; @@ -131,10 +132,6 @@ public Backpack getBackpack(Inventory inventory) { @Override public Backpack getBackpack(ItemStack item) { - if (item == null) { - return null; - } - if (item != null && item.hasItemMeta()) { ItemMeta meta = item.getItemMeta(); PersistentDataContainer dataContainer = meta.getPersistentDataContainer(); @@ -159,6 +156,22 @@ public Backpack getBackpack(ItemStack item) { return null; } + @Override + public ZIPBackpackType getBackpackType(ItemStack item) { + if (item != null && item.hasItemMeta()) { + ItemMeta meta = item.getItemMeta(); + PersistentDataContainer dataContainer = meta.getPersistentDataContainer(); + + if (dataContainer.has(this.backpackIdentifierKey, PersistentDataType.STRING)) { + String backpackIdentifier = dataContainer.get(this.backpackIdentifierKey, PersistentDataType.STRING); + BackpackType backpackType = this.registry.getTypeByName(backpackIdentifier); + return backpackType; + } + } + + return null; + } + @Override public boolean isBackpack(ItemStack item) { if (item == null) { diff --git a/zip-plugin/src/main/java/net/imprex/zip/BackpackListener.java b/zip-plugin/src/main/java/net/imprex/zip/BackpackListener.java index 0656fae..7d0f5ad 100644 --- a/zip-plugin/src/main/java/net/imprex/zip/BackpackListener.java +++ b/zip-plugin/src/main/java/net/imprex/zip/BackpackListener.java @@ -1,5 +1,6 @@ package net.imprex.zip; +import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -17,7 +18,6 @@ import org.bukkit.inventory.ItemStack; import net.imprex.zip.api.ZIPBackpackType; -import net.imprex.zip.api.ZIPRecipe; import net.imprex.zip.config.MessageConfig; import net.imprex.zip.config.MessageKey; @@ -38,12 +38,7 @@ public BackpackListener(BackpackPlugin plugin) { @EventHandler public void onPlayerJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); - for (ZIPBackpackType backpackType : this.backpackRegistry.getType()) { - ZIPRecipe recipe = backpackType.getRecipe(); - if (recipe.canDiscover()) { - player.discoverRecipe(recipe.getKey()); - } - } + this.backpackRegistry.discoverRecipes(player); if (player.isOp() || player.hasPermission("zeroinventoryproblems.notification")) { this.updateSystem.checkForUpdates(player); @@ -120,9 +115,17 @@ public void onBlockPlace(BlockPlaceEvent event) { @EventHandler(ignoreCancelled = false) public void onCraftItem(CraftItemEvent event) { + HumanEntity player = event.getWhoClicked(); ItemStack item = event.getCurrentItem(); - if (this.backpackHandler.isBackpack(item)) { - event.setCancelled(true); + + ZIPBackpackType type = this.backpackHandler.getBackpackType(item); + if (type.hasCraftingPermission()) { + String permission = type.getCraftingPermission(); + if (!player.hasPermission(permission)) { + event.setCancelled(true); + + this.messageConfig.send(player, MessageKey.YouDontHaveTheFollowingPermission, permission); + } } - } + } } \ No newline at end of file diff --git a/zip-plugin/src/main/java/net/imprex/zip/BackpackRegistry.java b/zip-plugin/src/main/java/net/imprex/zip/BackpackRegistry.java index 875228b..fb90f96 100644 --- a/zip-plugin/src/main/java/net/imprex/zip/BackpackRegistry.java +++ b/zip-plugin/src/main/java/net/imprex/zip/BackpackRegistry.java @@ -7,8 +7,10 @@ import java.util.Map; import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import net.imprex.zip.api.ZIPBackpackType; +import net.imprex.zip.api.ZIPRecipe; import net.imprex.zip.api.ZIPRegistry; import net.imprex.zip.config.BackpackConfig; import net.imprex.zip.config.BackpackTypeConfig; @@ -36,6 +38,8 @@ public void register() { BackpackRecipe recipe = backpackType.getRecipe(); Bukkit.addRecipe(recipe); } + + Bukkit.getOnlinePlayers().forEach(this::discoverRecipes); } public void unregister() { @@ -49,6 +53,18 @@ public void unregister() { this.backpackType.clear(); } + @Override + public void discoverRecipes(Player player, boolean force) { + for (ZIPBackpackType backpackType : this.getType()) { + ZIPRecipe recipe = backpackType.getRecipe(); + if (recipe.canDiscover() + || (backpackType.hasCraftingPermission() && player.hasPermission(backpackType.getCraftingPermission())) + || force) { + player.discoverRecipe(recipe.getKey()); + } + } + } + @Override public BackpackType getTypeByName(String name) { return this.backpackType.get(name.toLowerCase(Locale.ROOT)); diff --git a/zip-plugin/src/main/java/net/imprex/zip/BackpackType.java b/zip-plugin/src/main/java/net/imprex/zip/BackpackType.java index 3284a2d..16500c7 100644 --- a/zip-plugin/src/main/java/net/imprex/zip/BackpackType.java +++ b/zip-plugin/src/main/java/net/imprex/zip/BackpackType.java @@ -67,4 +67,14 @@ public ItemStack getItem() { public BackpackRecipe getRecipe() { return this.recipe; } + + @Override + public boolean hasCraftingPermission() { + return this.config.craftingPermission != null; + } + + @Override + public String getCraftingPermission() { + return this.config.craftingPermission; + } } \ No newline at end of file diff --git a/zip-plugin/src/main/java/net/imprex/zip/config/BackpackTypeConfig.java b/zip-plugin/src/main/java/net/imprex/zip/config/BackpackTypeConfig.java index 217c0b7..61973f7 100644 --- a/zip-plugin/src/main/java/net/imprex/zip/config/BackpackTypeConfig.java +++ b/zip-plugin/src/main/java/net/imprex/zip/config/BackpackTypeConfig.java @@ -18,6 +18,8 @@ public class BackpackTypeConfig { public RecipeConfig recipe; + public String craftingPermission; + public BackpackTypeConfig(ConfigurationSection config, String key) { if (config == null) { throw new IllegalArgumentException("Config section for backpack type " + key + " not found"); @@ -59,6 +61,16 @@ public BackpackTypeConfig(ConfigurationSection config, String key) { throw new IllegalArgumentException("Config value lore was not found in backpack type " + key); } + if (config.contains("craftingPermission") && config.isString("craftingPermission")) { + String permission = config.getString("craftingPermission"); + if (permission != null) { + permission = permission.trim(); + if (permission.length() != 0) { + this.craftingPermission = permission; + } + } + } + ConfigurationSection recipeSection = config.getConfigurationSection("recipe"); RecipeConfig recipeConfig = new RecipeConfig(recipeSection, key); this.recipe = recipeConfig; diff --git a/zip-plugin/src/main/resources/config/config-1.19.yml b/zip-plugin/src/main/resources/config/config-1.19.yml index b4b5562..53d8bee 100644 --- a/zip-plugin/src/main/resources/config/config-1.19.yml +++ b/zip-plugin/src/main/resources/config/config-1.19.yml @@ -22,6 +22,7 @@ type: E: LEAD C: CHEST D: DIAMOND + craftingPermission: 'zeroinventoryproblems.crafting.big' medium: uniqueName: 'medium' displayName: '&7Medium &eBackpack' @@ -42,6 +43,7 @@ type: E: LEAD C: CHEST I: IRON_INGOT + craftingPermission: 'zeroinventoryproblems.crafting.medium' small: uniqueName: 'small' displayName: '&7Small &eBackpack' @@ -60,4 +62,5 @@ type: L: LEATHER S: STICK E: LEAD - C: CHEST \ No newline at end of file + C: CHEST + craftingPermission: 'zeroinventoryproblems.crafting.small' \ No newline at end of file diff --git a/zip-plugin/src/main/resources/config/config-1.20.yml b/zip-plugin/src/main/resources/config/config-1.20.yml index b4b5562..53d8bee 100644 --- a/zip-plugin/src/main/resources/config/config-1.20.yml +++ b/zip-plugin/src/main/resources/config/config-1.20.yml @@ -22,6 +22,7 @@ type: E: LEAD C: CHEST D: DIAMOND + craftingPermission: 'zeroinventoryproblems.crafting.big' medium: uniqueName: 'medium' displayName: '&7Medium &eBackpack' @@ -42,6 +43,7 @@ type: E: LEAD C: CHEST I: IRON_INGOT + craftingPermission: 'zeroinventoryproblems.crafting.medium' small: uniqueName: 'small' displayName: '&7Small &eBackpack' @@ -60,4 +62,5 @@ type: L: LEATHER S: STICK E: LEAD - C: CHEST \ No newline at end of file + C: CHEST + craftingPermission: 'zeroinventoryproblems.crafting.small' \ No newline at end of file diff --git a/zip-plugin/src/main/resources/lang/en_US.yml b/zip-plugin/src/main/resources/lang/en_US.yml index 46c27e3..22be703 100644 --- a/zip-plugin/src/main/resources/lang/en_US.yml +++ b/zip-plugin/src/main/resources/lang/en_US.yml @@ -1,6 +1,6 @@ prefix: "&8[&eZIP&8] &7" notAConsoleCommand: "This command can not be executed from the console" -youDontHaveTheFollowingPermission: "You Don''t have the following permission &8\"&e{0}&8\"" +youDontHaveTheFollowingPermission: "You don''t have the following permission &8\"&e{0}&8\"" ThisBackpackNoLongerExist: "This backpack no longer exist" clickHereToSeeTheLatestRelease: "Click here to see the latest release" ANewReleaseIsAvailable: "A new version is available" @@ -50,4 +50,5 @@ enterANumberBetweenArgsAndArgs: "Please enter a number between {0} and {1}" loreLineCreate: "The lore line {0} was added" loreLineChange: "The lore line {0} was changed" loreLineDelete: "The lore line {0} was deleted" -maxLoreCountReached: "You have reached the max lore count of {0}" \ No newline at end of file +maxLoreCountReached: "You have reached the max lore count of {0}" +unableToLoadBackpack: "Backpack can't be loaded!" \ No newline at end of file