Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to Buffers to DataSlots #279

Merged
merged 13 commits into from
Aug 3, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- Fixed the output calculations for vanilla smelting in the alloy smelter.
- Fixed Primitive Alloy Smelter not serializing burn time
- Fixed linking between conduit connector shape and selected conduit.
- Added FriendlyByteBuffer to networkslots.

## [6.0.3-alpha] - 2023-07-08

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.enderio.core.common.network.slot.NetworkDataSlot;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;

import java.util.function.Supplier;

Expand All @@ -21,7 +22,7 @@ public void fromNBT(Tag nbt) {
if (nbt instanceof CompoundTag compoundTag) {
getter.get().deserializeNBT(compoundTag);
} else {
throw new IllegalStateException("Invalid compound tag was passed over the network.");
throw new IllegalStateException("Invalid conduit/compound tag was passed over the network.");
}
}

Expand All @@ -36,4 +37,20 @@ protected int hashCode(ConduitBundle value) {
int code = value.serializeNBT().hashCode();
return code;
}

@Override
public void toBuffer(FriendlyByteBuf buf, ConduitBundle value) {
buf.writeNbt(value.serializeNBT());
}

@Override
public ConduitBundle valueFromBuffer(FriendlyByteBuf buf) {
try {
ConduitBundle conduitBundle = getter.get();
conduitBundle.deserializeNBT(buf.readNbt());
return conduitBundle;
} catch (Exception e) {
throw new IllegalStateException("Invalid conduit/compound tag buffer was passed over the network.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
import com.enderio.core.common.network.CoreNetwork;
import com.enderio.core.common.network.S2CDataSlotUpdate;
import com.enderio.core.common.network.slot.NetworkDataSlot;
import io.netty.buffer.Unpooled;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
Expand All @@ -21,7 +24,10 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Base block entity class for EnderIO.
Expand Down Expand Up @@ -77,7 +83,7 @@ public void clientTick() {
*/
@Override
public CompoundTag getUpdateTag() {
return createDataSlotUpdate(true);
return createDataSlotUpdate();
}

/**
Expand All @@ -89,12 +95,11 @@ public void handleUpdateTag(CompoundTag tag) {
clientHandleDataSync(tag);
}

@Nullable
private CompoundTag createDataSlotUpdate(boolean fullUpdate) {
private CompoundTag createDataSlotUpdate() {
ferriarnus marked this conversation as resolved.
Show resolved Hide resolved
ListTag dataList = new ListTag();
for (int i = 0; i < dataSlots.size(); i++) {
var slot = dataSlots.get(i);
var nbt = slot.serializeNBT(fullUpdate);
var nbt = slot.serializeNBT(true);
if (nbt == null)
continue;

Expand All @@ -105,15 +110,29 @@ private CompoundTag createDataSlotUpdate(boolean fullUpdate) {
dataList.add(slotTag);
}

if (!fullUpdate && dataList.isEmpty()) {
return null;
}

CompoundTag data = new CompoundTag();
data.put("Data", dataList);
return data;
}

@Nullable
private FriendlyByteBuf createBufferSlotUpdate() {
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
boolean empty = true;
for (int i = 0; i < dataSlots.size(); i++) {
NetworkDataSlot<?> networkDataSlot = dataSlots.get(i);
if (networkDataSlot.needsUpdate()) {
empty = false;
buf.writeInt(i);
networkDataSlot.writeBuffer(buf);
}
}
if (empty) {
return null;
}
return buf;
}

public void addDataSlot(NetworkDataSlot<?> slot) {
dataSlots.add(slot);
}
Expand All @@ -132,10 +151,11 @@ public <T> void clientUpdateSlot(@Nullable NetworkDataSlot<T> slot, T value) {
}

if (dataSlots.contains(slot)) {
CompoundTag updateData = new CompoundTag();
updateData.putInt("Index", dataSlots.indexOf(slot));
updateData.put("Data", slot.serializeValueNBT(value));
CoreNetwork.sendToServer(new C2SDataSlotChange(getBlockPos(), updateData));
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
buf.writeInt(dataSlots.indexOf(slot));
slot.toBuffer(buf, value);
CoreNetwork.sendToServer(new C2SDataSlotChange(getBlockPos(), buf));
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), Block.UPDATE_NEIGHBORS);
justliliandev marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -144,7 +164,7 @@ public <T> void clientUpdateSlot(@Nullable NetworkDataSlot<T> slot, T value) {
*/
@UseOnly(LogicalSide.SERVER)
public void sync() {
var syncData = createDataSlotUpdate(false);
var syncData = createBufferSlotUpdate();
if (syncData != null) {
CoreNetwork.sendToTracking(level.getChunkAt(getBlockPos()), new S2CDataSlotUpdate(getBlockPos(), syncData));
}
Expand All @@ -168,12 +188,34 @@ public void clientHandleDataSync(CompoundTag syncData) {
}
}

@UseOnly(LogicalSide.CLIENT)
public void clientHandleBufferSync(FriendlyByteBuf buf) {
boolean hasdata = true;
while (hasdata) { //read until we can't
int index = -1;
try {
index = buf.readInt();
} catch (Exception e) {
hasdata = false;
continue;
}
dataSlots.get(index).fromBuffer(buf);
}

for (Runnable task : afterDataSync) {
task.run();
}
}

@UseOnly(LogicalSide.SERVER)
public void serverHandleDataChange(CompoundTag data) {
if (data.contains("Index", Tag.TAG_INT) && data.contains("Data")) {
int slotIdx = data.getInt("Index");
dataSlots.get(slotIdx).fromNBT(data.get("Data"));
public void serverHandleBufferChange(FriendlyByteBuf buf) {
int index = -1;
try {
index = buf.readInt();
} catch (Exception e) {
throw new IllegalStateException("Invalid buffer was passed over the network to the server.");
}
dataSlots.get(index).fromBuffer(buf);
}

// endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.enderio.core.common.network;

import com.enderio.core.common.blockentity.EnderBlockEntity;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.ServerLevelAccessor;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.NetworkEvent;
Expand All @@ -21,16 +17,16 @@ public class C2SDataSlotChange implements Packet {

// You shouldn't really send null, but its "technically" valid.
@Nullable
private final CompoundTag updateData;
private final FriendlyByteBuf updateData;

public C2SDataSlotChange(BlockPos pos, CompoundTag updateData) {
public C2SDataSlotChange(BlockPos pos, FriendlyByteBuf updateData) {
this.pos = pos;
this.updateData = updateData;
}

public C2SDataSlotChange(FriendlyByteBuf buf) {
pos = buf.readBlockPos();
updateData = buf.readNbt();
updateData = new FriendlyByteBuf(buf.copy());
}

@Override
Expand All @@ -44,13 +40,13 @@ public void handle(NetworkEvent.Context context) {

BlockEntity be = level.getBlockEntity(pos);
if (be instanceof EnderBlockEntity enderBlockEntity) {
enderBlockEntity.serverHandleDataChange(updateData);
enderBlockEntity.serverHandleBufferChange(updateData);
}
}

protected void write(FriendlyByteBuf writeInto) {
writeInto.writeBlockPos(pos);
writeInto.writeNbt(updateData);
writeInto.writeBytes(updateData);
ferriarnus marked this conversation as resolved.
Show resolved Hide resolved
}

public static class Handler extends PacketHandler<C2SDataSlotChange> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.NetworkEvent;
Expand All @@ -19,16 +19,16 @@ public class S2CDataSlotUpdate implements Packet {

// You shouldn't really send null, but its "technically" valid.
@Nullable
private final CompoundTag slotData;
private final FriendlyByteBuf slotData;

public S2CDataSlotUpdate(BlockPos pos, CompoundTag slotData) {
public S2CDataSlotUpdate(BlockPos pos, FriendlyByteBuf buf) {
this.pos = pos;
this.slotData = slotData;
this.slotData = buf;
}

public S2CDataSlotUpdate(FriendlyByteBuf buf) {
pos = buf.readBlockPos();
slotData = buf.readNbt();
slotData = new FriendlyByteBuf(buf.copy());
}

@Override
Expand All @@ -42,14 +42,14 @@ public void handle(NetworkEvent.Context context) {
if (level != null) {
BlockEntity be = level.getBlockEntity(pos);
if (be instanceof EnderBlockEntity enderBlockEntity) {
enderBlockEntity.clientHandleDataSync(slotData);
enderBlockEntity.clientHandleBufferSync(slotData);
}
}
}

protected void write(FriendlyByteBuf writeInto) {
writeInto.writeBlockPos(pos);
writeInto.writeNbt(slotData);
writeInto.writeBytes(slotData);
}

public static class Handler extends PacketHandler<S2CDataSlotUpdate> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.enderio.core.common.network.slot;

import com.enderio.core.EnderCore;
import net.minecraft.nbt.ByteTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;

import java.util.function.Consumer;
import java.util.function.Supplier;
Expand All @@ -26,4 +26,18 @@ protected Boolean valueFromNBT(Tag nbt) {
throw new IllegalStateException("Invalid boolean tag was passed over the network.");
}
}

@Override
public void toBuffer(FriendlyByteBuf buf, Boolean value) {
buf.writeBoolean(value);
}

@Override
public Boolean valueFromBuffer(FriendlyByteBuf buf) {
try {
return buf.readBoolean();
} catch (Exception e) {
throw new IllegalStateException("Invalid boolean buffer was passed over the network.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.nbt.IntTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;

import java.util.function.Consumer;
import java.util.function.Supplier;
Expand All @@ -25,7 +26,21 @@ protected T valueFromNBT(Tag nbt) {
if (nbt instanceof IntTag intTag) {
return enumClass.getEnumConstants()[intTag.getAsInt()];
} else {
throw new IllegalStateException("Invalid int tag was passed over the network.");
throw new IllegalStateException("Invalid enum/int tag was passed over the network.");
}
}

@Override
public void toBuffer(FriendlyByteBuf buf, T value) {
buf.writeInt(value.ordinal());
}

@Override
public T valueFromBuffer(FriendlyByteBuf buf) {
try {
return enumClass.getEnumConstants()[buf.readInt()];
} catch (Exception e) {
throw new IllegalStateException("Invalid enum/int buffer was passed over the network.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.nbt.FloatTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;

import java.util.function.Consumer;
import java.util.function.Supplier;
Expand All @@ -25,4 +26,18 @@ protected Float valueFromNBT(Tag nbt) {
throw new IllegalStateException("Invalid float tag was passed over the network.");
}
}

@Override
public void toBuffer(FriendlyByteBuf buf, Float value) {
buf.writeFloat(value);
}

@Override
public Float valueFromBuffer(FriendlyByteBuf buf) {
try {
return buf.readFloat();
} catch (Exception e) {
throw new IllegalStateException("Invalid float buffer was passed over the network.");
}
}
}