Add ability configure passable check to ignore certain blocks.

This commit is contained in:
asofold 2012-10-05 17:14:41 +02:00
parent 73d2f79f8a
commit 1f10a81448
5 changed files with 104 additions and 10 deletions

View File

@ -228,6 +228,10 @@ public class NoCheatPlus extends JavaPlugin implements Listener {
// Read the configuration files.
ConfigManager.init(this);
final ConfigFile config = ConfigManager.getConfigFile();
BlockProperties.applyConfig(config, ConfPaths.COMPATIBILITY_BLOCKS); // Temp probably,
// List the events listeners and register.
Bukkit.getPluginManager().registerEvents(this, this);
@ -244,6 +248,15 @@ public class NoCheatPlus extends JavaPlugin implements Listener {
new InventoryListener(),
new MovingListener(),
new Workarounds(),
new INotifyReload() {
@Override
public void onReload() {
BlockProperties.init();
// TODO: This is a quick workaround, might later be split to other folder/files.
final ConfigFile config = ConfigManager.getConfigFile();
BlockProperties.applyConfig(config, ConfPaths.COMPATIBILITY_BLOCKS);
}
},
}){
addComponent(obj);
}
@ -264,7 +277,6 @@ public class NoCheatPlus extends JavaPlugin implements Listener {
}
}, 1207, 1207);
ConfigFile config = ConfigManager.getConfigFile();
// Setup the graphs, plotters and start Metrics.
if (config.getBoolean(ConfPaths.MISCELLANEOUS_REPORTTOMETRICS)) {

View File

@ -487,4 +487,12 @@ public abstract class ConfPaths {
*/
public static final String STRINGS = "strings";
// Compatibility section (possibly temporary).
@GlobalConfig
public static final String COMPATIBILITY = "compatibility.";
public static final String COMPATIBILITY_BLOCKS = COMPATIBILITY + "blocks.";
// Sub paths that are used with different path prefixes potentially.
public static final String SUB_IGNOREPASSABLE = "ignorepassable";
}

View File

@ -2,6 +2,7 @@ package fr.neatmonster.nocheatplus.config;
import java.lang.reflect.Field;
import org.bukkit.Material;
import org.bukkit.configuration.MemorySection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.yaml.snakeyaml.DumperOptions;
@ -58,6 +59,53 @@ public class ConfigFile extends YamlConfiguration {
else if (value > max) return max;
else return value;
}
/**
* Attempt to get a type id from the path somehow, return null if nothing found.<br>
* Will attempt to interpret strings, will return negative or out of range values.
* @param path
* @return
*/
public Integer getTypeId(final String path){
return getTypeId(path, null);
}
/**
* Attempt to get a type id from the path somehow, return preset if nothing found.<br>
* Will attempt to interpret strings, will return negative or out of range values.
* @param path
* @param preset
* @return
*/
public Integer getTypeId(final String path, final Integer preset){
String content = getString(path, null);
if (content != null){
Integer id = parseTypeId(content);
if (id != null) return id;
}
int id = getInt(path, Integer.MAX_VALUE);
return id == Integer.MAX_VALUE ? preset : id;
}
/**
* Attempt to get an int id from a string.<br>
* Will return out of range numbers, attempts to parse materials.
* @param content
* @return
*/
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('.', '_'));
if (mat != null) return mat.getId();
}
catch (Exception e) {}
return null;
}
/**
* Do this after reading new data.

View File

@ -4,6 +4,8 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import org.bukkit.Material;
/*
* M""""""'YMM .8888b dP dP MM'""""'YMM .8888b oo
* M mmmm. `M 88 " 88 88 M' .mmm. `M 88 "
@ -427,6 +429,10 @@ public class DefaultConfig extends ConfigFile {
set(ConfPaths.STRINGS + ".tempkick1", "ncp tempkick [player] 1 Wait a minute!");
set(ConfPaths.STRINGS + ".tempkick5", "ncp tempkick [player] 5 You have five minutes to think about it!");
set(ConfPaths.COMPATIBILITY_BLOCKS + ConfPaths.SUB_IGNOREPASSABLE, new LinkedList<String>(Arrays.asList(new String[]{
Material.WOODEN_DOOR.name(), Material.IRON_DOOR_BLOCK.name(),
})));
// Update internal factory based on all the new entries to the "actions" section.
regenerateActionLists();
}

View File

@ -17,6 +17,9 @@ import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import fr.neatmonster.nocheatplus.config.ConfPaths;
import fr.neatmonster.nocheatplus.config.ConfigFile;
/**
* Poperties of blocks.
*
@ -245,16 +248,21 @@ public class BlockProperties {
public static final int F_IGN_PASSABLE = 0x8;
static{
try{
initTools();
initBlocks();
}
catch(Throwable t){
t.printStackTrace();
}
init();
}
public static void init() {
try{
initTools();
initBlocks();
}
catch(Throwable t){
t.printStackTrace();
}
}
private static void initTools() {
tools.clear();
tools.put(268, new ToolProps(ToolType.SWORD, MaterialBase.WOOD));
tools.put(269, new ToolProps(ToolType.SPADE, MaterialBase.WOOD));
tools.put(270, new ToolProps(ToolType.PICKAXE, MaterialBase.WOOD));
@ -283,8 +291,7 @@ public class BlockProperties {
tools.put(359, new ToolProps(ToolType.SHEARS, MaterialBase.NONE));
}
private static void initBlocks() {
private static void initBlocks() {
for (int i = 0; i <maxBlocks; i++){
blocks[i] = null; // hmmm
}
@ -864,5 +871,18 @@ public class BlockProperties {
public static final boolean isPassable(final Location loc) {
return isPassable(((org.bukkit.craftbukkit.CraftWorld) loc.getWorld()).getHandle(), loc.getX(), loc.getY(), loc.getZ(), loc.getBlock().getTypeId());
}
/**
* API access to read extra properties from files.
* @param config
*/
public static void applyConfig(final ConfigFile config, final String pathPrefix) {
// Ignore passable.
for (final String input : config.getStringList(pathPrefix + ConfPaths.SUB_IGNOREPASSABLE)){
final Integer id = ConfigFile.parseTypeId(input);
if (id == null || id < 0 || id >= 4096) CheckUtils.logWarning("[NoCheatplus] Bad block id (" + pathPrefix + ConfPaths.SUB_IGNOREPASSABLE + "): " + input);
else blockFlags[id] |= F_IGN_PASSABLE;
}
}
}