Adapt to API being put to NCP !

This commit is contained in:
asofold 2012-08-08 01:38:28 +02:00
parent f7079693b0
commit 6fcc3d016a
10 changed files with 13 additions and 509 deletions

View File

@ -1,6 +1,6 @@
name: CompatNoCheatPlus
main: me.asofold.bpl.cncp.CompatNoCheatPlus
version: 4.0.0
version: 5.0.0
depend:
- NoCheatPlus
softdepend:

View File

@ -12,31 +12,20 @@ import me.asofold.bpl.cncp.config.compatlayer.CompatConfig;
import me.asofold.bpl.cncp.config.compatlayer.NewConfig;
import me.asofold.bpl.cncp.hooks.Hook;
import me.asofold.bpl.cncp.hooks.generic.HookPlayerClass;
import me.asofold.bpl.cncp.hooks.ncp.CheckType;
import me.asofold.bpl.cncp.hooks.ncp.NCPHook;
import me.asofold.bpl.cncp.hooks.ncp.NCPHookManager;
import me.asofold.bpl.cncp.setttings.Settings;
import me.asofold.bpl.cncp.utils.Utils;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitScheduler;
import fr.neatmonster.nocheatplus.checks.CheckEvent;
import fr.neatmonster.nocheatplus.checks.blockbreak.Direction.DirectionEvent;
import fr.neatmonster.nocheatplus.checks.blockbreak.FastBreak.FastBreakEvent;
import fr.neatmonster.nocheatplus.checks.blockbreak.NoSwing.NoSwingEvent;
import fr.neatmonster.nocheatplus.checks.fight.Angle.AngleEvent;
import fr.neatmonster.nocheatplus.checks.fight.Speed.SpeedEvent;
import fr.neatmonster.nocheatplus.checks.moving.CreativeFly.CreativeFlyEvent;
import fr.neatmonster.nocheatplus.checks.moving.NoFall.NoFallEvent;
import fr.neatmonster.nocheatplus.checks.moving.SurvivalFly.SurvivalFlyEvent;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.hooks.NCPHook;
import fr.neatmonster.nocheatplus.hooks.NCPHookManager;
/**
* Quick attempt to provide compatibility to NoCheatPlus (by NeatMonster) for some other plugins that change the vanilla game mechanichs, for instance by fast block breaking.
@ -245,25 +234,5 @@ public class CompatNoCheatPlus extends JavaPlugin implements Listener {
super.onDisable();
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled=true)
final void onCheckFail(final CheckEvent event){
// TODO: This will be replaced by NCP invoking NCPHookManager.should... directly.
final CheckType checkId;
// horrible :)
if (event instanceof SurvivalFlyEvent) checkId = CheckType.MOVING_SURVIVALFLY;
else if (event instanceof CreativeFlyEvent) checkId = CheckType.MOVING_CREATIVEFLY;
else if (event instanceof NoFallEvent) checkId = CheckType.MOVING_NOFALL;
else if (event instanceof FastBreakEvent) checkId = CheckType.BLOCKBREAK_FASTBREAK;
else if (event instanceof NoSwingEvent) checkId = CheckType.BLOCKBREAK_NOSWING;
else if (event instanceof DirectionEvent) checkId = CheckType.BLOCKBREAK_DIRECTION;
else if (event instanceof SpeedEvent) checkId = CheckType.FIGHT_SPEED;
else if (event instanceof AngleEvent) checkId = CheckType.FIGHT_ANGLE;
else checkId = CheckType.UNKNOWN;
if (NCPHookManager.shouldCancelVLProcessing(checkId, event.getPlayer())) event.setCancelled(true);
}
}

View File

@ -1,10 +1,10 @@
package me.asofold.bpl.cncp.hooks;
import me.asofold.bpl.cncp.hooks.ncp.CheckType;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import fr.neatmonster.nocheatplus.checks.CheckType;
/**
* Extend this to make sure that future changes will not break your hooks.<br>
* Probable changes:<br>

View File

@ -1,10 +1,10 @@
package me.asofold.bpl.cncp.hooks;
import me.asofold.bpl.cncp.hooks.ncp.CheckType;
import me.asofold.bpl.cncp.hooks.ncp.NCPHook;
import org.bukkit.event.Listener;
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.hooks.NCPHook;
/**
* Interface for hooking into another plugin.<br>
* NOTE: You also have to implement the methods described in NCPHook.<br>

View File

@ -5,10 +5,11 @@ import java.util.HashSet;
import java.util.Set;
import me.asofold.bpl.cncp.hooks.AbstractHook;
import me.asofold.bpl.cncp.hooks.ncp.CheckType;
import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.checks.CheckType;
public final class HookPlayerClass extends AbstractHook {
private final Set<String> classNames = new HashSet<String>();

View File

@ -4,7 +4,6 @@ import java.util.HashMap;
import java.util.Map;
import me.asofold.bpl.cncp.hooks.AbstractHook;
import me.asofold.bpl.cncp.hooks.ncp.CheckType;
import me.asofold.bpl.cncp.utils.PluginGetter;
import org.bukkit.entity.Entity;
@ -18,6 +17,8 @@ import com.gmail.nossr50.events.fake.FakeBlockBreakEvent;
import com.gmail.nossr50.events.fake.FakeBlockDamageEvent;
import com.gmail.nossr50.events.fake.FakeEntityDamageByEntityEvent;
import fr.neatmonster.nocheatplus.checks.CheckType;
public final class HookmcMMO extends AbstractHook implements Listener {
private static final Map<CheckType, Integer> cancelChecksBlockBreak = new HashMap<CheckType, Integer>();

View File

@ -1,13 +0,0 @@
package me.asofold.bpl.cncp.hooks.ncp;
/**
* Extend this class for maximum future compatibility.<br>
* Especially the onCheckFailure method might get extended with check specific arguments,
* this class will provide compatibility with older method signatures, where possible.
* @author mc_dev
*
*/
public abstract class AbstractNCPHook implements NCPHook {
}

