Skip to content

Commit

Permalink
[BLEEDING/INSTABLE] Not that much desperate: Random code changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
asofold committed Oct 25, 2013
1 parent ced4e0e commit 76eb1c2
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 24 deletions.
Expand Up @@ -10,6 +10,7 @@
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.checks.ViolationData;
import fr.neatmonster.nocheatplus.utilities.PlayerLocation;
import fr.neatmonster.nocheatplus.utilities.TrigUtil;

/*
* MM'""""'YMM dP oo MM""""""""`M dP
Expand Down Expand Up @@ -145,7 +146,7 @@ public Location check(final Player player, final PlayerLocation from, final Play
if (vd.needsParameters()){
vd.setParameter(ParameterName.LOCATION_FROM, String.format(Locale.US, "%.2f, %.2f, %.2f", from.getX(), from.getY(), from.getZ()));
vd.setParameter(ParameterName.LOCATION_TO, String.format(Locale.US, "%.2f, %.2f, %.2f", to.getX(), to.getY(), to.getZ()));
vd.setParameter(ParameterName.DISTANCE, String.format(Locale.US, "%.2f", to.getLocation().distance(from.getLocation())));
vd.setParameter(ParameterName.DISTANCE, String.format(Locale.US, "%.2f", TrigUtil.distance(from, to)));
}
if (executeActions(vd))
// Compose a new location based on coordinates of "newTo" and viewing direction of "event.getTo()"
Expand Down
@@ -0,0 +1,40 @@
package fr.neatmonster.nocheatplus.checks.moving;

import org.bukkit.Location;

import fr.neatmonster.nocheatplus.utilities.TrigUtil;

/**
* Consistency of a Location/thing concerning a move with a from and a to location.
* @author mc_dev
*
*/
public enum MoveConsistency {
/**
* Consistent with the to-location. Highest priority.
*/
TO,
/**
* Consistent with the from-location.
*/
FROM,
/**
* Not consistent. Lowest priority.
*/
INCONSISTENT;



public static final double maxDistance = 1.25;
public static final double maxDistanceSq = maxDistance * maxDistance;

public static MoveConsistency getConsistency(final Location from, final Location to, final Location loc) {
if (to != null && TrigUtil.distanceSquared(to, loc) < maxDistanceSq) {
return TO;
} else if (from != null && TrigUtil.distanceSquared(from, loc) < maxDistanceSq) {
return FROM;
} else {
return INCONSISTENT;
}
}
}
Expand Up @@ -203,6 +203,7 @@ public static void onWorldUnload(final World world){
// HOT FIX
/** Inconsistency-flag. Set on moving inside of vehicles, reset on exiting properly. Workaround for VehicleLeaveEvent missing. */
public boolean wasInVehicle = false;
public MoveConsistency vehicleConsistency = MoveConsistency.INCONSISTENT;

/**
* Clear the data of the fly checks (not more-packets).
Expand All @@ -224,6 +225,7 @@ public void clearFlyData() {
sfDirty = false;
sfLowJump = false;
mediumLiftOff = defaultMediumLiftOff;
vehicleConsistency = MoveConsistency.INCONSISTENT;
}

/**
Expand Down Expand Up @@ -255,6 +257,7 @@ public void onSetBack(final Location setBack) {
sfLowJump = false;
mediumLiftOff = defaultMediumLiftOff;
removeAllVelocity();
vehicleConsistency = MoveConsistency.INCONSISTENT; // Not entirely sure here.
}

/**
Expand Down Expand Up @@ -304,6 +307,7 @@ public void resetPositions(final double x, final double y, final double z) {
sfLowJump = false;
mediumLiftOff = defaultMediumLiftOff;
// TODO: other buffers ?
// No reset of vehicleConsistency.
}

/**
Expand Down
Expand Up @@ -9,6 +9,7 @@
import java.util.Set;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
Expand Down Expand Up @@ -254,7 +255,7 @@ public void onBlockPlace(final BlockPlaceEvent event) {
* @param id
* @return
*/
private final boolean canJumpOffTop(final int id){
private static boolean canJumpOffTop(final int id){
// TODO: Test if this can be removed!
return BlockProperties.isGround(id) || BlockProperties.isSolid(id);
}
Expand Down Expand Up @@ -504,7 +505,7 @@ else if (time < data.timeSprinting){
if (cc.debug){
LogUtil.logWarning("[NoCheatPlus] VehicleExitEvent missing for: " + player.getName());
}
onPlayerVehicleLeave(player);
onPlayerVehicleLeave(player, null);
// if (BlockProperties.isRails(pFrom.getTypeId())){
// Always clear no fall data, let Minecraft do fall damage.
data.noFallSkipAirCheck = true; // Might allow one time cheat.
Expand Down Expand Up @@ -710,7 +711,7 @@ else if (checkCf){
* @param event
*/
@EventHandler(priority=EventPriority.MONITOR, ignoreCancelled = false)
public final void onPlayerMoveMonitor(final PlayerMoveEvent event){
public void onPlayerMoveMonitor(final PlayerMoveEvent event){
// TODO: revise: cancelled events.
final long now = System.currentTimeMillis();
final Player player = event.getPlayer();
Expand Down Expand Up @@ -1086,13 +1087,24 @@ public void onVehicleMove(final Entity vehicle, final Location from, final Locat
return;
}
if (vehicle.isDead() || !vehicle.isValid()) {
onPlayerVehicleLeave(player);
onPlayerVehicleLeave(player, vehicle);
return;
}
if (!from.getWorld().equals(to.getWorld())) return;

Location newTo = null;
final MovingData data = MovingData.getData(player);
data.vehicleConsistency = MoveConsistency.getConsistency(from, to, player.getLocation());
switch (data.vehicleConsistency) {
case FROM:
case TO:
data.resetPositions(player.getLocation()); // TODO: Drop MC 1.4!
break;
case INCONSISTENT:
// TODO: Any exploits exist? -> TeleportUtil.forceMount(player, vehicle)
break;
}

Location newTo = null;
data.sfNoLowJump = true;

final MovingConfig cc = MovingConfig.getConfig(player);
Expand Down Expand Up @@ -1281,46 +1293,101 @@ public void onWorldunload(final WorldUnloadEvent event){
public void onVehicleExit(final VehicleExitEvent event){
final Entity entity = event.getExited();
if (!(entity instanceof Player)) return;
onPlayerVehicleLeave((Player) entity);
onPlayerVehicleLeave((Player) entity, event.getVehicle());
}

@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onVehicleDestroyLowest(final VehicleDestroyEvent event){
// Prevent destroying ones own vehicle.
final Entity attacker = event.getAttacker();
if (!(attacker instanceof Player)) {
return;
}
final Vehicle vehicle = event.getVehicle();
if (!attacker.equals(vehicle.getPassenger())) {
return;
}
final Player player = (Player) attacker;
if (!creativeFly.isEnabled(player) && !survivalFly.isEnabled(player)) {
return;
}
event.setCancelled(true);
player.sendMessage(ChatColor.DARK_RED + "Destroying your own vehicle is disabled.");
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onVehicleDestroy(final VehicleDestroyEvent event){
final Entity entity = event.getVehicle().getPassenger();
if (!(entity instanceof Player)) return;
onPlayerVehicleLeave((Player) entity);
onPlayerVehicleLeave((Player) entity, event.getVehicle());
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public final void onPlayerVehicleEnter(final VehicleEnterEvent event){
public void onPlayerVehicleEnter(final VehicleEnterEvent event){
final Entity entity = event.getEntered();
if (!(entity instanceof Player)){
return;
}
final Player player = (Player) entity;
final MovingData data = MovingData.getData(player);
data.removeAllVelocity();
// Event should have a vehicle, in case check this last.
data.vehicleConsistency = MoveConsistency.getConsistency(event.getVehicle().getLocation(), null, player.getLocation());
// TODO: more resetting, visible check ?
}

private final void onPlayerVehicleLeave(final Player player){
/**
* Call on leaving or just having left a vehicle.
* @param player
* @param vehicle May be null in case of "not possible to determine".
*/
private void onPlayerVehicleLeave(final Player player, final Entity vehicle){
final MovingData data = MovingData.getData(player);
data.wasInVehicle = false;
// if (data.morePacketsVehicleTaskId != -1){
// // Await set-back.
// // TODO: might still set ordinary set-backs ?
// return;
// }
// Reset survivalfly set-back to prevent the worst damage.
final Location loc = player.getLocation();

final MovingConfig cc = MovingConfig.getConfig(player);
// TODO: Loc can be inconsistent, determine which to use !
final Location pLoc = player.getLocation();
Location loc = pLoc; // The location to use as set-back.
// TODO: Which vehicle to use ?
// final Entity vehicle = player.getVehicle();
if (vehicle != null) {
final Location vLoc = vehicle.getLocation();
// Workaround for some entities/animals that don't fire VehicleMoveEventS.
if (!normalVehicles.contains(vehicle.getType()) || cc.noFallVehicleReset){
data.noFallSkipAirCheck = true; // Might allow one time cheat.
data.clearNoFallData();
}
// Check consistency with vehicle location.
if (MoveConsistency.getConsistency(vLoc, null, pLoc) == MoveConsistency.INCONSISTENT) {
// TODO: Consider teleporting the player (...)
// TODO: What with the case of vehicle moved to another world !?
loc = vLoc; //
if (data.vehicleConsistency != MoveConsistency.INCONSISTENT) {
final Location oldLoc = new Location(pLoc.getWorld(), data.toX, data.toY, data.toZ);
if (MoveConsistency.getConsistency(oldLoc, null, pLoc) != MoveConsistency.INCONSISTENT) {
loc = oldLoc;
}
}

}
if (cc.debug) {
System.out.println(player.getName() + " vehicle leave: " + vehicle.getType() + "@" + pLoc.distance(vLoc));
}
}

// Adjust loc if in liquid (meant for boats !?).
if (BlockProperties.isLiquid(loc.getBlock().getTypeId())){
loc.setY(Location.locToBlock(loc.getY()) + 1.25);
}
final Entity vehicle = player.getVehicle();
final EntityType vehicleType = vehicle == null ? null : vehicle.getType();
if (vehicleType != null && !normalVehicles.contains(vehicleType) || MovingConfig.getConfig(player).noFallVehicleReset){
data.noFallSkipAirCheck = true; // Might allow one time cheat.
data.clearNoFallData();

if (cc.debug) {
System.out.println(player.getName() + " vehicle leave: " + pLoc.toString() + (pLoc.equals(loc) ? "" : " / player at: " + pLoc.toString()));
}
data.resetPositions(loc);
data.setSetBack(loc);
Expand All @@ -1346,7 +1413,7 @@ public void onPlayerToggleSprint(final PlayerToggleSprintEvent event){
}

@Override
public final void onTick(final int tick, final long timeLast) {
public void onTick(final int tick, final long timeLast) {
// Hover checks !
if (tick % hoverTicksStep != 0){
// Only check every so and so ticks.
Expand Down Expand Up @@ -1410,7 +1477,7 @@ else if (data.sfHoverLoginTicks > 0){
* @param info
* @return
*/
private final 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 MoveInfo info) {
// Check if player is on ground.
final Location loc = player.getLocation();
info.set(player, loc, null, cc.yOnGround);
Expand Down Expand Up @@ -1446,7 +1513,7 @@ private final boolean checkHover(final Player player, final MovingData data, fin
return res;
}

private final void handleHoverViolation(final Player player, final Location loc, final MovingConfig cc, final MovingData data) {
private void handleHoverViolation(final Player player, final Location loc, final MovingConfig cc, final MovingData data) {
// Check nofall damage (!).
if (cc.sfHoverFallDamage && noFall.isEnabled(player)){
// Consider adding 3/3.5 to fall distance if fall distance > 0?
Expand Down
Expand Up @@ -10,6 +10,7 @@
import fr.neatmonster.nocheatplus.utilities.BlockProperties;
import fr.neatmonster.nocheatplus.utilities.PassableRayTracing;
import fr.neatmonster.nocheatplus.utilities.PlayerLocation;
import fr.neatmonster.nocheatplus.utilities.TrigUtil;

public class Passable extends Check {

Expand Down Expand Up @@ -119,12 +120,21 @@ else if (manhattan > 0){
return null;
}

// Discard inconsistent locations.
// TODO: Might get rid of using the in-between loc - needs use-case checking.
if (loc != null && (TrigUtil.distance(from, to) > 0.75 || TrigUtil.distance(from, loc) > 0.125)) {
loc = null;
}

// Prefer the set-back location from the data.
if (data.hasSetBack()){
final Location ref = data.getSetBack(to);
if (BlockProperties.isPassable(from.getBlockCache(), ref)){
if (BlockProperties.isPassable(from.getBlockCache(), ref) || TrigUtil.distance(from, loc) > 0.13){
// if (BlockProperties.isPassableExact(from.getBlockCache(), ref)){
loc = ref;
if (cc.debug) {
System.out.println(player.getName() + " Using set-back location for passable.");
}
}
}

Expand All @@ -142,8 +152,16 @@ else if (manhattan > 0){
if (executeActions(vd)) {
// TODO: Consider another set back position for this, also keeping track of players moving around in blocks.
final Location newTo;
if (loc != null) newTo = loc;
else newTo = from.getLocation();
if (loc != null) {
newTo = loc;
}
else {
// TODO: Consider logging this one.
newTo = from.getLocation();
if (cc.debug) {
System.out.println(player.getName() + " Using from location for passable.");
}
}
newTo.setYaw(to.getYaw());
newTo.setPitch(to.getPitch());
return newTo;
Expand Down
Expand Up @@ -18,6 +18,7 @@
import fr.neatmonster.nocheatplus.utilities.BlockProperties;
import fr.neatmonster.nocheatplus.utilities.PlayerLocation;
import fr.neatmonster.nocheatplus.utilities.StringUtil;
import fr.neatmonster.nocheatplus.utilities.TrigUtil;
import fr.neatmonster.nocheatplus.utilities.build.BuildParameters;

/*
Expand Down Expand Up @@ -1300,7 +1301,7 @@ private final Location handleViolation(final long now, final double result, fina
if (vd.needsParameters()) {
vd.setParameter(ParameterName.LOCATION_FROM, String.format(Locale.US, "%.2f, %.2f, %.2f", from.getX(), from.getY(), from.getZ()));
vd.setParameter(ParameterName.LOCATION_TO, String.format(Locale.US, "%.2f, %.2f, %.2f", to.getX(), to.getY(), to.getZ()));
vd.setParameter(ParameterName.DISTANCE, String.format(Locale.US, "%.2f", to.getLocation().distance(from.getLocation())));
vd.setParameter(ParameterName.DISTANCE, String.format(Locale.US, "%.2f", TrigUtil.distance(from, to)));
vd.setParameter(ParameterName.TAGS, StringUtil.join(tags, "+"));
}
// Some resetting is done in MovingListener.
Expand Down
Expand Up @@ -37,5 +37,19 @@ else if (passenger == null && !vehicle.isDead()){
System.out.println(player.getName() + " vehicle set back: " + location);
}
}

/**
* Force mounting the vehicle including teleportation.
* @param passenger
* @param vehicle
*/
public static void forceMount(Entity passenger, Entity vehicle) {
if (vehicle.getPassenger() != null) {
vehicle.eject();
}
if (passenger.teleport(vehicle) && !vehicle.isDead() && vehicle.isValid()) {
vehicle.setPassenger(passenger);
}
}

}

0 comments on commit 76eb1c2

Please sign in to comment.