A bit of refactoring, maybe fixed the "can't edit config file" bug,

handle MC 1.0 versions better.
This commit is contained in:
Evenprime 2011-11-23 14:37:20 +01:00
parent 79d545f5e3
commit 0459235c43
13 changed files with 97 additions and 60 deletions

View File

@ -0,0 +1,10 @@
package cc.co.evenprime.bukkit.nocheat;
import java.util.List;
import cc.co.evenprime.bukkit.nocheat.config.cache.ConfigurationCache;
public interface EventManager {
public List<String> getActiveChecks(ConfigurationCache cc);
}

View File

@ -3,6 +3,7 @@ package cc.co.evenprime.bukkit.nocheat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -23,10 +24,10 @@ import cc.co.evenprime.bukkit.nocheat.debug.PerformanceManager.Type;
import cc.co.evenprime.bukkit.nocheat.events.BlockPlaceEventManager; import cc.co.evenprime.bukkit.nocheat.events.BlockPlaceEventManager;
import cc.co.evenprime.bukkit.nocheat.events.BlockBreakEventManager; import cc.co.evenprime.bukkit.nocheat.events.BlockBreakEventManager;
import cc.co.evenprime.bukkit.nocheat.events.FightEventManager; import cc.co.evenprime.bukkit.nocheat.events.FightEventManager;
import cc.co.evenprime.bukkit.nocheat.events.EventManager; import cc.co.evenprime.bukkit.nocheat.events.EventManagerImpl;
import cc.co.evenprime.bukkit.nocheat.events.ChatEventManager; import cc.co.evenprime.bukkit.nocheat.events.ChatEventManager;
import cc.co.evenprime.bukkit.nocheat.events.MovingEventManager; import cc.co.evenprime.bukkit.nocheat.events.MovingEventManager;
import cc.co.evenprime.bukkit.nocheat.events.PlayerTeleportEventManager; import cc.co.evenprime.bukkit.nocheat.events.WorkaroundsEventManager;
import cc.co.evenprime.bukkit.nocheat.events.TimedEventManager; import cc.co.evenprime.bukkit.nocheat.events.TimedEventManager;
import cc.co.evenprime.bukkit.nocheat.log.LogLevel; import cc.co.evenprime.bukkit.nocheat.log.LogLevel;
import cc.co.evenprime.bukkit.nocheat.log.LogManager; import cc.co.evenprime.bukkit.nocheat.log.LogManager;
@ -40,16 +41,16 @@ import cc.co.evenprime.bukkit.nocheat.log.LogManager;
*/ */
public class NoCheat extends JavaPlugin { public class NoCheat extends JavaPlugin {
private ConfigurationManager conf; private ConfigurationManager conf;
private LogManager log; private LogManager log;
private PlayerManager players; private PlayerManager players;
private PerformanceManager performance; private PerformanceManager performance;
private List<EventManager> eventManagers; private List<EventManagerImpl> eventManagers;
private LagMeasureTask lagMeasureTask; private LagMeasureTask lagMeasureTask;
private int taskId = -1; private int taskId = -1;
public NoCheat() { public NoCheat() {
@ -96,14 +97,16 @@ public class NoCheat extends JavaPlugin {
// Then set up the performance counters // Then set up the performance counters
this.performance = new PerformanceManager(); this.performance = new PerformanceManager();
eventManagers = new ArrayList<EventManager>(8); // Big enough eventManagers = new ArrayList<EventManagerImpl>(8); // Big enough
// Then set up the event listeners // Then set up the event listeners
eventManagers.add(new MovingEventManager(this)); eventManagers.add(new MovingEventManager(this));
eventManagers.add(new PlayerTeleportEventManager(this)); eventManagers.add(new WorkaroundsEventManager(this));
eventManagers.add(new ChatEventManager(this)); eventManagers.add(new ChatEventManager(this));
eventManagers.add(new BlockBreakEventManager(this)); eventManagers.add(new BlockBreakEventManager(this));
eventManagers.add(new BlockPlaceEventManager(this)); eventManagers.add(new BlockPlaceEventManager(this));
eventManagers.add(new FightEventManager(this)); eventManagers.add(new FightEventManager(this));
System.out.println(Bukkit.getVersion());
TimedEventManager m = new TimedEventManager(this); TimedEventManager m = new TimedEventManager(this);
taskId = m.taskId; // There's a bukkit task, remember its id taskId = m.taskId; // There's a bukkit task, remember its id
eventManagers.add(m); eventManagers.add(m);

View File

@ -26,7 +26,7 @@ public class FlatFileConfiguration extends Configuration {
public void load(ActionMapper action) throws IOException { public void load(ActionMapper action) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line = null; String line = null;

View File

@ -4,14 +4,15 @@ import java.util.List;
import org.bukkit.World; import org.bukkit.World;
import cc.co.evenprime.bukkit.nocheat.EventManager;
import cc.co.evenprime.bukkit.nocheat.NoCheat; import cc.co.evenprime.bukkit.nocheat.NoCheat;
import cc.co.evenprime.bukkit.nocheat.config.cache.ConfigurationCache; import cc.co.evenprime.bukkit.nocheat.config.cache.ConfigurationCache;
import cc.co.evenprime.bukkit.nocheat.events.EventManager; import cc.co.evenprime.bukkit.nocheat.events.EventManagerImpl;
import cc.co.evenprime.bukkit.nocheat.log.LogLevel; import cc.co.evenprime.bukkit.nocheat.log.LogLevel;
public class ActiveCheckPrinter { public class ActiveCheckPrinter {
public static void printActiveChecks(NoCheat plugin, List<EventManager> eventManagers) { public static void printActiveChecks(NoCheat plugin, List<EventManagerImpl> eventManagers) {
boolean introPrinted = false; boolean introPrinted = false;
String intro = "[NoCheat] Active Checks: "; String intro = "[NoCheat] Active Checks: ";

View File

@ -27,7 +27,7 @@ import cc.co.evenprime.bukkit.nocheat.debug.PerformanceManager.Type;
* relevant checks * relevant checks
* *
*/ */
public class BlockBreakEventManager extends EventManager { public class BlockBreakEventManager extends EventManagerImpl {
private final List<BlockBreakCheck> checks; private final List<BlockBreakCheck> checks;

View File

@ -24,7 +24,7 @@ import cc.co.evenprime.bukkit.nocheat.debug.PerformanceManager.Type;
* checks * checks
* *
*/ */
public class BlockPlaceEventManager extends EventManager { public class BlockPlaceEventManager extends EventManagerImpl {
private final List<BlockPlaceCheck> checks; private final List<BlockPlaceCheck> checks;

View File

@ -19,7 +19,7 @@ import cc.co.evenprime.bukkit.nocheat.config.cache.ConfigurationCache;
import cc.co.evenprime.bukkit.nocheat.data.ChatData; import cc.co.evenprime.bukkit.nocheat.data.ChatData;
import cc.co.evenprime.bukkit.nocheat.debug.PerformanceManager.Type; import cc.co.evenprime.bukkit.nocheat.debug.PerformanceManager.Type;
public class ChatEventManager extends EventManager { public class ChatEventManager extends EventManagerImpl {
private final List<ChatCheck> checks; private final List<ChatCheck> checks;

View File

@ -27,6 +27,7 @@ import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerToggleSprintEvent; import org.bukkit.event.player.PlayerToggleSprintEvent;
import org.bukkit.event.player.PlayerVelocityEvent; import org.bukkit.event.player.PlayerVelocityEvent;
import cc.co.evenprime.bukkit.nocheat.EventManager;
import cc.co.evenprime.bukkit.nocheat.NoCheat; import cc.co.evenprime.bukkit.nocheat.NoCheat;
import cc.co.evenprime.bukkit.nocheat.config.cache.ConfigurationCache; import cc.co.evenprime.bukkit.nocheat.config.cache.ConfigurationCache;
import cc.co.evenprime.bukkit.nocheat.debug.Performance; import cc.co.evenprime.bukkit.nocheat.debug.Performance;
@ -38,18 +39,18 @@ import cc.co.evenprime.bukkit.nocheat.debug.Performance;
* time it takes to handle the event. * time it takes to handle the event.
* *
*/ */
public abstract class EventManager { public abstract class EventManagerImpl implements EventManager {
protected final NoCheat plugin; protected final NoCheat plugin;
private static class BlockL extends BlockListener { private static class BlockL extends BlockListener {
private final EventManager m; private final EventManagerImpl m;
private final Priority priority; private final Priority priority;
private final boolean ignoreCancelledEvents; private final boolean ignoreCancelledEvents;
private final Performance measureTime; private final Performance measureTime;
public BlockL(EventManager m, Priority priority, boolean ignoreCancelled, Performance measureTime) { public BlockL(EventManagerImpl m, Priority priority, boolean ignoreCancelled, Performance measureTime) {
this.m = m; this.m = m;
this.priority = priority; this.priority = priority;
this.ignoreCancelledEvents = ignoreCancelled; this.ignoreCancelledEvents = ignoreCancelled;
@ -101,12 +102,12 @@ public abstract class EventManager {
private static class PlayerL extends PlayerListener { private static class PlayerL extends PlayerListener {
private final EventManager m; private final EventManagerImpl m;
private final Priority priority; private final Priority priority;
private final boolean ignoreCancelledEvents; private final boolean ignoreCancelledEvents;
private final Performance measureTime; private final Performance measureTime;
public PlayerL(EventManager m, Priority priority, boolean ignoreCancelled, Performance measureTime) { public PlayerL(EventManagerImpl m, Priority priority, boolean ignoreCancelled, Performance measureTime) {
this.m = m; this.m = m;
this.priority = priority; this.priority = priority;
this.ignoreCancelledEvents = ignoreCancelled; this.ignoreCancelledEvents = ignoreCancelled;
@ -243,12 +244,12 @@ public abstract class EventManager {
private static class EntityL extends EntityListener { private static class EntityL extends EntityListener {
private final EventManager m; private final EventManagerImpl m;
private final Priority priority; private final Priority priority;
private final boolean ignoreCancelledEvents; private final boolean ignoreCancelledEvents;
private final Performance measureTime; private final Performance measureTime;
public EntityL(EventManager m, Priority priority, boolean ignoreCancelled, Performance measureTime) { public EntityL(EventManagerImpl m, Priority priority, boolean ignoreCancelled, Performance measureTime) {
this.m = m; this.m = m;
this.priority = priority; this.priority = priority;
this.ignoreCancelledEvents = ignoreCancelled; this.ignoreCancelledEvents = ignoreCancelled;
@ -285,7 +286,7 @@ public abstract class EventManager {
} }
} }
public EventManager(NoCheat plugin) { public EventManagerImpl(NoCheat plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@ -314,6 +315,14 @@ public abstract class EventManager {
} }
} }
/*
* (non-Javadoc)
*
* @see
* cc.co.evenprime.bukkit.nocheat.events.EventManager#getActiveChecks(cc
* .co.evenprime.bukkit.nocheat.config.cache.ConfigurationCache)
*/
@Override
public List<String> getActiveChecks(ConfigurationCache cc) { public List<String> getActiveChecks(ConfigurationCache cc) {
return Collections.emptyList(); return Collections.emptyList();
} }

View File

@ -23,7 +23,7 @@ import cc.co.evenprime.bukkit.nocheat.config.cache.ConfigurationCache;
import cc.co.evenprime.bukkit.nocheat.data.FightData; import cc.co.evenprime.bukkit.nocheat.data.FightData;
import cc.co.evenprime.bukkit.nocheat.debug.PerformanceManager.Type; import cc.co.evenprime.bukkit.nocheat.debug.PerformanceManager.Type;
public class FightEventManager extends EventManager { public class FightEventManager extends EventManagerImpl {
private final List<FightCheck> checks; private final List<FightCheck> checks;

View File

@ -10,6 +10,7 @@ import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority; import org.bukkit.event.Event.Priority;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerVelocityEvent; import org.bukkit.event.player.PlayerVelocityEvent;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
@ -34,7 +35,7 @@ import cc.co.evenprime.bukkit.nocheat.debug.PerformanceManager.Type;
* evaluate the check results and decide what to * evaluate the check results and decide what to
* *
*/ */
public class MovingEventManager extends EventManager { public class MovingEventManager extends EventManagerImpl {
private final List<MovingCheck> checks; private final List<MovingCheck> checks;
@ -50,6 +51,7 @@ public class MovingEventManager extends EventManager {
registerListener(Event.Type.PLAYER_MOVE, Priority.Lowest, true, plugin.getPerformance(Type.MOVING)); registerListener(Event.Type.PLAYER_MOVE, Priority.Lowest, true, plugin.getPerformance(Type.MOVING));
registerListener(Event.Type.PLAYER_VELOCITY, Priority.Monitor, true, plugin.getPerformance(Type.VELOCITY)); registerListener(Event.Type.PLAYER_VELOCITY, Priority.Monitor, true, plugin.getPerformance(Type.VELOCITY));
registerListener(Event.Type.BLOCK_PLACE, Priority.Monitor, true, plugin.getPerformance(Type.BLOCKPLACE)); registerListener(Event.Type.BLOCK_PLACE, Priority.Monitor, true, plugin.getPerformance(Type.BLOCKPLACE));
registerListener(Event.Type.PLAYER_TELEPORT, Priority.Highest, false, null);
} }
@Override @Override
@ -82,6 +84,23 @@ public class MovingEventManager extends EventManager {
} }
} }
@Override
protected void handlePlayerTeleportEvent(final PlayerTeleportEvent event, final Priority priority) {
// No typo here, I really want to only handle cancelled events
if(!event.isCancelled())
return;
NoCheatPlayer player = plugin.getPlayer(event.getPlayer());
final MovingData data = player.getData().moving;
if(data.teleportTo.isSet() && data.teleportTo.equals(event.getTo())) {
event.setCancelled(false);
}
return;
}
@Override @Override
protected void handlePlayerMoveEvent(final PlayerMoveEvent event, final Priority priority) { protected void handlePlayerMoveEvent(final PlayerMoveEvent event, final Priority priority) {

View File

@ -21,21 +21,33 @@ import cc.co.evenprime.bukkit.nocheat.data.TimedData;
import cc.co.evenprime.bukkit.nocheat.debug.Performance; import cc.co.evenprime.bukkit.nocheat.debug.Performance;
import cc.co.evenprime.bukkit.nocheat.debug.PerformanceManager.Type; import cc.co.evenprime.bukkit.nocheat.debug.PerformanceManager.Type;
public class TimedEventManager extends EventManager { public class TimedEventManager extends EventManagerImpl {
private final List<TimedCheck> checks; private final List<TimedCheck> checks;
private final Performance timedPerformance; private final Performance timedPerformance;
public final int taskId; public int taskId = -1;
public TimedEventManager(final NoCheat plugin) { public TimedEventManager(final NoCheat plugin) {
super(plugin); super(plugin);
checks = new ArrayList<TimedCheck>(1); checks = new ArrayList<TimedCheck>(1);
checks.add(new GodmodeCheck(plugin));
this.timedPerformance = plugin.getPerformance(Type.TIMED); this.timedPerformance = plugin.getPerformance(Type.TIMED);
try {
// Get an error thrown if "b" doesn't exist
EntityPlayer.class.getMethod("b", Boolean.class);
checks.add(new GodmodeCheck(plugin));
} catch(NoSuchMethodException e1) {
System.out.println("[NoCheat]: Couldn't find needed method for \"timed.godmode\" check, disabling it. You probably run an incompatible craftbukkit version.");
System.out.println("[NoCheat]: This problem may be fixed in a newer version of NoCheat (when available).");
return;
}
// "register a listener" for passed time // "register a listener" for passed time
this.taskId = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() { this.taskId = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {

View File

@ -10,46 +10,28 @@ import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerToggleSprintEvent; import org.bukkit.event.player.PlayerToggleSprintEvent;
import cc.co.evenprime.bukkit.nocheat.NoCheat; import cc.co.evenprime.bukkit.nocheat.NoCheat;
import cc.co.evenprime.bukkit.nocheat.NoCheatPlayer;
import cc.co.evenprime.bukkit.nocheat.data.MovingData;
/** /**
* Only place that listens to Player-teleport related events and dispatches them * Only place that listens to Player-teleport related events and dispatches them
* to relevant checks * to relevant checks
* *
*/ */
public class PlayerTeleportEventManager extends EventManager { public class WorkaroundsEventManager extends EventManagerImpl {
public PlayerTeleportEventManager(NoCheat plugin) { public WorkaroundsEventManager(NoCheat plugin) {
super(plugin); super(plugin);
registerListener(Event.Type.PLAYER_MOVE, Priority.Highest, false, null); registerListener(Event.Type.PLAYER_MOVE, Priority.Highest, false, null);
registerListener(Event.Type.PLAYER_TOGGLE_SPRINT, Priority.Highest, false, null); registerListener(Event.Type.PLAYER_TOGGLE_SPRINT, Priority.Highest, false, null);
registerListener(Event.Type.PLAYER_TELEPORT, Priority.Monitor, true, null); registerListener(Event.Type.PLAYER_TELEPORT, Priority.Monitor, true, null);
registerListener(Event.Type.PLAYER_TELEPORT, Priority.Highest, false, null);
registerListener(Event.Type.PLAYER_PORTAL, Priority.Monitor, true, null); registerListener(Event.Type.PLAYER_PORTAL, Priority.Monitor, true, null);
registerListener(Event.Type.PLAYER_RESPAWN, Priority.Monitor, true, null); registerListener(Event.Type.PLAYER_RESPAWN, Priority.Monitor, true, null);
} }
@Override @Override
protected void handlePlayerTeleportEvent(final PlayerTeleportEvent event, final Priority priority) { protected void handlePlayerTeleportEvent(final PlayerTeleportEvent event, final Priority priority) {
if(priority.equals(Priority.Monitor)) { handleTeleportation(event.getPlayer());
handleTeleportation(event.getPlayer());
} else {
// No typo here, I really want to only handle cancelled events
if(!event.isCancelled())
return;
NoCheatPlayer player = plugin.getPlayer(event.getPlayer());
final MovingData data = player.getData().moving;
if(data.teleportTo.isSet() && data.teleportTo.equals(event.getTo())) {
if(player.getConfiguration().debug.overrideIdiocy) {
event.setCancelled(false);
}
}
}
} }
@Override @Override

View File

@ -61,6 +61,7 @@ public class NoCheatPlayerImpl implements NoCheatPlayer {
public void increaseAge(int ticks) { public void increaseAge(int ticks) {
EntityPlayer p = ((CraftPlayer) player).getHandle(); EntityPlayer p = ((CraftPlayer) player).getHandle();
for(int i = 0; i < ticks; i++) { for(int i = 0; i < ticks; i++) {
// TODO: This is highly fragile and breaks every update!! // TODO: This is highly fragile and breaks every update!!
p.b(true); // Catch up with the server, one tick at a time p.b(true); // Catch up with the server, one tick at a time