Fix Hologram

This commit is contained in:
sky 2020-03-16 22:21:25 +08:00
parent 6623ae5594
commit 3f91fb5de8
5 changed files with 82 additions and 16 deletions

View File

@ -38,7 +38,7 @@ public class Hologram {
public Hologram toAll() { public Hologram toAll() {
this.viewAll = true; this.viewAll = true;
return this; return refresh();
} }
public Hologram autoDelete() { public Hologram autoDelete() {
@ -69,14 +69,18 @@ public class Hologram {
viewer.setVisible(location.getWorld().equals(viewer.getPlayer().getWorld()) && location.distance(viewer.getPlayer().getLocation()) < viewDistance); viewer.setVisible(location.getWorld().equals(viewer.getPlayer().getWorld()) && location.distance(viewer.getPlayer().getLocation()) < viewDistance);
if (viewer.isVisible()) { if (viewer.isVisible()) {
THologram.submit(() -> { THologram.submit(() -> {
if (viewer.isSpawned()) { try {
NMS.handle().sendPacketEntityTeleport(viewer.getPlayer(), viewer.getId(), location); if (viewer.isSpawned()) {
} else { NMS.handle().sendPacketEntityTeleport(viewer.getPlayer(), viewer.getId(), location);
viewer.setSpawned(true); } else {
TPacketHandler.sendPacket(viewer.getPlayer(), THologramHandler.copy(viewer.getId(), location).get()); viewer.setSpawned(true);
TPacketHandler.sendPacket(viewer.getPlayer(), THologramHandler.copy(viewer.getId()).get()); TPacketHandler.sendPacket(viewer.getPlayer(), THologramHandler.copy(viewer.getId(), location).get());
TPacketHandler.sendPacket(viewer.getPlayer(), THologramHandler.copy(viewer.getId()).get());
}
TPacketHandler.sendPacket(viewer.getPlayer(), THologramHandler.copy(viewer.getId(), text).get());
} catch (Throwable t) {
t.printStackTrace();
} }
TPacketHandler.sendPacket(viewer.getPlayer(), THologramHandler.copy(viewer.getId(), text).get());
}); });
} else { } else {
destroy(viewer); destroy(viewer);
@ -97,7 +101,13 @@ public class Hologram {
return this; return this;
} }
this.text = text; this.text = text;
THologram.submit(() -> viewers.forEach(v -> TPacketHandler.sendPacket(v.getPlayer(), THologramHandler.copy(v.getId(), text).get()))); THologram.submit(() -> {
try {
viewers.forEach(v -> TPacketHandler.sendPacket(v.getPlayer(), THologramHandler.copy(v.getId(), text).get()));
} catch (Throwable t) {
t.printStackTrace();
}
});
return this; return this;
} }
@ -106,7 +116,13 @@ public class Hologram {
return this; return this;
} }
this.location = location.clone(); this.location = location.clone();
THologram.submit(() -> viewers.forEach(v -> NMS.handle().sendPacketEntityTeleport(v.getPlayer(), v.getId(), location))); THologram.submit(() -> {
try {
viewers.forEach(v -> NMS.handle().sendPacketEntityTeleport(v.getPlayer(), v.getId(), location));
} catch (Throwable t) {
t.printStackTrace();
}
});
return this; return this;
} }
@ -134,7 +150,13 @@ public class Hologram {
return this; return this;
} }
viewer.setSpawned(false); viewer.setSpawned(false);
THologram.submit(() -> NMS.handle().sendPacketEntityDestroy(viewer.getPlayer(), viewer.getId())); THologram.submit(() -> {
try {
NMS.handle().sendPacketEntityDestroy(viewer.getPlayer(), viewer.getId());
} catch (Throwable t) {
t.printStackTrace();
}
});
return this; return this;
} }

View File

