Clean up Hologram class code

This commit is contained in:
Graphica 2017-05-27 16:14:48 -05:00 committed by cnr
parent 2c6112347a
commit 7baf3bad30

View File

@ -24,13 +24,13 @@ import org.bukkit.util.Vector;
import mineplex.core.common.util.UtilEnt;
import mineplex.core.common.util.UtilPlayer;
/**
* Floating text object with interaction and entity follow capabilities.
*/
public class Hologram {
public enum HologramTarget {
BLACKLIST, WHITELIST;
}
private Packet _destroy1_8;
/**
* 1.7 packets uses both EntityIDs while 1.8 uses only the first.
*/
@ -38,6 +38,7 @@ public class Hologram {
private Entity _followEntity;
private HologramManager _hologramManager;
private String[] _hologramText = new String[0];
/**
* Keeps track of the holograms movements. This fixes offset that
* occasionally happens when moving a hologram around.
@ -60,15 +61,42 @@ public class Hologram {
private long _maxLifetime = -1;
private long _startTime;
public Hologram(HologramManager hologramManager, Location location, String... text) {
/**
* Construct a standard hologram.
*
* @param hologramManager The hologram manager.
* @param location The location at which to display the hologram.
* @param text An array of text lines which the hologram should display.
*/
public Hologram(HologramManager hologramManager, Location location, String... text)
{
this(hologramManager, location, false, -1l, text);
}
public Hologram(HologramManager hologramManager, Location location, boolean hideBoundingBox, String... text) {
/**
* Construct a hologram with a specified bounding box.
*
* @param hologramManager The hologram manager.
* @param location The location at which to display the hologram.
* @param hideBoundingBox Whether to hide the bounding box of the hologram.
* @param text An array of text lines which the hologram should display.
*/
public Hologram(HologramManager hologramManager, Location location, boolean hideBoundingBox, String... text)
{
this(hologramManager, location, hideBoundingBox, -1l, text);
}
public Hologram(HologramManager hologramManager, Location location, boolean hideBoundingBox, long maxLifetime, String... text) {
/**
* Construct a hologram with a limited lifetime.
*
* @param hologramManager The hologram manager.
* @param location The location at which to display the hologram.
* @param hideBoundingBox Whether to hide the bounding box of the hologram.
* @param maxLifetime The max lifetime of the hologram, specified in milliseconds.
* @param text An array of text lines which the hologram should display.
*/
public Hologram(HologramManager hologramManager, Location location, boolean hideBoundingBox, long maxLifetime, String... text)
{
_hologramManager = hologramManager;
_location = location.clone();
_maxLifetime = maxLifetime;
@ -76,9 +104,16 @@ public class Hologram {
setText(text);
}
public Hologram setInteraction(HologramInteraction interact) {
/**
* Set the interaction handler for the hologram.
*
* @param interact The handler.
*
* @return the original hologram object.
*/
public Hologram setInteraction(HologramInteraction interact)
{
_interaction = interact;
return this;
}
@ -89,37 +124,50 @@ public class Hologram {
/**
* Adds the player to the Hologram to be effected by Whitelist or Blacklist
*/
public Hologram addPlayer(Player player) {
public Hologram addPlayer(Player player)
{
return addPlayer(player.getUniqueId());
}
/**
* Adds the player to the Hologram to be effected by Whitelist or Blacklist
*/
public Hologram addPlayer(UUID player) {
public Hologram addPlayer(UUID player)
{
_playersInList.add(player);
return this;
}
/**
* Warning! Bounding box if hidden will hide holograms for 1.8 to 1.8.2
* Hides the bounding box for the hologram. <br>
*
* @return
* <b>Warning! Bounding box if hidden will hide holograms for 1.8 to 1.8.2</b>
*
* @return the original hologram object.
*/
public Hologram setHideBoundingBox() {
public Hologram setHideBoundingBox()
{
_hideBoundingBox = true;
return this;
}
/**
* Is there a player entry in the hologram for Whitelist and Blacklist
* @return if there is a player entry in the holograms whitelist/blacklist.
*/
public boolean containsPlayer(Player player) {
public boolean containsPlayer(Player player)
{
return _playersInList.contains(player.getUniqueId());
}
protected Packet getDestroyPacket() {
if (_makeDestroyPackets) {
/**
* Generates a packet to destroy the hologram client-side.
*
* @return the packet.
*/
protected Packet getDestroyPacket()
{
if (_makeDestroyPackets)
{
makeDestroyPacket();
_makeDestroyPackets = false;
}
@ -127,32 +175,44 @@ public class Hologram {
return _destroy1_8;
}
public Entity getEntityFollowing() {
/**
* @return the entity that this hologram is currently following.
*/
public Entity getEntityFollowing()
{
return _followEntity;
}
/**
* Get who can see the hologram
*
* @Whitelist = Only people added can see the hologram
* @Blacklist = Anyone but people added can see the hologram
* @return The functionality that is currently being used for the object's internal
* player list.
* {@link HologramTarget#WHITELIST} = Only people added can see the hologram
* {@link HologramTarget#BLACKLIST} = Anyone but people added can see the hologram
*/
public HologramTarget getHologramTarget() {
public HologramTarget getHologramTarget()
{
return _target;
}
/**
* Get the hologram location
* @return the current location of the hologram.
*/
public Location getLocation() {
public Location getLocation()
{
return _location.clone();
}
protected ArrayList<Player> getNearbyPlayers() {
ArrayList<Player> nearbyPlayers = new ArrayList<Player>();
/**
* @return A list of players that can currently see the hologram.
*/
protected ArrayList<Player> getNearbyPlayers()
{
ArrayList<Player> nearbyPlayers = new ArrayList<>();
for (Player player : getLocation().getWorld().getPlayers()) {
if (isVisible(player)) {
for (Player player : getLocation().getWorld().getPlayers())
{
if (isVisible(player))
{
nearbyPlayers.add(player);
}
}
@ -160,26 +220,37 @@ public class Hologram {
return nearbyPlayers;
}
protected ArrayList<Player> getPlayersTracking() {
/**
* @return The list of players that are in the holograms whitelist or blacklist.
*/
protected ArrayList<Player> getPlayersTracking()
{
return _playersTracking;
}
protected void checkSpawnPackets() {
if (_makeSpawnPackets) {
/**
* Generates hologram spawn packets if they have not been created already.
*/
protected void checkSpawnPackets()
{
if (_makeSpawnPackets)
{
makeSpawnPackets();
_makeSpawnPackets = false;
}
}
/**
* Get the text in the hologram
* @return the current text being displayed by the hologram.
*/
public String[] getText() {
public String[] getText()
{
// We reverse it again as the hologram would otherwise display the text
// from the bottom row to the top row
String[] reversed = new String[_hologramText.length];
for (int i = 0; i < reversed.length; i++) {
for (int i = 0; i < reversed.length; i++)
{
reversed[i] = _hologramText[reversed.length - (i + 1)];
}
@ -187,27 +258,45 @@ public class Hologram {
}
/**
* Get the view distance the hologram is viewable from. Default is 70
* @return the view distance the hologram is viewable from. Default is 70
*/
public int getViewDistance() {
public int getViewDistance()
{
return _viewDistance;
}
/**
* Is the hologram holograming?
* @return Is the hologram holograming?
*/
public boolean isInUse() {
public boolean isInUse()
{
return _lastMovement != null;
}
public boolean isRemoveOnEntityDeath() {
/**
* @return whether to delete the hologram when the entity it is following dies.
*/
public boolean isRemoveOnEntityDeath()
{
return _removeEntityDeath;
}
public boolean isVisible(Player player) {
if (getLocation().getWorld() == player.getWorld()) {
if ((getHologramTarget() == HologramTarget.WHITELIST) == containsPlayer(player)) {
if (getLocation().distance(player.getLocation()) < getViewDistance()) {
/**
* Determines whether the hologram is visible to a player based on view distance, whitelist /
* blacklist, and current world.
*
* @param player The player to check.
*
* @return whether the hologram is visible to the player.
*/
public boolean isVisible(Player player)
{
if (getLocation().getWorld() == player.getWorld())
{
if ((getHologramTarget() == HologramTarget.WHITELIST) == containsPlayer(player))
{
if (getLocation().distance(player.getLocation()) < getViewDistance())
{
return true;
}
}
@ -216,34 +305,49 @@ public class Hologram {
return false;
}
private void makeDestroyPacket() {
/**
* Generates a packet to destroy the hologram client-side.
*/
private void makeDestroyPacket()
{
int[] entityIds1_8 = new int[_entityIds.size()];
for (int i = 0; i < _entityIds.size(); i++) {
for (int i = 0; i < _entityIds.size(); i++)
{
entityIds1_8[i] = _entityIds.get(i);
}
_destroy1_8 = new PacketPlayOutEntityDestroy(entityIds1_8);
}
private void makeSpawnPackets() {
/**
* Generates spawn packets for the hologram.
*/
private void makeSpawnPackets()
{
_packets1_8 = new Packet[_hologramText.length];
_packets1_9 = new Packet[_hologramText.length];
if (_entityIds.size() < _hologramText.length) {
if (_entityIds.size() < _hologramText.length)
{
_makeDestroyPackets = true;
for (int i = _entityIds.size(); i < _hologramText.length; i++) {
for (int i = _entityIds.size(); i < _hologramText.length; i++)
{
_entityIds.add(Integer.valueOf(UtilEnt.getNewEntityId()));
}
} else {
}
else
{
_makeDestroyPackets = true;
while (_entityIds.size() > _hologramText.length) {
while (_entityIds.size() > _hologramText.length)
{
_entityIds.remove(_hologramText.length);
}
}
for (int textRow = 0; textRow < _hologramText.length; textRow++) {
for (int textRow = 0; textRow < _hologramText.length; textRow++)
{
PacketPlayOutSpawnEntityLiving packet1_8 = makeSpawnPacket1_8(textRow, _entityIds.get(textRow), _hologramText[textRow]);
PacketPlayOutSpawnEntityLiving packet1_9 = makeSpawnPacket1_9(textRow, _entityIds.get(textRow), _hologramText[textRow]);
@ -253,7 +357,7 @@ public class Hologram {
}
/**
* Used for sending 1.9 clients holograms with no bounding boxes
* Used for sending 1.9 clients holograms with no bounding boxes.
*/
private PacketPlayOutSpawnEntityLiving makeSpawnPacket1_9(int textRow, int entityId, String lineOfText)
{
@ -269,7 +373,11 @@ public class Hologram {
return packet;
}
private PacketPlayOutSpawnEntityLiving makeSpawnPacket1_8(int textRow, int entityId, String lineOfText) {
/**
* Used for sending 1.8 clients holograms.
*/
private PacketPlayOutSpawnEntityLiving makeSpawnPacket1_8(int textRow, int entityId, String lineOfText)
{
PacketPlayOutSpawnEntityLiving packet = new PacketPlayOutSpawnEntityLiving();
DataWatcher watcher = new DataWatcher(null);
@ -290,41 +398,54 @@ public class Hologram {
}
/**
* Removes the player from the Hologram so they are no longer effected by
* Whitelist or Blacklist
* Removes a player from the Hologram so they are no longer effected by
* whitelist or blacklist.
*
* @param player The player to remove.
*
* @return the original hologram object.
*/
public Hologram removePlayer(Player player) {
public Hologram removePlayer(Player player)
{
return removePlayer(player.getUniqueId());
}
/**
* Removes the player from the Hologram so they are no longer effected by
* Whitelist or Blacklist
* whitelist or blacklist
*
* @param player The player to remove.
*
* @return the original hologram object.
*/
public Hologram removePlayer(UUID player) {
public Hologram removePlayer(UUID player)
{
_playersInList.remove(player);
return this;
}
/**
* If the entity moves, the hologram will update its position to appear
* relative to the movement.
* Sets an entity to which the hologram will remain relative to in position.
*
* @Please note the hologram updates every tick.
* @param entityToFollow the entity which to follow.
*
* @return the original hologram object.
*/
public Hologram setFollowEntity(Entity entityToFollow) {
_followEntity = entityToFollow;
relativeToEntity = entityToFollow == null ? null
: _location.clone().subtract(entityToFollow.getLocation()).toVector();
relativeToEntity = entityToFollow == null ? null : _location.clone().subtract(entityToFollow.getLocation()).toVector();
return this;
}
/**
* Set who can see the hologram
* Set how the hologram's internal player list is used.
*
* @Whitelist = Only people added can see the hologram
* @Blacklist = Anyone but people added can see the hologram
* @param newTarget The target which defines how the list is used.
* {@link HologramTarget#WHITELIST} = Only people added can see the hologram
* {@link HologramTarget#BLACKLIST} = Anyone but people added can see the hologram
*
* @retuen the original hologram object.
*/
public Hologram setHologramTarget(HologramTarget newTarget) {
_target = newTarget;
@ -332,44 +453,61 @@ public class Hologram {
}
/**
* Sets the hologram to appear at this location
* Change the location of the hologram.
*
* @param newLocation the location to which to teleport the hologram.
*
* @return the original hologram object.
*/
public Hologram setLocation(Location newLocation) {
public Hologram setLocation(Location newLocation)
{
_makeSpawnPackets = true;
Location oldLocation = getLocation();
_location = newLocation.clone();
if (getEntityFollowing() != null) {
if (getEntityFollowing() != null)
{
relativeToEntity = _location.clone().subtract(getEntityFollowing().getLocation()).toVector();
}
if (isInUse()) {
if (isInUse())
{
ArrayList<Player> canSee = getNearbyPlayers();
Iterator<Player> itel = _playersTracking.iterator();
while (itel.hasNext()) {
while (itel.hasNext())
{
Player player = itel.next();
if (!canSee.contains(player)) {
if (!canSee.contains(player))
{
itel.remove();
if (player.getWorld() == getLocation().getWorld()) {
if (player.getWorld() == getLocation().getWorld())
{
UtilPlayer.sendPacket(player, getDestroyPacket());
}
}
}
itel = canSee.iterator();
checkSpawnPackets();
while (itel.hasNext()) {
while (itel.hasNext())
{
Player player = itel.next();
if (!_playersTracking.contains(player)) {
if (!_playersTracking.contains(player))
{
_playersTracking.add(player);
itel.remove();
UtilPlayer.sendPacket(player, UtilPlayer.is1_9(player) ? _packets1_9 : _packets1_8);
}
}
if (!canSee.isEmpty()) {
if (!canSee.isEmpty())
{
_lastMovement.add(new Vector(newLocation.getX() - oldLocation.getX(),
newLocation.getY() - oldLocation.getY(), newLocation.getZ() - oldLocation.getZ()));
@ -381,9 +519,12 @@ public class Hologram {
int i = 0;
if (x >= -128 && x <= 127 && y >= -128 && y <= 127 && z >= -128 && z <= 127) {
if (x >= -128 && x <= 127 && y >= -128 && y <= 127 && z >= -128 && z <= 127)
{
_lastMovement.subtract(new Vector(x / 32D, y / 32D, z / 32D));
for (Integer entityId : _entityIds) {
for (Integer entityId : _entityIds)
{
PacketPlayOutEntity.PacketPlayOutRelEntityMove relMove = new PacketPlayOutEntity.PacketPlayOutRelEntityMove();
relMove.a = entityId;
@ -394,13 +535,16 @@ public class Hologram {
packets1_8[i] = relMove;
i++;
}
} else {
}
else
{
x = (int) Math.floor(32 * newLocation.getX());
z = (int) Math.floor(32 * newLocation.getZ());
_lastMovement = new Vector(newLocation.getX() - (x / 32D), 0, newLocation.getZ() - (z / 32D));
for (Integer entityId : _entityIds) {
for (Integer entityId : _entityIds)
{
PacketPlayOutEntityTeleport teleportPacket = new PacketPlayOutEntityTeleport();
teleportPacket.a = entityId;
teleportPacket.b = x;
@ -413,61 +557,93 @@ public class Hologram {
}
}
for (Player player : canSee) {
for (Packet packet : packets1_8) {
for (Player player : canSee)
{
for (Packet packet : packets1_8)
{
UtilPlayer.sendPacket(player, packet);
}
}
}
}
return this;
}
/**
* @return the time at which the hologram was last started.
*/
public long getStartTime()
{
return _startTime;
}
/**
* @return the max time after the hologram was started for which it will live.
*/
public long getMaxLifetime()
{
return _maxLifetime;
}
public Hologram setRemoveOnEntityDeath() {
/**
* Set the hologram to stop when the entity it is following dies.
*
* @return the original hologram object.
*/
public Hologram setRemoveOnEntityDeath()
{
_removeEntityDeath = true;
return this;
}
public boolean isEntityId(int entityId) {
/**
* @param entityId an entity id.
*
* @return whether the entity ID is represented by this hologram object.
*/
public boolean isEntityId(int entityId)
{
return _entityIds.contains(entityId);
}
/**
* Set the hologram text
*
* @param newLines array of text lines for the hologram to display.
*
* @return the original hologram object.
*/
public Hologram setText(String... newLines) {
public Hologram setText(String... newLines)
{
String[] newText = new String[newLines.length];
for (int i = 0; i < newText.length; i++) {
for (int i = 0; i < newText.length; i++)
{
newText[i] = newLines[newText.length - (i + 1)];
}
if (newText.equals(_hologramText))
{
return this;
}
if (isInUse()) {
if (isInUse())
{
int[] destroy1_8 = new int[0];
ArrayList<Packet> packets1_8 = new ArrayList<Packet>();
ArrayList<Packet> packets1_8 = new ArrayList<>();
ArrayList<Packet> packets1_9 = new ArrayList<>();
if (_hologramText.length != newText.length) {
if (_hologramText.length != newText.length)
{
_makeDestroyPackets = true;
}
for (int i = 0; i < Math.max(_hologramText.length, newText.length); i++) {
// If more lines than previously
if (i >= _hologramText.length) {
for (int i = 0; i < Math.max(_hologramText.length, newText.length); i++)
{
if (i >= _hologramText.length) // If more lines than previously
{
// Add entity id and send spawn packets
// You add a entity id because the new hologram needs
int entityId = UtilEnt.getNewEntityId();
@ -476,14 +652,16 @@ public class Hologram {
packets1_8.add(makeSpawnPacket1_8(i, entityId, newText[i]));
packets1_9.add(makeSpawnPacket1_9(i, entityId, newText[i]));
}
// If less lines than previously
else if (i >= newText.length) {
else if (i >= newText.length) // If less lines than previously
{
// Remove entity id and send destroy packets
Integer entityId = _entityIds.remove(newText.length);
destroy1_8 = Arrays.copyOf(destroy1_8, destroy1_8.length + 1);
destroy1_8[destroy1_8.length - 1] = entityId;
} else if (!newText[i].equals(_hologramText[i])) {
}
else if (!newText[i].equals(_hologramText[i]))
{
// Send update metadata packets
Integer entityId = _entityIds.get(i);
@ -495,10 +673,12 @@ public class Hologram {
watcher1_8.a(2, newText[i], EntityArmorStand.META_CUSTOMNAME, newText[i]);
watcher1_8.a(3, (byte) 1, EntityArmorStand.META_CUSTOMNAME_VISIBLE, true);
}
{
watcher1_9.a(0, (byte) 32, EntityArmorStand.META_ENTITYDATA, (byte) 32);
watcher1_9.a(2, newText[i], EntityArmorStand.META_CUSTOMNAME, newText[i]);
watcher1_9.a(3, (byte) 1, EntityArmorStand.META_CUSTOMNAME_VISIBLE, true);
if (_hideBoundingBox)
{
watcher1_9.a(10, (byte) 16, EntityArmorStand.META_ARMOR_OPTION, (byte) 16);
@ -510,13 +690,16 @@ public class Hologram {
}
}
if (destroy1_8.length > 0) {
if (destroy1_8.length > 0)
{
packets1_8.add(new PacketPlayOutEntityDestroy(destroy1_8));
packets1_9.add(new PacketPlayOutEntityDestroy(destroy1_8));
}
for (Player player : _playersTracking) {
for (Player player : _playersTracking)
{
List<Packet> packets = UtilPlayer.is1_9(player) ? packets1_9 : packets1_8;
for (Packet packet : packets)
{
UtilPlayer.sendPacket(player, packet);
@ -532,18 +715,26 @@ public class Hologram {
/**
* Set the distance the hologram is viewable from. Default is 70
*
* @param newDistance The distance in blocks.
*
* @return the original hologram object.
*/
public Hologram setViewDistance(int newDistance) {
public Hologram setViewDistance(int newDistance)
{
_viewDistance = newDistance;
return setLocation(getLocation());
}
/**
* Start the hologram
* Start the hologram, displaying it to players.
*
* @return the original hologram object.
*/
public Hologram start() {
if (!isInUse()) {
public Hologram start()
{
if (!isInUse())
{
_startTime = System.currentTimeMillis();
_hologramManager.addHologram(this);
@ -553,9 +744,13 @@ public class Hologram {
_lastMovement = new Vector();
}
return this;
}
/**
* Sends hologram spawn packets to players.
*/
private void sendPackets()
{
checkSpawnPackets();
@ -566,6 +761,13 @@ public class Hologram {
}
}
/**
* Generates spawn packets based on minecraft version.
*
* @param player The player for which to generate the packets.
*
* @return the list of packets generated.
*/
public Packet[] getSpawnPackets(Player player)
{
checkSpawnPackets();
@ -574,13 +776,19 @@ public class Hologram {
}
/**
* Stop the hologram
* Stop the hologram, effectively destroying it once
* garbage collection has occurred.
*
* @return the original hologram object.
*/
public Hologram stop() {
if (isInUse()) {
public Hologram stop()
{
if (isInUse())
{
_hologramManager.removeHologram(this);
for (Player player : _playersTracking) {
for (Player player : _playersTracking)
{
UtilPlayer.sendPacket(player, getDestroyPacket());
}
@ -591,4 +799,11 @@ public class Hologram {
return this;
}
/**
* Enum defining to whom the hologram is displayed.
*/
public enum HologramTarget
{
BLACKLIST, WHITELIST
}
}