Small code cleanup

This commit is contained in:
filoghost 2019-05-29 10:50:48 +02:00
parent aeb3991337
commit fc091d9471
5 changed files with 108 additions and 87 deletions

View File

@ -19,6 +19,7 @@ import org.bukkit.plugin.Plugin;
import com.gmail.filoghost.holographicdisplays.nms.interfaces.NMSManager;
import com.gmail.filoghost.holographicdisplays.object.CraftHologram;
import com.gmail.filoghost.holographicdisplays.object.line.CraftHologramLine;
public interface ProtocolLibHook {
@ -26,6 +27,10 @@ public interface ProtocolLibHook {
public void sendDestroyEntitiesPacket(Player player, CraftHologram hologram);
public void sendDestroyEntitiesPacket(Player player, CraftHologramLine line);
public void sendCreateEntitiesPacket(Player player, CraftHologram hologram);
public void sendCreateEntitiesPacket(Player player, CraftHologramLine line);
}

View File

@ -143,14 +143,13 @@ public class ProtocolLibHookImpl implements ProtocolLibHook {
return;
}
Collection<RelativePlaceholder> relativePlaceholders = hologramLine.getRelativePlaceholders();
if (relativePlaceholders == null || relativePlaceholders.isEmpty()) {
if (!hologramLine.hasRelativePlaceholders()) {
return;
}
spawnEntityPacket = new WrapperPlayServerSpawnEntityLiving(packet.deepClone());
WrappedWatchableObject customNameWatchableObject = spawnEntityPacket.getMetadata().getWatchableObject(customNameWatcherIndex);
replaceRelativePlaceholders(customNameWatchableObject, player, relativePlaceholders);
replaceRelativePlaceholders(customNameWatchableObject, player, hologramLine.getRelativePlaceholders());
event.setPacket(spawnEntityPacket.getHandle());
} else if (packet.getType() == PacketType.Play.Server.SPAWN_ENTITY) {
@ -183,8 +182,7 @@ public class ProtocolLibHookImpl implements ProtocolLibHook {
return;
}
Collection<RelativePlaceholder> relativePlaceholders = hologramLine.getRelativePlaceholders();
if (relativePlaceholders == null || relativePlaceholders.isEmpty()) {
if (!hologramLine.hasRelativePlaceholders()) {
return;
}
@ -195,7 +193,7 @@ public class ProtocolLibHookImpl implements ProtocolLibHook {
WrappedWatchableObject watchableObject = dataWatcherValues.get(i);
if (watchableObject.getIndex() == customNameWatcherIndex) {
if (replaceRelativePlaceholders(watchableObject, player, relativePlaceholders)) {
if (replaceRelativePlaceholders(watchableObject, player, hologramLine.getRelativePlaceholders())) {
event.setPacket(entityMetadataPacket.getHandle());
}
@ -274,64 +272,89 @@ public class ProtocolLibHookImpl implements ProtocolLibHook {
}
@Override
public void sendDestroyEntitiesPacket(Player player, CraftHologramLine line) {
if (!line.isSpawned()) {
return;
}
List<Integer> ids = Utils.newList();
for (int id : line.getEntitiesIDs()) {
ids.add(id);
}
if (!ids.isEmpty()) {
WrapperPlayServerEntityDestroy packet = new WrapperPlayServerEntityDestroy();
packet.setEntities(ids);
packet.sendPacket(player);
}
}
@Override
public void sendCreateEntitiesPacket(Player player, CraftHologram hologram) {
for (CraftHologramLine line : hologram.getLinesUnsafe()) {
if (line.isSpawned()) {
sendCreateEntitiesPacket(player, line);
}
}
@Override
public void sendCreateEntitiesPacket(Player player, CraftHologramLine line) {
if (!line.isSpawned()) {
return;
}
if (line instanceof CraftTextLine) {
CraftTextLine textLine = (CraftTextLine) line;
if (textLine.isSpawned()) {
sendSpawnArmorStandPacket(player, (NMSArmorStand) textLine.getNmsNameble());
}
} else if (line instanceof CraftItemLine) {
CraftItemLine itemLine = (CraftItemLine) line;
if (itemLine.isSpawned()) {
AbstractPacket itemPacket = new WrapperPlayServerSpawnEntity(itemLine.getNmsItem().getBukkitEntityNMS(), ObjectTypes.ITEM_STACK, 1);
itemPacket.sendPacket(player);
if (line instanceof CraftTextLine) {
CraftTextLine textLine = (CraftTextLine) line;
if (textLine.isSpawned()) {
sendSpawnArmorStandPacket(player, (NMSArmorStand) textLine.getNmsNameble());
}
} else if (line instanceof CraftItemLine) {
CraftItemLine itemLine = (CraftItemLine) line;
if (itemLine.isSpawned()) {
AbstractPacket itemPacket = new WrapperPlayServerSpawnEntity(itemLine.getNmsItem().getBukkitEntityNMS(), ObjectTypes.ITEM_STACK, 1);
itemPacket.sendPacket(player);
sendSpawnArmorStandPacket(player, (NMSArmorStand) itemLine.getNmsVehicle());
sendVehicleAttachPacket(player, itemLine.getNmsVehicle().getIdNMS(), itemLine.getNmsItem().getIdNMS());
WrapperPlayServerEntityMetadata itemDataPacket = new WrapperPlayServerEntityMetadata();
WrappedDataWatcher dataWatcher = new WrappedDataWatcher();
if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_9_R1)) {
Object itemStackObject = NMSVersion.isGreaterEqualThan(NMSVersion.v1_11_R1) ? itemLine.getNmsItem().getRawItemStack() : Optional.of(itemLine.getNmsItem().getRawItemStack());
dataWatcher.setObject(new WrappedDataWatcherObject(itemstackMetadataWatcherIndex, itemSerializer), itemStackObject);
dataWatcher.setObject(new WrappedDataWatcherObject(1, intSerializer), 300);
dataWatcher.setObject(new WrappedDataWatcherObject(0, byteSerializer), (byte) 0);
} else {
dataWatcher.setObject(itemstackMetadataWatcherIndex, itemLine.getNmsItem().getRawItemStack());
dataWatcher.setObject(1, 300);
dataWatcher.setObject(0, (byte) 0);
}
itemDataPacket.setEntityMetadata(dataWatcher.getWatchableObjects());
itemDataPacket.setEntityId(itemLine.getNmsItem().getIdNMS());
itemDataPacket.sendPacket(player);
}
sendSpawnArmorStandPacket(player, (NMSArmorStand) itemLine.getNmsVehicle());
sendVehicleAttachPacket(player, itemLine.getNmsVehicle().getIdNMS(), itemLine.getNmsItem().getIdNMS());
WrapperPlayServerEntityMetadata itemDataPacket = new WrapperPlayServerEntityMetadata();
WrappedDataWatcher dataWatcher = new WrappedDataWatcher();
if (NMSVersion.isGreaterEqualThan(NMSVersion.v1_9_R1)) {
Object itemStackObject = NMSVersion.isGreaterEqualThan(NMSVersion.v1_11_R1) ? itemLine.getNmsItem().getRawItemStack() : Optional.of(itemLine.getNmsItem().getRawItemStack());
dataWatcher.setObject(new WrappedDataWatcherObject(itemstackMetadataWatcherIndex, itemSerializer), itemStackObject);
dataWatcher.setObject(new WrappedDataWatcherObject(1, intSerializer), 300);
dataWatcher.setObject(new WrappedDataWatcherObject(0, byteSerializer), (byte) 0);
} else {
dataWatcher.setObject(itemstackMetadataWatcherIndex, itemLine.getNmsItem().getRawItemStack());
dataWatcher.setObject(1, 300);
dataWatcher.setObject(0, (byte) 0);
}
// Unsafe cast, however both CraftTextLine and CraftItemLine are touchable.
CraftTouchableLine touchableLine = (CraftTouchableLine) line;
if (touchableLine.isSpawned() && touchableLine.getTouchSlime() != null) {
CraftTouchSlimeLine touchSlime = touchableLine.getTouchSlime();
if (touchSlime.isSpawned()) {
sendSpawnArmorStandPacket(player, (NMSArmorStand) touchSlime.getNmsVehicle());
AbstractPacket slimePacket = new WrapperPlayServerSpawnEntityLiving(touchSlime.getNmsSlime().getBukkitEntityNMS());
slimePacket.sendPacket(player);
sendVehicleAttachPacket(player, touchSlime.getNmsVehicle().getIdNMS(), touchSlime.getNmsSlime().getIdNMS());
}
}
itemDataPacket.setEntityMetadata(dataWatcher.getWatchableObjects());
itemDataPacket.setEntityId(itemLine.getNmsItem().getIdNMS());
itemDataPacket.sendPacket(player);
}
}
// Unsafe cast, however both CraftTextLine and CraftItemLine are touchable.
CraftTouchableLine touchableLine = (CraftTouchableLine) line;
if (touchableLine.isSpawned() && touchableLine.getTouchSlime() != null) {
CraftTouchSlimeLine touchSlime = touchableLine.getTouchSlime();
if (touchSlime.isSpawned()) {
sendSpawnArmorStandPacket(player, (NMSArmorStand) touchSlime.getNmsVehicle());
AbstractPacket slimePacket = new WrapperPlayServerSpawnEntityLiving(touchSlime.getNmsSlime().getBukkitEntityNMS());
slimePacket.sendPacket(player);
sendVehicleAttachPacket(player, touchSlime.getNmsVehicle().getIdNMS(), touchSlime.getNmsSlime().getIdNMS());
}
}
}

View File

@ -231,28 +231,24 @@ public class CraftHologram implements Hologram, com.gmail.filoghost.holograms.ap
@Override
public void setAllowPlaceholders(boolean allowPlaceholders) {
if (this.allowPlaceholders != allowPlaceholders) {
if (this.allowPlaceholders == allowPlaceholders) {
return;
}
if (allowPlaceholders) {
// Now allowed, previously weren't
for (CraftHologramLine line : lines) {
if (line instanceof CraftTextLine) {
PlaceholdersManager.trackIfNecessary((CraftTextLine) line);
}
}
} else {
// Now not allowed
for (CraftHologramLine line : lines) {
if (line instanceof CraftTextLine) {
PlaceholdersManager.untrack((CraftTextLine) line);
}
for (CraftHologramLine line : lines) {
if (line instanceof CraftTextLine) {
CraftTextLine textLine = (CraftTextLine) line;
if (allowPlaceholders) {
// Now allowed, previously weren't
PlaceholdersManager.trackIfNecessary(textLine);
} else {
// Now not allowed
PlaceholdersManager.untrack(textLine);
}
}
this.allowPlaceholders = allowPlaceholders;
}
this.allowPlaceholders = allowPlaceholders;
}

View File

@ -72,6 +72,10 @@ public abstract class CraftHologramLine implements HologramLine {
return null;
}
public boolean hasRelativePlaceholders() {
return getRelativePlaceholders() != null && !getRelativePlaceholders().isEmpty();
}
public abstract int[] getEntitiesIDs();
public abstract void teleport(double x, double y, double z);

View File

@ -56,7 +56,6 @@ public class PlaceholdersManager {
public static void load(Plugin plugin) {
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
@Override
@ -103,7 +102,6 @@ public class PlaceholdersManager {
}
public static void untrack(CraftTextLine line) {
if (line == null || !line.isSpawned()) {
return;
}
@ -119,25 +117,21 @@ public class PlaceholdersManager {
}
public static void trackIfNecessary(CraftTextLine line) {
NMSNameable nameableEntity = line.getNmsNameble();
String name = line.getText();
if (nameableEntity == null) {
return;
}
boolean updateName = false;
String name = line.getText();
if (name == null || name.isEmpty()) {
return;
}
boolean updateName = false;
// Lazy initialization.
Set<Placeholder> normalPlaceholders = null;
Map<String, PlaceholderReplacer> bungeeReplacers = null;
Map<String, PlaceholderReplacer> worldsOnlinePlayersReplacers = null;
Map<String, Placeholder> animationsPlaceholders = null;
@ -326,7 +320,7 @@ public class PlaceholdersManager {
}
if (Utils.isThereNonNull(normalPlaceholders, bungeeReplacers, worldsOnlinePlayersReplacers, animationsPlaceholders)) {
DynamicLineData lineData = new DynamicLineData(nameableEntity, name);
if (normalPlaceholders != null) {
@ -364,7 +358,6 @@ public class PlaceholdersManager {
private static void updatePlaceholders(DynamicLineData lineData) {
String oldCustomName = lineData.getEntity().getCustomNameNMS();
String newCustomName = lineData.getOriginalName();