@ -20,6 +20,7 @@ import org.bukkit.event.player.*;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.Queue; import java.util.Queue;
import java.util.UUID; import java.util.UUID;
@ -62,7 +63,7 @@ class THologramHandler implements Listener {
if (learnTarget == null) { if (learnTarget == null) {
return true; return true;
} }
if (packet.is("PacketPlayOutSpawnEntity") && packet.read("a", Integer.TYPE) == learnTarget.getEntityId()) { if ((packet.is("PacketPlayOutSpawnEntity") || packet.is("PacketPlayOutSpawnEntityLiving")) && packet.read("a", Integer.TYPE) == learnTarget.getEntityId()) {
packetSpawn = packet; packetSpawn = packet;
return false; return false;
} }
@ -82,8 +83,11 @@ class THologramHandler implements Listener {
} }
public static Packet copy(int id, Location location) { public static Packet copy(int id, Location location) {
Packet packet = THologramHandler.getPacketSpawn().copy("e", "f", "j", "h", "i", "j", "k", "l"); Packet packet = THologramHandler.getPacketSpawn().copy(NMS.handle().asNMS("PacketPlayOutSpawnEntity"), "e", "f", "j", "h", "i", "j", "k", "l");
packet.write("a", id); packet.write("a", id);
if (Version.isAfter(Version.v1_14)) {
packet.write("k", NMS.handle().asEntityType("armor_stand"));
}
if (Version.isAfter(Version.v1_9)) { if (Version.isAfter(Version.v1_9)) {
packet.write("b", UUID.randomUUID()); packet.write("b", UUID.randomUUID());
packet.write("c", location.getX()); packet.write("c", location.getX());
@ -116,8 +120,12 @@ class THologramHandler implements Listener {
try { try {
Object i = Ref.getUnsafe().allocateInstance(element.getClass()); Object i = Ref.getUnsafe().allocateInstance(element.getClass());
SimpleReflection.setFieldValue(element.getClass(), i, "a", a); SimpleReflection.setFieldValue(element.getClass(), i, "a", a);
SimpleReflection.setFieldValue(element.getClass(), i, "b", name);
SimpleReflection.setFieldValue(element.getClass(), i, "c", c); SimpleReflection.setFieldValue(element.getClass(), i, "c", c);
if (Version.isAfter(Version.v1_14)) {
SimpleReflection.setFieldValue(element.getClass(), i, "b", Optional.of(NMS.handle().ofChatComponentText(name)));
} else {
SimpleReflection.setFieldValue(element.getClass(), i, "b", name);
}
copy.add(i); copy.add(i);
} catch (InstantiationException e) { } catch (InstantiationException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -93,4 +93,10 @@ public abstract class NMS {
abstract public <T extends Entity> T spawn(Location location, Class<T> entity, Consumer<T> e); abstract public <T extends Entity> T spawn(Location location, Class<T> entity, Consumer<T> e);
abstract public Object ofChatComponentText(String source);
abstract public Class<?> asNMS(String name);
abstract public Object asEntityType(String name);
} }

View File

@ -479,4 +479,27 @@ public class NMSImpl extends NMS {
return ((CraftWorld) location.getWorld()).addEntity((net.minecraft.server.v1_13_R2.Entity) createEntity, CreatureSpawnEvent.SpawnReason.CUSTOM); return ((CraftWorld) location.getWorld()).addEntity((net.minecraft.server.v1_13_R2.Entity) createEntity, CreatureSpawnEvent.SpawnReason.CUSTOM);
} }
} }
@Override
public Object ofChatComponentText(String source) {
return new net.minecraft.server.v1_12_R1.ChatComponentText(source);
}
@Override
public Class<?> asNMS(String name) {
try {
return Class.forName("net.minecraft.server." + Version.getBukkitVersion() + "." + name);
} catch (Throwable ignored) {
return null;
}
}
@Override
public Object asEntityType(String name) {
if (Version.isAfter(Version.v1_14)) {
return net.minecraft.server.v1_14_R1.EntityTypes.a(name).orElse(null);
} else {
return net.minecraft.server.v1_13_R2.EntityTypes.a(name);
}
}
} }

View File

@ -53,15 +53,22 @@ public class Packet {
} }
public Packet copy(String... copyField) { public Packet copy(String... copyField) {
return copy(packetClass, copyField);
}
public Packet copy(Class clazz, String... copyField) {
if (clazz == null) {
clazz = this.packetClass;
}
Object packet; Object packet;
try { try {
packet = packetClass.newInstance(); packet = clazz.newInstance();
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
return null; return null;
} }
for (String field : copyField) { for (String field : copyField) {
SimpleReflection.setFieldValue(this.packetClass, packet, field, SimpleReflection.getFieldValue(this.packetClass, origin, field)); SimpleReflection.setFieldValue(clazz, packet, field, SimpleReflection.getFieldValue(clazz, origin, field));
} }
return new Packet(packet); return new Packet(packet);
} }