Refine location interfaces. Continue (Default/) SetBackStorage, naming.

This commit is contained in:
asofold 2016-04-29 14:47:40 +02:00
parent 64cbab1b0a
commit 5b8039c5ae
7 changed files with 258 additions and 34 deletions

View File

@ -1,5 +1,10 @@
package fr.neatmonster.nocheatplus.checks.moving.location.setback;
import org.bukkit.Location;
import fr.neatmonster.nocheatplus.components.location.ILocationWithLook;
import fr.neatmonster.nocheatplus.time.monotonic.Monotonic;
/**
* A default extension of SetBackStorage, featuring convenience methods for
* set-back locations with standard naming, including a default set-back.
@ -8,8 +13,8 @@ package fr.neatmonster.nocheatplus.checks.moving.location.setback;
* <li>1: Mid-term. Typically used by more packets checks.</li>
* <li>2: Safe-medium. Typically last ground/reset-condition or a default-medium
* in case of boats.</li>
* <li>3: Last valid. The latest end point of a move, not prevented by any
* check.</li>
* <li>3: Last-move. The latest end point of a move, not prevented by any check.
* This should usually be the most recent entry.</li>
* </ul>
*
* @author asofold
@ -19,8 +24,111 @@ public class DefaultSetBackStorage extends SetBackStorage {
// TODO: In case of keeping track of past moves, last valid could be skipped (vehicles are currently not planned to have past moves accessible).
public static final int indexDefault = 0;
public static final int indexMidTerm = 1;
public static final int indexSafeMedium = 2;
public static final int indexLastMove = 3;
public DefaultSetBackStorage() {
super(4, 0);
}
public boolean isMidTermEntryValid() {
return isEntryValid(indexMidTerm);
}
public boolean isSafeMediumEntryValid() {
return isEntryValid(indexSafeMedium);
}
public boolean isLastMoveEntryValid() {
return isEntryValid(indexLastMove);
}
/**
* Get the 'mid-term' set-back entry, disregarding validity.
* @return
*/
public SetBackEntry getMidTermEntry() {
return entries[indexMidTerm];
}
/**
* Get the 'safe-medium' set-back entry, disregarding validity.
* @return
*/
public SetBackEntry getSafeMediumEntry() {
return entries[indexSafeMedium];
}
/**
* Get the 'last-move' set-back entry, disregarding validity of the entry itself.
* @return
*/
public SetBackEntry getLastMoveEntry() {
return entries[indexLastMove];
}
/**
* Update the location for the default entry in-place. Time values are set
* as well.
*
* @param loc
*/
public void setMidTermEntry(final Location loc) {
getMidTermEntry().set(loc, ++time, Monotonic.millis());
}
/**
* Update the location for the default entry in-place. Time values are set
* as well.
*
* @param loc
*/
public void setMidTermEntry(final ILocationWithLook loc) {
getMidTermEntry().set(loc, ++time, Monotonic.millis());
}
/**
* Update the location for the default entry in-place. Time values are set
* as well.
*
* @param loc
*/
public void setSafeMediumEntry(final Location loc) {
getSafeMediumEntry().set(loc, ++time, Monotonic.millis());
}
/**
* Update the location for the default entry in-place. Time values are set
* as well.
*
* @param loc
*/
public void setSafeMediumEntry(final ILocationWithLook loc) {
getSafeMediumEntry().set(loc, ++time, Monotonic.millis());
}
/**
* Update the location for the default entry in-place. Time values are set
* as well.
*
* @param loc
*/
public void setLastMoveEntry(final Location loc) {
getLastMoveEntry().set(loc, ++time, Monotonic.millis());
}
/**
* Update the location for the default entry in-place. Time values are set
* as well.
*
* @param loc
*/
public void setLastMoveEntry(final ILocationWithLook loc) {
getLastMoveEntry().set(loc, ++time, Monotonic.millis());
}
// TODO: Getter methods with fall-back to default entry. Not sure: get a Location instance or just the entry?
}

View File

