Skip to content

Commit ea0dd61

Browse files
Restarted development
1 parent c9ff671 commit ea0dd61

File tree

234 files changed

+2611
-10032
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

234 files changed

+2611
-10032
lines changed

1_13/build.gradle

-22
This file was deleted.

1_13/src/main/java/me/illusion/skyblockcore/v1_13/LocationGrabber.java

-52
This file was deleted.

src/main/java/me/illusion/skyblockcore/shared/packet/Packet.java SkyblockCore-Common/src/main/java/me/illusion/skyblockcore/common/communication/packet/Packet.java

+26-42
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,55 @@
1-
package me.illusion.skyblockcore.shared.packet;
1+
package me.illusion.skyblockcore.common.communication.packet;
22

33
import com.google.common.io.ByteArrayDataInput;
44
import com.google.common.io.ByteArrayDataOutput;
55
import com.google.common.io.ByteStreams;
6-
import lombok.SneakyThrows;
7-
import me.illusion.skyblockcore.shared.packet.data.PacketDirection;
8-
import net.md_5.bungee.api.chat.BaseComponent;
9-
import net.md_5.bungee.chat.ComponentSerializer;
10-
11-
import java.io.*;
6+
import java.io.ByteArrayInputStream;
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.ObjectInput;
9+
import java.io.ObjectInputStream;
10+
import java.io.ObjectOutputStream;
1211
import java.util.UUID;
12+
import lombok.SneakyThrows;
1313

