Port to new fabric networking api

This commit is contained in:
modmuss50 2020-12-20 22:27:01 +00:00
parent 2ea779acd1
commit 8add71f5ac
2 changed files with 78 additions and 90 deletions

View file

@ -24,17 +24,12 @@
package techreborn.packets; package techreborn.packets;
import net.fabricmc.fabric.api.network.PacketContext;
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items; import net.minecraft.item.Items;
import net.minecraft.network.Packet;
import net.minecraft.network.listener.ServerPlayPacketListener;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import reborncore.common.network.ExtendedPacketBuffer; import reborncore.common.network.IdentifiedPacket;
import reborncore.common.network.NetworkManager; import reborncore.common.network.NetworkManager;
import techreborn.TechReborn; import techreborn.TechReborn;
import techreborn.blockentity.machine.iron.IronFurnaceBlockEntity; import techreborn.blockentity.machine.iron.IronFurnaceBlockEntity;
@ -47,8 +42,6 @@ import techreborn.blockentity.storage.item.StorageUnitBaseBlockEntity;
import techreborn.config.TechRebornConfig; import techreborn.config.TechRebornConfig;
import techreborn.init.TRContent; import techreborn.init.TRContent;
import java.util.function.BiConsumer;
public class ServerboundPackets { public class ServerboundPackets {
public static final Identifier AESU = new Identifier(TechReborn.MOD_ID, "aesu"); public static final Identifier AESU = new Identifier(TechReborn.MOD_ID, "aesu");
@ -61,80 +54,79 @@ public class ServerboundPackets {
public static final Identifier EXPERIENCE = new Identifier(TechReborn.MOD_ID, "experience"); public static final Identifier EXPERIENCE = new Identifier(TechReborn.MOD_ID, "experience");
public static void init() { public static void init() {
registerPacketHandler(AESU, (extendedPacketBuffer, context) -> { NetworkManager.registerServerBoundHandler(AESU, (server, player, handler, buf, responseSender) -> {
BlockPos pos = extendedPacketBuffer.readBlockPos(); BlockPos pos = buf.readBlockPos();
int buttonID = extendedPacketBuffer.readInt(); int buttonID = buf.readInt();
boolean shift = extendedPacketBuffer.readBoolean(); boolean shift = buf.readBoolean();
boolean ctrl = extendedPacketBuffer.readBoolean(); boolean ctrl = buf.readBoolean();
context.getTaskQueue().execute(() -> { server.execute(() -> {
BlockEntity blockEntity = context.getPlayer().world.getBlockEntity(pos); BlockEntity blockEntity = player.world.getBlockEntity(pos);
if (blockEntity instanceof AdjustableSUBlockEntity) { if (blockEntity instanceof AdjustableSUBlockEntity) {
((AdjustableSUBlockEntity) blockEntity).handleGuiInputFromClient(buttonID, shift, ctrl); ((AdjustableSUBlockEntity) blockEntity).handleGuiInputFromClient(buttonID, shift, ctrl);
} }
}); });
}); });
registerPacketHandler(AUTO_CRAFTING_LOCK, (extendedPacketBuffer, context) -> { NetworkManager.registerServerBoundHandler(AUTO_CRAFTING_LOCK, (server, player, handler, buf, responseSender) -> {
BlockPos machinePos = extendedPacketBuffer.readBlockPos(); BlockPos machinePos = buf.readBlockPos();
boolean locked = extendedPacketBuffer.readBoolean(); boolean locked = buf.readBoolean();
context.getTaskQueue().execute(() -> { server.execute(() -> {
BlockEntity BlockEntity = context.getPlayer().world.getBlockEntity(machinePos); BlockEntity BlockEntity = player.world.getBlockEntity(machinePos);
if (BlockEntity instanceof AutoCraftingTableBlockEntity) { if (BlockEntity instanceof AutoCraftingTableBlockEntity) {
((AutoCraftingTableBlockEntity) BlockEntity).locked = locked; ((AutoCraftingTableBlockEntity) BlockEntity).locked = locked;
} }
}); });
}); });
registerPacketHandler(FUSION_CONTROL_SIZE, (extendedPacketBuffer, context) -> { NetworkManager.registerServerBoundHandler(FUSION_CONTROL_SIZE, (server, player, handler, buf, responseSender) -> {
int sizeDelta = extendedPacketBuffer.readInt(); int sizeDelta = buf.readInt();
BlockPos pos = extendedPacketBuffer.readBlockPos(); BlockPos pos = buf.readBlockPos();
context.getTaskQueue().execute(() -> { server.execute(() -> {
BlockEntity blockEntity = context.getPlayer().world.getBlockEntity(pos); BlockEntity blockEntity = player.world.getBlockEntity(pos);
if (blockEntity instanceof FusionControlComputerBlockEntity) { if (blockEntity instanceof FusionControlComputerBlockEntity) {
((FusionControlComputerBlockEntity) blockEntity).changeSize(sizeDelta); ((FusionControlComputerBlockEntity) blockEntity).changeSize(sizeDelta);
} }
}); });
}); });
registerPacketHandler(ROLLING_MACHINE_LOCK, (extendedPacketBuffer, context) -> { NetworkManager.registerServerBoundHandler(ROLLING_MACHINE_LOCK, (server, player, handler, buf, responseSender) -> {
BlockPos machinePos = extendedPacketBuffer.readBlockPos(); BlockPos machinePos = buf.readBlockPos();
boolean locked = extendedPacketBuffer.readBoolean(); boolean locked = buf.readBoolean();
context.getTaskQueue().execute(() -> { server.execute(() -> {
BlockEntity BlockEntity = context.getPlayer().world.getBlockEntity(machinePos); BlockEntity BlockEntity = player.world.getBlockEntity(machinePos);
if (BlockEntity instanceof RollingMachineBlockEntity) { if (BlockEntity instanceof RollingMachineBlockEntity) {
((RollingMachineBlockEntity) BlockEntity).locked = locked; ((RollingMachineBlockEntity) BlockEntity).locked = locked;
} }
}); });
}); });
registerPacketHandler(STORAGE_UNIT_LOCK, (extendedPacketBuffer, context) -> { NetworkManager.registerServerBoundHandler(STORAGE_UNIT_LOCK, (server, player, handler, buf, responseSender) -> {
BlockPos machinePos = extendedPacketBuffer.readBlockPos(); BlockPos machinePos = buf.readBlockPos();
boolean locked = extendedPacketBuffer.readBoolean(); boolean locked = buf.readBoolean();
context.getTaskQueue().execute(() -> { server.execute(() -> {
BlockEntity BlockEntity = context.getPlayer().world.getBlockEntity(machinePos); BlockEntity BlockEntity = player.world.getBlockEntity(machinePos);
if (BlockEntity instanceof StorageUnitBaseBlockEntity) { if (BlockEntity instanceof StorageUnitBaseBlockEntity) {
((StorageUnitBaseBlockEntity) BlockEntity).setLocked(locked); ((StorageUnitBaseBlockEntity) BlockEntity).setLocked(locked);
} }
}); });
}); });
registerPacketHandler(REFUND, (extendedPacketBuffer, context) -> { NetworkManager.registerServerBoundHandler(REFUND, (server, player, handler, buf, responseSender) -> {
if (!TechRebornConfig.allowManualRefund) { if (!TechRebornConfig.allowManualRefund) {
return; return;
} }
context.getTaskQueue().execute(() -> { server.execute(() -> {
PlayerEntity playerMP = context.getPlayer(); for (int i = 0; i < player.inventory.size(); i++) {
for (int i = 0; i < playerMP.inventory.size(); i++) { ItemStack stack = player.inventory.getStack(i);
ItemStack stack = playerMP.inventory.getStack(i);
if (stack.getItem() == TRContent.MANUAL) { if (stack.getItem() == TRContent.MANUAL) {
playerMP.inventory.removeStack(i); player.inventory.removeStack(i);
playerMP.inventory.insertStack(new ItemStack(Items.BOOK)); player.inventory.insertStack(new ItemStack(Items.BOOK));
playerMP.inventory.insertStack(TRContent.Ingots.REFINED_IRON.getStack()); player.inventory.insertStack(TRContent.Ingots.REFINED_IRON.getStack());
return; return;
} }
} }
@ -142,88 +134,84 @@ public class ServerboundPackets {
}); });
registerPacketHandler(CHUNKLOADER, (extendedPacketBuffer, context) -> { NetworkManager.registerServerBoundHandler(CHUNKLOADER, (server, player, handler, buf, responseSender) -> {
BlockPos pos = extendedPacketBuffer.readBlockPos(); BlockPos pos = buf.readBlockPos();
int buttonID = extendedPacketBuffer.readInt(); int buttonID = buf.readInt();
boolean sync = extendedPacketBuffer.readBoolean(); boolean sync = buf.readBoolean();
context.getTaskQueue().execute(() -> { server.execute(() -> {
BlockEntity blockEntity = context.getPlayer().world.getBlockEntity(pos); BlockEntity blockEntity = player.world.getBlockEntity(pos);
if (blockEntity instanceof ChunkLoaderBlockEntity) { if (blockEntity instanceof ChunkLoaderBlockEntity) {
((ChunkLoaderBlockEntity) blockEntity).handleGuiInputFromClient(buttonID, sync ? context.getPlayer() : null); ((ChunkLoaderBlockEntity) blockEntity).handleGuiInputFromClient(buttonID, sync ? player : null);
} }
}); });
}); });
registerPacketHandler(EXPERIENCE, (extendedPacketBuffer, context) -> { NetworkManager.registerServerBoundHandler(EXPERIENCE, (server, player, handler, buf, responseSender) -> {
BlockPos pos = extendedPacketBuffer.readBlockPos(); BlockPos pos = buf.readBlockPos();
context.getTaskQueue().execute(() -> { server.execute(() -> {
BlockEntity blockEntity = context.getPlayer().world.getBlockEntity(pos); BlockEntity blockEntity = player.world.getBlockEntity(pos);
if (blockEntity instanceof IronFurnaceBlockEntity) { if (blockEntity instanceof IronFurnaceBlockEntity) {
((IronFurnaceBlockEntity) blockEntity).handleGuiInputFromClient(context.getPlayer()); ((IronFurnaceBlockEntity) blockEntity).handleGuiInputFromClient(player);
} }
}); });
}); });
} }
private static void registerPacketHandler(Identifier identifier, BiConsumer<ExtendedPacketBuffer, PacketContext> consumer) { public static IdentifiedPacket createPacketAesu(int buttonID, boolean shift, boolean ctrl, AdjustableSUBlockEntity blockEntity) {
ServerSidePacketRegistry.INSTANCE.register(identifier, (packetContext, packetByteBuf) -> consumer.accept(new ExtendedPacketBuffer(packetByteBuf), packetContext)); return NetworkManager.createServerBoundPacket(AESU, buf -> {
} buf.writeBlockPos(blockEntity.getPos());
buf.writeInt(buttonID);
public static Packet<ServerPlayPacketListener> createPacketAesu(int buttonID, boolean shift, boolean ctrl, AdjustableSUBlockEntity blockEntity) { buf.writeBoolean(shift);
return NetworkManager.createServerBoundPacket(AESU, extendedPacketBuffer -> { buf.writeBoolean(ctrl);
extendedPacketBuffer.writeBlockPos(blockEntity.getPos());
extendedPacketBuffer.writeInt(buttonID);
extendedPacketBuffer.writeBoolean(shift);
extendedPacketBuffer.writeBoolean(ctrl);
}); });
} }
public static Packet<ServerPlayPacketListener> createPacketAutoCraftingTableLock(AutoCraftingTableBlockEntity machine, boolean locked) { public static IdentifiedPacket createPacketAutoCraftingTableLock(AutoCraftingTableBlockEntity machine, boolean locked) {
return NetworkManager.createServerBoundPacket(AUTO_CRAFTING_LOCK, extendedPacketBuffer -> { return NetworkManager.createServerBoundPacket(AUTO_CRAFTING_LOCK, buf -> {
extendedPacketBuffer.writeBlockPos(machine.getPos()); buf.writeBlockPos(machine.getPos());
extendedPacketBuffer.writeBoolean(locked); buf.writeBoolean(locked);
}); });
} }
public static Packet<ServerPlayPacketListener> createPacketFusionControlSize(int sizeDelta, BlockPos pos) { public static IdentifiedPacket createPacketFusionControlSize(int sizeDelta, BlockPos pos) {
return NetworkManager.createServerBoundPacket(FUSION_CONTROL_SIZE, extendedPacketBuffer -> { return NetworkManager.createServerBoundPacket(FUSION_CONTROL_SIZE, buf -> {
extendedPacketBuffer.writeInt(sizeDelta); buf.writeInt(sizeDelta);
extendedPacketBuffer.writeBlockPos(pos); buf.writeBlockPos(pos);
}); });
} }
public static Packet<ServerPlayPacketListener> createPacketRollingMachineLock(RollingMachineBlockEntity machine, boolean locked) { public static IdentifiedPacket createPacketRollingMachineLock(RollingMachineBlockEntity machine, boolean locked) {
return NetworkManager.createServerBoundPacket(ROLLING_MACHINE_LOCK, extendedPacketBuffer -> { return NetworkManager.createServerBoundPacket(ROLLING_MACHINE_LOCK, buf -> {
extendedPacketBuffer.writeBlockPos(machine.getPos()); buf.writeBlockPos(machine.getPos());
extendedPacketBuffer.writeBoolean(locked); buf.writeBoolean(locked);
}); });
} }
public static Packet<ServerPlayPacketListener> createPacketStorageUnitLock(StorageUnitBaseBlockEntity machine, boolean locked) { public static IdentifiedPacket createPacketStorageUnitLock(StorageUnitBaseBlockEntity machine, boolean locked) {
return NetworkManager.createServerBoundPacket(STORAGE_UNIT_LOCK, extendedPacketBuffer -> { return NetworkManager.createServerBoundPacket(STORAGE_UNIT_LOCK, buf -> {
extendedPacketBuffer.writeBlockPos(machine.getPos()); buf.writeBlockPos(machine.getPos());
extendedPacketBuffer.writeBoolean(locked); buf.writeBoolean(locked);
}); });
} }
public static Packet<ServerPlayPacketListener> createRefundPacket() { public static IdentifiedPacket createRefundPacket() {
return NetworkManager.createServerBoundPacket(REFUND, extendedPacketBuffer -> { return NetworkManager.createServerBoundPacket(REFUND, extendedPacketBuffer -> {
}); });
} }
public static Packet<ServerPlayPacketListener> createPacketChunkloader(int buttonID, ChunkLoaderBlockEntity blockEntity, boolean sync) { public static IdentifiedPacket createPacketChunkloader(int buttonID, ChunkLoaderBlockEntity blockEntity, boolean sync) {
return NetworkManager.createServerBoundPacket(CHUNKLOADER, extendedPacketBuffer -> { return NetworkManager.createServerBoundPacket(CHUNKLOADER, buf -> {
extendedPacketBuffer.writeBlockPos(blockEntity.getPos()); buf.writeBlockPos(blockEntity.getPos());
extendedPacketBuffer.writeInt(buttonID); buf.writeInt(buttonID);
extendedPacketBuffer.writeBoolean(sync); buf.writeBoolean(sync);
}); });
} }
public static Packet<ServerPlayPacketListener> createPacketExperience(IronFurnaceBlockEntity blockEntity) { public static IdentifiedPacket createPacketExperience(IronFurnaceBlockEntity blockEntity) {
return NetworkManager.createServerBoundPacket(EXPERIENCE, extendedPacketBuffer -> extendedPacketBuffer.writeBlockPos(blockEntity.getPos())); return NetworkManager.createServerBoundPacket(EXPERIENCE, extendedPacketBuffer -> extendedPacketBuffer.writeBlockPos(blockEntity.getPos()));
} }

View file

@ -28,9 +28,9 @@
}, },
"depends": { "depends": {
"fabricloader": ">=0.6.3", "fabricloader": ">=0.6.3",
"fabric": "*", "fabric": ">=0.28.3",
"reborncore": "*", "reborncore": "*",
"team_reborn_energy": ">=0.1.0", "team_reborn_energy": ">=0.1.1",
"fabric-biome-api-v1": ">=3.0.0" "fabric-biome-api-v1": ">=3.0.0"
}, },
"authors": [ "authors": [