new custom item json type, simplifying some things

This commit is contained in:
Oth3r 2024-09-21 19:52:44 -05:00
commit 2bffecf784
5 changed files with 95 additions and 58 deletions

View file

@ -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<String> itemIDs = new ArrayList<>();
@SerializedName("item-tags")
private ArrayList<String> itemTags = new ArrayList<>();
public CustomItem() {}
public CustomItem(ArrayList<String> itemIDs, ArrayList<String> itemTags) {
this.itemIDs = itemIDs;
this.itemTags = itemTags;
}
public ArrayList<String> getItemIDs() {
return itemIDs;
}
public ArrayList<String> 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;
}
}

View file

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

View file

@ -45,18 +45,15 @@ public class HandSetting {
@SerializedName("usable")
private boolean usable = false;
@SerializedName("custom-items")
private ArrayList<String> customItems = new ArrayList<>();
@SerializedName("custom-tags")
private ArrayList<String> customTags = new ArrayList<>();
private CustomItem customItems = new CustomItem();
public Filter() {}
public Filter(boolean block, boolean food, boolean usable, ArrayList<String> customItems, ArrayList<String> 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<String> getCustomItems() {
public CustomItem getCustomItems() {
return customItems;
}
public ArrayList<String> getCustomTags() {
return customTags;
}
}
}

View file

@ -299,11 +299,11 @@ public class ServerConfig implements CustomFile<ServerConfig> {
!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 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 ArrayList<>())
)
),
new HandSetting(
@ -312,11 +312,11 @@ public class ServerConfig implements CustomFile<ServerConfig> {
!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 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<>()
new ArrayList<>())
)
)
);
@ -333,11 +333,11 @@ public class ServerConfig implements CustomFile<ServerConfig> {
!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 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 ArrayList<>())
)
),
new HandSetting(
@ -346,11 +346,11 @@ public class ServerConfig implements CustomFile<ServerConfig> {
!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 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<>()
new ArrayList<>())
)
)
);

View file

@ -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