1414
public abstract class Packet {
1515

1616
private final byte identifier;
17-
private final PacketDirection direction;
17+
private final UUID uuid;
1818

1919
private ByteArrayDataOutput stream;
2020
private ByteArrayDataInput input;
2121

2222
public Packet(byte[] bytes) {
2323
input = ByteStreams.newDataInput(bytes);
2424
identifier = readByte();
25-
direction = PacketDirection.fromIndex(readByte());
26-
25+
uuid = readUUID();
2726
}
2827

29-
public Packet(PacketDirection direction) {
30-
28+
public Packet() {
3129
this.identifier = PacketManager.getIdentifier(getClass());
32-
this.direction = direction;
30+
this.uuid = UUID.randomUUID();
3331

3432
validateStream();
3533
writeByte(identifier);
36-
writeByte(direction.getIndex());
34+
writeUUID(uuid);
3735
}
3836

39-
public abstract void write();
40-
4137
protected byte getIdentifier() {
4238
return identifier;
4339
}
4440

45-
protected PacketDirection getDirection() {
46-
return direction;
47-
}
48-
4941
private void validateStream() {
50-
if (stream == null)
42+
if (stream == null) {
5143
stream = ByteStreams.newDataOutput();
52-
}
53-
54-
protected void writeBoolean(boolean value) {
55-
validateStream();
56-
stream.writeBoolean(value);
44+
}
5745
}
5846

5947
protected void writeByte(byte value) {
6048
validateStream();
6149
stream.writeByte(value);
6250
}
6351

64-
protected void writeByteArray(byte... bytes) {
52+
protected void writeByteArray(byte[] bytes) {
6553
validateStream();
6654
writeInt(bytes.length);
6755
stream.write(bytes);
@@ -107,28 +95,21 @@ protected void writeUUID(UUID uuid) {
10795
writeLong(uuid.getLeastSignificantBits());
10896
}
10997

110-
protected void writeBungeeText(BaseComponent... text) {
111-
writeString(ComponentSerializer.toString(text));
112-
}
113-
114-
11598
@SneakyThrows
11699
protected void writeObject(Object object) {
117100
validateStream();
118-
try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos)) {
101+
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
102+
ObjectOutputStream out = new ObjectOutputStream(bos);
119103
out.writeObject(object);
120104
out.flush();
121105
byte[] bytes = bos.toByteArray();
122106
writeByteArray(bytes);
107+
out.close();
123108
}
124109
// ignore close exception
125110
}
126111

127112

128-
protected boolean readBoolean() {
129-
return input.readBoolean();
130-
}
131-
132113
protected byte readByte() {
133114
return input.readByte();
134115
}
@@ -137,8 +118,9 @@ protected byte[] readByteArray() {
137118
int arraySize = readInt();
138119
byte[] array = new byte[arraySize];
139120

140-
for (int index = 0; index < arraySize; index++)
121+
for (int index = 0; index < arraySize; index++) {
141122
array[index] = readByte();
123+
}
142124

143125
return array;
144126
}
@@ -163,15 +145,13 @@ protected UUID readUUID() {
163145
return new UUID(readLong(), readLong());
164146
}
165147

166-
protected BaseComponent[] readBungeeText() {
167-
return ComponentSerializer.parse(readString());
168-
}
169148

170149
@SneakyThrows
171150
protected Object readObject() {
172151
byte[] bytes = readByteArray();
173152

174-
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes); ObjectInput in = new ObjectInputStream(bis)) {
153+
try (ByteArrayInputStream bis = new ByteArrayInputStream(
154+
bytes); ObjectInput in = new ObjectInputStream(bis)) {
175155
return in.readObject();
176156
}
177157

@@ -181,4 +161,8 @@ protected Object readObject() {
181161
public byte[] getAllBytes() {
182162
return stream.toByteArray();
183163
}
164+
165+
public UUID getPacketId() {
166+
return uuid;
167+
}
184168
}

src/main/java/me/illusion/skyblockcore/shared/packet/PacketHandler.java SkyblockCore-Common/src/main/java/me/illusion/skyblockcore/common/communication/packet/PacketHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package me.illusion.skyblockcore.shared.packet;
1+
package me.illusion.skyblockcore.common.communication.packet;
22

33
public interface PacketHandler<T extends Packet> {
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package me.illusion.skyblockcore.common.communication.packet;
2+
3+
import com.google.common.cache.Cache;
4+
import com.google.common.cache.CacheBuilder;
5+
import java.lang.reflect.InvocationTargetException;
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.HashSet;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Set;
12+
import java.util.UUID;
13+
import java.util.concurrent.CompletableFuture;
14+
import java.util.concurrent.TimeUnit;
15+
import java.util.function.Predicate;
16+
17+
public class PacketManager {
18+
19+
20+
private static final Map<Byte, Class<? extends Packet>> identifiers = new HashMap<>();
21+
private final List<PacketProcessor> processors = new ArrayList<>();
22+
private final Map<Byte, List<PacketHandler<Packet>>> handlers = new HashMap<>();
23+
24+
private final Cache<UUID, Boolean> ignoredPackets = CacheBuilder.newBuilder()
25+
.expireAfterWrite(5, TimeUnit.SECONDS)
26+
.build();
27+
28+
private final PacketWaiter waiter;
29+
30+
public PacketManager() {
31+
waiter = new PacketWaiter(this);
32+
33+
34+
}
35+
36+
public static void registerPacket(int packetId, Class<? extends Packet> packetClass) {
37+
registerPacket((byte) packetId, packetClass);
38+
}
39+
40+
public static void registerPacket(byte packetId, Class<? extends Packet> packetClass) {
41+
if (identifiers.containsKey(packetId)) {
42+
throw new UnsupportedOperationException(
43+
"Packet identifier for packet " + packetClass.getSimpleName()
44+
+ " is already registered. ");
45+
}
46+
47+
identifiers.put(packetId, packetClass);
48+
}
49+
50+
51+
public static byte getIdentifier(Class<? extends Packet> clazz) {
52+
for (Map.Entry<Byte, Class<? extends Packet>> entry : identifiers.entrySet()) {
53+
if (clazz.equals(entry.getValue())) {
54+
return entry.getKey();
55+
}
56+
}
57+
58+
// If it isn't registered, just register it.
59+
registerPacket(identifiers.size(), clazz);
60+
61+
return getIdentifier(clazz);
62+
}
63+
64+
public Class<? extends Packet> getPacketClass(byte identifier) {
65+
return identifiers.get(identifier);
66+
}
67+
68+
public void registerProcessor(PacketProcessor processor) {
69+
processors.add(processor);
70+
processor.addCallback(this::read);
71+
}
72+
73+
public CompletableFuture<Void> send(Packet packet) {
74+
75+
ignoredPackets.put(packet.getPacketId(), true);
76+
77+
Set<CompletableFuture<Void>> futures = new HashSet<>();
78+
79+
try {
80+
for (PacketProcessor processor : processors) {
81+
futures.add(processor.send(packet));
82+
}
83+
84+
byte id = packet.getIdentifier();
85+
List<PacketHandler<Packet>> handler = handlers.get(id);
86+
87+
if (handler == null) {
88+
System.out.println("No handlers for packet " + packet.getClass().getSimpleName() + " " + id);
89+
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
90+
}
91+
92+
for (PacketHandler<Packet> packetHandler : handler) {
93+
packetHandler.onSend(packet);
94+
}
95+
96+
} catch (Exception e) {
97+
e.printStackTrace();
98+
}
99+
100+
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
101+
}
102+
103+
public Packet read(byte[] bytes) {
104+
Class<? extends Packet> type = getPacketClass(bytes[0]);
105+
106+
if (type == null) {
107+
return null;
108+
}
109+
110+
try {
111+
Packet packet = type.getConstructor(byte[].class).newInstance(bytes);
112+
113+
UUID packetId = packet.getPacketId();
114+
115+
if (ignoredPackets.getIfPresent(packetId) != null) {
116+
ignoredPackets.invalidate(packetId);
117+
return null;
118+
}
119+
120+
List<PacketHandler<Packet>> handler = handlers.get(bytes[0]);
121+
122+
if (handler != null) {
123+
for (PacketHandler<Packet> packetHandler : handler) {
124+
packetHandler.onReceive(packet);
125+
}
126+
}
127+
128+
return packet;
129+
} catch (InstantiationException | IllegalAccessException | InvocationTargetException |
130+
NoSuchMethodException e) {
131+
e.printStackTrace();
132+
}
133+
134+
return null;
135+
}
136+
137+
public <T extends Packet> void subscribe(Class<T> packetClass, PacketHandler<T> handler) {
138+
byte identifier = getIdentifier(packetClass);
139+
140+
handlers.putIfAbsent(identifier, new ArrayList<>());
141+
handlers.get(identifier).add((PacketHandler<Packet>) handler);
142+
}
143+
144+
public <T extends Packet> CompletableFuture<T> await(Class<T> clazz, Predicate<T> predicate) {
145+
return waiter.await(clazz, predicate);
146+
}
147+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package me.illusion.skyblockcore.common.communication.packet;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.function.Consumer;
5+
6+
public interface PacketProcessor {
7+
8+
CompletableFuture<Void> send(Packet packet);
9+
10+
void addCallback(Consumer<byte[]> receivedPacket);
11+
12+
13+
}

0 commit comments

Comments
 (0)