Refactoring

This commit is contained in:
filoghost 2021-08-19 10:37:18 +02:00
parent 9fc8fceea7
commit 163b881837
8 changed files with 94 additions and 47 deletions

View File

@ -48,10 +48,11 @@ public abstract class BaseClickableLine extends BaseHologramLine {
private boolean isInClickRange(Player player) {
Location playerLocation = player.getLocation();
BaseLinePosition position = this.getPosition();
double xDiff = playerLocation.getX() - this.getX();
double yDiff = playerLocation.getY() + 1.3 - this.getY(); // Use shoulder height
double zDiff = playerLocation.getZ() - this.getZ();
double xDiff = playerLocation.getX() - position.getX();
double yDiff = playerLocation.getY() + 1.3 - position.getY(); // Use shoulder height
double zDiff = playerLocation.getZ() - position.getZ();
double distanceSquared = (xDiff * xDiff) + (yDiff * yDiff) + (zDiff * zDiff);
return distanceSquared < 5 * 5;

View File

@ -12,6 +12,7 @@ import org.bukkit.GameMode;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public abstract class BaseHologramLine extends BaseHologramComponent implements EditableHologramLine {
@ -19,7 +20,7 @@ public abstract class BaseHologramLine extends BaseHologramComponent implements
private final BaseHologram hologram;
private final LineTracker<?> tracker;
private double x, y, z;
private BaseLinePosition position;
protected BaseHologramLine(BaseHologram hologram) {
Preconditions.notNull(hologram, "parent hologram");
@ -39,28 +40,21 @@ public abstract class BaseHologramLine extends BaseHologramComponent implements
@Override
public final void setPosition(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
position = new BaseLinePosition(x, y, z);
setChanged();
}
public @NotNull BaseLinePosition getPosition() {
if (position == null) {
throw new IllegalStateException("position not set");
}
return position;
}
public @Nullable World getWorldIfLoaded() {
return hologram.getPositionWorldIfLoaded();
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
public boolean isInLoadedChunk() {
return hologram.isInLoadedChunk();
}

View File

@ -0,0 +1,54 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package me.filoghost.holographicdisplays.plugin.hologram.base;
public final class BaseLinePosition {
private final double x, y, z;
public BaseLinePosition(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
BaseLinePosition other = (BaseLinePosition) obj;
return Double.doubleToLongBits(this.x) == Double.doubleToLongBits(other.x)
&& Double.doubleToLongBits(this.y) == Double.doubleToLongBits(other.y)
&& Double.doubleToLongBits(this.z) == Double.doubleToLongBits(other.z);
}
@Override
public int hashCode() {
int result = 1;
result = 31 * result + Double.hashCode(x);
result = 31 * result + Double.hashCode(y);
result = 31 * result + Double.hashCode(z);
return result;
}
}

View File

@ -61,7 +61,7 @@ public abstract class ClickableLineTracker<T extends BaseClickableLine> extends
@Override
protected void addSpawnPackets(NMSPacketList packetList) {
if (spawnClickableEntity) {
clickableEntity.addSpawnPackets(packetList, positionX, getClickablePositionY(), positionZ);
clickableEntity.addSpawnPackets(packetList, position.getX(), getClickablePositionY(), position.getZ());
}
}
@ -80,7 +80,7 @@ public abstract class ClickableLineTracker<T extends BaseClickableLine> extends
if (spawnClickableEntityChanged) {
if (spawnClickableEntity) {
clickableEntity.addSpawnPackets(packetList, positionX, getClickablePositionY(), positionZ);
clickableEntity.addSpawnPackets(packetList, position.getX(), getClickablePositionY(), position.getZ());
} else {
clickableEntity.addDestroyPackets(packetList);
}
@ -91,12 +91,12 @@ public abstract class ClickableLineTracker<T extends BaseClickableLine> extends
@Override
protected void addPositionChangePackets(NMSPacketList packetList) {
if (spawnClickableEntity) {
clickableEntity.addTeleportPackets(packetList, positionX, getClickablePositionY(), positionZ);
clickableEntity.addTeleportPackets(packetList, position.getX(), getClickablePositionY(), position.getZ());
}
}
private double getClickablePositionY() {
return positionY + ((line.getHeight() - ClickableNMSPacketEntity.SLIME_HEIGHT) / 2);
return position.getY() + ((line.getHeight() - ClickableNMSPacketEntity.SLIME_HEIGHT) / 2);
}
}

View File

@ -5,6 +5,7 @@
*/
package me.filoghost.holographicdisplays.plugin.hologram.tracking;
import me.filoghost.holographicdisplays.plugin.hologram.base.BaseLinePosition;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@ -24,7 +25,7 @@ public class CollisionHelper {
private static final double REQUIRED_DISTANCE_Z = REQUIRED_DISTANCE_X;
private static final double REQUIRED_DISTANCE_Y = PLAYER_HEIGHT_RADIUS + PLAYER_GROW_HEIGHT + ITEM_HEIGHT_RADIUS;
public static boolean isInPickupRange(Player player, double itemX, double itemY, double itemZ) {
public static boolean isInPickupRange(Player player, BaseLinePosition itemPosition) {
/*
* Normally, the server determines if a player is picking up a dropped item
* by checking if the bounding boxes of the player and the item intersect.
@ -61,10 +62,10 @@ public class CollisionHelper {
// The Y position is on lowest point of the bounding box, not in the center
double playerCenterY = playerLocation.getY() + PLAYER_HEIGHT_RADIUS;
double itemCenterY = itemY + ITEM_HEIGHT_RADIUS;
double itemCenterY = itemPosition.getY() + ITEM_HEIGHT_RADIUS;
double xDiff = Math.abs(playerLocation.getX() - itemX);
double zDiff = Math.abs(playerLocation.getZ() - itemZ);
double xDiff = Math.abs(playerLocation.getX() - itemPosition.getX());
double zDiff = Math.abs(playerLocation.getZ() - itemPosition.getZ());
double yDiff = Math.abs(playerCenterY - itemCenterY);
return xDiff <= REQUIRED_DISTANCE_X

View File

@ -39,7 +39,7 @@ public class ItemLineTracker extends ClickableLineTracker<BaseItemLine> {
if (spawnItemEntity && hasTrackedPlayers()) {
for (Player trackedPlayer : getTrackedPlayers()) {
if (CollisionHelper.isInPickupRange(trackedPlayer, positionX, positionY, positionZ)) {
if (CollisionHelper.isInPickupRange(trackedPlayer, position)) {
line.onPickup(trackedPlayer);
}
}
@ -83,7 +83,7 @@ public class ItemLineTracker extends ClickableLineTracker<BaseItemLine> {
super.addSpawnPackets(packetList);
if (spawnItemEntity) {
itemEntity.addSpawnPackets(packetList, positionX, positionY, positionZ, itemStack);
itemEntity.addSpawnPackets(packetList, position.getX(), position.getY(), position.getZ(), itemStack);
}
}
@ -104,7 +104,7 @@ public class ItemLineTracker extends ClickableLineTracker<BaseItemLine> {
if (spawnItemEntityChanged) {
if (spawnItemEntity) {
itemEntity.addSpawnPackets(packetList, positionX, positionY, positionZ, itemStack);
itemEntity.addSpawnPackets(packetList, position.getX(), position.getY(), position.getZ(), itemStack);
} else {
itemEntity.addDestroyPackets(packetList);
}
@ -120,7 +120,7 @@ public class ItemLineTracker extends ClickableLineTracker<BaseItemLine> {
super.addPositionChangePackets(packetList);
if (spawnItemEntity) {
itemEntity.addTeleportPackets(packetList, positionX, positionY, positionZ);
itemEntity.addTeleportPackets(packetList, position.getX(), position.getY(), position.getZ());
}
}

View File

@ -7,17 +7,18 @@ package me.filoghost.holographicdisplays.plugin.hologram.tracking;
import me.filoghost.holographicdisplays.common.nms.NMSPacketList;
import me.filoghost.holographicdisplays.plugin.hologram.base.BaseHologramLine;
import me.filoghost.holographicdisplays.plugin.hologram.base.BaseLinePosition;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
import java.util.Objects;
abstract class PositionBasedLineTracker<T extends BaseHologramLine> extends LineTracker<T> {
private static final int ENTITY_VIEW_RANGE = 64;
protected double positionX;
protected double positionY;
protected double positionZ;
protected BaseLinePosition position;
private boolean positionChanged;
PositionBasedLineTracker(T line) {
@ -27,13 +28,9 @@ abstract class PositionBasedLineTracker<T extends BaseHologramLine> extends Line
@MustBeInvokedByOverriders
@Override
protected void detectChanges() {
double positionX = line.getX();
double positionY = line.getY();
double positionZ = line.getZ();
if (this.positionX != positionX || this.positionY != positionY || this.positionZ != positionZ) {
this.positionX = positionX;
this.positionY = positionY;
this.positionZ = positionZ;
BaseLinePosition position = line.getPosition();
if (!Objects.equals(this.position, position)) {
this.position = position;
this.positionChanged = true;
}
}
@ -51,8 +48,8 @@ abstract class PositionBasedLineTracker<T extends BaseHologramLine> extends Line
return false;
}
double diffX = Math.abs(playerLocation.getX() - positionX);
double diffZ = Math.abs(playerLocation.getZ() - positionZ);
double diffX = Math.abs(playerLocation.getX() - position.getX());
double diffZ = Math.abs(playerLocation.getZ() - position.getZ());
return diffX <= (double) ENTITY_VIEW_RANGE
&& diffZ <= (double) ENTITY_VIEW_RANGE

View File

@ -77,11 +77,11 @@ public class TextLineTracker extends ClickableLineTracker<BaseTextLine> {
super.addSpawnPackets(packetList);
if (!allowPlaceholders) {
textEntity.addSpawnPackets(packetList, positionX, positionY, positionZ, displayText.getWithoutReplacements());
textEntity.addSpawnPackets(packetList, position.getX(), position.getY(), position.getZ(), displayText.getWithoutReplacements());
} else if (displayText.containsIndividualPlaceholders()) {
textEntity.addSpawnPackets(packetList, positionX, positionY, positionZ, displayText::getWithIndividualReplacements);
textEntity.addSpawnPackets(packetList, position.getX(), position.getY(), position.getZ(), displayText::getWithIndividualReplacements);
} else {
textEntity.addSpawnPackets(packetList, positionX, positionY, positionZ, displayText.getWithGlobalReplacements());
textEntity.addSpawnPackets(packetList, position.getX(), position.getY(), position.getZ(), displayText.getWithGlobalReplacements());
}
}
@ -111,7 +111,7 @@ public class TextLineTracker extends ClickableLineTracker<BaseTextLine> {
@Override
protected void addPositionChangePackets(NMSPacketList packetList) {
super.addPositionChangePackets(packetList);
textEntity.addTeleportPackets(packetList, positionX, positionY, positionZ);
textEntity.addTeleportPackets(packetList, position.getX(), position.getY(), position.getZ());
}
}