Skip to content

Commit

Permalink
Add update packet
Browse files Browse the repository at this point in the history
  • Loading branch information
gigabit101 committed Aug 22, 2023
1 parent 3c6c377 commit 8b88baf
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.gigabit101.rebornstorage.blockentities.BlockEntityMultiCrafter;
import net.gigabit101.rebornstorage.packet.PacketGui;
import net.gigabit101.rebornstorage.packet.PacketHandler;
import net.gigabit101.rebornstorage.packet.PacketRequestMultiblockUpdate;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
Expand Down Expand Up @@ -53,6 +54,11 @@ public InteractionResult use(BlockState blockState, Level level, BlockPos blockP

if (tile.getMultiblockController() != null)
{
if(!tile.getMultiblockController().isAssembled())
{
//Check with the server
PacketHandler.sendToServer(new PacketRequestMultiblockUpdate(blockPos));
}
if (!tile.getMultiblockController().isAssembled())
{
if (tile.getMultiblockController().getLastValidationException() != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,18 @@ public boolean isAssembled()
return this.assemblyState == AssemblyState.Assembled;
}

public void setAssembled(boolean assembled)
{
if(assembled)
{
this.assemblyState = AssemblyState.Assembled;
}
else
{
this.assemblyState = AssemblyState.Disassembled;
}
}

private void selectNewReferenceCoord()
{
IMultiblockPart theChosenOne = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.gigabit101.rebornstorage.core.multiblock.MultiblockControllerBase;
import net.gigabit101.rebornstorage.core.multiblock.rectangular.RectangularMultiblockControllerBase;
import net.gigabit101.rebornstorage.init.ModBlocks;
import net.gigabit101.rebornstorage.packet.PacketHandler;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.Level;
import net.minecraftforge.items.ItemStackHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public static void register()
int disc = 0;
HANDLER.registerMessage(disc++, PacketGui.class, PacketGui::encode, PacketGui::decode, PacketGui.Handler::handle);
HANDLER.registerMessage(disc++, PacketChangeMode.class, PacketChangeMode::encode, PacketChangeMode::decode, PacketChangeMode.Handler::handle);
HANDLER.registerMessage(disc++, PacketRequestMultiblockUpdate.class, PacketRequestMultiblockUpdate::encode, PacketRequestMultiblockUpdate::decode, PacketRequestMultiblockUpdate.Handler::handle);
HANDLER.registerMessage(disc++, PacketMultiblockUpdate.class, PacketMultiblockUpdate::encode, PacketMultiblockUpdate::decode, PacketMultiblockUpdate.Handler::handle);

}

public static void sendTo(Object msg, ServerPlayer player)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package net.gigabit101.rebornstorage.packet;

import net.gigabit101.rebornstorage.blockentities.BlockEntityMultiCrafter;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.network.NetworkEvent;

import java.util.function.Supplier;

public class PacketMultiblockUpdate
{
private final BlockPos blockPos;
private final boolean assembled;

public PacketMultiblockUpdate(BlockPos blockPos, boolean assembled)
{
this.blockPos = blockPos;
this.assembled = assembled;
}

public static void encode(PacketMultiblockUpdate packetGui, FriendlyByteBuf buf)
{
buf.writeBlockPos(packetGui.blockPos);
buf.writeBoolean(packetGui.assembled);
}

public static PacketMultiblockUpdate decode(FriendlyByteBuf buf)
{
return new PacketMultiblockUpdate(buf.readBlockPos(), buf.readBoolean());
}

public static class Handler
{
public static void handle(final PacketMultiblockUpdate message, Supplier<NetworkEvent.Context> ctx)
{
ctx.get().enqueueWork(() ->
{
ServerPlayer player = ctx.get().getSender();
if (player == null) return;

BlockEntity blockEntity = player.level().getBlockEntity(message.blockPos);
if(blockEntity != null && blockEntity instanceof BlockEntityMultiCrafter blockEntityMultiCrafter)
{
blockEntityMultiCrafter.getMultiBlock().setAssembled(message.assembled);
}
});
ctx.get().setPacketHandled(true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package net.gigabit101.rebornstorage.packet;

import net.gigabit101.rebornstorage.blockentities.BlockEntityMultiCrafter;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.network.NetworkEvent;

import java.util.function.Supplier;

public class PacketRequestMultiblockUpdate
{
private final BlockPos blockPos;

public PacketRequestMultiblockUpdate(BlockPos blockPos)
{
this.blockPos = blockPos;
}

public static void encode(PacketRequestMultiblockUpdate packetGui, FriendlyByteBuf buf)
{
buf.writeBlockPos(packetGui.blockPos);
}

public static PacketRequestMultiblockUpdate decode(FriendlyByteBuf buf)
{
return new PacketRequestMultiblockUpdate(buf.readBlockPos());
}

public static class Handler
{
public static void handle(final PacketRequestMultiblockUpdate message, Supplier<NetworkEvent.Context> ctx)
{
ctx.get().enqueueWork(() ->
{
ServerPlayer player = ctx.get().getSender();
if (player == null) return;

BlockEntity blockEntity = player.level().getBlockEntity(message.blockPos);
if(blockEntity != null && blockEntity instanceof BlockEntityMultiCrafter blockEntityMultiCrafter)
{
boolean assembled = blockEntityMultiCrafter.getMultiBlock().isAssembled();
PacketHandler.sendTo(new PacketMultiblockUpdate(message.blockPos, assembled), player);
}
});
ctx.get().setPacketHandled(true);
}
}
}

0 comments on commit 8b88baf

Please sign in to comment.