Skip to content

Commit 13aca56

Browse files
Fix: Network clientbound handler derp
1 parent d58d5ac commit 13aca56

3 files changed

Lines changed: 53 additions & 12 deletions

File tree

.changeset/lemon-eggs-sip.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"CreateColonies": patch
3+
---
4+
5+
fix: The network part had a logical error in it, which meant some code that was only meant for clients got loaded on the server by accident. Fixes #27
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package nl.motionlesstrain.createcolonies.network.messages;
2+
3+
import net.neoforged.neoforge.network.handling.IPayloadContext;
4+
import net.neoforged.neoforge.network.handling.IPayloadHandler;
5+
import nl.motionlesstrain.createcolonies.network.NetworkMessage;
6+
7+
public abstract class ClientBoundNetworkMessage implements NetworkMessage {
8+
abstract ClientSideHandler getHandler();
9+
10+
protected void handlePacket(IPayloadContext ctx) {
11+
getHandler().handleMessage(ctx);
12+
}
13+
14+
public interface ClientSideHandler {
15+
void handleMessage(IPayloadContext ctx);
16+
}
17+
18+
public abstract static class Factory<T extends ClientBoundNetworkMessage> implements NetworkMessage.Factory<T> {
19+
20+
@Override
21+
public IPayloadHandler<T> handler() {
22+
return ClientBoundNetworkMessage::handlePacket;
23+
}
24+
}
25+
}

src/main/java/nl/motionlesstrain/createcolonies/network/messages/SaveNBTFileMessage.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,22 @@
2525

2626
import static nl.motionlesstrain.createcolonies.CreateColonies.MODID;
2727

28-
public record SaveNBTFileMessage(String filePath, CompoundTag fileContents) implements NetworkMessage {
28+
public class SaveNBTFileMessage extends ClientBoundNetworkMessage {
2929
private static final CustomPacketPayload.Type<SaveNBTFileMessage> TYPE = new CustomPacketPayload.Type<>(
3030
ResourceLocation.fromNamespaceAndPath(MODID, "save_nbt_file")
3131
);
3232

33+
private final String filePath;
34+
private final CompoundTag fileContents;
35+
36+
public SaveNBTFileMessage(final String filePath, final CompoundTag fileContents) {
37+
this.filePath = filePath;
38+
this.fileContents = fileContents;
39+
}
40+
41+
public String filePath() { return filePath; }
42+
public CompoundTag fileContents() { return fileContents; }
43+
3344
@Override
3445
public @NotNull Type<? extends CustomPacketPayload> type() {
3546
return TYPE;
@@ -44,24 +55,29 @@ public record SaveNBTFileMessage(String filePath, CompoundTag fileContents) impl
4455

4556
private static final Logger LOGGER = LogUtils.getLogger();
4657

47-
private static class ClientNBTSaver implements IPayloadHandler<SaveNBTFileMessage> {
58+
@Override
59+
ClientSideHandler getHandler() {
60+
return new ClientNBTSaver();
61+
}
62+
63+
private class ClientNBTSaver implements ClientBoundNetworkMessage.ClientSideHandler {
4864
@Override
49-
public void handle(SaveNBTFileMessage message, @NotNull IPayloadContext context) {
65+
public void handleMessage(@NotNull IPayloadContext context) {
5066
final Path gamePath = Minecraft.getInstance().gameDirectory.toPath();
51-
final Path saveFile = gamePath.resolve(message.filePath);
67+
final Path saveFile = gamePath.resolve(filePath);
5268
try {
53-
NbtIo.writeCompressed(message.fileContents, saveFile);
69+
NbtIo.writeCompressed(fileContents, saveFile);
5470
final @Nullable Player player = Minecraft.getInstance().player;
5571
if (player != null) {
5672
player.displayClientMessage(Component.translatable("nl.motionlesstrain.createcolonies.convert.confirm"), false);
5773
}
5874
} catch (IOException e) {
59-
LOGGER.error("Could not save file {}", message.filePath, e);
75+
LOGGER.error("Could not save file {}", filePath, e);
6076
}
6177
}
6278
}
6379

64-
public static class Factory implements NetworkMessage.Factory<SaveNBTFileMessage> {
80+
public static class Factory extends ClientBoundNetworkMessage.Factory<SaveNBTFileMessage> {
6581

6682
@Override
6783
public Type<SaveNBTFileMessage> type() {
@@ -72,10 +88,5 @@ public Type<SaveNBTFileMessage> type() {
7288
public StreamCodec<? super RegistryFriendlyByteBuf, SaveNBTFileMessage> streamCodec() {
7389
return STREAM_CODEC;
7490
}
75-
76-
@Override
77-
public IPayloadHandler<SaveNBTFileMessage> handler() {
78-
return new ClientNBTSaver();
79-
}
8091
}
8192
}

0 commit comments

Comments
 (0)