Skip to content

Commit

Permalink
fix aphorism tile changes not sync'ing to tracking players
Browse files Browse the repository at this point in the history
  • Loading branch information
desht committed Aug 28, 2024
1 parent bce5562 commit 90e3b12
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private static int nextId() {

public static void init() {
registerMessage(PacketAphorismTileUpdate.class,
PacketAphorismTileUpdate::toBytes, PacketAphorismTileUpdate::new, PacketAphorismTileUpdate::handle, PLAY_TO_SERVER);
PacketAphorismTileUpdate::toBytes, PacketAphorismTileUpdate::new, PacketAphorismTileUpdate::handle);
registerMessage(PacketChangeGPSToolCoordinate.class,
PacketChangeGPSToolCoordinate::toBytes, PacketChangeGPSToolCoordinate::new, PacketChangeGPSToolCoordinate::handle, PLAY_TO_SERVER);
registerMessage(PacketUpdateGPSAreaTool.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,23 @@

package me.desht.pneumaticcraft.common.network;

import me.desht.pneumaticcraft.client.util.ClientUtils;
import me.desht.pneumaticcraft.common.block.AphorismTileBlock;
import me.desht.pneumaticcraft.common.block.entity.AphorismTileBlockEntity;
import me.desht.pneumaticcraft.common.util.PneumaticCraftUtils;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.NetworkEvent;

import java.util.Arrays;
import java.util.Objects;
import java.util.function.Supplier;

/**
* Received on: SERVER
* Received on: BOTH
* Sent by the client after editing an Aphorism Tile
* Sent by the server to all tracking clients to update them
*/
public class PacketAphorismTileUpdate extends LocationIntPacket {

Expand All @@ -39,6 +42,7 @@ public class PacketAphorismTileUpdate extends LocationIntPacket {
private final int textRotation;
private final byte margin;
private final boolean invis;
private final int playerId;

public PacketAphorismTileUpdate(FriendlyByteBuf buffer) {
super(buffer);
Expand All @@ -51,15 +55,21 @@ public PacketAphorismTileUpdate(FriendlyByteBuf buffer) {
}
margin = buffer.readByte();
invis = buffer.readBoolean();
playerId = buffer.readInt();
}

public PacketAphorismTileUpdate(AphorismTileBlockEntity tile) {
this(tile, null);
}

public PacketAphorismTileUpdate(AphorismTileBlockEntity tile, Player player) {
super(tile.getBlockPos());

text = tile.getTextLines();
textRotation = tile.textRotation;
margin = tile.getMarginSize();
invis = tile.getBlockState().getValue(AphorismTileBlock.INVISIBLE);
playerId = player == null ? 0 : player.getId();
}

@Override
Expand All @@ -71,21 +81,39 @@ public void toBytes(FriendlyByteBuf buffer) {
Arrays.stream(text).forEach(s -> buffer.writeUtf(s, MAX_LENGTH));
buffer.writeByte(margin);
buffer.writeBoolean(invis);
buffer.writeInt(playerId);
}

public void handle(Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
Player player = Objects.requireNonNull(ctx.get().getSender());
if (PneumaticCraftUtils.canPlayerReach(player, pos)) {
PneumaticCraftUtils.getTileEntityAt(player.level(), pos, AphorismTileBlockEntity.class).ifPresent(te -> {
te.setTextLines(text, false);
te.textRotation = textRotation;
te.setMarginSize(margin);
te.setInvisible(invis);
});
if (ctx.get().getDirection() == NetworkDirection.PLAY_TO_CLIENT) {
// client-side; just update the tile (as long as the packet isn't from us)
Player player = ClientUtils.getClientPlayer();
if (playerId != player.getId()) {
updateAphorismTile(player);
}
} else {
// server-side; also send response packet to all players tracking the tile
Player player = Objects.requireNonNull(ctx.get().getSender());
if (PneumaticCraftUtils.canPlayerReach(player, pos)) {
AphorismTileBlockEntity tile = updateAphorismTile(player);
if (tile != null) {
NetworkHandler.sendToAllTracking(new PacketAphorismTileUpdate(tile, player), player.level(), pos);
}
}
}
});
ctx.get().setPacketHandled(true);
}

private AphorismTileBlockEntity updateAphorismTile(Player player) {
return PneumaticCraftUtils.getTileEntityAt(player.level(), pos, AphorismTileBlockEntity.class).map(te -> {
te.setTextLines(text, false);
te.textRotation = textRotation;
te.setMarginSize(margin);
te.setInvisible(invis);
return te;
}).orElse(null);
}

}

0 comments on commit 90e3b12

Please sign in to comment.