mirror of
https://github.com/NoCheatPlus/NoCheatPlus.git
synced 2025-01-11 18:27:47 +01:00
Add a fail for null-worlds on updating set-back locations internally.
Setting a set-back location with a null World will now lead to a NullPointerException. Setting null worlds on set-back locations is not intended by contract, however due to using temporary Location instances with Entity.getLocation(Location) and setting the world to null afterwards to prevent memory leaks, also considering the number of places where setSetBack is used, this seems to be an appropriate measure to track better or help excluding violations of that contract.
This commit is contained in:
parent
dcb48c8800
commit
1f0aafb63e
@ -1,36 +1,45 @@
|
||||
package fr.neatmonster.nocheatplus.checks.moving;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
import fr.neatmonster.nocheatplus.utilities.PlayerLocation;
|
||||
|
||||
/**
|
||||
* Auxiliary methods for Location handling, mainly intended for use with set-back locations.
|
||||
* @author mc_dev
|
||||
*
|
||||
*/
|
||||
public class LocUtil {
|
||||
|
||||
/**
|
||||
* Simple get a copy (not actually using cloning).
|
||||
* Get a copy of a location (not actually using cloning).
|
||||
* @param loc
|
||||
* @return
|
||||
* @return A new Location instance.
|
||||
* @throws NullPointerException if World is null.
|
||||
*/
|
||||
public static final Location clone(final Location loc){
|
||||
return new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
|
||||
return new Location(testWorld(loc.getWorld()), loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone with given yaw and pitch.
|
||||
* Get a copy of a location (not actually using cloning), override yaw and pitch with given values.
|
||||
* @param loc
|
||||
* @param yaw
|
||||
* @param pitch
|
||||
* @return
|
||||
* @return A new Location instance.
|
||||
* @throws NullPointerException if the resulting world is null.
|
||||
*/
|
||||
public static final Location clone(final Location loc, final float yaw, final float pitch){
|
||||
return new Location(loc.getWorld(), loc.getX(), loc.getY(), loc.getZ(), yaw, pitch);
|
||||
return new Location(testWorld(loc.getWorld()), loc.getX(), loc.getY(), loc.getZ(), yaw, pitch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone with yaw and pitch of ref, use ref if setBack is null.
|
||||
* @param setBack
|
||||
* @param ref
|
||||
* @return
|
||||
* Clone setBack, with yaw and pitch taken from ref, if setBack is null, ref is cloned fully.
|
||||
* @param setBack Can be null.
|
||||
* @param ref Must not be null.
|
||||
* @return A new Location instance.
|
||||
* @throws NullPointerException if the resulting world is null.
|
||||
*/
|
||||
public static final Location clone(final Location setBack, final Location ref) {
|
||||
if (setBack == null){
|
||||
@ -49,26 +58,46 @@ public class LocUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* SA
|
||||
* Update setBack by loc.
|
||||
* @param setBack
|
||||
* @param loc
|
||||
* @throws NullPointerException if loc.getWorld() is null.
|
||||
*/
|
||||
public static final void set(final Location setBack, final Location loc) {
|
||||
setBack.setWorld(loc.getWorld());
|
||||
setBack.setWorld(testWorld(loc.getWorld()));
|
||||
setBack.setX(loc.getX());
|
||||
setBack.setY(loc.getY());
|
||||
setBack.setZ(loc.getZ());
|
||||
setBack.setYaw(loc.getYaw());
|
||||
setBack.setPitch(loc.getPitch());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update setBack by loc.
|
||||
* @param setBack
|
||||
* @param loc
|
||||
* @throws NullPointerException if loc.getWorld() is null.
|
||||
*/
|
||||
public static final void set(final Location setBack, final PlayerLocation loc) {
|
||||
setBack.setWorld(loc.getWorld());
|
||||
setBack.setWorld(testWorld(loc.getWorld()));
|
||||
setBack.setX(loc.getX());
|
||||
setBack.setY(loc.getY());
|
||||
setBack.setZ(loc.getZ());
|
||||
setBack.setYaw(loc.getYaw());
|
||||
setBack.setPitch(loc.getPitch());
|
||||
}
|
||||
|
||||
/**
|
||||
* Throw a NullPointerException if world is null.
|
||||
* @param world
|
||||
* @return
|
||||
*/
|
||||
private static World testWorld(final World world) {
|
||||
if (world == null) {
|
||||
throw new NullPointerException("World must not be null.");
|
||||
} else {
|
||||
return world;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user