@ -3,7 +3,7 @@ package fr.neatmonster.nocheatplus.checks.moving.location.setback;
import org.bukkit.Location;
import org.bukkit.World;
import fr.neatmonster.nocheatplus.utilities.RichBoundsLocation;
import fr.neatmonster.nocheatplus.components.location.ILocationWithLook;
/**
* Mutable location with timing and validity information. Not complex objects
@ -12,7 +12,7 @@ import fr.neatmonster.nocheatplus.utilities.RichBoundsLocation;
* @author asofold
*
*/
public class SetBackEntry {
public class SetBackEntry implements ILocationWithLook {
private String worldName;
private double x, y, z;
@ -22,7 +22,7 @@ public class SetBackEntry {
// (use count, last use time, flags)
private boolean isValid = false;
SetBackEntry set(Location loc, int time, long msTime) {
public SetBackEntry set(final Location loc, final int time, final long msTime) {
worldName = loc.getWorld().getName();
x = loc.getX();
y = loc.getY();
@ -35,8 +35,8 @@ public class SetBackEntry {
return this;
}
SetBackEntry set(RichBoundsLocation loc, int time, long msTime) {
worldName = loc.getWorld().getName();
public SetBackEntry set(final ILocationWithLook loc, final int time, final long msTime) {
worldName = loc.getWorldName();
x = loc.getX();
y = loc.getY();
z = loc.getZ();
@ -48,22 +48,32 @@ public class SetBackEntry {
return this;
}
@Override
public String getWorldName() {
return worldName;
}
@Override
public double getX() {
return x;
}
@Override
public double getY() {
return y;
}
@Override
public double getZ() {
return z;
}
@Override
public float getPitch() {
return pitch;
}
@Override
public float getYaw() {
return yaw;
}
@ -80,10 +90,6 @@ public class SetBackEntry {
return isValid;
}
public String getWorldName() {
return worldName;
}
public void setX(double x) {
this.x = x;
}
@ -121,17 +127,24 @@ public class SetBackEntry {
}
/**
* Retrieve a Bukkit Location instance, using the given world.
*
* @param world
* @return A new Location object, containing the given world, ready to be used.
* @return A new Location object, containing the given world, ready to be
* used.
* @throws IllegalArgumentException
* In case the name of the given world does not match the stored
* one.
* @throws IllegalStateException
* In case the set-back entry is not valid.
*/
public Location getLocation(final World world) {
if (!world.getName().equals(worldName)) {
throw new IllegalArgumentException("The name of the given world must equal the stored world name.");
}
if (!isValid) {
throw new IllegalStateException("Can't return a Location instance from an invalid state.");
}
return new Location(world, x, y, z, yaw, pitch);
}

View File

@ -2,6 +2,7 @@ package fr.neatmonster.nocheatplus.checks.moving.location.setback;
import org.bukkit.Location;
import fr.neatmonster.nocheatplus.components.location.ILocationWithLook;
import fr.neatmonster.nocheatplus.time.monotonic.Monotonic;
/**
@ -34,6 +35,15 @@ public class SetBackStorage {
this.defaultIndex = defaultIndex;
}
/**
* Maximum number of stored locations, disregarding validity.
*
* @return
*/
public int size() {
return entries.length;
}
/**
* Oldest by time value.
*
@ -52,6 +62,24 @@ public class SetBackStorage {
return oldestEntry;
}
/**
* Latest by time value.
*
* @return
*/
public SetBackEntry getLatestValidEntry() {
int latestTime = 0;
SetBackEntry latestEntry = null;
for (int i = 0; i < entries.length; i++) {
final SetBackEntry entry = entries[i];
if (entry.isValid() && entry.getTime() > latestTime) {
latestTime = entry.getTime();
latestEntry = entry;
}
}
return latestEntry;
}
/**
* Retrieve the entry at a given index, only if valid. Set fallBack to true,
* in order to fall back to the default entry, if a default index is set,
@ -62,7 +90,7 @@ public class SetBackStorage {
* If to allow falling back to the default entry.
* @return
*/
public SetBackEntry getEntry(final int index, final boolean fallBack) {
public SetBackEntry getValidEntry(final int index, final boolean fallBack) {
if (entries[index].isValid()) {
return entries[index];
}
@ -92,7 +120,20 @@ public class SetBackStorage {
}
/**
* Invalidate all entries and reset the default to the given location.
* Hard-reset all entries to the given location. All will have the same time
* value.
*
* @param loc
*/
public void resetAll(final ILocationWithLook loc) {
time ++;
for (int i = 0; i < entries.length; i++) {
entries[i].set(loc, time, Monotonic.millis());
}
}
/**
* Invalidate all entries and reset the default entry to the given location.
* Internal time is increased. If no default is set, resetAll is called.
*
* @param loc
@ -108,12 +149,19 @@ public class SetBackStorage {
}
/**
* Maximum number of stored locations, disregarding validity.
* Invalidate all entries and reset the default entry to the given location.
* Internal time is increased. If no default is set, resetAll is called.
*
* @return
* @param loc
*/
public int size() {
return entries.length;
public void resetAllLazily(final ILocationWithLook loc) {
if (defaultIndex < 0) {
resetAll(loc);
}
else {
invalidateAll();
entries[defaultIndex].set(loc, ++time, Monotonic.millis());
}
}
/**
@ -121,7 +169,7 @@ public class SetBackStorage {
*
* @return
*/
public boolean isAnySetBackValid() {
public boolean isAnyEntryValid() {
for (int i = 0; i < entries.length; i++) {
if (entries[i].isValid()) {
return true;
@ -136,15 +184,42 @@ public class SetBackStorage {
* @param index
* @return
*/
public boolean isSetBackValid(final int index) {
public boolean isEntryValid(final int index) {
return entries[index].isValid();
}
public boolean isDefaultSetBackValid() {
public boolean isDefaultEntryValid() {
return defaultIndex >= 0 && entries[defaultIndex].isValid();
}
// TODO: Has/get/set for location, ILocation (IPosition for keeping the world + default world?).
/**
* Get the default set-back entry, disregarding validity.
*
* @return In case no default is set, null is returned. Otherwise the
* default entry is returned disregarding validity.
*/
public SetBackEntry getDefaultEntry() {
return defaultIndex < 0 ? null : entries[defaultIndex];
}
/**
* Update the location for the default entry in-place. Time values are set
* as well.
*
* @param loc
*/
public void setDefaultEntry(final Location loc) {
getDefaultEntry().set(loc, ++time, Monotonic.millis());
}
/**
* Update the location for the default entry in-place. Time values are set
* as well.
*
* @param loc
*/
public void setDefaultEntry(final ILocationWithLook loc) {
getDefaultEntry().set(loc, ++time, Monotonic.millis());
}
}

View File

@ -8,7 +8,7 @@ import org.bukkit.World;
* @author asofold
*
*/
public interface IBukkitLocation extends ILocation {
public interface IBukkitLocation extends ILocationWithLook {
public World getWorld();

View File

@ -0,0 +1,11 @@
package fr.neatmonster.nocheatplus.components.location;
/**
* A standard location (3D) with looking direction.
*
* @author asofold
*
*/
public interface ILocationWithLook extends ILocation, ILook {
}

View File

@ -0,0 +1,25 @@
package fr.neatmonster.nocheatplus.components.location;
/**
* Having a looking direction, represented by pitch and yaw.
*
* @author asofold
*
*/
public interface ILook {
/**
* Angle for vertical looking direction component in grad.
*
* @return
*/
public float getPitch();
/**
* Angle on xz-plane for the looking direction in grad.
*
* @return
*/
public float getYaw();
}

View File

@ -128,20 +128,12 @@ public class RichBoundsLocation implements IBukkitLocation, IBlockPosition {
return z;
}
/**
* Gets the yaw.
*
* @return the yaw
*/
@Override
public float getYaw() {
return yaw;
}
/**
* Gets the pitch.
*
* @return the pitch
*/
@Override
public float getPitch() {
return pitch;
}