Rename + extend config support for reading Material.

This commit is contained in:
asofold 2014-05-18 20:33:24 +02:00
parent c93edcbbe9
commit c6dbd7330b
3 changed files with 89 additions and 4 deletions

View File

@ -111,7 +111,7 @@ public class InventoryConfig extends ACheckConfig {
fastConsumeCheck = data.getBoolean(ConfPaths.INVENTORY_FASTCONSUME_CHECK); fastConsumeCheck = data.getBoolean(ConfPaths.INVENTORY_FASTCONSUME_CHECK);
fastConsumeDuration = (long) (1000.0 * data.getDouble(ConfPaths.INVENTORY_FASTCONSUME_DURATION)); fastConsumeDuration = (long) (1000.0 * data.getDouble(ConfPaths.INVENTORY_FASTCONSUME_DURATION));
fastConsumeWhitelist = data.getBoolean(ConfPaths.INVENTORY_FASTCONSUME_WHITELIST); 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); fastConsumeActions = data.getOptimizedActionList(ConfPaths.INVENTORY_FASTCONSUME_ACTIONS, Permissions.INVENTORY_FASTCONSUME);
instantBowCheck = data.getBoolean(ConfPaths.INVENTORY_INSTANTBOW_CHECK); instantBowCheck = data.getBoolean(ConfPaths.INVENTORY_INSTANTBOW_CHECK);

View File

@ -12,26 +12,52 @@ import fr.neatmonster.nocheatplus.logging.LogUtil;
public class RawConfigFile extends YamlConfiguration{ 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.<br> * Attempt to get an int id from a string.<br>
* Will return out of range numbers, attempts to parse materials. * Will return out of range numbers, attempts to parse materials.
* @param content * @param content
* @return * @return
*/ */
public static Integer parseTypeId(String content) { @SuppressWarnings("deprecation")
public static Integer parseTypeId(String content) {
content = content.trim().toUpperCase(); content = content.trim().toUpperCase();
try{ try{
return Integer.parseInt(content); return Integer.parseInt(content);
} }
catch(NumberFormatException e){} catch(NumberFormatException e){}
try{ try{
Material mat = Material.matchMaterial(content.replace(' ', '_').replace('-', '_').replace('.', '_')); Material mat = Material.matchMaterial(prepareMatchMaterial(content));
if (mat != null) return mat.getId(); if (mat != null) return mat.getId();
} }
catch (Exception e) {} catch (Exception e) {}
return null; return null;
} }
/**
* Attempt to get a Material from a string.<br>
* 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. // Not static.
//////////////// ////////////////
@ -119,7 +145,7 @@ public class RawConfigFile extends YamlConfiguration{
* @param path * @param path
* @param target Collection to fill ids into. * @param target Collection to fill ids into.
*/ */
public void readMaterialFromList(final String path, final Collection<Integer> target) { public void readMaterialIdsFromList(final String path, final Collection<Integer> target) {
final List<String> content = getStringList(path); final List<String> content = getStringList(path);
if (content == null || content.isEmpty()) return; if (content == null || content.isEmpty()) return;
for (final String entry : content){ for (final String entry : content){
@ -133,6 +159,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<Material> target) {
final List<String> 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) /* (non-Javadoc)
* @see org.bukkit.configuration.file.YamlConfiguration#saveToString() * @see org.bukkit.configuration.file.YamlConfiguration#saveToString()
*/ */

View File

@ -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);
}
}
}