2016-06-15 16:43:59 +02:00
|
|
|
/*
|
2016-08-02 13:08:22 +02:00
|
|
|
* This file ("PacketClientToServer.java") is part of the Actually Additions mod for Minecraft.
|
2016-06-15 16:43:59 +02:00
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
|
|
|
* http://ellpeck.de/actaddlicense
|
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2016-06-15 16:43:59 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.network;
|
|
|
|
|
2018-05-10 11:38:58 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
2024-03-02 21:23:08 +01:00
|
|
|
import net.minecraft.nbt.CompoundTag;
|
|
|
|
import net.minecraft.network.FriendlyByteBuf;
|
|
|
|
import net.minecraftforge.network.NetworkEvent;
|
2016-06-15 16:43:59 +02:00
|
|
|
|
2021-10-16 18:46:41 +02:00
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
2021-04-19 21:20:23 +02:00
|
|
|
public class PacketClientToServer {
|
2016-06-15 16:43:59 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
private CompoundTag data;
|
2016-06-15 16:43:59 +02:00
|
|
|
private IDataHandler handler;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public PacketClientToServer() {
|
2016-06-15 16:43:59 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public PacketClientToServer(CompoundTag data, IDataHandler handler) {
|
2016-06-15 16:43:59 +02:00
|
|
|
this.data = data;
|
|
|
|
this.handler = handler;
|
|
|
|
}
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public static PacketClientToServer fromBytes(FriendlyByteBuf buffer) {
|
2019-05-02 09:10:29 +02:00
|
|
|
try {
|
2024-03-02 21:23:08 +01:00
|
|
|
CompoundTag data = buffer.readNbt();
|
2016-06-15 16:43:59 +02:00
|
|
|
|
|
|
|
int handlerId = buffer.readInt();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (handlerId >= 0 && handlerId < PacketHandler.DATA_HANDLERS.size()) {
|
2021-10-16 18:46:41 +02:00
|
|
|
return new PacketClientToServer(data, PacketHandler.DATA_HANDLERS.get(handlerId));
|
2016-06-15 16:43:59 +02:00
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
} catch (Exception e) {
|
2018-05-10 11:38:58 +02:00
|
|
|
ActuallyAdditions.LOGGER.error("Something went wrong trying to receive a server packet!", e);
|
2016-06-15 16:43:59 +02:00
|
|
|
}
|
2021-10-16 18:46:41 +02:00
|
|
|
return new PacketClientToServer();
|
2016-06-15 16:43:59 +02:00
|
|
|
}
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public static void toBytes(PacketClientToServer message, FriendlyByteBuf buffer) {
|
2021-10-16 18:46:41 +02:00
|
|
|
buffer.writeNbt(message.data);
|
|
|
|
buffer.writeInt(PacketHandler.DATA_HANDLERS.indexOf(message.handler));
|
2016-06-15 16:43:59 +02:00
|
|
|
}
|
|
|
|
|
2021-10-16 18:46:41 +02:00
|
|
|
public static void handle(final PacketClientToServer message, final Supplier<NetworkEvent.Context> ctx) {
|
|
|
|
ctx.get().enqueueWork( () -> {
|
|
|
|
if (message.data != null && message.handler != null) {
|
|
|
|
message.handler.handleData(message.data, ctx.get());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ctx.get().setPacketHandled(true);
|
2016-06-15 16:43:59 +02:00
|
|
|
}
|
|
|
|
}
|