[BREAKING] MoveInfo as generic super class of PlayerMoveInfo.

This commit is contained in:
asofold 2016-05-12 15:37:37 +02:00
parent 929578acf5
commit d5658cfddf
6 changed files with 147 additions and 109 deletions

View File

@ -14,7 +14,7 @@ import fr.neatmonster.nocheatplus.checks.ViolationData;
import fr.neatmonster.nocheatplus.checks.moving.MovingConfig;
import fr.neatmonster.nocheatplus.checks.moving.MovingData;
import fr.neatmonster.nocheatplus.checks.moving.model.LiftOffEnvelope;
import fr.neatmonster.nocheatplus.checks.moving.model.MoveInfo;
import fr.neatmonster.nocheatplus.checks.moving.model.PlayerMoveInfo;
import fr.neatmonster.nocheatplus.checks.moving.util.MovingUtil;
import fr.neatmonster.nocheatplus.compat.MCAccess;
import fr.neatmonster.nocheatplus.utilities.BlockProperties;
@ -25,20 +25,20 @@ import fr.neatmonster.nocheatplus.utilities.StringUtil;
*/
public class Critical extends Check {
private MoveInfo moveInfo;
private PlayerMoveInfo moveInfo;
/**
* Instantiates a new critical check.
*/
public Critical() {
super(CheckType.FIGHT_CRITICAL);
moveInfo = new MoveInfo(mcAccess);
moveInfo = new PlayerMoveInfo(mcAccess);
}
@Override
public void setMCAccess(MCAccess mcAccess) {
super.setMCAccess(mcAccess);
moveInfo = new MoveInfo(mcAccess);
moveInfo = new PlayerMoveInfo(mcAccess);
}
/**

View File

@ -32,7 +32,7 @@ import fr.neatmonster.nocheatplus.checks.moving.location.tracking.LocationTrace;
import fr.neatmonster.nocheatplus.checks.moving.location.tracking.LocationTrace.TraceEntry;
import fr.neatmonster.nocheatplus.checks.moving.model.LiftOffEnvelope;
import fr.neatmonster.nocheatplus.checks.moving.model.PlayerMoveData;
import fr.neatmonster.nocheatplus.checks.moving.model.MoveInfo;
import fr.neatmonster.nocheatplus.checks.moving.model.PlayerMoveInfo;
import fr.neatmonster.nocheatplus.checks.moving.util.MovingUtil;
import fr.neatmonster.nocheatplus.compat.BridgeEnchant;
import fr.neatmonster.nocheatplus.compat.BridgeHealth;
@ -87,20 +87,20 @@ public class FightListener extends CheckListener implements JoinLeaveListener{
private final Location useLoc2 = new Location(null, 0, 0, 0);
/** MoveInfo instance for temporary use with shouldCheckSurvivalFly. */
private MoveInfo moveInfo;
private PlayerMoveInfo moveInfo;
private final Counters counters = NCPAPIProvider.getNoCheatPlusAPI().getGenericInstance(Counters.class);
private final int idCancelDead = counters.registerKey("canceldead");
public FightListener(){
super(CheckType.FIGHT);
moveInfo = new MoveInfo(mcAccess);
moveInfo = new PlayerMoveInfo(mcAccess);
}
@Override
public void setMCAccess(MCAccess mcAccess) {
super.setMCAccess(mcAccess);
moveInfo = new MoveInfo(mcAccess);
moveInfo = new PlayerMoveInfo(mcAccess);
}
/**

View File

@ -50,7 +50,7 @@ import fr.neatmonster.nocheatplus.checks.combined.CombinedData;
import fr.neatmonster.nocheatplus.checks.moving.location.LocUtil;
import fr.neatmonster.nocheatplus.checks.moving.magic.Magic;
import fr.neatmonster.nocheatplus.checks.moving.model.PlayerMoveData;
import fr.neatmonster.nocheatplus.checks.moving.model.MoveInfo;
import fr.neatmonster.nocheatplus.checks.moving.model.PlayerMoveInfo;
import fr.neatmonster.nocheatplus.checks.moving.player.CreativeFly;
import fr.neatmonster.nocheatplus.checks.moving.player.MorePackets;
import fr.neatmonster.nocheatplus.checks.moving.player.NoFall;
@ -184,15 +184,15 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
final MovingData data = MovingData.getData(player);
final MovingConfig cc = MovingConfig.getConfig(player);
final MoveInfo moveInfo = aux.useMoveInfo();
final PlayerMoveInfo moveInfo = aux.usePlayerMoveInfo();
final Location loc = player.getLocation(useLoc);
moveInfo.set(player, loc, null, cc.yOnGround);
if (!MovingUtil.shouldCheckSurvivalFly(player, moveInfo.from, data, cc)) {
aux.returnMoveInfo(moveInfo);
aux.returnPlayerMoveInfo(moveInfo);
useLoc.setWorld(null);
return;
}
aux.returnMoveInfo(moveInfo);
aux.returnPlayerMoveInfo(moveInfo);
if (!data.hasSetBack() || blockY + 1D < data.getSetBackY()) {
useLoc.setWorld(null);
@ -242,10 +242,10 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
final Location loc = player.getLocation(useLoc);
final MovingData data = MovingData.getData(player);
Location target = null;
final MoveInfo moveInfo = aux.useMoveInfo();
final PlayerMoveInfo moveInfo = aux.usePlayerMoveInfo();
moveInfo.set(player, loc, null, cc.yOnGround);
final boolean sfCheck = MovingUtil.shouldCheckSurvivalFly(player, moveInfo.from, data, cc);
aux.returnMoveInfo(moveInfo);
aux.returnPlayerMoveInfo(moveInfo);
if (sfCheck) {
target = data.getSetBack(loc);
}
@ -403,7 +403,7 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
// Fire one or two moves here.
final MovingConfig cc = MovingConfig.getConfig(player);
final MoveInfo moveInfo = aux.useMoveInfo();
final PlayerMoveInfo moveInfo = aux.usePlayerMoveInfo();
final Location loc = player.getLocation(moveInfo.useLoc);
final PlayerMoveData lastMove = data.playerMoves.getFirstPastMove();
// TODO: On pistons pulling the player back: -1.15 yDistance for split move 1 (untracked position > 0.5 yDistance!).
@ -443,7 +443,7 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
}
// Cleanup.
data.joinOrRespawn = false;
aux.returnMoveInfo(moveInfo);
aux.returnPlayerMoveInfo(moveInfo);
}
/**
@ -459,7 +459,7 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
* @return If cancelled/done, i.e. not to process further split moves.
*/
private boolean checkPlayerMove(final Player player, final Location from, final Location to,
final boolean mightBeMultipleMoves, final MoveInfo moveInfo, final MovingData data, final MovingConfig cc,
final boolean mightBeMultipleMoves, final PlayerMoveInfo moveInfo, final MovingData data, final MovingConfig cc,
final PlayerMoveEvent event) {
Location newTo = null;
@ -1320,10 +1320,10 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
if (data.isTeleported(to)) {
// Set-back.
final Location teleported = data.getTeleported();
final MoveInfo moveInfo = aux.useMoveInfo();
final PlayerMoveInfo moveInfo = aux.usePlayerMoveInfo();
moveInfo.set(player, teleported, null, cc.yOnGround);
data.onSetBack(moveInfo.from);
aux.returnMoveInfo(moveInfo);
aux.returnPlayerMoveInfo(moveInfo);
// Reset stuff.
Combined.resetYawRate(player, teleported.getYaw(), System.currentTimeMillis(), true); // TODO: Not sure.
@ -1414,7 +1414,7 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
return;
}
final MovingConfig cc = MovingConfig.getConfig(player);
final MoveInfo moveInfo = aux.useMoveInfo();
final PlayerMoveInfo moveInfo = aux.usePlayerMoveInfo();
final double yOnGround = Math.max(cc.noFallyOnGround, cc.yOnGround);
final Location loc = player.getLocation(useLoc);
moveInfo.set(player, loc, null, yOnGround);
@ -1423,7 +1423,7 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
if (event.isCancelled() || !MovingUtil.shouldCheckSurvivalFly(player, pLoc, data, cc) || !noFall.isEnabled(player, cc)) {
data.clearNoFallData();
useLoc.setWorld(null);
aux.returnMoveInfo(moveInfo);
aux.returnPlayerMoveInfo(moveInfo);
return;
}
boolean allowReset = true;
@ -1447,7 +1447,7 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
// TODO: Also reset in other cases (moved too quickly)?
}
}
aux.returnMoveInfo(moveInfo);
aux.returnPlayerMoveInfo(moveInfo);
final float fallDistance = player.getFallDistance();
final double damage = BridgeHealth.getDamage(event);
final float yDiff = (float) (data.noFallMaxY - loc.getY());
@ -1675,17 +1675,17 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
}
final MovingData data = MovingData.getData(player);
final MovingConfig cc = MovingConfig.getConfig(player);
final MoveInfo moveInfo = aux.useMoveInfo();
final PlayerMoveInfo moveInfo = aux.usePlayerMoveInfo();
final Location loc = player.getLocation(useLoc);
moveInfo.set(player, loc, null, cc.yOnGround);
// TODO: data.isVelocityJumpPhase() might be too harsh, but prevents too easy abuse.
if (!MovingUtil.shouldCheckSurvivalFly(player, moveInfo.from, data, cc) || data.isVelocityJumpPhase() ||
BlockProperties.isOnGroundOrResetCond(player, loc, cc.yOnGround)) {
useLoc.setWorld(null);
aux.returnMoveInfo(moveInfo);
aux.returnPlayerMoveInfo(moveInfo);
return;
}
aux.returnMoveInfo(moveInfo);
aux.returnPlayerMoveInfo(moveInfo);
useLoc.setWorld(null);
// TODO: Configurable.
// TODO: Confine to minimum activation ticks.
@ -1724,7 +1724,7 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
// Only check every so and so ticks.
return;
}
final MoveInfo info = aux.useMoveInfo();
final PlayerMoveInfo info = aux.usePlayerMoveInfo();
for (final String playerName : hoverTicks) {
// TODO: put players into the set (+- one tick would not matter ?)
// TODO: might add an online flag to data !
@ -1767,7 +1767,7 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
}
hoverTicks.removeAll(rem);
rem.clear();
aux.returnMoveInfo(info);
aux.returnPlayerMoveInfo(info);
useLoc.setWorld(null);
}
@ -1799,7 +1799,7 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
* @param info
* @return
*/
private boolean checkHover(final Player player, final MovingData data, final MovingConfig cc, final MoveInfo info) {
private boolean checkHover(final Player player, final MovingData data, final MovingConfig cc, final PlayerMoveInfo info) {
// Check if player is on ground.
final Location loc = player.getLocation(useLoc); // useLoc.setWorld(null) is done in onTick.
info.set(player, loc, null, cc.yOnGround);
@ -1818,7 +1818,7 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
else {
if (data.sfHoverTicks > cc.sfHoverTicks) {
// Re-Check if survivalfly can apply at all.
final MoveInfo moveInfo = aux.useMoveInfo();
final PlayerMoveInfo moveInfo = aux.usePlayerMoveInfo();
moveInfo.set(player, loc, null, cc.yOnGround);
if (MovingUtil.shouldCheckSurvivalFly(player, moveInfo.from, data, cc)) {
handleHoverViolation(player, loc, cc, data);
@ -1831,7 +1831,7 @@ public class MovingListener extends CheckListener implements TickListener, IRemo
res = false;
data.sfHoverTicks = 0;
}
aux.returnMoveInfo(moveInfo);
aux.returnPlayerMoveInfo(moveInfo);
}
else res = false;
}

View File

@ -1,67 +1,79 @@
package fr.neatmonster.nocheatplus.checks.moving.model;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.compat.MCAccess;
import fr.neatmonster.nocheatplus.utilities.BlockCache;
import fr.neatmonster.nocheatplus.utilities.PlayerLocation;
/**
* Coupling from and to PlayerLocation objects with a block cache for easy
* storage and reuse.
*
* @author mc_dev
*
*/
public class MoveInfo {
/**
* Might need cloning for passing to external API. This is not initialized
* in set. World is set to null on cleanup!
*/
public final Location useLoc = new Location(null, 0, 0, 0);
public final BlockCache cache;
public final PlayerLocation from;
public final PlayerLocation to;
public MoveInfo(final MCAccess mcAccess){
cache = mcAccess.getBlockCache(null);
from = new PlayerLocation(mcAccess, null);
to = new PlayerLocation(mcAccess, null);
}
/**
* Initialize from, and if given to. Note that useLoc and data are left
* untouched.
*
* @param player
* @param from
* Must not be null.
* @param to
* Can be null.
* @param yOnGround
*/
public final void set(final Player player, final Location from, final Location to, final double yOnGround){
this.cache.setAccess(from.getWorld());
this.from.set(from, player, yOnGround);
this.from.setBlockCache(cache);
if (to != null){
this.to.set(to, player, yOnGround);
this.to.setBlockCache(cache);
}
// Note: using set to reset to by passing null won't work.
}
/**
* Clear caches and remove World references and such. Also resets the world
* of useLoc.
*/
public final void cleanup(){
useLoc.setWorld(null);
from.cleanup();
to.cleanup();
cache.cleanup();
}
}
package fr.neatmonster.nocheatplus.checks.moving.model;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import fr.neatmonster.nocheatplus.compat.MCAccess;
import fr.neatmonster.nocheatplus.utilities.BlockCache;
import fr.neatmonster.nocheatplus.utilities.RichEntityLocation;
/**
* Represent a move with start and end point. Short-term use of
* RichEntityLocation instances (from and to), and a Location for temporary use.
*
* @author asofold
*
* @param <REL>
* Location type to use.
*/
public abstract class MoveInfo <REL extends RichEntityLocation, E extends Entity> {
/**
* Would need cloning for passing to external API. This is not initialized
* in set. World is set to null on cleanup!
*/
public final Location useLoc = new Location(null, 0, 0, 0);
public final BlockCache cache;
public final REL from;
public final REL to;
public MoveInfo(final MCAccess mcAccess, REL from, REL to){
cache = mcAccess.getBlockCache(null);
this.from = from;
this.to = to;
}
/**
* Initialize from, and if given to. Note that useLoc and data are left
* untouched.
*
* @param entity
* @param from
* Must not be null.
* @param to
* Can be null.
* @param yOnGround
*/
public final void set(final E entity, final Location from, final Location to, final double yOnGround){
this.cache.setAccess(from.getWorld());
this.from.setBlockCache(cache);
set(this.from, from, entity, yOnGround);
if (to != null){
this.to.setBlockCache(cache);
set(this.to, to, entity, yOnGround);
}
// Note: using set to reset to by passing null won't work.
}
/**
* Called after setting BlockCache for the passed rLoc. (Needed to avoid
* issues with generics.)
*
* @param rLoc
* @param loc
* @param entity
* @param yOnGround
*/
protected abstract void set(REL rLoc, Location loc, E entity, double yOnGround);
/**
* Clear caches and remove World references and such. Also resets the world
* of useLoc.
*/
public final void cleanup(){
useLoc.setWorld(null);
from.cleanup();
to.cleanup();
cache.cleanup();
}
}

View File

@ -0,0 +1,26 @@
package fr.neatmonster.nocheatplus.checks.moving.model;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.compat.MCAccess;
import fr.neatmonster.nocheatplus.utilities.PlayerLocation;
/**
* Player specific MoveInfo.
*
* @author asofold
*
*/
public class PlayerMoveInfo extends MoveInfo<PlayerLocation, Player> {
public PlayerMoveInfo(final MCAccess mcAccess){
super(mcAccess, new PlayerLocation(mcAccess, null), new PlayerLocation(mcAccess, null));
}
@Override
protected void set(PlayerLocation rLoc, Location loc, Player player, double yOnGround) {
rLoc.set(loc, player, yOnGround);
}
}

View File

@ -8,7 +8,7 @@ import org.bukkit.entity.Player;
import fr.neatmonster.nocheatplus.checks.moving.MovingConfig;
import fr.neatmonster.nocheatplus.checks.moving.MovingData;
import fr.neatmonster.nocheatplus.checks.moving.model.MoveInfo;
import fr.neatmonster.nocheatplus.checks.moving.model.PlayerMoveInfo;
import fr.neatmonster.nocheatplus.compat.MCAccess;
import fr.neatmonster.nocheatplus.components.IRegisterAsGenericInstance;
import fr.neatmonster.nocheatplus.components.MCAccessHolder;
@ -29,14 +29,14 @@ public class AuxMoving implements MCAccessHolder, IRegisterAsGenericInstance {
* Unused instances.<br>
* Might be better due to cascading events in case of actions or plugins doing strange things.
*/
private final List<MoveInfo> parkedInfo = new ArrayList<MoveInfo>(10);
private final List<PlayerMoveInfo> parkedPlayerMoveInfo = new ArrayList<PlayerMoveInfo>(10);
public MoveInfo useMoveInfo() {
if (parkedInfo.isEmpty()) {
return new MoveInfo(mcAccess);
public PlayerMoveInfo usePlayerMoveInfo() {
if (parkedPlayerMoveInfo.isEmpty()) {
return new PlayerMoveInfo(mcAccess);
}
else {
return parkedInfo.remove(parkedInfo.size() - 1);
return parkedPlayerMoveInfo.remove(parkedPlayerMoveInfo.size() - 1);
}
}
@ -44,9 +44,9 @@ public class AuxMoving implements MCAccessHolder, IRegisterAsGenericInstance {
* Cleanup and add to parked.
* @param moveInfo
*/
public void returnMoveInfo(final MoveInfo moveInfo) {
public void returnPlayerMoveInfo(final PlayerMoveInfo moveInfo) {
moveInfo.cleanup();
parkedInfo.add(moveInfo);
parkedPlayerMoveInfo.add(moveInfo);
}
/**
@ -70,11 +70,11 @@ public class AuxMoving implements MCAccessHolder, IRegisterAsGenericInstance {
* @param cc
*/
public void resetPositionsAndMediumProperties(final Player player, final Location loc, final MovingData data, final MovingConfig cc) {
final MoveInfo moveInfo = useMoveInfo();
final PlayerMoveInfo moveInfo = usePlayerMoveInfo();
moveInfo.set(player, loc, null, cc.yOnGround);
data.resetPositions(moveInfo.from);
data.adjustMediumProperties(moveInfo.from);
returnMoveInfo(moveInfo);
returnPlayerMoveInfo(moveInfo);
}
@Override
@ -94,10 +94,10 @@ public class AuxMoving implements MCAccessHolder, IRegisterAsGenericInstance {
*/
public void clear() {
// Call cleanup on all parked info, just in case.
for (final MoveInfo info : parkedInfo) {
for (final PlayerMoveInfo info : parkedPlayerMoveInfo) {
info.cleanup();
}
parkedInfo.clear();
parkedPlayerMoveInfo.clear();
}
}