Skip to content

Commit

Permalink
Less object creation with short-use Locations + other preparation.
Browse files Browse the repository at this point in the history
* Make use of Entity.getLocation(Location) in most places, for starters.
* Prepare a check for moving location consistency.
  • Loading branch information
asofold committed Feb 23, 2014
1 parent 16db04f commit abdeb12
Show file tree
Hide file tree
Showing 23 changed files with 243 additions and 95 deletions.
Expand Up @@ -11,6 +11,9 @@ public class BlockCacheBukkit extends BlockCache{

protected World world;

/** Temporary use. Use LocUtil.clone before passing on. Call setWorld(null) after use. */
protected final Location useLoc = new Location(null, 0, 0, 0);

public BlockCacheBukkit(World world) {
setAccess(world);
}
Expand Down Expand Up @@ -49,8 +52,9 @@ public boolean standsOnEntity(final Entity entity, final double minX, final doub
if (type != EntityType.BOAT){ // && !(other instanceof Minecart))
continue;
}
final Location loc = entity.getLocation();
if (Math.abs(loc.getY() - minY) < 0.7){
final double locY = entity.getLocation(useLoc).getY();
useLoc.setWorld(null);
if (Math.abs(locY - minY) < 0.7){
// TODO: A "better" estimate is possible, though some more tolerance would be good.
return true;
}
Expand Down
Expand Up @@ -13,6 +13,9 @@
* The Direction check will find out if a player tried to interact with something that's not in their field of view.
*/
public class Direction extends Check {

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

/**
* Instantiates a new direction check.
Expand All @@ -36,7 +39,7 @@ public boolean check(final Player player, final Block block, final BlockBreakDat

// How far "off" is the player with their aim. We calculate from the players eye location and view direction to
// the center of the target block. If the line of sight is more too far off, "off" will be bigger than 0.
final Location loc = player.getLocation();
final Location loc = player.getLocation(useLoc);
final Vector direction = loc.getDirection();
final double off = TrigUtil.directionCheck(loc, player.getEyeHeight(), direction, block, TrigUtil.DIRECTION_PRECISION);

Expand All @@ -52,10 +55,11 @@ public boolean check(final Player player, final Block block, final BlockBreakDat
// cancel the event.
cancel = executeActions(player, data.directionVL, distance,
BlockBreakConfig.getConfig(player).directionActions);
} else
} else {
// Player did likely nothing wrong, reduce violation counter to reward them.
data.directionVL *= 0.9D;

}
useLoc.setWorld(null);
return cancel;
}
}
Expand Up @@ -3,6 +3,7 @@
import java.util.Map;

import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;

Expand All @@ -16,6 +17,9 @@
* The Reach check will find out if a player interacts with something that's too far away.
*/
public class Reach extends Check {

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

/** The maximum distance allowed to interact with a block in creative mode. */
public static final double CREATIVE_DISTANCE = 5.6D;
Expand Down Expand Up @@ -47,7 +51,10 @@ public boolean check(final Player player, final Block block, final BlockBreakDat

// Distance is calculated from eye location to center of targeted block. If the player is further away from their
// target than allowed, the difference will be assigned to "distance".
final double distance = TrigUtil.distance(player.getEyeLocation(), block) - distanceLimit;
final Location loc = player.getLocation(useLoc);
loc.setY(loc.getY() + player.getEyeHeight());
final double distance = TrigUtil.distance(loc, block) - distanceLimit;
useLoc.setWorld(null);

if (distance > 0) {
// They failed, increment violation level.
Expand Down
Expand Up @@ -36,6 +36,9 @@ public class BlockInteractListener extends CheckListener {
/** Speed of interaction. */
private final Speed speed = addCheck(new Speed());

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

public BlockInteractListener(){
super(CheckType.BLOCKINTERACT);
}
Expand Down Expand Up @@ -86,7 +89,7 @@ protected void onPlayerInteract(final PlayerInteractEvent event) {
boolean cancelled = false;

final BlockFace face = event.getBlockFace();
final Location loc = player.getLocation();
final Location loc = player.getLocation(useLoc);

// Interaction speed.
if (!cancelled && speed.isEnabled(player) && speed.check(player, data, cc)){
Expand Down Expand Up @@ -114,5 +117,6 @@ protected void onPlayerInteract(final PlayerInteractEvent event) {
event.setUseItemInHand(Result.DENY);
event.setCancelled(true);
}
useLoc.setWorld(null);
}
}
Expand Up @@ -75,6 +75,9 @@ public static int getBlockPlaceHash(final Block block, final Material mat){
/** The speed check. */
private final Speed speed = addCheck(new Speed());

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

public BlockPlaceListener(){
super(CheckType.BLOCKPLACE);
}
Expand Down Expand Up @@ -131,6 +134,7 @@ public void onBlockPlace(final BlockPlaceEvent event) {
if (!cancelled && reach.isEnabled(player) && reach.check(player, block, data, cc)) {
cancelled = true;
}
useLoc.setWorld(null);

// Direction check.
if (!cancelled && direction.isEnabled(player) && direction.check(player, block, blockAgainst, data, cc)) {
Expand Down Expand Up @@ -264,7 +268,7 @@ public void onProjectileLaunch(final ProjectileLaunchEvent event) {
boolean cancel = false;
if (speed.isEnabled(player)){
final long now = System.currentTimeMillis();
final Location loc = player.getLocation();
final Location loc = player.getLocation(useLoc);
if (Combined.checkYawRate(player, loc.getYaw(), now, loc.getWorld().getName())){
// Yawrate (checked extra).
cancel = true;
Expand All @@ -285,19 +289,20 @@ else if (Improbable.check(player, 0.6f, now, "blockplace.speed")){
// Do nothing !
// TODO: Might have further flags?
}
else if (!BlockProperties.isPassable(projectile.getLocation())){
else if (!BlockProperties.isPassable(projectile.getLocation(useLoc))){
// Launch into a block.
// TODO: This might be a general check later.
cancel = true;
}
else{
if (!BlockProperties.isPassable(player.getEyeLocation(), projectile.getLocation())){
if (!BlockProperties.isPassable(player.getEyeLocation(), projectile.getLocation(useLoc))){
// (Spare a useLoc2, for this is seldom rather.)
// Something between player
// TODO: This might be a general check later.
cancel = true;
}
else{
final Material mat = player.getLocation().getBlock().getType();
final Material mat = player.getLocation(useLoc).getBlock().getType();
final long flags = BlockProperties.F_CLIMBABLE | BlockProperties.F_LIQUID | BlockProperties.F_IGN_PASSABLE;
if (mat != Material.AIR && (BlockProperties.getBlockFlags(mat.getId()) & flags) == 0 && !mcAccess.hasGravity(mat)){
// Still fails on piston traps etc.
Expand All @@ -313,5 +318,7 @@ else if (!BlockProperties.isPassable(projectile.getLocation())){
if (cancel){
event.setCancelled(true);
}
// Cleanup.
useLoc.setWorld(null);
}
}
Expand Up @@ -13,6 +13,9 @@
* The Direction check will find out if a player tried to interact with something that's not in their field of view.
*/
public class Direction extends Check {

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

/**
* Instantiates a new direction check.
Expand All @@ -38,7 +41,7 @@ public boolean check(final Player player, final Block placed, final Block agains

// How far "off" is the player with their aim. We calculate from the players eye location and view direction to
// the center of the target block. If the line of sight is more too far off, "off" will be bigger than 0.
final Location loc = player.getLocation();
final Location loc = player.getLocation(useLoc);
final Vector direction = loc.getDirection();
double off = TrigUtil.directionCheck(loc, player.getEyeHeight(), direction, against, TrigUtil.DIRECTION_PRECISION);

Expand Down Expand Up @@ -75,10 +78,11 @@ else if (placed.getZ() < against.getZ())
// Execute whatever actions are associated with this check and the violation level and find out if we should
// cancel the event.
cancel = executeActions(player, data.directionVL, distance, cc.directionActions);
} else
} else {
// Player did likely nothing wrong, reduce violation counter to reward them.
data.directionVL *= 0.9D;

}
useLoc.setWorld(null);
return cancel;
}
}
Expand Up @@ -3,6 +3,7 @@
import java.util.Map;

import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;

Expand All @@ -22,6 +23,9 @@ public class Reach extends Check {

/** The maximum distance allowed to interact with a block in survival mode. */
public static final double SURVIVAL_DISTANCE = 5.2D;

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

/**
* Instantiates a new reach check.
Expand All @@ -35,6 +39,7 @@ public Reach() {
*
* @param player
* the player
* @param loc
* @param cc
* @param data2
* @param location
Expand All @@ -49,7 +54,9 @@ public boolean check(final Player player, final Block block, final BlockPlaceDat

// Distance is calculated from eye location to center of targeted block. If the player is further away from their
// target than allowed, the difference will be assigned to "distance".
final double distance = TrigUtil.distance(player.getEyeLocation(), block) - distanceLimit;
final Location eyeLoc = player.getLocation(useLoc);
eyeLoc.setY(eyeLoc.getY() + player.getEyeHeight());
final double distance = TrigUtil.distance(eyeLoc, block) - distanceLimit;

if (distance > 0) {
// They failed, increment violation level.
Expand All @@ -66,7 +73,9 @@ public boolean check(final Player player, final Block block, final BlockPlaceDat
data.reachVL *= 0.9D;
}


// Cleanup.
useLoc.setWorld(null);

return cancel;
}

Expand Down
Expand Up @@ -49,7 +49,7 @@ public boolean check(final Player player, final boolean worldChanged) {
data.angleHits.remove(time);

// Add the new location to the map.
data.angleHits.put(System.currentTimeMillis(), player.getLocation());
data.angleHits.put(System.currentTimeMillis(), player.getLocation()); // This needs to be a copy at present.

// Not enough data to calculate deltas.
if (data.angleHits.size() < 2)
Expand Down
Expand Up @@ -38,14 +38,11 @@ public Critical() {
* the player
* @return true, if successful
*/
public boolean check(final Player player) {
public boolean check(final Player player, final Location loc) {
final FightConfig cc = FightConfig.getConfig(player);
final FightData data = FightData.getData(player);

boolean cancel = false;

// We'll need the PlayerLocation to know some important stuff.
final Location loc = player.getLocation();

final float mcFallDistance = player.getFallDistance();
final MovingConfig mCc = MovingConfig.getConfig(player);
Expand Down
Expand Up @@ -30,7 +30,7 @@ public Direction() {
* the damaged
* @return true, if successful
*/
public boolean check(final Player player, final Entity damaged) {
public boolean check(final Player player, final Location loc, final Entity damaged, final Location dLoc) {
final FightConfig cc = FightConfig.getConfig(player);
final FightData data = FightData.getData(player);

Expand All @@ -47,14 +47,11 @@ public boolean check(final Player player, final Entity damaged) {
// entity.height is broken and will always be 0, therefore. Calculate height instead based on boundingBox.
final double height = mcAccess.getHeight(damaged);

final Location dLoc = damaged.getLocation();

// TODO: allow any hit on the y axis (might just adapt interface to use foot position + height)!

// How far "off" is the player with their aim. We calculate from the players eye location and view direction to
// the center of the target entity. If the line of sight is more too far off, "off" will be bigger than 0.
final Location loc = player.getLocation();
final Vector direction = player.getEyeLocation().getDirection();
final Vector direction = loc.getDirection();

final double off;
if (cc.directionStrict){
Expand Down

0 comments on commit abdeb12

Please sign in to comment.