Skip to content

Commit

Permalink
feat: add permission check when crafting items
Browse files Browse the repository at this point in the history
fix: missing translation keys in translation config
fix: can't craft items #19
  • Loading branch information
NgLoader committed Mar 22, 2024
1 parent 1f28a6f commit 3bcad7c
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 18 deletions.
4 changes: 4 additions & 0 deletions zip-api/src/main/java/net/imprex/zip/api/ZIPBackpackType.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ public interface ZIPBackpackType {
ItemStack getItem();

ZIPRecipe getRecipe();

boolean hasCraftingPermission();

String getCraftingPermission();
}
2 changes: 2 additions & 0 deletions zip-api/src/main/java/net/imprex/zip/api/ZIPHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public interface ZIPHandler {

ZIPBackpack getBackpack(ItemStack item);

ZIPBackpackType getBackpackType(ItemStack item);

boolean isBackpack(ItemStack item);
}
8 changes: 8 additions & 0 deletions zip-api/src/main/java/net/imprex/zip/api/ZIPRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ZIPBackpackType> getType();
}
21 changes: 17 additions & 4 deletions zip-plugin/src/main/java/net/imprex/zip/BackpackHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down
23 changes: 13 additions & 10 deletions zip-plugin/src/main/java/net/imprex/zip/BackpackListener.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
}
}
16 changes: 16 additions & 0 deletions zip-plugin/src/main/java/net/imprex/zip/BackpackRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -36,6 +38,8 @@ public void register() {
BackpackRecipe recipe = backpackType.getRecipe();
Bukkit.addRecipe(recipe);
}

Bukkit.getOnlinePlayers().forEach(this::discoverRecipes);
}

public void unregister() {
Expand All @@ -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));
Expand Down
10 changes: 10 additions & 0 deletions zip-plugin/src/main/java/net/imprex/zip/BackpackType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion zip-plugin/src/main/resources/config/config-1.19.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type:
E: LEAD
C: CHEST
D: DIAMOND
craftingPermission: 'zeroinventoryproblems.crafting.big'
medium:
uniqueName: 'medium'
displayName: '&7Medium &eBackpack'
Expand All @@ -42,6 +43,7 @@ type:
E: LEAD
C: CHEST
I: IRON_INGOT
craftingPermission: 'zeroinventoryproblems.crafting.medium'
small:
uniqueName: 'small'
displayName: '&7Small &eBackpack'
Expand All @@ -60,4 +62,5 @@ type:
L: LEATHER
S: STICK
E: LEAD
C: CHEST
C: CHEST
craftingPermission: 'zeroinventoryproblems.crafting.small'
5 changes: 4 additions & 1 deletion zip-plugin/src/main/resources/config/config-1.20.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type:
E: LEAD
C: CHEST
D: DIAMOND
craftingPermission: 'zeroinventoryproblems.crafting.big'
medium:
uniqueName: 'medium'
displayName: '&7Medium &eBackpack'
Expand All @@ -42,6 +43,7 @@ type:
E: LEAD
C: CHEST
I: IRON_INGOT
craftingPermission: 'zeroinventoryproblems.crafting.medium'
small:
uniqueName: 'small'
displayName: '&7Small &eBackpack'
Expand All @@ -60,4 +62,5 @@ type:
L: LEATHER
S: STICK
E: LEAD
C: CHEST
C: CHEST
craftingPermission: 'zeroinventoryproblems.crafting.small'
5 changes: 3 additions & 2 deletions zip-plugin/src/main/resources/lang/en_US.yml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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}"
maxLoreCountReached: "You have reached the max lore count of {0}"
unableToLoadBackpack: "Backpack can't be loaded!"

0 comments on commit 3bcad7c

Please sign in to comment.