From b28b6f4d8aba55414652c071e4b175377a9f85c3 Mon Sep 17 00:00:00 2001 From: P3pp3rF1y Date: Thu, 14 Nov 2024 01:11:59 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Fixed=20list=20config=20v?= =?UTF-8?q?alues=20to=20have=20proper=20validation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../tile/ApothecaryCauldronBlockEntity.java | 5 +- .../reliquary/entities/shot/ShotBase.java | 6 +- .../reliquary/items/MidasTouchstoneItem.java | 2 +- src/main/java/reliquary/reference/Config.java | 68 ++++++++++--------- 5 files changed, 43 insertions(+), 40 deletions(-) diff --git a/gradle.properties b/gradle.properties index d7c1f1bb..8b3e09e9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -13,7 +13,7 @@ loader_version_range=[4,) mod_id=reliquary mod_name=Reliquary Reincarnations mod_license=GNU General Public License v3.0 -mod_version=2.0.44 +mod_version=2.0.45 mod_group_id=reliquary mod_authors=P3pp3rF1y mod_description=Two words: magical swag. Oh, and a gun. diff --git a/src/main/java/reliquary/blocks/tile/ApothecaryCauldronBlockEntity.java b/src/main/java/reliquary/blocks/tile/ApothecaryCauldronBlockEntity.java index 0d9c5a14..e1ea91c5 100644 --- a/src/main/java/reliquary/blocks/tile/ApothecaryCauldronBlockEntity.java +++ b/src/main/java/reliquary/blocks/tile/ApothecaryCauldronBlockEntity.java @@ -46,7 +46,6 @@ import reliquary.util.potions.PotionHelper; import java.util.HashSet; -import java.util.List; import java.util.Set; public class ApothecaryCauldronBlockEntity extends BlockEntityBase implements IJadeDataChangeIndicator { @@ -296,9 +295,9 @@ private int getRedstoneAmpLimit() { private Set getHeatSources() { Set heatSources = new HashSet<>(); - List heatSourceBlockNames = Config.COMMON.blocks.apothecaryCauldron.heatSources.get(); - heatSourceBlockNames.forEach(blockName -> heatSources.add(BuiltInRegistries.BLOCK.get(ResourceLocation.parse(blockName)))); + Config.COMMON.blocks.apothecaryCauldron.heatSources.get() + .forEach(blockName -> heatSources.add(BuiltInRegistries.BLOCK.get(ResourceLocation.parse(blockName)))); //defaults that can't be removed. heatSources.add(Blocks.LAVA); heatSources.add(Blocks.FIRE); diff --git a/src/main/java/reliquary/entities/shot/ShotBase.java b/src/main/java/reliquary/entities/shot/ShotBase.java index d149d630..14b3b331 100644 --- a/src/main/java/reliquary/entities/shot/ShotBase.java +++ b/src/main/java/reliquary/entities/shot/ShotBase.java @@ -28,9 +28,7 @@ import reliquary.util.potions.PotionHelper; import javax.annotation.Nullable; -import java.util.Iterator; -import java.util.List; -import java.util.Optional; +import java.util.*; @SuppressWarnings("squid:S2160") public abstract class ShotBase extends Projectile { @@ -376,7 +374,7 @@ private void ricochet(Direction sideHit) { */ void seekTarget() { Entity closestTarget = null; - List huntableEntitiesBlacklist = Config.COMMON.items.seekerShot.huntableEntitiesBlacklist.get(); + Set huntableEntitiesBlacklist = new HashSet<>(Config.COMMON.items.seekerShot.huntableEntitiesBlacklist.get()); List targetsList = level().getEntities(this, new AABB(getX() - 5, getY() - 5, getZ() - 5, getX() + 5, getY() + 5, getZ() + 5), Mob.class::isInstance); diff --git a/src/main/java/reliquary/items/MidasTouchstoneItem.java b/src/main/java/reliquary/items/MidasTouchstoneItem.java index 310d4b32..3bcc2fe3 100644 --- a/src/main/java/reliquary/items/MidasTouchstoneItem.java +++ b/src/main/java/reliquary/items/MidasTouchstoneItem.java @@ -75,7 +75,7 @@ private void addGlowstoneCharge(ItemStack stack, int chargeToAdd) { } private void doRepairAndDamageTouchstone(ItemStack touchstone, Player player) { - List goldItems = Config.COMMON.items.midasTouchstone.goldItems.get(); + List goldItems = Config.COMMON.items.midasTouchstone.getGoldItems(); IItemHandler playerInventory = InventoryHelper.getItemHandlerFrom(player); if (playerInventory == null) { diff --git a/src/main/java/reliquary/reference/Config.java b/src/main/java/reliquary/reference/Config.java index 407ec371..09e75f4a 100644 --- a/src/main/java/reliquary/reference/Config.java +++ b/src/main/java/reliquary/reference/Config.java @@ -17,17 +17,21 @@ import javax.annotation.Nullable; import java.util.*; +import java.util.function.Predicate; +import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; import static reliquary.util.RegistryHelper.getItemRegistryName; -@SuppressWarnings("squid:S1192") //no issue repeating the same string literal as they are independent +@SuppressWarnings({"java:S4968", "squid:S1192"}) // ? extends String is the type parameter returned from defineList so it can't be just String here | no issue repeating the same string literal as they are independent public class Config { private Config() { } private static final int ITEM_CAP = 9999; + private static final Pattern REGISTRY_NAME_PATTERN = Pattern.compile("([a-z0-9_.-]+:[a-z0-9_/.-]+)"); + private static final Predicate REGISTRY_NAME_MATCHER = o -> o instanceof String s && REGISTRY_NAME_PATTERN.matcher(s).matches(); public static T getOrDefault(ModConfigSpec.ConfigValue value, ModConfigSpec configSpec) { return configSpec.isLoaded() ? value.get() : value.getDefault(); @@ -355,7 +359,7 @@ public static class AngelHeartVialSettings { public final DestructionCatalystSettings destructionCatalyst; public static class DestructionCatalystSettings { - public final ConfigValue> mundaneBlocks; + public final ConfigValue> mundaneBlocks; public final IntValue gunpowderCost; public final IntValue gunpowderWorth; public final IntValue gunpowderLimit; @@ -368,7 +372,7 @@ public static class DestructionCatalystSettings { mundaneBlocks = builder .comment("List of mundane blocks the catalyst will break") - .define("mundaneBlocks", getMundaneBlocksDefault()); + .defineList("mundaneBlocks", getMundaneBlocksDefault(), () -> "minecraft:dirt", REGISTRY_NAME_MATCHER); gunpowderCost = builder .comment("Number of gunpowder it costs per catalyst use") @@ -807,7 +811,7 @@ public static class KrakenShellSettings { public final LanternOfParanoiaSettings lanternOfParanoia; public static class LanternOfParanoiaSettings { - public final ConfigValue> torches; + public final ConfigValue> torches; public final IntValue minLightLevel; public final IntValue placementScanRadius; @@ -816,7 +820,8 @@ public static class LanternOfParanoiaSettings { torches = builder .comment("List of torches that are supported by the lantern") - .define("torches", Lists.newArrayList(getItemRegistryName(Items.TORCH))); + .defineList("torches", () -> Lists.newArrayList(getItemRegistryName(Items.TORCH)), + () -> getItemRegistryName(Items.TORCH), REGISTRY_NAME_MATCHER); minLightLevel = builder .comment("Minimum light level below which the lantern will place torches") .defineInRange("minLightLevel", 1, 0, 15); @@ -832,7 +837,7 @@ public static class LanternOfParanoiaSettings { public final MidasTouchstoneSettings midasTouchstone; public static class MidasTouchstoneSettings { - public final ConfigValue> goldItems; + public final ConfigValue> goldItems; public final IntValue glowstoneCost; public final IntValue glowstoneWorth; public final IntValue glowstoneLimit; @@ -842,7 +847,7 @@ public static class MidasTouchstoneSettings { goldItems = builder .comment("Gold items that can be repaired by the touchstone") - .define("goldItems", new ArrayList<>()); + .defineListAllowEmpty("goldItems", ArrayList::new, () -> getItemRegistryName(Items.GOLDEN_AXE), REGISTRY_NAME_MATCHER); glowstoneCost = builder .comment("Number of glowstone that the repair costs") @@ -858,6 +863,11 @@ public static class MidasTouchstoneSettings { builder.pop(); } + + @SuppressWarnings("unchecked") + public List getGoldItems() { + return (List) goldItems.get(); + } } public final MobCharmSettings mobCharm; @@ -906,14 +916,10 @@ public static class MobCharmSettings { .define("keepAlmostDestroyedDisplayed", true); entityBlockList = builder .comment("List of hostile entities that are not supposed to have mob charms registered for them") - .defineList("entityBlockList", this::getDefaultEntityBlockList, this::getNewElement, entityName -> ((String) entityName).matches(REGISTRY_NAME_MATCHER)); + .defineList("entityBlockList", this::getDefaultEntityBlockList, () -> BuiltInRegistries.ENTITY_TYPE.getKey(EntityType.ZOMBIE).toString() , entityName -> ((String) entityName).matches(REGISTRY_NAME_MATCHER)); builder.pop(); } - private String getNewElement() { - return "example_mod:example_entity"; - } - private List getDefaultEntityBlockList() { List ret = new ArrayList<>(); ret.add("minecraft:ender_dragon"); @@ -1050,8 +1056,8 @@ public static class RendingGaleSettings { public final BooleanValue canPushProjectiles; public final IntValue pedestalFlightRange; public final IntValue pedestalCostPerSecond; - public final ConfigValue> pushableEntitiesBlacklist; - public final ConfigValue> pushableProjectilesBlacklist; + public final ConfigValue> pushableEntitiesBlacklist; + public final ConfigValue> pushableProjectilesBlacklist; RendingGaleSettings(ModConfigSpec.Builder builder) { builder.comment("Rending Gale settings").push("rendingGale"); @@ -1094,11 +1100,11 @@ public static class RendingGaleSettings { pushableEntitiesBlacklist = builder .comment("List of entities that are banned from being pushed by the Rending Gale") - .define("pushableEntitiesBlacklist", new ArrayList<>()); + .defineListAllowEmpty("pushableEntitiesBlacklist", ArrayList::new, () -> BuiltInRegistries.ENTITY_TYPE.getKey(EntityType.ZOMBIE).toString(), REGISTRY_NAME_MATCHER); pushableProjectilesBlacklist = builder .comment("List of projectiles that are banned from being pushed by the Rending Gale") - .define("pushableProjectilesBlacklist", new ArrayList<>()); + .defineListAllowEmpty("pushableProjectilesBlacklist", ArrayList::new, () -> BuiltInRegistries.ENTITY_TYPE.getKey(EntityType.ARROW).toString(), REGISTRY_NAME_MATCHER); builder.pop(); } @@ -1153,14 +1159,11 @@ public static class RodOfLyssaSettings { .define("stealFromPlayers", true); entityBlockList = builder.comment("List of entities on which lyssa rod doesn't work - full registry name is required here") - .defineList("entityBlockList", new ArrayList<>(), this::getNewElement, mapping -> ((String) mapping).matches(ENTITY_NAME_MATCHER)); + .defineList("entityBlockList", new ArrayList<>(), () -> BuiltInRegistries.ENTITY_TYPE.getKey(EntityType.ZOMBIE).toString() + , mapping -> ((String) mapping).matches(ENTITY_NAME_MATCHER)); builder.pop(); } - private String getNewElement() { - return "example_mod:example_entity"; - } - public boolean canStealFromEntity(Entity entity) { if (blockedEntities == null) { initBlockedEntityTypes(); @@ -1180,14 +1183,15 @@ private void initBlockedEntityTypes() { public final SeekerShotSettings seekerShot; public static class SeekerShotSettings { - public final ConfigValue> huntableEntitiesBlacklist; + public final ConfigValue> huntableEntitiesBlacklist; SeekerShotSettings(ModConfigSpec.Builder builder) { builder.comment("Seeker Shot settings").push("seekerShot"); huntableEntitiesBlacklist = builder .comment("Entities that are banned from being tracked by seeker shot") - .define("huntableEntitiesBlacklist", new ArrayList<>()); + .defineListAllowEmpty("huntableEntitiesBlacklist", ArrayList::new, + () -> BuiltInRegistries.ENTITY_TYPE.getKey(EntityType.ZOMBIE).toString(), REGISTRY_NAME_MATCHER); builder.pop(); } @@ -1196,7 +1200,7 @@ public static class SeekerShotSettings { public final SojournerStaffSettings sojournerStaff; public static class SojournerStaffSettings { - public final ConfigValue> torches; + public final ConfigValue> torches; public final IntValue maxCapacityPerItemType; public final IntValue maxRange; public final IntValue tilePerCostMultiplier; @@ -1209,7 +1213,7 @@ public static class SojournerStaffSettings { torches = builder .comment("List of torches that are supported by the staff") - .define("torches", getDefaultTorches()); + .defineList("torches", this::getDefaultTorches, () -> getItemRegistryName(Items.TORCH), REGISTRY_NAME_MATCHER); maxCapacityPerItemType = builder .comment("Number of items the staff can store per item type") @@ -1346,7 +1350,7 @@ public static class AltarSettings { public static class ApothecaryCauldronSettings { public final IntValue redstoneLimit; public final IntValue cookTime; - public final ConfigValue> heatSources; + public final ConfigValue> heatSources; public final IntValue glowstoneLimit; ApothecaryCauldronSettings(ModConfigSpec.Builder builder) { @@ -1362,7 +1366,7 @@ public static class ApothecaryCauldronSettings { heatSources = builder .comment("List of acceptable heat sources") - .define("heatSources", new ArrayList<>()); + .defineListAllowEmpty("heatSources", ArrayList::new, () -> BuiltInRegistries.BLOCK.getKey(Blocks.CAMPFIRE).toString(), REGISTRY_NAME_MATCHER); glowstoneLimit = builder .comment("Limit of glowstone that can be used in cauldron to make POTION more potent") @@ -1403,8 +1407,8 @@ public static class FertileLilypadSettings { public static class InterdictionTorchSettings { public final IntValue pushRadius; public final BooleanValue canPushProjectiles; - public final ConfigValue> pushableEntitiesBlacklist; - public final ConfigValue> pushableProjectilesBlacklist; + public final ConfigValue> pushableEntitiesBlacklist; + public final ConfigValue> pushableProjectilesBlacklist; InterdictionTorchSettings(ModConfigSpec.Builder builder) { builder.comment("Interdiction Torch settings").push("interdictionTorch"); @@ -1419,11 +1423,13 @@ public static class InterdictionTorchSettings { pushableEntitiesBlacklist = builder .comment("List of entities that are banned from being pushed by the torch") - .define("pushableEntitiesBlacklist", new ArrayList<>()); + .defineListAllowEmpty("pushableEntitiesBlacklist", ArrayList::new, + () -> BuiltInRegistries.ENTITY_TYPE.getKey(EntityType.ZOMBIE).toString(), REGISTRY_NAME_MATCHER); pushableProjectilesBlacklist = builder .comment("List of projectiles that are banned from being pushed by the torch") - .define("pushableProjectilesBlacklist", new ArrayList<>()); + .defineListAllowEmpty("pushableProjectilesBlacklist", ArrayList::new, + () -> BuiltInRegistries.ENTITY_TYPE.getKey(EntityType.ARROW).toString(), REGISTRY_NAME_MATCHER); builder.pop(); }