View File

@ -1,87 +0,0 @@
package me.asofold.bpl.cncp.hooks.ncp;
/**
* Type of checks and their groups (potentially parents).
* @author mc_dev
*
*/
public enum CheckType {
// GENERAL
/**
* Use to register for all checks.
*/
ALL,
/**
* Do not use to register, only for internals / compatibility issues,
* it might be passed to NCPHook.onCheckFailure.
*/
UNKNOWN,
// MOVING
MOVING,
MOVING_NOFALL(MOVING),
MOVING_SURVIVALFLY(MOVING),
MOVING_CREATIVEFLY(MOVING),
MOVING_MOREPACKETS(MOVING),
// FIGHT
FIGHT,
FIGHT_SPEED(FIGHT),
FIGHT_NOSWING(FIGHT),
FIGHT_DIRECTION(FIGHT),
FIGHT_REACH(FIGHT),
FIGHT_ANGLE(FIGHT),
FIGHT_CRITICAL(FIGHT),
FIGHT_GODMODE(FIGHT),
FIGHT_INSTANTHEAL(FIGHT),
FIGHT_KNOCKBACK(FIGHT),
// BLOCKBREAK
BLOCKBREAK,
BLOCKBREAK_FASTBREAK(BLOCKBREAK),
BLOCKBREAK_NOSWING(BLOCKBREAK),
BLOCKBREAK_DIRECTION(BLOCKBREAK),
BLOCKBREAK_REACH(BLOCKBREAK),
// BLOCKPLACE
BLOCKPLACE,
BLOCKPLACE_FASTPLACE(BLOCKPLACE),
BLOCKPLACE_NOSWING(BLOCKPLACE),
BLOCKPLACE_DIRECTION(BLOCKPLACE),
BLOCKPLACE_SPEED(BLOCKPLACE),
BLOCKPLACE_REACH(BLOCKPLACE),
// BLOCKINTERACT
BLOCKINTERACT,
BLOCKINTERACT_NOSWING(BLOCKINTERACT),
BLOCKINTERACT_DIRECTION(BLOCKINTERACT),
BLOCKINTERACT_REACH(BLOCKINTERACT),
// INVENTORY
INVENTORY,
INVENTORY_DROP(INVENTORY),
INVENTORY_INSTANTBOW(INVENTORY),
INVENTORY_INSTANTEAT(INVENTORY),
// CHAT
CHAT,
CHAT_ARRIVALS(CHAT),
CHAT_NOPWNAGE(CHAT),
; // end of members.
/**
* The check group, null if it is a group itself.
*/
public final CheckType group;
private CheckType(){
group = null;
}
private CheckType(CheckType group){
this.group = group;
}
}

View File

@ -1,39 +0,0 @@
package me.asofold.bpl.cncp.hooks.ncp;
import org.bukkit.entity.Player;
/**
* Compatibility hooks have to implement this.<br>
* To set VL etc they have to make use of another API,
* be it in NCPHookManager or probably rather better in NCP or some NCPAPI class.
* @author mc_dev
*
*/
public interface NCPHook{
/**
* For logging purposes.
* @return
*/
public String getHookName();
/**
* For logging purposes.
* @return
*/
public String getHookVersion();
/**
* This is called on failure of a check.<br>
* This is the minimal interface, it might later be extended by specific information
* like (target) locations and VL, but with this a lot is possible already (see cncp).<br>
* See AbstractNCPHook for future compatibility questions.
* @param checkType the check that failed.
* @param player The player that failed the check.
* @return If to cancel the check failure processing.
*/
public boolean onCheckFailure(CheckType checkType, Player player);
}

