diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/inventory/InventoryConfig.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/inventory/InventoryConfig.java
index ea70196c..963a5902 100644
--- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/inventory/InventoryConfig.java
+++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/inventory/InventoryConfig.java
@@ -111,7 +111,7 @@ public class InventoryConfig extends ACheckConfig {
fastConsumeCheck = data.getBoolean(ConfPaths.INVENTORY_FASTCONSUME_CHECK);
fastConsumeDuration = (long) (1000.0 * data.getDouble(ConfPaths.INVENTORY_FASTCONSUME_DURATION));
fastConsumeWhitelist = data.getBoolean(ConfPaths.INVENTORY_FASTCONSUME_WHITELIST);
- data.readMaterialFromList(ConfPaths.INVENTORY_FASTCONSUME_ITEMS, fastConsumeItems);
+ data.readMaterialIdsFromList(ConfPaths.INVENTORY_FASTCONSUME_ITEMS, fastConsumeItems);
fastConsumeActions = data.getOptimizedActionList(ConfPaths.INVENTORY_FASTCONSUME_ACTIONS, Permissions.INVENTORY_FASTCONSUME);
instantBowCheck = data.getBoolean(ConfPaths.INVENTORY_INSTANTBOW_CHECK);
diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/config/RawConfigFile.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/config/RawConfigFile.java
index 5cb09975..78dd45cf 100644
--- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/config/RawConfigFile.java
+++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/config/RawConfigFile.java
@@ -12,26 +12,52 @@ import fr.neatmonster.nocheatplus.logging.LogUtil;
public class RawConfigFile extends YamlConfiguration{
+ private static String prepareMatchMaterial(String content) {
+ return content.replace(' ', '_').replace('-', '_').replace('.', '_');
+ }
+
/**
* Attempt to get an int id from a string.
* Will return out of range numbers, attempts to parse materials.
* @param content
* @return
*/
- public static Integer parseTypeId(String content) {
+ @SuppressWarnings("deprecation")
+ public static Integer parseTypeId(String content) {
content = content.trim().toUpperCase();
try{
return Integer.parseInt(content);
}
catch(NumberFormatException e){}
try{
- Material mat = Material.matchMaterial(content.replace(' ', '_').replace('-', '_').replace('.', '_'));
+ Material mat = Material.matchMaterial(prepareMatchMaterial(content));
if (mat != null) return mat.getId();
}
catch (Exception e) {}
return null;
}
+ /**
+ * Attempt to get a Material from a string.
+ * Will attempt to match the name but also type ids.
+ * @param content
+ * @return
+ */
+ @SuppressWarnings("deprecation")
+ public static Material parseMaterial(String content) {
+ content = content.trim().toUpperCase();
+ try{
+ Integer id = Integer.parseInt(content);
+ return Material.getMaterial(id);
+ }
+ catch(NumberFormatException e){}
+ try{
+ return Material.matchMaterial(prepareMatchMaterial(content));
+ }
+ catch (Exception e) {}
+ return null;
+ }
+
////////////////
// Not static.
////////////////
@@ -119,7 +145,7 @@ public class RawConfigFile extends YamlConfiguration{
* @param path
* @param target Collection to fill ids into.
*/
- public void readMaterialFromList(final String path, final Collection target) {
+ public void readMaterialIdsFromList(final String path, final Collection target) {
final List content = getStringList(path);
if (content == null || content.isEmpty()) return;
for (final String entry : content){
@@ -132,6 +158,25 @@ public class RawConfigFile extends YamlConfiguration{
}
}
}
+
+ /**
+ * Outputs warnings to console.
+ * @param path
+ * @param target Collection to fill materials into.
+ */
+ public void readMaterialFromList(final String path, final Collection target) {
+ final List content = getStringList(path);
+ if (content == null || content.isEmpty()) return;
+ for (final String entry : content){
+ final Material mat = parseMaterial(entry);
+ if (mat == null){
+ LogUtil.logWarning("[NoCheatPlus] Bad material entry (" + path +"): " + entry);
+ }
+ else{
+ target.add(mat);
+ }
+ }
+ }
/* (non-Javadoc)
* @see org.bukkit.configuration.file.YamlConfiguration#saveToString()
diff --git a/NCPCore/src/test/java/fr/neatmonster/nocheatplus/test/TestConfig.java b/NCPCore/src/test/java/fr/neatmonster/nocheatplus/test/TestConfig.java
new file mode 100644
index 00000000..b7d11566
--- /dev/null
+++ b/NCPCore/src/test/java/fr/neatmonster/nocheatplus/test/TestConfig.java
@@ -0,0 +1,40 @@
+package fr.neatmonster.nocheatplus.test;
+
+import static org.junit.Assert.fail;
+
+import org.bukkit.Material;
+import org.junit.Test;
+
+import fr.neatmonster.nocheatplus.config.RawConfigFile;
+
+public class TestConfig {
+
+ private void testReadMaterial(String input, Material expectedMat) {
+ Material mat = RawConfigFile.parseMaterial(input);
+ if (expectedMat != mat) {
+ fail("Expected " + expectedMat + " for input '" + input + "', got instead: " + mat);
+ }
+ }
+
+ @SuppressWarnings("deprecation")
+ @Test
+ public void testReadMaterial() {
+ // Some really needed parts first.
+ testReadMaterial("water lily", Material.WATER_LILY);
+ testReadMaterial("water-lily", Material.WATER_LILY);
+ testReadMaterial("watEr_lily", Material.WATER_LILY);
+
+ testReadMaterial("flint and steel", Material.FLINT_AND_STEEL);
+ testReadMaterial("259", Material.FLINT_AND_STEEL);
+
+ // Generic test.
+ for (final Material mat : Material.values()) {
+ if (mat.name().equalsIgnoreCase("LOCKED_CHEST")) {
+ continue;
+ }
+ testReadMaterial(mat.name(), mat);
+ testReadMaterial(Integer.toString(mat.getId()), mat);
+ }
+
+ }
+}