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

View file

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

View file

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