Skip to content

Commit

Permalink
An attempt to deal with #315, #310, #301
Browse files Browse the repository at this point in the history
  • Loading branch information
xaw3ep committed Aug 12, 2023
1 parent ebd5bb8 commit de0486c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public static int getBlockPlaceHash(final Block block, final Material mat) {

/** For temporary use: LocUtil.clone before passing deeply, call setWorld(null) after use. */
private final Location useLoc = new Location(null, 0, 0, 0);
private final Location useLoc2 = new Location(null, 0, 0, 0);

private final Counters counters = NCPAPIProvider.getNoCheatPlusAPI().getGenericInstance(Counters.class);
private final int idBoatsAnywhere = counters.registerKey("boatsanywhere");
Expand Down Expand Up @@ -533,7 +534,7 @@ public void onProjectileLaunch(final ProjectileLaunchEvent event) {
boolean cancel = false;
if (speed.isEnabled(player, pData)) {
final long now = System.currentTimeMillis();
final Location loc = player.getLocation(useLoc);
final Location loc = player.getLocation(useLoc2);
if (Combined.checkYawRate(player, loc.getYaw(), now, loc.getWorld().getName(), pData)) {
// Yawrate (checked extra).
cancel = true;
Expand All @@ -558,18 +559,18 @@ else if (Improbable.check(player, cc.speedImprobableWeight, now, "blockplace.spe
// Do nothing !
// TODO: Might have further flags?
}
else if (!BlockProperties.isPassable(projectile.getLocation(useLoc))) {
else if (!BlockProperties.isPassable(projectile.getLocation(useLoc2))) {
// Launch into a block.
cancel = true;
}
else {
if (!BlockProperties.isPassable(player.getEyeLocation(), projectile.getLocation(useLoc))) {
if (!BlockProperties.isPassable(player.getEyeLocation(), projectile.getLocation(useLoc2))) {
// (Spare a useLoc2, for this is seldom rather.)
// Something between player
cancel = true;
}
else {
final Material mat = player.getLocation(useLoc).getBlock().getType();
final Material mat = player.getLocation(useLoc2).getBlock().getType();
final long flags = BlockFlags.F_CLIMBABLE | BlockFlags.F_LIQUID | BlockFlags.F_IGN_PASSABLE;
if (!BlockProperties.isAir(mat) && (BlockFlags.getBlockFlags(mat) & flags) == 0 && !mcAccess.getHandle().hasGravity(mat)) {
// Still fails on piston traps etc.
Expand All @@ -591,7 +592,7 @@ else if (!BlockProperties.isPassable(projectile.getLocation(useLoc))) {
event.setCancelled(true);
}
// Cleanup.
useLoc.setWorld(null);
useLoc2.setWorld(null);
}

// TODO: remove this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ public class MovingListener extends CheckListener implements TickListener, IRemo

/** Location for temporary use with getLocation(useLoc). Always call setWorld(null) after use. Use LocUtil.clone before passing to other API. */
final Location useLoc = new Location(null, 0, 0, 0); // TODO: Put to use...
final Location useBedLeaveLoc = new Location(null, 0, 0, 0);
final Location useChangeWorldLoc = new Location(null, 0, 0, 0);
final Location useDeathLoc = new Location(null, 0, 0, 0);
final Location useFallLoc = new Location(null, 0, 0, 0);
final Location useJoinLoc = new Location(null, 0, 0, 0);
final Location useLeaveLoc = new Location(null, 0, 0, 0);
final Location useToggleFlightLoc = new Location(null, 0, 0, 0);
final Location useTickLoc = new Location(null, 0, 0, 0);

/** Auxiliary functionality. */
private final AuxMoving aux = NCPAPIProvider.getNoCheatPlusAPI().getGenericInstance(AuxMoving.class);
Expand Down Expand Up @@ -298,7 +306,7 @@ public void onPlayerBedLeave(final PlayerBedLeaveEvent event) {
// Check if the player has to be reset.
// To "cancel" the event, we teleport the player.
Location newTo = null;
final Location loc = player.getLocation(useLoc);
final Location loc = player.getLocation(useBedLeaveLoc);
final PlayerMoveInfo moveInfo = aux.usePlayerMoveInfo();
moveInfo.set(player, loc, null, cc.yOnGround);
final boolean sfCheck = MovingUtil.shouldCheckSurvivalFly(player, moveInfo.from, moveInfo.to, data, cc, pData);
Expand All @@ -317,7 +325,7 @@ public void onPlayerBedLeave(final PlayerBedLeaveEvent event) {
noFall.checkDamage(player, y, data, pData);
}
// Cleanup
useLoc.setWorld(null);
useBedLeaveLoc.setWorld(null);
// Teleport.
data.prepareSetBack(newTo); // Should be enough.
player.teleport(newTo, BridgeMisc.TELEPORT_CAUSE_CORRECTION_OF_POSITION);
Expand Down Expand Up @@ -374,14 +382,14 @@ public void onPlayerChangedWorld(final PlayerChangedWorldEvent event) {
if (!pData.isCheckActive(CheckType.MOVING, player)) return;
final MovingConfig cc = pData.getGenericInstance(MovingConfig.class);
data.clearMostMovingCheckData();
final Location loc = player.getLocation(useLoc);
final Location loc = player.getLocation(useChangeWorldLoc);
data.setSetBack(loc);
if (cc.loadChunksOnWorldChange) MovingUtil.ensureChunksLoaded(player, loc, "world change", data, cc, pData);
aux.resetPositionsAndMediumProperties(player, loc, data, cc);
data.resetTrace(player, loc, TickTask.getTick(), mcAccess.getHandle(), cc);
// Just in case.
if (cc.enforceLocation) playersEnforce.add(player.getName());
useLoc.setWorld(null);
useChangeWorldLoc.setWorld(null);
}


Expand Down Expand Up @@ -1785,11 +1793,11 @@ public void onPlayerDeath(final PlayerDeathEvent event) {
final MovingData data = pData.getGenericInstance(MovingData.class);
//final MovingConfig cc = pData.getGenericInstance(MovingConfig.class);
data.clearMostMovingCheckData();
data.setSetBack(player.getLocation(useLoc)); // TODO: Monitor this change (!).
data.setSetBack(player.getLocation(useDeathLoc)); // TODO: Monitor this change (!).
data.isUsingItem = false;
// Log location.
if (pData.isDebugActive(checkType)) debug(player, "Death: " + player.getLocation(useLoc));
useLoc.setWorld(null);
if (pData.isDebugActive(checkType)) debug(player, "Death: " + player.getLocation(useDeathLoc));
useDeathLoc.setWorld(null);
}


Expand Down Expand Up @@ -2103,7 +2111,7 @@ private void confirmSetBack(final Player player, final boolean fakeNews, final M
final PlayerMoveInfo moveInfo = aux.usePlayerMoveInfo();
moveInfo.set(player, teleported != null ? teleported : fallbackTeleported, null, cc.yOnGround);
if (cc.loadChunksOnTeleport) {
MovingUtil.ensureChunksLoaded(player, teleported,
MovingUtil.ensureChunksLoaded(player, teleported != null ? teleported : fallbackTeleported,
"teleport", data, cc, pData);
}
data.onSetBack(moveInfo.from);
Expand Down Expand Up @@ -2245,15 +2253,15 @@ private void checkFallDamageEvent(final Player player, final EntityDamageEvent e
final MovingConfig cc = pData.getGenericInstance(MovingConfig.class);
final PlayerMoveInfo moveInfo = aux.usePlayerMoveInfo();
final double yOnGround = Math.max(cc.noFallyOnGround, cc.yOnGround);
final Location loc = player.getLocation(useLoc);
final Location loc = player.getLocation(useFallLoc);
moveInfo.set(player, loc, null, yOnGround);
final PlayerLocation pLoc = moveInfo.from;
pLoc.collectBlockFlags(yOnGround);

if (event.isCancelled() || !MovingUtil.shouldCheckSurvivalFly(player, pLoc, moveInfo.to, data, cc, pData)
|| !noFall.isEnabled(player, pData)) {
data.clearNoFallData();
useLoc.setWorld(null);
useFallLoc.setWorld(null);
aux.returnPlayerMoveInfo(moveInfo);
return;
}
Expand Down Expand Up @@ -2298,7 +2306,7 @@ else if (noFallVL(player, "fakefall", data, cc)) {
if (dataDamage <= 0.0) {
// Cancel the event.
event.setCancelled(true);
useLoc.setWorld(null);
useFallLoc.setWorld(null);
aux.returnPlayerMoveInfo(moveInfo);
return;
}
Expand Down Expand Up @@ -2357,7 +2365,7 @@ else if (noFallVL(player, "fakefall", data, cc)) {
}
// Entity fall-distance should be reset elsewhere.
// Cleanup.
useLoc.setWorld(null);
useFallLoc.setWorld(null);
}


Expand Down Expand Up @@ -2426,10 +2434,10 @@ public void playerJoins(final Player player) {

final IPlayerData pData = DataManager.getPlayerData(player);
if (!pData.isCheckActive(CheckType.MOVING, player)) return;
dataOnJoin(player, player.getLocation(useLoc), false, pData.getGenericInstance(MovingData.class),
dataOnJoin(player, player.getLocation(useJoinLoc), false, pData.getGenericInstance(MovingData.class),
pData.getGenericInstance(MovingConfig.class), pData.isDebugActive(checkType));
// Cleanup.
useLoc.setWorld(null);
useJoinLoc.setWorld(null);
}


Expand Down Expand Up @@ -2533,7 +2541,7 @@ public void playerLeaves(final Player player) {
final IPlayerData pData = DataManager.getPlayerData(player);
if (!pData.isCheckActive(CheckType.MOVING, player)) return;
final MovingData data = pData.getGenericInstance(MovingData.class);
final Location loc = player.getLocation(useLoc);
final Location loc = player.getLocation(useLeaveLoc);
// Debug logout.
if (pData.isDebugActive(checkType)) StaticLog.logInfo("Player " + player.getName() + " leaves at location: " + loc.toString());

Expand Down Expand Up @@ -2587,7 +2595,7 @@ public void playerLeaves(final Player player) {
}
}
}
useLoc.setWorld(null);
useLeaveLoc.setWorld(null);
// Adjust data.
survivalFly.setReallySneaking(player, false);
noFall.onLeave(player, data, pData);
Expand Down Expand Up @@ -2625,17 +2633,17 @@ public void onPlayerToggleFlight(final PlayerToggleFlightEvent event) {
final MovingData data = pData.getGenericInstance(MovingData.class);
final MovingConfig cc = pData.getGenericInstance(MovingConfig.class);
final PlayerMoveInfo moveInfo = aux.usePlayerMoveInfo();
final Location loc = player.getLocation(useLoc);
final Location loc = player.getLocation(useToggleFlightLoc);
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, moveInfo.to, data, cc, pData)
|| data.isVelocityJumpPhase() || BlockProperties.isOnGroundOrResetCond(player, loc, cc.yOnGround)) {
useLoc.setWorld(null);
useToggleFlightLoc.setWorld(null);
aux.returnPlayerMoveInfo(moveInfo);
return;
}
aux.returnPlayerMoveInfo(moveInfo);
useLoc.setWorld(null);
useToggleFlightLoc.setWorld(null);
// TODO: Confine to minimum activation ticks.
data.addVelocity(player, cc, 0.0, 0.3, 0.0);
}
Expand All @@ -2653,7 +2661,7 @@ public void onTick(final int tick, final long timeLast) {
checkOnTickHover();
}
// Cleanup.
useLoc.setWorld(null);
useTickLoc.setWorld(null);
}


Expand Down Expand Up @@ -2730,7 +2738,7 @@ else if (player.isDead() || player.isSleeping() || player.isInsideVehicle()) {
continue;
}
final MovingData data = DataManager.getGenericInstance(player, MovingData.class);
final Location newTo = enforceLocation(player, player.getLocation(useLoc), data);
final Location newTo = enforceLocation(player, player.getLocation(useTickLoc), data);
if (newTo != null) {
data.prepareSetBack(newTo);
player.teleport(newTo, BridgeMisc.TELEPORT_CAUSE_CORRECTION_OF_POSITION);
Expand Down Expand Up @@ -2770,7 +2778,7 @@ private boolean checkHover(final Player player, final MovingData data, final Mov
final PlayerMoveInfo info) {

// Check if player is on ground.
final Location loc = player.getLocation(useLoc); // useLoc.setWorld(null) is done in onTick.
final Location loc = player.getLocation(useTickLoc); // useLoc.setWorld(null) is done in onTick.
info.set(player, loc, null, cc.yOnGround);
// (Could use useLoc of MoveInfo here. Note orderm though.)
final boolean res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class MovingUtil {
* Always set world to null after use, careful with nested methods. Main thread only.
*/
private static final Location useLoc = new Location(null, 0, 0, 0);
private static final Location useLoc2 = new Location(null, 0, 0, 0);
// /** Fast scan flags for 'mostly air'. */
// private static final long FLAGS_SCAN_FOR_GROUND_OR_RESETCOND =
// BlockFlags.F_SOLID | BlockFlags.F_GROUND
Expand Down Expand Up @@ -377,7 +378,7 @@ public static Location checkUntrackedLocation(final Location loc) {
if (entity.getType() != EntityType.PLAYER) {
continue;
}
final Location refLoc = entity.getLocation(useLoc);
final Location refLoc = entity.getLocation(useLoc2);
// Exempt world spawn.
// TODO: Exempt other warps -> HASH based exemption (expire by time, keep high count)?
if (TrigUtil.isSamePos(loc, refLoc) && (entity instanceof Player)) {
Expand Down Expand Up @@ -406,7 +407,7 @@ else if (TrigUtil.isSamePos(refLoc, otherLastMove.to.getX(), otherLastMove.to.ge
}
}
}
useLoc.setWorld(null); // Cleanup.
useLoc2.setWorld(null); // Cleanup.
if (untrackedData == null) {
return null;
}
Expand Down

0 comments on commit de0486c

Please sign in to comment.