Only show version warning if an unsupported CraftBukkit version is

used + real MC 1.0.0 support without losing MC 1.8.1 Beta support.
This commit is contained in:
Evenprime 2011-11-23 19:23:16 +01:00
parent 36b97f246a
commit 48d6c141ca
8 changed files with 122 additions and 32 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>NoCheat</groupId>
<artifactId>NoCheat</artifactId>
<version>2.16d</version>
<version>2.17</version>
<packaging>jar</packaging>
<name>NoCheat</name>
<properties>
@ -19,7 +19,9 @@
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
<version>1.8.1-R5-SNAPSHOT</version>
<version>1.8.1-R5-20111119.223146-84</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
<repositories>

View File

@ -20,15 +20,14 @@ import cc.co.evenprime.bukkit.nocheat.debug.LagMeasureTask;
import cc.co.evenprime.bukkit.nocheat.debug.Performance;
import cc.co.evenprime.bukkit.nocheat.debug.PerformanceManager;
import cc.co.evenprime.bukkit.nocheat.debug.PerformanceManager.Type;
import cc.co.evenprime.bukkit.nocheat.events.BlockPlaceEventManager;
import cc.co.evenprime.bukkit.nocheat.events.BlockBreakEventManager;
import cc.co.evenprime.bukkit.nocheat.events.FightEventManager;
import cc.co.evenprime.bukkit.nocheat.events.EventManagerImpl;
import cc.co.evenprime.bukkit.nocheat.events.BlockPlaceEventManager;
import cc.co.evenprime.bukkit.nocheat.events.ChatEventManager;
import cc.co.evenprime.bukkit.nocheat.events.EventManagerImpl;
import cc.co.evenprime.bukkit.nocheat.events.FightEventManager;
import cc.co.evenprime.bukkit.nocheat.events.MovingEventManager;
import cc.co.evenprime.bukkit.nocheat.events.WorkaroundsEventManager;
import cc.co.evenprime.bukkit.nocheat.events.TimedEventManager;
import cc.co.evenprime.bukkit.nocheat.events.WorkaroundsEventManager;
import cc.co.evenprime.bukkit.nocheat.log.LogLevel;
import cc.co.evenprime.bukkit.nocheat.log.LogManager;
@ -50,7 +49,13 @@ public class NoCheat extends JavaPlugin {
private LagMeasureTask lagMeasureTask;
private int taskId = -1;
private int taskId = -1;
private MCVersion mcVersion = MCVersion.Unknown;
public enum MCVersion {
MC100, MC181, Unknown
}
public NoCheat() {
@ -86,7 +91,15 @@ public class NoCheat extends JavaPlugin {
// First set up logging
this.log = new LogManager();
log.logToConsole(LogLevel.LOW, "[NoCheat] This version is for CB #1337. It may break at any time and for any other version.");
// find out Minecraft version
if(Bukkit.getVersion().contains("MC: 1.0.0")) {
this.mcVersion = MCVersion.MC100;
} else if(Bukkit.getVersion().contains("MC: 1.8.1")) {
this.mcVersion = MCVersion.MC181;
} else {
this.mcVersion = MCVersion.Unknown;
log.logToConsole(LogLevel.LOW, "[NoCheat] You run an unsupported version of Minecraft. Some parts of NoCheat get disabled for your safety.");
}
// Then set up in memory per player data storage
this.players = new PlayerManager(this);
@ -106,7 +119,6 @@ public class NoCheat extends JavaPlugin {
eventManagers.add(new BlockPlaceEventManager(this));
eventManagers.add(new FightEventManager(this));
System.out.println(Bukkit.getVersion());
TimedEventManager m = new TimedEventManager(this);
taskId = m.taskId; // There's a bukkit task, remember its id
eventManagers.add(m);
@ -194,4 +206,7 @@ public class NoCheat extends JavaPlugin {
return players.getPlayer(player);
}
public MCVersion getMCVersion() {
return mcVersion;
}
}

View File

@ -11,6 +11,7 @@ import org.bukkit.entity.Player;
import cc.co.evenprime.bukkit.nocheat.NoCheat;
import cc.co.evenprime.bukkit.nocheat.NoCheatPlayer;
import cc.co.evenprime.bukkit.nocheat.player.NoCheatPlayerImpl;
import cc.co.evenprime.bukkit.nocheat.player.PlayerFactory;
/**
* Provide secure access to player-specific data objects for various checks or
@ -36,9 +37,9 @@ public class PlayerManager {
NoCheatPlayerImpl p = this.players.get(player.getName());
if(p == null) {
// TODO: Differentiate which player"type" should be created, e.g.
// based on bukkit version
p = new NoCheatPlayerImpl(player, plugin, new BaseData());
// PlayerFactory may create different players based on the
// MCVersion that we are running
p = PlayerFactory.createPlayer(player, plugin);
this.players.put(player.getName(), p);
}

View File

@ -33,21 +33,9 @@ public class TimedEventManager extends EventManagerImpl {
checks = new ArrayList<TimedCheck>(1);
checks.add(new GodmodeCheck(plugin));
this.timedPerformance = plugin.getPerformance(Type.TIMED);
try {
// Get an error thrown if "entityPlayer.b(boolean)" 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
this.taskId = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {

View File

@ -1,5 +1,7 @@
package cc.co.evenprime.bukkit.nocheat.player;
import java.lang.reflect.Method;
import net.minecraft.server.EntityPlayer;
import net.minecraft.server.MobEffectList;
@ -14,12 +16,17 @@ import cc.co.evenprime.bukkit.nocheat.data.BaseData;
public class NoCheatPlayerImpl implements NoCheatPlayer {
private Player player;
private final NoCheat plugin;
private final BaseData data;
private long lastUsedTime;
protected Player player;
protected final NoCheat plugin;
protected final BaseData data;
protected long lastUsedTime;
// The method that's used to artifically "fast-forward" the player
protected static Method incAge = null;
public NoCheatPlayerImpl(Player player, NoCheat plugin, BaseData data) {
System.out.println(incAge != null ? incAge.toString() : null);
this.player = player;
this.plugin = plugin;
this.data = data;
@ -60,11 +67,20 @@ public class NoCheatPlayerImpl implements NoCheatPlayer {
}
public void increaseAge(int ticks) {
if(incAge == null) {
player.setTicksLived(player.getTicksLived() + ticks);
return;
}
EntityPlayer p = ((CraftPlayer) player).getHandle();
for(int i = 0; i < ticks; i++) {
// TODO: This is highly fragile and breaks every update!!
p.b(true); // Catch up with the server, one tick at a time
try {
incAge.invoke(p, true);
} catch(Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,23 @@
package cc.co.evenprime.bukkit.nocheat.player;
import net.minecraft.server.EntityPlayer;
import org.bukkit.entity.Player;
import cc.co.evenprime.bukkit.nocheat.NoCheat;
import cc.co.evenprime.bukkit.nocheat.data.BaseData;
public class NoCheatPlayer_100 extends NoCheatPlayerImpl {
static {
try {
incAge = EntityPlayer.class.getMethod("a", boolean.class);
} catch(Exception e) {
System.out.println("NoCheat couldn't initialize variable incAge");
}
}
public NoCheatPlayer_100(Player player, NoCheat plugin, BaseData data) {
super(player, plugin, data);
}
}

View File

@ -0,0 +1,24 @@
package cc.co.evenprime.bukkit.nocheat.player;
import net.minecraft.server.EntityPlayer;
import org.bukkit.entity.Player;
import cc.co.evenprime.bukkit.nocheat.NoCheat;
import cc.co.evenprime.bukkit.nocheat.data.BaseData;
public class NoCheatPlayer_181 extends NoCheatPlayerImpl {
public NoCheatPlayer_181(Player player, NoCheat plugin, BaseData data) {
super(player, plugin, data);
}
static {
try {
incAge = EntityPlayer.class.getMethod("b", boolean.class);
} catch(Exception e) {
System.out.println("NoCheat couldn't initialize variable incAge");
}
}
}

View File

@ -0,0 +1,21 @@
package cc.co.evenprime.bukkit.nocheat.player;
import org.bukkit.entity.Player;
import cc.co.evenprime.bukkit.nocheat.NoCheat;
import cc.co.evenprime.bukkit.nocheat.data.BaseData;
public class PlayerFactory {
public static NoCheatPlayerImpl createPlayer(Player player, NoCheat plugin) {
switch (plugin.getMCVersion()) {
case MC100:
return new NoCheatPlayer_100(player, plugin, new BaseData());
case MC181:
return new NoCheatPlayer_181(player, plugin, new BaseData());
default:
return new NoCheatPlayerImpl(player, plugin, new BaseData());
}
}
}