-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathHologramHandler.java
More file actions
executable file
·158 lines (143 loc) · 5.77 KB
/
HologramHandler.java
File metadata and controls
executable file
·158 lines (143 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package protocolsupportlegacysupport.features.hologram;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.util.Vector;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.reflect.StructureModifier;
import protocolsupport.api.Connection;
import protocolsupport.api.ProtocolSupportAPI;
import protocolsupport.api.ProtocolType;
import protocolsupport.api.ProtocolVersion;
import protocolsupport.api.events.ConnectionOpenEvent;
import protocolsupportlegacysupport.ProtocolSupportLegacySupport;
import protocolsupportlegacysupport.features.AbstractFeature;
import protocolsupportlegacysupport.features.hologram.armorstand.ArmorStandData;
import protocolsupportlegacysupport.features.hologram.armorstand.ArmorStandTracker;
import protocolsupportlegacysupport.utils.PacketUtils;
import protocolsupportlegacysupport.utils.SimpleClientBoundPacketListener;
public class HologramHandler extends AbstractFeature<Void> implements Listener {
public static final int LISTENER_PRIORITY = Integer.MIN_VALUE + Short.MAX_VALUE;
@Override
protected void enable0(Void config) {
Bukkit.getPluginManager().registerEvents(this, ProtocolSupportLegacySupport.getInstance());
ProtocolSupportAPI.getConnections().forEach(this::initConnection);
}
@Override
protected void disable0() {
HandlerList.unregisterAll();
for (Connection connection : ProtocolSupportAPI.getConnections()) {
ArmorStandTracker tracker = connection.removeMetadata(metadata_key);
if (tracker != null) {
List<PacketContainer> packets = new ArrayList<>();
tracker.destroyAll(packets);
for (PacketContainer packet : packets) {
PacketUtils.sendPacket(connection, packet);
}
}
}
}
private static final String metadata_key = "PSLS_HOLOGRAM";
@EventHandler(priority = EventPriority.LOWEST)
public void onConnectionOpen(ConnectionOpenEvent event) {
initConnection(event.getConnection());
}
private void initConnection(Connection connection) {
connection.addMetadata(metadata_key, new ArmorStandTracker(connection));
connection.addPacketListener(new SimpleClientBoundPacketListener(connection) {
{
registerHandler(PacketType.Play.Server.LOGIN, (connection, packet) -> {
getTracker(connection).destroyAll(null);
return null;
});
registerHandler(PacketType.Play.Server.RESPAWN, (connection, packet) -> {
getTracker(connection).destroyAll(null);
return null;
});
registerHandler(PacketType.Play.Server.SPAWN_ENTITY_LIVING, (connection, packet) -> {
if (packet.getIntegers().read(1) != PacketUtils.ARMORSTAND_TYPE_ID) {
return null;
}
return initArmorStand(getTracker(connection), packet.getIntegers().read(0), packet.getDoubles());
});
registerHandler(PacketType.Play.Server.SPAWN_ENTITY, (connection, packet) -> {
if (packet.getEntityTypeModifier().read(0) != EntityType.ARMOR_STAND) {
return null;
}
return initArmorStand(getTracker(connection), packet.getIntegers().read(0), packet.getDoubles());
});
registerHandler(PacketType.Play.Server.ENTITY_DESTROY, (connection, packet) -> {
List<Integer> entityIds = new ArrayList<>(packet.getIntLists().read(0));
ArmorStandTracker tracker = getTracker(connection);
List<PacketContainer> packets = new ArrayList<>();
Iterator<Integer> entityIdsIter = entityIds.iterator();
while (entityIdsIter.hasNext()) {
Integer entityId = entityIdsIter.next();
if (tracker.has(entityId)) {
tracker.destroy(packets, entityId);
}
}
if (!entityIds.isEmpty()) {
packets.add(PacketUtils.createEntityDestroyPacket(entityIds));
}
return packets;
});
registerHandler(PacketType.Play.Server.ENTITY_METADATA, (connection, packet) -> {
ArmorStandData data = getArmorStand(connection, packet.getIntegers().read(0));
if (data == null) {
return null;
}
List<PacketContainer> packets = new ArrayList<>();
data.addMeta(packets, packet.getWatchableCollectionModifier().read(0));
return packets;
});
registerHandler(PacketType.Play.Server.ENTITY_TELEPORT, (connection, packet) -> {
ArmorStandData data = getArmorStand(connection, packet.getIntegers().read(0));
if (data == null) {
return null;
}
StructureModifier<Double> doubles = packet.getDoubles();
List<PacketContainer> packets = new ArrayList<>();
data.setLocation(packets, new Vector(doubles.read(0), doubles.read(1), doubles.read(2)));
return packets;
});
}
@Override
public void onPacketSending(PacketEvent event) {
ProtocolVersion verison = connection.getVersion();
if ((verison.getProtocolType() != ProtocolType.PC) || verison.isAfterOrEq(ProtocolVersion.MINECRAFT_1_8)) {
return;
}
super.onPacketSending(event);
}
}, LISTENER_PRIORITY);
}
protected ArmorStandTracker getTracker(Connection connection) {
return connection.getMetadata(metadata_key);
}
protected ArmorStandData getArmorStand(Connection connection, int entityId) {
return getTracker(connection).get(entityId);
}
protected List<PacketContainer> initArmorStand(ArmorStandTracker tracker, int entityId, StructureModifier<Double> doubles) {
List<PacketContainer> packets;
if (tracker.has(entityId)) {
packets = new ArrayList<>();
tracker.destroy(packets, entityId);
} else {
packets = Collections.emptyList();
}
double x = doubles.read(0);
double y = doubles.read(1);
double z = doubles.read(2);
tracker.add(entityId, new Vector(x, y, z));
return packets;
}
}