Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Packet objects, bump versions, fix editor config
- Loading branch information
Showing
13 changed files
with
185 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 0 additions & 11 deletions
11
src/main/java/com/denizenscript/clientizen/network/Channels.java
This file was deleted.
Oops, something went wrong.
61 changes: 0 additions & 61 deletions
61
src/main/java/com/denizenscript/clientizen/network/DataDeserializer.java
This file was deleted.
Oops, something went wrong.
69 changes: 0 additions & 69 deletions
69
src/main/java/com/denizenscript/clientizen/network/DataSerializer.java
This file was deleted.
Oops, something went wrong.
30 changes: 17 additions & 13 deletions
30
src/main/java/com/denizenscript/clientizen/network/NetworkManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,54 @@ | ||
package com.denizenscript.clientizen.network; | ||
|
||
import com.denizenscript.clientizen.Clientizen; | ||
import com.denizenscript.clientizen.network.packets.ReceiveScriptsPacketIn; | ||
import com.denizenscript.clientizen.network.packets.RunScriptPacketIn; | ||
import com.denizenscript.clientizen.network.packets.SendConfirmationPacketOut; | ||
import com.denizenscript.denizencore.utilities.CoreConfiguration; | ||
import com.denizenscript.denizencore.utilities.debugging.Debug; | ||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; | ||
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; | ||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.util.Identifier; | ||
|
||
public class NetworkManager { | ||
|
||
public static void init() { | ||
Debug.log("Initializing NetworkManager..."); | ||
ScriptNetworking.init(); | ||
registerInPacket(new ReceiveScriptsPacketIn()); | ||
registerInPacket(new RunScriptPacketIn()); | ||
} | ||
|
||
public static void onConnect() { | ||
debugNetwork("Sending join confirmation packet..."); | ||
send(Channels.SEND_CONFIRM, null); | ||
send(new SendConfirmationPacketOut()); | ||
} | ||
|
||
public static void registerInChannel(Identifier channel, ClientizenReceiver receiver) { | ||
public static void registerInPacket(PacketIn packet) { | ||
final Identifier channel = Clientizen.id(packet.getName()); | ||
if (!ClientPlayNetworking.registerGlobalReceiver(channel, (client, handler, buf, responseSender) -> { | ||
debugNetwork("Received plugin message on channel " + channel); | ||
receiver.receive(new DataDeserializer(buf)); | ||
packet.process(buf); | ||
})) { | ||
Debug.echoError("Tried registering plugin channel '" + channel + "', but it is already registered!"); | ||
Debug.echoError("Tried registering in packet on channel '" + channel + "', but a packet is already registered for that channel!"); | ||
} | ||
} | ||
|
||
public static void send(Identifier channel, DataSerializer serializer) { | ||
public static void send(PacketOut packet) { | ||
// TODO: re-add this check? doesn't work on ClientPlayConnectionEvents.JOIN, might be too early | ||
// if (!ClientPlayNetworking.canSend(identifier)) { | ||
// Debug.echoError("Cannot send to channel " + channel); | ||
// return; | ||
// } | ||
debugNetwork("Sending message on channel " + channel); | ||
ClientPlayNetworking.send(channel, serializer != null ? serializer.output : PacketByteBufs.empty()); | ||
debugNetwork("Sending message on channel " + packet.channel); | ||
PacketByteBuf buf = PacketByteBufs.create(); | ||
packet.writeTo(buf); | ||
ClientPlayNetworking.send(packet.channel, buf); | ||
} | ||
|
||
public static void debugNetwork(String debug) { | ||
if (CoreConfiguration.debugExtraInfo) { | ||
Debug.log(debug); | ||
} | ||
} | ||
|
||
@FunctionalInterface | ||
public interface ClientizenReceiver { | ||
void receive(DataDeserializer message); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/denizenscript/clientizen/network/PacketIn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.denizenscript.clientizen.network; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
public abstract class PacketIn { | ||
|
||
public abstract void process(ByteBuf data); | ||
|
||
public abstract String getName(); | ||
|
||
public static String readString(ByteBuf buf) { | ||
byte[] bytes = new byte[buf.readInt()]; | ||
buf.readBytes(bytes); | ||
return new String(bytes, StandardCharsets.UTF_8); | ||
} | ||
|
||
public static Map<String, String> readStringMap(ByteBuf buf) { | ||
int size = buf.readInt(); | ||
Map<String, String> stringMap = new HashMap<>(size); | ||
for (int i = 0; i < size; i++) { | ||
String key = readString(buf); | ||
String value = readString(buf); | ||
stringMap.put(key, value); | ||
} | ||
return stringMap; | ||
} | ||
|
||
public static <T> T readNullable(ByteBuf buf, Function<ByteBuf, T> readMethod) { | ||
return buf.readBoolean() ? readMethod.apply(buf) : null; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/denizenscript/clientizen/network/PacketOut.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.denizenscript.clientizen.network; | ||
|
||
import com.denizenscript.clientizen.Clientizen; | ||
import io.netty.buffer.ByteBuf; | ||
import net.minecraft.util.Identifier; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Map; | ||
import java.util.function.BiConsumer; | ||
|
||
public abstract class PacketOut { | ||
|
||
public final Identifier channel = Clientizen.id(getName()); | ||
|
||
public void writeTo(ByteBuf buf) {} // Some packets may not send any data | ||
|
||
public abstract String getName(); | ||
|
||
public static void writeString(ByteBuf buf, String str) { | ||
byte[] bytes = str.getBytes(StandardCharsets.UTF_8); | ||
buf.writeInt(bytes.length); | ||
buf.writeBytes(bytes); | ||
} | ||
|
||
public void writeStringMap(ByteBuf buf, Map<String, String> stringMap) { | ||
buf.writeInt(stringMap.size()); | ||
for (Map.Entry<String, String> entry : stringMap.entrySet()) { | ||
writeString(buf, entry.getKey()); | ||
writeString(buf, entry.getValue()); | ||
} | ||
} | ||
|
||
public <T> void writeNullable(ByteBuf buf, T object, BiConsumer<ByteBuf, T> writeMethod) { | ||
if (object != null) { | ||
buf.writeBoolean(true); | ||
writeMethod.accept(buf, object); | ||
} | ||
else { | ||
buf.writeBoolean(false); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/denizenscript/clientizen/network/packets/FireEventPacketOut.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.denizenscript.clientizen.network.packets; | ||
|
||
import com.denizenscript.clientizen.network.PacketOut; | ||
import io.netty.buffer.ByteBuf; | ||
|
||
import java.util.Map; | ||
|
||
public class FireEventPacketOut extends PacketOut { | ||
|
||
public FireEventPacketOut(String id, Map<String, String> data) { | ||
this.id = id; | ||
this.data = data; | ||
} | ||
|
||
String id; | ||
Map<String, String> data; | ||
|
||
@Override | ||
public void writeTo(ByteBuf buf) { | ||
writeString(buf, id); | ||
writeStringMap(buf, data); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "fire_event"; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/denizenscript/clientizen/network/packets/ReceiveScriptsPacketIn.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.denizenscript.clientizen.network.packets; | ||
|
||
import com.denizenscript.clientizen.network.PacketIn; | ||
import com.denizenscript.denizencore.DenizenCore; | ||
import com.denizenscript.denizencore.scripts.ScriptHelper; | ||
import com.denizenscript.denizencore.utilities.YamlConfiguration; | ||
import io.netty.buffer.ByteBuf; | ||
|
||
import java.util.Map; | ||
|
||
public class ReceiveScriptsPacketIn extends PacketIn { | ||
|
||
@Override | ||
public void process(ByteBuf data) { | ||
Map<String, String> scriptsMap = readStringMap(data); | ||
DenizenCore.runOnMainThread(() -> { | ||
ScriptHelper.buildAdditionalScripts.clear(); | ||
for (Map.Entry<String, String> entry : scriptsMap.entrySet()) { | ||
ScriptHelper.buildAdditionalScripts.add(scripts -> scripts.add(YamlConfiguration.load(ScriptHelper.clearComments(entry.getKey(), entry.getValue(), true)))); | ||
} | ||
DenizenCore.reloadScripts(true, null); | ||
}); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "set_scripts"; | ||
} | ||
} |
Oops, something went wrong.