(add) Generic hook for the Player class name, defaults to exempt all non

CraftPlayer classes from checks.
This commit is contained in:
asofold 2012-07-24 08:22:16 +02:00
parent b293b29314
commit 4e45a0f415
4 changed files with 105 additions and 1 deletions

View File

@ -26,7 +26,8 @@ VERSION HISTORY
---------------------------
(0.3.0)
- (remove) Direct hooking of Citizens.
- (add) Generic hook for the Player class name, defaults to exempt all non CraftPlayer classes from checks.
- (remove) Direct hooking of Citizens (+direct CraftBukkit dependency).
(0.2.2)
- (bugfix) Add noswing treatment for mcMMO.

View File

@ -11,6 +11,7 @@ import java.util.logging.Logger;
import me.asofold.bukkit.cncp.config.compatlayer.CompatConfig;
import me.asofold.bukkit.cncp.config.compatlayer.NewConfig;
import me.asofold.bukkit.cncp.hooks.Hook;
import me.asofold.bukkit.cncp.hooks.generic.HookPlayerClass;
import me.asofold.bukkit.cncp.setttings.GroupHooks;
import me.asofold.bukkit.cncp.setttings.Settings;
import me.asofold.bukkit.cncp.utils.Utils;
@ -47,6 +48,8 @@ public class CompatNoCheatPlus extends JavaPlugin implements Listener {
private final Settings settings = new Settings();
private final HookPlayerClass hookPlayerClass = new HookPlayerClass();
/**
* Flag if plugin is enabled.
*/
@ -144,6 +147,7 @@ public class CompatNoCheatPlus extends JavaPlugin implements Listener {
* Add standard hooks if available.
*/
private void addAvailableHooks() {
addHook(hookPlayerClass);
try{
addHook(new me.asofold.bukkit.cncp.hooks.mcmmo.HookmcMMO());
}
@ -175,6 +179,10 @@ public class CompatNoCheatPlus extends JavaPlugin implements Listener {
cfg.load();
if (Settings.addDefaults(cfg)) cfg.save();
settings.fromConfig(cfg);
// Set hookPlayerClass properties
hookPlayerClass.setClassNames(settings.exemptPlayerClassNames);
hookPlayerClass.setExemptAll(settings.exemptAllPlayerClassNames);
hookPlayerClass.setPlayerClassName(settings.playerClassName);
// Re-enable plugins that were not yet on the list:
Server server = getServer();
Logger logger = server.getLogger();
@ -217,6 +225,7 @@ public class CompatNoCheatPlus extends JavaPlugin implements Listener {
}
});
}
return true;
}

View File

@ -0,0 +1,77 @@
package me.asofold.bukkit.cncp.hooks.generic;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import me.asofold.bukkit.cncp.hooks.AbstractHook;
import fr.neatmonster.nocheatplus.checks.CheckEvent;
public final class HookPlayerClass extends AbstractHook {
private final Set<String> classNames = new HashSet<String>();
private boolean exemptAll = true;
private boolean checkSuperClass = true;
/**
* Normal class name.
*/
private String playerClassName = "CraftPlayer";
public HookPlayerClass(){
this.classNames.addAll(classNames);
}
public final void setClassNames(final Collection<String> classNames){
this.classNames.clear();
this.classNames.addAll(classNames);
}
public final void setExemptAll(final boolean exemptAll){
this.exemptAll = exemptAll;
}
public final void setPlayerClassName(final String playerClassName){
this.playerClassName = playerClassName;
}
public final void setCheckSuperClass(final boolean superClass){
this.checkSuperClass = superClass;
}
@Override
public final String getHookName() {
return "PlayerClass(default)";
}
@Override
public final String getHookVersion() {
return "0.0";
}
@Override
public final void processEvent(final String group, final String check, final CheckEvent event) {
if (exemptAll && !event.getPlayer().getBukkitPlayer().getClass().getSimpleName().equals(playerClassName)) event.setCancelled(true);
else {
if (classNames.isEmpty()) return;
final Class<?> clazz = event.getPlayer().getBukkitPlayer().getClass();
final String name = clazz.getSimpleName();
if (classNames.contains(name)) event.setCancelled(true);
else if (checkSuperClass){
while (true){
final Class<?> superClass = clazz.getSuperclass();
if (superClass == null) return;
final String superName = superClass.getSimpleName();
if (superName.equals("Object")) return;
else if (classNames.contains(superName)){
event.setCancelled(true);
return;
}
}
}
}
}
}

View File

@ -1,6 +1,8 @@
package me.asofold.bukkit.cncp.setttings;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Set;
import me.asofold.bukkit.cncp.config.compatlayer.CompatConfig;
@ -10,11 +12,20 @@ import me.asofold.bukkit.cncp.config.compatlayer.NewConfig;
public class Settings {
public Set<String> forceEnableLater = new LinkedHashSet<String>();
public Set<String> loadPlugins = new LinkedHashSet<String>();
public Set<String> exemptPlayerClassNames = new HashSet<String>();
public boolean exemptAllPlayerClassNames = true;
public String playerClassName = "CraftPlayer";
public boolean exemptSuperClass = true;
public static CompatConfig getDefaultConfig(){
CompatConfig cfg = new NewConfig(null);
Settings ref = new Settings();
cfg.set("plugins.force-enable-later", ConfigUtil.asList(new String[]{ "NoCheatPlus" }));
cfg.set("plugins.ensure-enable", ConfigUtil.asList(new String[]{ "WorldGuard" }));
cfg.set("plugins.hooks.player-class.exempt-names", new LinkedList<String>());
cfg.set("plugins.hooks.player-class.exempt-all", ref.exemptAllPlayerClassNames);
cfg.set("plugins.hooks.player-class.class-name", ref.playerClassName);
cfg.set("plugins.hooks.player-class.super-class", ref.exemptSuperClass);
return cfg;
}
@ -23,9 +34,15 @@ public class Settings {
}
public boolean fromConfig(CompatConfig cfg){
Settings ref = new Settings();
// plugins to force enabling after this plugin.
ConfigUtil.readStringSetFromList(cfg, "plugins.force-enable-later", forceEnableLater, true, true, false);
ConfigUtil.readStringSetFromList(cfg, "plugins.ensure-enable", loadPlugins, true, true, false);
// Generic player class name hook:
ConfigUtil.readStringSetFromList(cfg, "plugins.hooks.player-class.exempt-names", exemptPlayerClassNames, true, true, false);
exemptAllPlayerClassNames = cfg.getBoolean("plugins.hooks.player-class.exempt-all", ref.exemptAllPlayerClassNames);
playerClassName = cfg.getString("plugins.hooks.player-class.class-name", ref.playerClassName);
exemptSuperClass = cfg.getBoolean("plugins.hooks.player-class.super-class", ref.exemptSuperClass);
return true;
}