diff --git a/src/main/java/one/oth3r/sit/file/CustomItem.java b/src/main/java/one/oth3r/sit/file/CustomItem.java new file mode 100644 index 0000000..ec1ef5f --- /dev/null +++ b/src/main/java/one/oth3r/sit/file/CustomItem.java @@ -0,0 +1,65 @@ +package one.oth3r.sit.file; + +import com.google.gson.annotations.SerializedName; +import net.minecraft.item.ItemStack; +import net.minecraft.registry.Registries; +import net.minecraft.registry.tag.TagKey; +import net.minecraft.util.Identifier; + +import java.util.ArrayList; + +public class CustomItem { + @SerializedName("item-ids") + private ArrayList itemIDs = new ArrayList<>(); + @SerializedName("item-tags") + private ArrayList itemTags = new ArrayList<>(); + + public CustomItem() {} + + public CustomItem(ArrayList itemIDs, ArrayList itemTags) { + this.itemIDs = itemIDs; + this.itemTags = itemTags; + } + + public ArrayList getItemIDs() { + return itemIDs; + } + + public ArrayList getItemTags() { + return itemTags; + } + + /** + * returns if the block is the correct type or not + * @param itemStack the blockstate to check + * @return if the type of block is matching the CustomBlock rules (e.g. if it is wood, ect.) + */ + public boolean checkItem(ItemStack itemStack) { + String itemId = Registries.ITEM.getId(itemStack.getItem()).toString(); + // check the custom item ids + for (String id : itemIDs) { + // if there is a match for the NOT(!) item, its filtered, false + if (id.startsWith("!") && id.substring(1).equalsIgnoreCase(itemId)) return false; + // if there is a match for the item, return true immediately + if (id.equalsIgnoreCase(itemId)) return true; + } + + // a boolean to check if one of the items are in a filtered tag + boolean tagCheck = false; + + // check the custom item tags + for (String tag : itemTags) { + // substring to remove # and if needed, "!" + // if a NOT tag + if (tag.startsWith("!")) { + // if there is a math for the NOT(!) tag, return false + if (itemStack.isIn(TagKey.of(Registries.ITEM.getKey(), Identifier.of(tag.substring(2))))) return false; + } + // else (normal tag), if there is a match, set tagCheck to true + else if (itemStack.isIn(TagKey.of(Registries.ITEM.getKey(), Identifier.of(tag.substring(1))))) tagCheck = true; + } + + // not returning true in the loop because there might be a (!) not tag that the item might fall into, after the item was already in another tag + return tagCheck; + } +} diff --git a/src/main/java/one/oth3r/sit/file/FileData.java b/src/main/java/one/oth3r/sit/file/FileData.java index 78877f0..fccba9a 100644 --- a/src/main/java/one/oth3r/sit/file/FileData.java +++ b/src/main/java/one/oth3r/sit/file/FileData.java @@ -99,12 +99,12 @@ public class FileData { )); public static final HandSetting MAIN_HAND = new HandSetting(HandSetting.SittingRequirement.EMPTY, new HandSetting.Filter( - false,false,false,new ArrayList<>(), new ArrayList<>(Arrays.asList("#minecraft:bookshelf_books","!#minecraft:lectern_books")))); + false,false,false,new CustomItem(new ArrayList<>(), new ArrayList<>(Arrays.asList("#minecraft:bookshelf_books","!#minecraft:lectern_books"))))); public static final HandSetting OFF_HAND = new HandSetting(HandSetting.SittingRequirement.FILTER, new HandSetting.Filter( - false,true,true, new ArrayList<>(Arrays.asList("minecraft:filled_map", + false,true,true, new CustomItem(new ArrayList<>(Arrays.asList("minecraft:filled_map", "minecraft:torch", "minecraft:soul_torch","minecraft:redstone_torch", "minecraft:lantern", "minecraft:soul_lantern")), - new ArrayList<>())); + new ArrayList<>()))); } } diff --git a/src/main/java/one/oth3r/sit/file/HandSetting.java b/src/main/java/one/oth3r/sit/file/HandSetting.java index 57779ea..02c3efa 100644 --- a/src/main/java/one/oth3r/sit/file/HandSetting.java +++ b/src/main/java/one/oth3r/sit/file/HandSetting.java @@ -45,18 +45,15 @@ public class HandSetting { @SerializedName("usable") private boolean usable = false; @SerializedName("custom-items") - private ArrayList customItems = new ArrayList<>(); - @SerializedName("custom-tags") - private ArrayList customTags = new ArrayList<>(); + private CustomItem customItems = new CustomItem(); public Filter() {} - public Filter(boolean block, boolean food, boolean usable, ArrayList customItems, ArrayList customTags) { + public Filter(boolean block, boolean food, boolean usable, CustomItem customItems) { this.block = block; this.food = food; this.usable = usable; this.customItems = customItems; - this.customTags = customTags; } public boolean isBlock() { @@ -71,12 +68,8 @@ public class HandSetting { return usable; } - public ArrayList getCustomItems() { + public CustomItem getCustomItems() { return customItems; } - - public ArrayList getCustomTags() { - return customTags; - } } } diff --git a/src/main/java/one/oth3r/sit/file/ServerConfig.java b/src/main/java/one/oth3r/sit/file/ServerConfig.java index 36db623..822e2e9 100644 --- a/src/main/java/one/oth3r/sit/file/ServerConfig.java +++ b/src/main/java/one/oth3r/sit/file/ServerConfig.java @@ -299,11 +299,11 @@ public class ServerConfig implements CustomFile { !Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.block", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isBlock()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.food", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isFood()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("hand.main.usable", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isUsable()))), - getFilterList( - new Gson().fromJson((String) properties.computeIfAbsent("hand.main.whitelist", a -> "[]"), listType), - new Gson().fromJson((String) properties.computeIfAbsent("hand.main.blacklist", a -> "[]"), listType) - ), - new ArrayList<>() + new CustomItem(getFilterList( + new Gson().fromJson((String) properties.computeIfAbsent("hand.main.whitelist", a -> "[]"), listType), + new Gson().fromJson((String) properties.computeIfAbsent("hand.main.blacklist", a -> "[]"), listType) + ), + new ArrayList<>()) ) ), new HandSetting( @@ -312,11 +312,11 @@ public class ServerConfig implements CustomFile { !Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.block", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isBlock()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.food", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isFood()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("hand.off.usable", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isUsable()))), - getFilterList( - new Gson().fromJson((String) properties.computeIfAbsent("hand.off.whitelist", a -> "[]"), listType), - new Gson().fromJson((String) properties.computeIfAbsent("hand.off.blacklist", a -> "[]"), listType) - ), - new ArrayList<>() + new CustomItem(getFilterList( + new Gson().fromJson((String) properties.computeIfAbsent("hand.off.whitelist", a -> "[]"), listType), + new Gson().fromJson((String) properties.computeIfAbsent("hand.off.blacklist", a -> "[]"), listType) + ), + new ArrayList<>()) ) ) ); @@ -333,11 +333,11 @@ public class ServerConfig implements CustomFile { !Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-block", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isBlock()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-food", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isFood()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("main-hand-usable", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.MAIN_HAND).getFilter().isUsable()))), - getFilterList( - new Gson().fromJson((String) properties.computeIfAbsent("main-hand-whitelist", a -> "[]"), listType), - new Gson().fromJson((String) properties.computeIfAbsent("main-hand-blacklist", a -> "[]"), listType) - ), - new ArrayList<>() + new CustomItem(getFilterList( + new Gson().fromJson((String) properties.computeIfAbsent("main-hand-whitelist", a -> "[]"), listType), + new Gson().fromJson((String) properties.computeIfAbsent("main-hand-blacklist", a -> "[]"), listType) + ), + new ArrayList<>()) ) ), new HandSetting( @@ -346,11 +346,11 @@ public class ServerConfig implements CustomFile { !Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-block", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isBlock()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-food", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isFood()))), !Boolean.parseBoolean((String) properties.computeIfAbsent("off-hand-usable", a -> String.valueOf(!defaultSittingConfig.getHand(Hand.OFF_HAND).getFilter().isUsable()))), - getFilterList( - new Gson().fromJson((String) properties.computeIfAbsent("off-hand-whitelist", a -> "[]"), listType), - new Gson().fromJson((String) properties.computeIfAbsent("off-hand-blacklist", a -> "[]"), listType) - ), - new ArrayList<>() + new CustomItem(getFilterList( + new Gson().fromJson((String) properties.computeIfAbsent("off-hand-whitelist", a -> "[]"), listType), + new Gson().fromJson((String) properties.computeIfAbsent("off-hand-blacklist", a -> "[]"), listType) + ), + new ArrayList<>()) ) ) ); diff --git a/src/main/java/one/oth3r/sit/utl/Utl.java b/src/main/java/one/oth3r/sit/utl/Utl.java index 60f2a6d..c02d49d 100644 --- a/src/main/java/one/oth3r/sit/utl/Utl.java +++ b/src/main/java/one/oth3r/sit/utl/Utl.java @@ -104,34 +104,13 @@ public class Utl { * @return if true, the item isn't filtered out */ public static boolean checkItem(HandSetting.Filter filter, ItemStack itemStack) { - // default to true if theres nothing + // default to true if there's nothing if (itemStack.isEmpty()) return true; - String itemId = Registries.ITEM.getId(itemStack.getItem()).toString(); - // check the custom item ids - for (String id : filter.getCustomItems()) { - // if there is a match for the NOT(!) item, its filtered, false - if (id.startsWith("!") && id.substring(1).equalsIgnoreCase(itemId)) return false; - // if there is a match for the item, return true immediately - if (id.equalsIgnoreCase(itemId)) return true; - } - // a boolean to check if one of the items are in a filtered tag - boolean tagCheck = false; - // check the custom item tags - for (String tag : filter.getCustomTags()) { - // substring to remove # and if needed, ! - // if a NOT tag - if (tag.startsWith("!")) { - // if there is a math for the NOT(!) tag, return false - if (itemStack.isIn(TagKey.of(Registries.ITEM.getKey(), Identifier.of(tag.substring(2))))) return false; - } - // else (normal tag), if there is a match, set tagCheck to true - else if (itemStack.isIn(TagKey.of(Registries.ITEM.getKey(), Identifier.of(tag.substring(1))))) tagCheck = true; - } + boolean itemcheck = filter.getCustomItems().checkItem(itemStack); - // if the item is in the tag, the boolean is true, return true - // not returning true in the loop because there might be a (!) not tag that the item might fall into, after the item was already in another tag - if (tagCheck) return tagCheck; + // iif the item passes the checks, return true + if (itemcheck) return true; // if none of the custom were met, try the default conditions