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;
|
2024-03-04 20:21:48 +01:00
|
|
|
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
|
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
|
import net.neoforged.neoforge.network.handling.PlayPayloadContext;
|
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
2016-06-15 16:43:59 +02:00
|
|
|
|
2024-03-04 20:21:48 +01:00
|
|
|
public record PacketClientToServer(CompoundTag data, IDataHandler handler) implements CustomPacketPayload {
|
|
|
|
public static final ResourceLocation ID = new ResourceLocation(ActuallyAdditions.MODID, "client_to_server");
|
2016-06-15 16:43:59 +02:00
|
|
|
|
2024-03-04 20:21:48 +01:00
|
|
|
public PacketClientToServer(Pair<CompoundTag, IDataHandler> data) {
|
|
|
|
this(data.getLeft(), data.getRight());
|
2016-06-15 16:43:59 +02:00
|
|
|
}
|
|
|
|
|
2024-03-04 20:21:48 +01:00
|
|
|
public PacketClientToServer(final FriendlyByteBuf buffer) {
|
|
|
|
this(fromBytes(buffer));
|
2016-06-15 16:43:59 +02:00
|
|
|
}
|
|
|
|
|
2024-03-04 20:21:48 +01:00
|
|
|
public static Pair<CompoundTag, IDataHandler> 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()) {
|
2024-03-04 20:21:48 +01:00
|
|
|
return Pair.of(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
|
|
|
}
|
2024-03-04 20:21:48 +01:00
|
|
|
return Pair.of(null, null);
|
2016-06-15 16:43:59 +02:00
|
|
|
}
|
|
|
|
|
2024-03-04 20:21:48 +01:00
|
|
|
public void write(FriendlyByteBuf buf) {
|
|
|
|
buf.writeNbt(data);
|
|
|
|
buf.writeInt(PacketHandler.DATA_HANDLERS.indexOf(handler));
|
2016-06-15 16:43:59 +02:00
|
|
|
}
|
|
|
|
|
2024-03-04 20:21:48 +01:00
|
|
|
public static void handle(final PacketClientToServer message, final PlayPayloadContext context) {
|
|
|
|
context.workHandler().submitAsync(
|
|
|
|
() -> {
|
|
|
|
if (message.data != null && message.handler != null) {
|
|
|
|
message.handler.handleData(message.data, context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ResourceLocation id() {
|
|
|
|
return ID;
|
2016-06-15 16:43:59 +02:00
|
|
|
}
|
|
|
|
}
|