Skip to content

Commit

Permalink
#31: Fix saving of target value and check for null target value list
Browse files Browse the repository at this point in the history
  • Loading branch information
DerFrZocker committed Aug 16, 2024
1 parent 18d19c4 commit 474b9d7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public JsonElement toJson(TargetListValue v) {

if (value.getValue() != null) {
JsonArray array = new JsonArray();
// #31: Add the array to object
jsonObject.add("value", array);
for (TargetBlockState targetBlockState : value.getValue()) {
array.add(targetBlockStateParser.toJson(targetBlockState));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ public FixedTargetListValue clone() {
@Override
public void setValueLocation(@NotNull ValueLocation valueLocation) {
super.setValueLocation(valueLocation);
for (TargetBlockState state : getValue()) {
state.setValueLocation(valueLocation);
// #31: Check for null
if (value != null) {
for (TargetBlockState state : getValue()) {
state.setValueLocation(valueLocation);
}
}
}

Expand All @@ -102,10 +105,13 @@ public void setValueLocation(@NotNull ValueLocation valueLocation) {
MessageTraversUtil.addIfNotNull(result, formatter.format(depth, key, null));
MessageTraversUtil.addIfNotNull(result, formatter.format(depth + 1, TraversKey.ofValueType(getValueType().getKey()), null));

for (TargetBlockState state : getValue()) {
// TODO: 5/3/23 Maybe remove the "entry" line
List<String> states = state.traverse(formatter, depth + 2, TraversKey.ofValueSetting("entry"));
result.addAll(states);
// #31: Check for null
if (value != null) {
for (TargetBlockState state : getValue()) {
// TODO: 5/3/23 Maybe remove the "entry" line
List<String> states = state.traverse(formatter, depth + 2, TraversKey.ofValueSetting("entry"));
result.addAll(states);
}
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,31 @@ public OreConfiguration createConfig(@NotNull OreConfiguration defaultConfigurat
blockStates = defaultConfiguration.targetStates;
} else {
blockStates = new ArrayList<>();

for (TargetBlockState targetValue : configuration.getTargets().getValue(worldInfo, random, position, limitedRegion)) {
RuleTest ruleTest;

if (targetValue.getTarget() instanceof AlwaysTrueRuleTest) {
ruleTest = AlwaysTrueTest.INSTANCE;
} else if (targetValue.getTarget() instanceof BlockMatchRuleTest rule) {
ruleTest = new BlockMatchTest(CraftMagicNumbers.getBlock(rule.getBlock()));
} else if (targetValue.getTarget() instanceof BlockStateMatchRuleTest rule) {
ruleTest = new BlockStateMatchTest(((CraftBlockData) rule.getBlockData()).getState());
} else if (targetValue.getTarget() instanceof RandomBlockMatchRuleTest rule) {
ruleTest = new RandomBlockMatchTest(CraftMagicNumbers.getBlock(rule.getMaterial()), rule.getProbability());
} else if (targetValue.getTarget() instanceof RandomBlockStateMatchRuleTest rule) {
ruleTest = new RandomBlockStateMatchTest(((CraftBlockData) rule.getBlockData()).getState(), rule.getProbability());
} else if (targetValue.getTarget() instanceof TagMatchRuleTest rule) {
ruleTest = new TagMatchTest(TagKey.create(net.minecraft.core.registries.Registries.BLOCK, CraftNamespacedKey.toMinecraft(rule.getTag())));
} else {
throw new IllegalArgumentException("Got unexpected rule test from class " + targetValue.getTarget().getClass());
List<TargetBlockState> targetBlockStates = configuration.getTargets().getValue(worldInfo, random, position, limitedRegion);

// #31: Check for null
if (targetBlockStates != null) {
for (TargetBlockState targetValue : targetBlockStates) {
RuleTest ruleTest;

if (targetValue.getTarget() instanceof AlwaysTrueRuleTest) {
ruleTest = AlwaysTrueTest.INSTANCE;
} else if (targetValue.getTarget() instanceof BlockMatchRuleTest rule) {
ruleTest = new BlockMatchTest(CraftMagicNumbers.getBlock(rule.getBlock()));
} else if (targetValue.getTarget() instanceof BlockStateMatchRuleTest rule) {
ruleTest = new BlockStateMatchTest(((CraftBlockData) rule.getBlockData()).getState());
} else if (targetValue.getTarget() instanceof RandomBlockMatchRuleTest rule) {
ruleTest = new RandomBlockMatchTest(CraftMagicNumbers.getBlock(rule.getMaterial()), rule.getProbability());
} else if (targetValue.getTarget() instanceof RandomBlockStateMatchRuleTest rule) {
ruleTest = new RandomBlockStateMatchTest(((CraftBlockData) rule.getBlockData()).getState(), rule.getProbability());
} else if (targetValue.getTarget() instanceof TagMatchRuleTest rule) {
ruleTest = new TagMatchTest(TagKey.create(net.minecraft.core.registries.Registries.BLOCK, CraftNamespacedKey.toMinecraft(rule.getTag())));
} else {
throw new IllegalArgumentException("Got unexpected rule test from class " + targetValue.getTarget().getClass());
}

blockStates.add(OreConfiguration.target(ruleTest, ((CraftBlockData) targetValue.getState()).getState()));
}

blockStates.add(OreConfiguration.target(ruleTest, ((CraftBlockData) targetValue.getState()).getState()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ private static List<TargetBlockState> getData(GuiValuesHolder guiValuesHolder, G
return Collections.emptyList();
}

return value.getValue();
// #31: Check for null
return value.getValue() == null ? Collections.emptyList() : value.getValue();
}

private static ItemStack getItemStack(Setting setting, GuiInfo guiInfo, TargetBlockState targetBlockState) {
Expand Down

0 comments on commit 474b9d7

Please sign in to comment.