Network done for now?

This commit is contained in:
Flanks255 2021-10-16 11:46:41 -05:00
parent 3da868fc29
commit cd37abb4ca
3 changed files with 29 additions and 30 deletions

View file

@ -16,6 +16,8 @@ import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;
import java.util.function.Supplier;
public class PacketClientToServer {
private CompoundNBT data;
@ -30,39 +32,31 @@ public class PacketClientToServer {
this.handler = handler;
}
@Override
public void fromBytes(ByteBuf buf) {
PacketBuffer buffer = new PacketBuffer(buf);
public static PacketClientToServer fromBytes(PacketBuffer buffer) {
try {
this.data = buffer.readNbt();
CompoundNBT data = buffer.readNbt();
int handlerId = buffer.readInt();
if (handlerId >= 0 && handlerId < PacketHandler.DATA_HANDLERS.size()) {
this.handler = PacketHandler.DATA_HANDLERS.get(handlerId);
return new PacketClientToServer(data, PacketHandler.DATA_HANDLERS.get(handlerId));
}
} catch (Exception e) {
ActuallyAdditions.LOGGER.error("Something went wrong trying to receive a server packet!", e);
}
return new PacketClientToServer();
}
@Override
public void toBytes(ByteBuf buf) {
PacketBuffer buffer = new PacketBuffer(buf);
buffer.writeNbt(this.data);
buffer.writeInt(PacketHandler.DATA_HANDLERS.indexOf(this.handler));
public static void toBytes(PacketClientToServer message, PacketBuffer buffer) {
buffer.writeNbt(message.data);
buffer.writeInt(PacketHandler.DATA_HANDLERS.indexOf(message.handler));
}
public static class Handler implements IMessageHandler<PacketClientToServer, IMessage> {
@Override
public IMessage onMessage(PacketClientToServer message, NetworkEvent.Context ctx) {
FMLCommonHandler.instance().getMinecraftServerInstance().addScheduledTask(() -> {
if (message.data != null && message.handler != null) {
message.handler.handleData(message.data, ctx);
}
});
return null;
}
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);
}
}

View file

@ -27,6 +27,7 @@ import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.container.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.NBTUtil;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.RegistryKey;
import net.minecraft.util.ResourceLocation;
@ -73,7 +74,7 @@ public final class PacketHandler {
@OnlyIn(Dist.CLIENT)
public void handleData(CompoundNBT compound, NetworkEvent.Context context) {
Minecraft mc = Minecraft.getInstance();
ItemStack stack = null; //new ItemStack(compound); //TODO
ItemStack stack = ItemStack.of(compound);
double inX = compound.getDouble("InX") + 0.5;
double inY = compound.getDouble("InY") + 0.78;
@ -182,8 +183,16 @@ public final class PacketHandler {
);
public static void init() {
THE_NETWORK.registerMessage(0, PacketServerToClient.class, PacketServerToClient::toBytes, PacketServerToClient::fromBytes, PacketServerToClient::handle, NetworkDirection.PLAY_TO_CLIENT);
THE_NETWORK.registerMessage(1, PacketClientToServer.class, PacketClientToServer.class, NetworkDirection.PLAY_TO_SERVER);
THE_NETWORK.messageBuilder(PacketServerToClient.class, 0, NetworkDirection.PLAY_TO_CLIENT)
.decoder(PacketServerToClient::fromBytes)
.encoder(PacketServerToClient::toBytes)
.consumer(PacketServerToClient::handle).add();
THE_NETWORK.messageBuilder(PacketClientToServer.class, 1, NetworkDirection.PLAY_TO_SERVER)
.decoder(PacketClientToServer::fromBytes)
.encoder(PacketClientToServer::toBytes)
.consumer(PacketClientToServer::handle).add();
DATA_HANDLERS.add(LASER_HANDLER);
DATA_HANDLERS.add(TILE_ENTITY_HANDLER);

View file

@ -10,14 +10,9 @@
package de.ellpeck.actuallyadditions.mod.network;
import com.mojang.brigadier.Message;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.network.NetworkEvent;
import java.util.function.Supplier;
@ -48,6 +43,7 @@ public class PacketServerToClient {
} catch (Exception e) {
ActuallyAdditions.LOGGER.error("Something went wrong trying to receive a client packet!", e);
}
return new PacketServerToClient();
}
public static void toBytes(final PacketServerToClient message, PacketBuffer buffer) {