View File

@ -1,328 +0,0 @@
package me.asofold.bpl.cncp.hooks.ncp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
/**
* After-check-failure hook manager to be integrated into NoCheatPlus.<br>
*
*
* Questions: sync for registering, or extra map for chat ? [maybe setLocked flag with sync object just for changing and query from external thread]
*
* @author mc_dev
*
*/
public final class NCPHookManager {
/* ----------------------------------------------------------------------- */
/* Internal data: */
/**
* Ids given to hooks.
*/
private static int maxHookId = 0;
/**
* Hook id to hook.
*/
private final static Map<Integer, NCPHook> allHooks = new HashMap<Integer, NCPHook>();
/**
* Mapping the check types to the hooks.
*/
private static final Map<CheckType, List<NCPHook>> hooksByChecks = new HashMap<CheckType, List<NCPHook>>();
/* ----------------------------------------------------------------- */
/* Internals: manage hooks internally */
private static Integer getNewHookId(){
maxHookId ++;
return maxHookId;
}
/**
* for registration purposes only.
* @param hook
* @return Unique id associated with that hook (returns an existing id if hook is already present).
*/
private static Integer getId(NCPHook hook){
if (hook == null) throw new NullPointerException("Hooks must not be null."); // just in case.
Integer id = null;
for (Integer refId : allHooks.keySet()){
if (hook == allHooks.get(refId)){
id = refId;
break;
}
}
if (id == null){
id = getNewHookId();
allHooks.put(id, hook);
}
return id;
}
/**
* Add hook to the hooksByChecks mappings, for the check type and if present, group type.
* assumes that the hook already has been registered in the allHooks map.
* @param checkType
* @param hook
*/
private static void addToMappings(CheckType checkType, NCPHook hook) {
addToMapping(checkType, hook);
if (checkType.group != null) addToMapping(checkType.group, hook);
}
/**
* Add to the mapping for given check type (only).
* @param checkId
* @param hook
*/
private static void addToMapping(CheckType checkType, NCPHook hook) {
List<NCPHook> hooks = hooksByChecks.get(checkType);
if (hooks == null){
hooks = new ArrayList<NCPHook>();
hooks.add(hook);
hooksByChecks.put(checkType, hooks);
}
else if (!hooks.contains(hook)) hooks.add(hook);
}
/**
* Remove from internal mappings, both allHooks and hooksByChecks.
* @param hook
* @param hookId
*/
private static void removeFromMappings(NCPHook hook, Integer hookId) {
allHooks.remove(hookId);
List<CheckType> rem = new LinkedList<CheckType>();
for (CheckType checkId : hooksByChecks.keySet()){
List<NCPHook> hooks = hooksByChecks.get(checkId);
if (hooks.remove(hook)){
if (hooks.isEmpty()) rem.add(checkId);
}
}
for (CheckType checkId : rem){
hooksByChecks.remove(checkId);
}
}
private static final String getHookDescription(final NCPHook hook){
return hook.getHookName() + " [" + hook.getHookVersion() + "]";
}
private static final void logHookAdded(NCPHook hook){
Bukkit.getLogger().info("[NoCheatPlus/Compat] Added hook: " + getHookDescription(hook));
}
private static final void logHookRemoved(NCPHook hook){
Bukkit.getLogger().info("[NoCheatPlus/Compat] Removed hook: " + getHookDescription(hook));
}
private static final void logHookFailure(final CheckType checkType, final Player player, final NCPHook hook, final Throwable t){
// TODO: might accumulate failure rate and only log every so and so seconds or disable hook if spamming (leads to ncp spam though)?
final StringBuilder builder = new StringBuilder(1024);
builder.append("[NoCheatPlus/Compat] Hook " + getHookDescription(hook) + " encountered an unexpected exception:\n");
builder.append("Processing: ");
if (checkType.group != null) builder.append("Group " + checkType.group + " ");
builder.append("Check " + checkType);
builder.append(" Player " + player.getName());
builder.append("\n");
builder.append("Exception (" + t.getClass().getSimpleName() + "): " + t.getMessage() + "\n");
for (StackTraceElement el : t.getStackTrace()){
builder.append(el.toString());
}
Bukkit.getLogger().severe(builder.toString());
}
private static final boolean applyHooks(final CheckType checkType, final Player player, final List<NCPHook> hooks) {
for (int i = 0; i < hooks.size(); i ++){
final NCPHook hook = hooks.get(i);
try{
if (hook.onCheckFailure(checkType, player)) return true;
}
catch (Throwable t){
// TODO: maybe distinguish some exceptions here (Interrupted ?).
logHookFailure(checkType, player, hook, t);
}
}
return false;
}
/* ------------------------------------------------------------------------------------- */
/* Internal API for NCP */
/*
* Internally NCP will now instead of events call hooks:
*/
/**
* This is called by checks when players fail them.
* @param checkId Id for the check, should be taken from the constants defined in this class.
* @param player The player that fails the check.
* @return if to cancel the VL processing.
*/
public static final boolean shouldCancelVLProcessing(final CheckType checkType, final Player player){
// checks for hooks registered for all events, only for the group and specifically for the check.
// A Paradigm could be to return true as soon as one hook has returned true.
// Most specific:
final List<NCPHook> hooksCheck = hooksByChecks.get(checkType);
if (hooksCheck != null){
if (applyHooks(checkType, player, hooksCheck)) return true;
}
// Group:
if (checkType.group != null){
final List<NCPHook> hooksGroup= hooksByChecks.get(checkType);
if (hooksCheck != null){
if (applyHooks(checkType, player, hooksGroup)) return true;
}
}
// general (all):
final List<NCPHook> hooksAll = hooksByChecks.get(CheckType.ALL);
if (hooksAll != null){
if (applyHooks(checkType, player, hooksAll)) return true;
}
return false;
}
/* ----------------------------------------------------------------- */
/* External API for adding hooks etc. */
/**
* Register a hook for a specific check type (all, group, or an individual check).
* @param checkType
* @param hook
* @return An id to identify the hook, will return the existing id if the hook was already present somewhere.
*/
public static Integer addHook(CheckType checkType, NCPHook hook){
Integer hookId = getId(hook);
addToMappings(checkType, hook);
logHookAdded(hook);
return hookId;
}
/**
* Register a hook for several individual checks ids (all, group, or an individual checks).
* @param checkTypes Array of check types to register the hook for. If you pass null this hook will be registered for all checks.
* @param hook
* @return
*/
public static Integer addHook(CheckType[] checkTypes, NCPHook hook){
if (checkTypes == null) checkTypes = new CheckType[]{CheckType.ALL};
Integer hookId = getId(hook);
for (CheckType checkType : checkTypes){
addToMappings(checkType, hook);
}
logHookAdded(hook);
return hookId;
}
/**
* Remove a hook.
* @param hook
* @return hook id if present, null otherwise.
*/
public static Integer removeHook(NCPHook hook){
Integer hookId = null;
for (Integer refId : allHooks.keySet()){
if (hook == allHooks.get(refId)){
hookId = refId;
break;
}
}
if (hookId == null) return null;
removeFromMappings(hook, hookId);
logHookRemoved(hook);
return hookId;
}
/**
* Remove a hook by its hook id (returned on adding hooks).
* @param hookId if present, null otherwise.
* @return
*/
public static NCPHook removeHook(Integer hookId){
NCPHook hook = allHooks.get(hookId);
if (hook == null) return null;
removeFromMappings(hook, hookId);
logHookRemoved(hook);
return hook;
}
/**
* Remove a hook by its name (case sensitive, exact match).
* @param hookName
* @return
*/
public static Collection<NCPHook> removeHooks(String hookName){
Collection<NCPHook> hooks = getHooksByName(hookName);
if (hooks.isEmpty()) return null;
removeHooks(hooks);
return hooks;
}
/**
* Remove a collection of hooks.
* @param hooks
* @return A set of the removed hooks ids.
*/
public static Set<Integer> removeHooks(Collection<NCPHook> hooks){
Set<Integer> ids = new HashSet<Integer>();
for (NCPHook hook : hooks){
Integer id = removeHook(hook);
if (id != null) ids.add(id);
}
return ids;
}
public static Collection<NCPHook> removeAllHooks(){
Collection<NCPHook> hooks = getAllHooks();
for (NCPHook hook : hooks){
removeHook(hook);
}
return hooks;
}
/**
* Get the hook by the hook name.
* @param hookName case sensitive (exact match).
* @return NCPHook if found, null otherwise.
*/
public static Collection<NCPHook> getHooksByName(String hookName){
List<NCPHook> hooks = new LinkedList<NCPHook>();
for (Integer refId : allHooks.keySet()){
NCPHook hook = allHooks.get(refId);
if (hook.getHookName().equals(hookName) && !hooks.contains(hook)) hooks.add(hook);
}
return hooks;
}
/**
* Get a collection of all hooks.
* @return
*/
public static Collection<NCPHook> getAllHooks(){
List<NCPHook> hooks = new LinkedList<NCPHook>();
hooks.addAll(allHooks.values());
return hooks;
}
}