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() {
this.viewAll = true;
return this;
return refresh();
}
public Hologram autoDelete() {
@ -69,14 +69,18 @@ public class Hologram {
viewer.setVisible(location.getWorld().equals(viewer.getPlayer().getWorld()) && location.distance(viewer.getPlayer().getLocation()) < viewDistance);
if (viewer.isVisible()) {
THologram.submit(() -> {
if (viewer.isSpawned()) {
NMS.handle().sendPacketEntityTeleport(viewer.getPlayer(), viewer.getId(), location);
} else {
viewer.setSpawned(true);
TPacketHandler.sendPacket(viewer.getPlayer(), THologramHandler.copy(viewer.getId(), location).get());
TPacketHandler.sendPacket(viewer.getPlayer(), THologramHandler.copy(viewer.getId()).get());
try {
if (viewer.isSpawned()) {
NMS.handle().sendPacketEntityTeleport(viewer.getPlayer(), viewer.getId(), location);
} else {
viewer.setSpawned(true);
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 {
destroy(viewer);
@ -97,7 +101,13 @@ public class Hologram {
return this;
}
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;
}
@ -106,7 +116,13 @@ public class Hologram {
return this;
}
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;
}
@ -134,7 +150,13 @@ public class Hologram {
return this;
}
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;
}

View File

@ -20,6 +20,7 @@ import org.bukkit.event.player.*;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.List;
import java.util.Optional;
import java.util.Queue;
import java.util.UUID;
@ -62,7 +63,7 @@ class THologramHandler implements Listener {
if (learnTarget == null) {
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;
return false;
}
@ -82,8 +83,11 @@ class THologramHandler implements Listener {
}
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);
if (Version.isAfter(Version.v1_14)) {
packet.write("k", NMS.handle().asEntityType("armor_stand"));
}
if (Version.isAfter(Version.v1_9)) {
packet.write("b", UUID.randomUUID());
packet.write("c", location.getX());
@ -116,8 +120,12 @@ class THologramHandler implements Listener {
try {
Object i = Ref.getUnsafe().allocateInstance(element.getClass());
SimpleReflection.setFieldValue(element.getClass(), i, "a", a);
SimpleReflection.setFieldValue(element.getClass(), i, "b", name);
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);
} catch (InstantiationException e) {
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 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);
}
}
@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) {
return copy(packetClass, copyField);
}
public Packet copy(Class clazz, String... copyField) {
if (clazz == null) {
clazz = this.packetClass;
}
Object packet;
try {
packet = packetClass.newInstance();
packet = clazz.newInstance();
} catch (Throwable t) {
t.printStackTrace();
return null;
}
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);
}