|
23 | 23 | import org.bukkit.Location; |
24 | 24 | import org.bukkit.entity.EntityType; |
25 | 25 | import org.bukkit.entity.Player; |
26 | | -import org.bukkit.plugin.RegisteredServiceProvider; |
27 | | -import org.bukkit.plugin.ServicePriority; |
28 | | -import org.bukkit.plugin.ServicesManager; |
29 | 26 | import org.bukkit.plugin.java.JavaPlugin; |
30 | 27 |
|
31 | 28 | import java.lang.reflect.InvocationTargetException; |
32 | 29 | import java.util.Collection; |
33 | 30 | import java.util.HashMap; |
34 | 31 | import java.util.Map; |
35 | | -import java.util.Optional; |
36 | | -import java.util.function.Consumer; |
37 | 32 |
|
38 | 33 | /** |
39 | 34 | * Created by iso2013 on 2/13/2018. |
40 | 35 | */ |
41 | | -public class PacketEntityAPI implements IPacketEntityAPI { |
| 36 | +public class PacketEntityAPI extends JavaPlugin implements IPacketEntityAPI { |
42 | 37 | public static final Map<EntityType, Integer> OBJECTS = new HashMap<>(); |
43 | 38 |
|
44 | 39 | static { |
@@ -73,83 +68,41 @@ public class PacketEntityAPI implements IPacketEntityAPI { |
73 | 68 | OBJECTS.put(EntityType.LIGHTNING, -1); |
74 | 69 | } |
75 | 70 |
|
76 | | - private static boolean compareVersions(String fullVersion) { |
77 | | - String[][] input = new String[][]{fullVersion.split("\\."), ProviderStub.FULL_VERSION.split("\\.")}; |
78 | | - int size = Math.max(input[0].length, input[1].length); |
79 | | - for (int s = 0; s < size; s++) { |
80 | | - if (input[0].length - 1 < s) return true; |
81 | | - if (input[1].length - 1 < s) return false; |
82 | | - if (input[0][s].equals(input[1][s])) continue; |
83 | | - return Integer.valueOf(input[0][s]) < Integer.valueOf(input[1][s]); |
84 | | - } |
85 | | - return false; |
86 | | - } |
87 | | - |
88 | | - private static JavaPlugin parent; |
89 | 71 | private static IPacketEntityAPI instance; |
90 | 72 |
|
91 | 73 | private static TaskChainFactory chainFactory; |
92 | 74 | /* |
93 | 75 | * Begin actual API implementation: |
94 | 76 | */ |
95 | | - private final EntityModifierRegistry modifierRegistry; |
96 | | - private final ProtocolManager manager; |
| 77 | + private EntityModifierRegistry modifierRegistry; |
| 78 | + private ProtocolManager manager; |
97 | 79 | private FakeEntityFactory fakeEntityFactory; |
98 | 80 | private EntityPacketFactory packetFactory; |
99 | 81 | private PacketEventDispatcher dispatcher; |
100 | 82 |
|
101 | | - private PacketEntityAPI() { |
| 83 | + @Override |
| 84 | + public void onEnable() { |
102 | 85 | this.modifierRegistry = new EntityModifierRegistry(); |
103 | 86 | this.manager = ProtocolLibrary.getProtocolManager(); |
104 | 87 | this.fakeEntityFactory = new FakeEntityFactory(this); |
105 | 88 | this.packetFactory = new EntityPacketFactory(); |
106 | 89 | this.dispatcher = new PacketEventDispatcher(this, manager); |
107 | 90 |
|
108 | | - chainFactory = BukkitTaskChainFactory.create(parent); |
109 | | - } |
| 91 | + chainFactory = BukkitTaskChainFactory.create(this); |
110 | 92 |
|
111 | | - public static void initialize(JavaPlugin parent, Consumer<IPacketEntityAPI> onLoad) { |
112 | | - ServicesManager m = Bukkit.getServicesManager(); |
| 93 | + if (instance == null) instance = this; |
113 | 94 |
|
114 | | - Collection<RegisteredServiceProvider<IPacketEntityAPI.ProviderStub>> r = m.getRegistrations(IPacketEntityAPI |
115 | | - .ProviderStub.class); |
116 | | - Optional<RegisteredServiceProvider<IPacketEntityAPI.ProviderStub>> top = r.stream() |
117 | | - .filter(p -> ProviderStub.MAJOR_VERSION.equals(p.getProvider().getMajorVersion())).findFirst(); |
118 | | - if (top.isPresent()) { |
119 | | - if (compareVersions(top.get().getProvider().getFullVersion())) { |
120 | | - m.unregister(IPacketEntityAPI.ProviderStub.class, top.get()); |
121 | | - } else return; |
122 | | - } |
123 | | - m.register(IPacketEntityAPI.ProviderStub.class, new PacketEntityAPI.ProviderStub(), parent, ServicePriority |
124 | | - .Normal); |
125 | | - |
126 | | - if (PacketEntityAPI.parent == null || parent.getName().equals("PacketEntityAPI")) { |
127 | | - PacketEntityAPI.parent = parent; |
128 | | - } |
| 95 | + } |
129 | 96 |
|
130 | | - PacketEntityAPI.instance = getLatestCompatibleVersion(); |
| 97 | + @Override |
| 98 | + public void onDisable() { |
131 | 99 |
|
132 | | - if (onLoad != null) |
133 | | - Bukkit.getScheduler().runTask(parent, () -> onLoad.accept(instance)); |
134 | 100 | } |
135 | 101 |
|
136 | 102 | public static TaskChainFactory getChainFactory() { |
137 | 103 | return chainFactory; |
138 | 104 | } |
139 | 105 |
|
140 | | - private static IPacketEntityAPI getLatestCompatibleVersion() { |
141 | | - RegisteredServiceProvider<IPacketEntityAPI.ProviderStub> match = Bukkit.getServicesManager() |
142 | | - .getRegistrations(IPacketEntityAPI.ProviderStub.class).stream() |
143 | | - .filter(p -> ProviderStub.MAJOR_VERSION.equals("0") && |
144 | | - ProviderStub.FULL_VERSION.equals(p.getProvider().getFullVersion()) || |
145 | | - ProviderStub.MAJOR_VERSION.equals(p.getProvider().getMajorVersion())).findFirst() |
146 | | - .orElse(null); |
147 | | - if (match == null) { |
148 | | - throw new IllegalStateException("No compatible API version found! List of current versions:"); |
149 | | - } |
150 | | - return match.getProvider().getInstance(); |
151 | | - } |
152 | | - |
153 | 106 | public static IFakeEntity getFakeEntity(int entityID) { |
154 | 107 | return instance.getFakeByID(entityID); |
155 | 108 | } |
@@ -221,7 +174,7 @@ public void dispatchPacket(IEntityPacket packet, Player target, int delay) { |
221 | 174 | PacketContainer c = packet.getRawPacket(); |
222 | 175 | if (c == null) return; |
223 | 176 | if (delay > 0) { |
224 | | - Bukkit.getScheduler().scheduleSyncDelayedTask(parent, () -> { |
| 177 | + Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> { |
225 | 178 | if (c.getType().isClient()) { |
226 | 179 | safeReceive(target, c); |
227 | 180 | } else { |
@@ -256,25 +209,4 @@ private void safeReceive(Player target, PacketContainer packet) { |
256 | 209 | public Collection<FakeEntity> getFakeEntities() { |
257 | 210 | return fakeEntityFactory.getFakeEntities(); |
258 | 211 | } |
259 | | - |
260 | | - private static class ProviderStub implements IPacketEntityAPI.ProviderStub { |
261 | | - static final String FULL_VERSION = "${project.version}"; |
262 | | - static final String MAJOR_VERSION = FULL_VERSION.substring(0, FULL_VERSION.indexOf(".")); |
263 | | - private static PacketEntityAPI inst; |
264 | | - |
265 | | - @Override |
266 | | - public String getMajorVersion() { |
267 | | - return MAJOR_VERSION; |
268 | | - } |
269 | | - |
270 | | - @Override |
271 | | - public String getFullVersion() { |
272 | | - return FULL_VERSION; |
273 | | - } |
274 | | - |
275 | | - @Override |
276 | | - public IPacketEntityAPI getInstance() { |
277 | | - return (inst == null ? (inst = new PacketEntityAPI()) : inst); |
278 | | - } |
279 | | - } |
280 | 212 | } |
0 commit comments