Skip to content

Commit

Permalink
fix: wrong node data syntax crashing the plugin
Browse files Browse the repository at this point in the history
fix: BlockFace enums in node data parser using int indices instead of specified enum names
feat: improve error verbosity
  • Loading branch information
Wertik committed Dec 5, 2024
1 parent fd53e73 commit 0b9cc66
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 159 deletions.
4 changes: 2 additions & 2 deletions blockregen-ancient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>BlockRegen</artifactId>
<groupId>nl.aurorion.blockregen</groupId>
<version>3.16.4</version>
<version>3.16.5-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -20,7 +20,7 @@
<dependency>
<groupId>nl.aurorion.blockregen</groupId>
<artifactId>blockregen-version</artifactId>
<version>3.16.4</version>
<version>3.16.5-SNAPSHOT</version>
<scope>compile</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,10 @@
public class AncientNodeDataParser implements NodeDataParser {

private final NodeDataDeserializer<AncientNodeData> nodeDataDeserializer = new NodeDataDeserializer<AncientNodeData>()
.property("age", (nodeData, value) -> {
int age = Integer.parseInt(value);
CropState state = CropState.values()[age];
nodeData.setCropState(state);
})
.property("facing", (nodeData, value) -> {
int id = Integer.parseInt(value);
BlockFace facing = BlockFace.values()[id];
nodeData.setFacing(facing);
})
.property("species", (nodeData, value) -> {
int id = Integer.parseInt(value);
TreeSpecies species = TreeSpecies.values()[id];
nodeData.setTreeSpecies(species);
})
.property("inverted", (nodeData, value) -> {
boolean inverted = Boolean.parseBoolean(value);
nodeData.setInverted(inverted);
})
.property("age", (nodeData, value) -> NodeDataDeserializer.tryParseEnum(value, CropState.class))
.property("facing", (nodeData, value) -> NodeDataDeserializer.tryParseEnum(value, BlockFace.class))
.property("species", (nodeData, value) -> NodeDataDeserializer.tryParseEnum(value, TreeSpecies.class))
.property("inverted", (nodeData, value) -> nodeData.setInverted(Boolean.parseBoolean(value)))
.property("skull", AncientNodeData::setSkull);

@Override
Expand Down
2 changes: 1 addition & 1 deletion blockregen-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>BlockRegen</artifactId>
<groupId>nl.aurorion.blockregen</groupId>
<version>3.16.4</version>
<version>3.16.5-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
11 changes: 11 additions & 0 deletions blockregen-common/src/main/java/nl/aurorion/blockregen/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package nl.aurorion.blockregen;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Pair<F, S> {
private final F first;
private final S second;
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ public <E extends Enum<E>> E parseEnum(String str, Class<E> clazz) {

public <E extends Enum<E>> E parseEnum(String str, Class<E> clazz, Consumer<Throwable> exceptionCallback) {

if (Strings.isNullOrEmpty(str))
if (Strings.isNullOrEmpty(str)) {
return null;
}

try {
return E.valueOf(clazz, str.trim().toUpperCase());
Expand Down
4 changes: 2 additions & 2 deletions blockregen-legacy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>BlockRegen</artifactId>
<groupId>nl.aurorion.blockregen</groupId>
<version>3.16.4</version>
<version>3.16.5-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -14,7 +14,7 @@
<dependency>
<groupId>nl.aurorion.blockregen</groupId>
<artifactId>blockregen-version</artifactId>
<version>3.16.4</version>
<version>3.16.5-SNAPSHOT</version>
<scope>compile</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,11 @@
public class LegacyNodeDataParser implements NodeDataParser {

private final NodeDataDeserializer<LegacyNodeData> nodeDataDeserializer = new NodeDataDeserializer<LegacyNodeData>()
.property("age", (nodeData, value) -> {
int age = Integer.parseInt(value);
CropState state = CropState.values()[age];
nodeData.setCropState(state);
})
.property("facing", (nodeData, value) -> {
int id = Integer.parseInt(value);
BlockFace facing = BlockFace.values()[id];
nodeData.setFacing(facing);
})
.property("species", (nodeData, value) -> {
int id = Integer.parseInt(value);
TreeSpecies species = TreeSpecies.values()[id];
nodeData.setTreeSpecies(species);
})
.property("inverted", (nodeData, value) -> {
boolean inverted = Boolean.parseBoolean(value);
nodeData.setInverted(inverted);
})
.property("skull", (LegacyNodeData::setSkull));
.property("age", (nodeData, value) -> NodeDataDeserializer.tryParseEnum(value, CropState.class))
.property("facing", (nodeData, value) -> NodeDataDeserializer.tryParseEnum(value, BlockFace.class))
.property("species", (nodeData, value) -> NodeDataDeserializer.tryParseEnum(value, TreeSpecies.class))
.property("inverted", (nodeData, value) -> nodeData.setInverted(Boolean.parseBoolean(value)))
.property("skull", LegacyNodeData::setSkull);

@Override
public NodeData parse(String input) {
Expand Down
10 changes: 5 additions & 5 deletions blockregen-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>BlockRegen</artifactId>
<groupId>nl.aurorion.blockregen</groupId>
<version>3.16.4</version>
<version>3.16.5-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -112,25 +112,25 @@
<dependency>
<groupId>nl.aurorion.blockregen</groupId>
<artifactId>blockregen-common</artifactId>
<version>3.16.4</version>
<version>3.16.5-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>nl.aurorion.blockregen</groupId>
<artifactId>blockregen-version</artifactId>
<version>3.16.4</version>
<version>3.16.5-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>nl.aurorion.blockregen</groupId>
<artifactId>blockregen-legacy</artifactId>
<version>3.16.4</version>
<version>3.16.5-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>nl.aurorion.blockregen</groupId>
<artifactId>blockregen-ancient</artifactId>
<version>3.16.4</version>
<version>3.16.5-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- Spigot API -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package nl.aurorion.blockregen.system.material;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.java.Log;
import nl.aurorion.blockregen.BlockRegen;
import nl.aurorion.blockregen.Pair;
import nl.aurorion.blockregen.system.material.parser.MaterialParser;
import nl.aurorion.blockregen.system.preset.struct.material.DynamicMaterial;
import nl.aurorion.blockregen.system.preset.struct.material.TargetMaterial;
Expand Down Expand Up @@ -41,18 +40,13 @@ public void registerParser(@Nullable String prefix, @NotNull MaterialParser pars
log.fine(String.format("Registered material parser with prefix %s", prefix));
}

@Nullable
public MaterialParser getParser(@Nullable String prefix) {
return this.registeredParsers.get((prefix == null ? null : prefix.toLowerCase()));
}

@Data
@AllArgsConstructor
public static class Pair<F, S> {
private final F first;
private final S second;
}

private Pair<TargetMaterial, Double> parseMaterialAndChance(MaterialParser parser, String input) {
@NotNull
private Pair<TargetMaterial, Double> parseMaterialAndChance(MaterialParser parser, String input) throws IllegalArgumentException {
// The part until the last colon that's not part of 'https://'
Matcher matcher = Pattern.compile("(?<!http(?s)):(?!//)").matcher(input);

Expand Down Expand Up @@ -106,7 +100,8 @@ private Pair<TargetMaterial, Double> parseMaterialAndChance(MaterialParser parse
}
}

public DynamicMaterial parseDynamicMaterial(String input) {
@NotNull
public DynamicMaterial parseDynamicMaterial(String input) throws IllegalArgumentException {
List<String> materials = Arrays.asList(input.split(";"));

// Materials without a chance.
Expand Down Expand Up @@ -142,8 +137,7 @@ public DynamicMaterial parseDynamicMaterial(String input) {
parser = getParser(null);

if (parser == null) {
log.warning(String.format("No valid parser found for material input %s", input));
return null;
throw new IllegalArgumentException(String.format("Material '%s' is invalid. No valid material parser found.", input));
}

log.fine("No prefix");
Expand Down Expand Up @@ -193,7 +187,8 @@ public DynamicMaterial parseDynamicMaterial(String input) {
* @return Parsed material or null when no parser was found.
* @throws IllegalArgumentException When the parser is unable to parse the material.
*/
public @Nullable TargetMaterial parseMaterial(@NotNull String input) throws IllegalArgumentException {
@NotNull
public TargetMaterial parseMaterial(@NotNull String input) throws IllegalArgumentException {

// Separate parts
String[] parts = new String[]{input};
Expand All @@ -214,8 +209,7 @@ public DynamicMaterial parseDynamicMaterial(String input) {
parser = getParser(null);

if (parser == null) {
log.fine(String.format("No valid parser found for material input %s", input));
return null;
throw new IllegalArgumentException(String.format("Material '%s' invalid. No valid parser found", input));
}
} else {
// remove parts[0] aka the parser prefix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import dev.lone.itemsadder.api.CustomBlock;
import nl.aurorion.blockregen.system.preset.struct.material.ItemsAdderMaterial;
import nl.aurorion.blockregen.system.preset.struct.material.TargetMaterial;
import org.jetbrains.annotations.NotNull;

public class ItemsAdderMaterialParser implements MaterialParser {

@Override
public TargetMaterial parseMaterial(String input) throws IllegalArgumentException {

public @NotNull TargetMaterial parseMaterial(String input) throws IllegalArgumentException {
if (!CustomBlock.isInRegistry(input)) {
throw new IllegalArgumentException(input + " is not a valid ItemsAdder custom block.");
throw new IllegalArgumentException(String.format("'%s' is not a valid ItemsAdder custom block.", input));
}

return new ItemsAdderMaterial(input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import nl.aurorion.blockregen.BlockRegen;
import nl.aurorion.blockregen.system.preset.struct.material.MMOIItemsMaterial;
import nl.aurorion.blockregen.system.preset.struct.material.TargetMaterial;
import org.jetbrains.annotations.NotNull;

public class MMOItemsMaterialParser implements MaterialParser{
public class MMOItemsMaterialParser implements MaterialParser {

private final BlockRegen plugin;

Expand All @@ -13,14 +14,12 @@ public MMOItemsMaterialParser(BlockRegen plugin) {
}

@Override
public TargetMaterial parseMaterial(String input) throws IllegalArgumentException {
int id;
public @NotNull TargetMaterial parseMaterial(String input) throws IllegalArgumentException {
try {
id = Integer.parseInt(input);
int id = Integer.parseInt(input);
return new MMOIItemsMaterial(plugin, id);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid MMOItem block id: " + input);
throw new IllegalArgumentException(String.format("Invalid MMOItem block id: '%s'.", input));
}

return new MMOIItemsMaterial(plugin, id);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nl.aurorion.blockregen.system.material.parser;

import nl.aurorion.blockregen.system.preset.struct.material.TargetMaterial;
import org.jetbrains.annotations.NotNull;

public interface MaterialParser {

Expand All @@ -9,12 +10,14 @@ public interface MaterialParser {
*
* @param input String to parse from with the material prefix already removed. (ex.: 'oraxen:caveblock', input = 'caveblock').
* @return Parsed TargetMaterial
*
* @throws IllegalArgumentException if the provided {@code input} is not a valid oraxen block id
*/
@NotNull
TargetMaterial parseMaterial(String input) throws IllegalArgumentException;

// Return true if the material syntax contains colons.
/**
* @return True if the material syntax contains colons.
*/
default boolean containsColon() {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.cryptomorin.xseries.XMaterial;
import lombok.extern.java.Log;
import nl.aurorion.blockregen.BlockRegen;
import nl.aurorion.blockregen.ParseUtil;
import nl.aurorion.blockregen.system.preset.struct.material.MinecraftMaterial;
import nl.aurorion.blockregen.system.preset.struct.material.TargetMaterial;
import nl.aurorion.blockregen.ParseUtil;
import nl.aurorion.blockregen.version.api.NodeData;
import org.jetbrains.annotations.NotNull;

@Log
public class MinecraftMaterialParser implements MaterialParser {
Expand All @@ -18,7 +19,7 @@ public MinecraftMaterialParser(BlockRegen plugin) {
}

@Override
public TargetMaterial parseMaterial(String input) throws IllegalArgumentException {
public @NotNull TargetMaterial parseMaterial(String input) throws IllegalArgumentException {
log.fine(String.format("Parsing MC material from %s", input));

boolean loadData = false;
Expand All @@ -33,7 +34,7 @@ public TargetMaterial parseMaterial(String input) throws IllegalArgumentExceptio
XMaterial xMaterial = ParseUtil.parseMaterial(materialPart, plugin.getVersionManager().isCurrentAbove("1.12.2", false));

if (xMaterial == null) {
throw new IllegalArgumentException("Could not parse minecraft material: " + materialPart);
throw new IllegalArgumentException("Could not parse minecraft material '" + materialPart + "'.");
}

if (loadData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import nl.aurorion.blockregen.BlockRegen;
import nl.aurorion.blockregen.system.preset.struct.material.OraxenMaterial;
import nl.aurorion.blockregen.system.preset.struct.material.TargetMaterial;
import org.jetbrains.annotations.NotNull;

public class OraxenMaterialParser implements MaterialParser {

Expand All @@ -14,9 +15,9 @@ public OraxenMaterialParser(BlockRegen plugin) {
}

@Override
public TargetMaterial parseMaterial(String input) throws IllegalArgumentException {
public @NotNull TargetMaterial parseMaterial(String input) throws IllegalArgumentException {
if (!OraxenBlocks.isOraxenBlock(input)) {
throw new IllegalArgumentException(input + " is not an Oraxen block");
throw new IllegalArgumentException(String.format("'%s' is not an Oraxen block.", input));
}

return new OraxenMaterial(this.plugin, input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ public void load(String name) {
TargetMaterial targetMaterial;
try {
targetMaterial = this.plugin.getMaterialManager().parseMaterial(targetMaterialInput);
if (targetMaterial == null) {
log.warning(String.format("Could not load preset %s, invalid target material %s.", name, targetMaterialInput));
return;
}
preset.setTargetMaterial(targetMaterial);
} catch (IllegalArgumentException e) {
log.warning(String.format("Could not load preset %s: %s", name, e.getMessage()));
Expand Down
Loading

0 comments on commit 0b9cc66

Please sign in to comment.