Skip to content

Commit

Permalink
fix: 🐛 Fixed list config values to have proper validation
Browse files Browse the repository at this point in the history
  • Loading branch information
P3pp3rF1y committed Nov 14, 2024
1 parent 01798bd commit b28b6f4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 40 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -296,9 +295,9 @@ private int getRedstoneAmpLimit() {

private Set<Block> getHeatSources() {
Set<Block> heatSources = new HashSet<>();
List<String> 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);
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/reliquary/entities/shot/ShotBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -376,7 +374,7 @@ private void ricochet(Direction sideHit) {
*/
void seekTarget() {
Entity closestTarget = null;
List<String> huntableEntitiesBlacklist = Config.COMMON.items.seekerShot.huntableEntitiesBlacklist.get();
Set<String> huntableEntitiesBlacklist = new HashSet<>(Config.COMMON.items.seekerShot.huntableEntitiesBlacklist.get());
List<Entity> targetsList = level().getEntities(this,
new AABB(getX() - 5, getY() - 5, getZ() - 5, getX() + 5, getY() + 5, getZ() + 5),
Mob.class::isInstance);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/reliquary/items/MidasTouchstoneItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private void addGlowstoneCharge(ItemStack stack, int chargeToAdd) {
}

private void doRepairAndDamageTouchstone(ItemStack touchstone, Player player) {
List<String> goldItems = Config.COMMON.items.midasTouchstone.goldItems.get();
List<String> goldItems = Config.COMMON.items.midasTouchstone.getGoldItems();

IItemHandler playerInventory = InventoryHelper.getItemHandlerFrom(player);
if (playerInventory == null) {
Expand Down
68 changes: 37 additions & 31 deletions src/main/java/reliquary/reference/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> REGISTRY_NAME_MATCHER = o -> o instanceof String s && REGISTRY_NAME_PATTERN.matcher(s).matches();

public static <T> T getOrDefault(ModConfigSpec.ConfigValue<T> value, ModConfigSpec configSpec) {
return configSpec.isLoaded() ? value.get() : value.getDefault();
Expand Down Expand Up @@ -355,7 +359,7 @@ public static class AngelHeartVialSettings {
public final DestructionCatalystSettings destructionCatalyst;

public static class DestructionCatalystSettings {
public final ConfigValue<List<String>> mundaneBlocks;
public final ConfigValue<List<? extends String>> mundaneBlocks;
public final IntValue gunpowderCost;
public final IntValue gunpowderWorth;
public final IntValue gunpowderLimit;
Expand All @@ -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")
Expand Down Expand Up @@ -807,7 +811,7 @@ public static class KrakenShellSettings {
public final LanternOfParanoiaSettings lanternOfParanoia;

public static class LanternOfParanoiaSettings {
public final ConfigValue<List<String>> torches;
public final ConfigValue<List<? extends String>> torches;
public final IntValue minLightLevel;
public final IntValue placementScanRadius;

Expand All @@ -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);
Expand All @@ -832,7 +837,7 @@ public static class LanternOfParanoiaSettings {
public final MidasTouchstoneSettings midasTouchstone;

public static class MidasTouchstoneSettings {
public final ConfigValue<List<String>> goldItems;
public final ConfigValue<List<? extends String>> goldItems;
public final IntValue glowstoneCost;
public final IntValue glowstoneWorth;
public final IntValue glowstoneLimit;
Expand All @@ -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")
Expand All @@ -858,6 +863,11 @@ public static class MidasTouchstoneSettings {

builder.pop();
}

@SuppressWarnings("unchecked")
public List<String> getGoldItems() {
return (List<String>) goldItems.get();
}
}

public final MobCharmSettings mobCharm;
Expand Down Expand Up @@ -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<String> getDefaultEntityBlockList() {
List<String> ret = new ArrayList<>();
ret.add("minecraft:ender_dragon");
Expand Down Expand Up @@ -1050,8 +1056,8 @@ public static class RendingGaleSettings {
public final BooleanValue canPushProjectiles;
public final IntValue pedestalFlightRange;
public final IntValue pedestalCostPerSecond;
public final ConfigValue<List<String>> pushableEntitiesBlacklist;
public final ConfigValue<List<String>> pushableProjectilesBlacklist;
public final ConfigValue<List<? extends String>> pushableEntitiesBlacklist;
public final ConfigValue<List<? extends String>> pushableProjectilesBlacklist;

RendingGaleSettings(ModConfigSpec.Builder builder) {
builder.comment("Rending Gale settings").push("rendingGale");
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
Expand All @@ -1180,14 +1183,15 @@ private void initBlockedEntityTypes() {
public final SeekerShotSettings seekerShot;

public static class SeekerShotSettings {
public final ConfigValue<List<String>> huntableEntitiesBlacklist;
public final ConfigValue<List<? extends String>> 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();
}
Expand All @@ -1196,7 +1200,7 @@ public static class SeekerShotSettings {
public final SojournerStaffSettings sojournerStaff;

public static class SojournerStaffSettings {
public final ConfigValue<List<String>> torches;
public final ConfigValue<List<? extends String>> torches;
public final IntValue maxCapacityPerItemType;
public final IntValue maxRange;
public final IntValue tilePerCostMultiplier;
Expand All @@ -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")
Expand Down Expand Up @@ -1346,7 +1350,7 @@ public static class AltarSettings {
public static class ApothecaryCauldronSettings {
public final IntValue redstoneLimit;
public final IntValue cookTime;
public final ConfigValue<List<String>> heatSources;
public final ConfigValue<List<? extends String>> heatSources;
public final IntValue glowstoneLimit;

ApothecaryCauldronSettings(ModConfigSpec.Builder builder) {
Expand All @@ -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")
Expand Down Expand Up @@ -1403,8 +1407,8 @@ public static class FertileLilypadSettings {
public static class InterdictionTorchSettings {
public final IntValue pushRadius;
public final BooleanValue canPushProjectiles;
public final ConfigValue<List<String>> pushableEntitiesBlacklist;
public final ConfigValue<List<String>> pushableProjectilesBlacklist;
public final ConfigValue<List<? extends String>> pushableEntitiesBlacklist;
public final ConfigValue<List<? extends String>> pushableProjectilesBlacklist;

InterdictionTorchSettings(ModConfigSpec.Builder builder) {
builder.comment("Interdiction Torch settings").push("interdictionTorch");
Expand All @@ -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();
}
Expand Down

0 comments on commit b28b6f4

Please sign in to comment.