Skip to content

Commit

Permalink
Packet for updating block entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Jul 12, 2023
1 parent e528a36 commit 1b933e7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.fusionflux.portalcubed.blocks.blockentities;

import com.fusionflux.portalcubed.blocks.PortalCubedBlocks;
import com.fusionflux.portalcubed.listeners.NbtSyncable;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
import net.minecraft.commands.CommandRuntimeException;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.Packet;
Expand All @@ -14,14 +17,15 @@
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
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;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CatapultBlockEntity extends BlockEntity implements ExtendedScreenHandlerFactory {
public class CatapultBlockEntity extends BlockEntity implements ExtendedScreenHandlerFactory, NbtSyncable {
private double destX;
private double destY;
private double destZ;
Expand Down Expand Up @@ -54,6 +58,32 @@ protected void saveAdditional(CompoundTag nbt) {
nbt.putDouble("Angle", angle);
}

@Override
public void syncNbt(CompoundTag nbt, ServerPlayer player) throws CommandRuntimeException {
destX = getDouble(nbt, player, "DestX", destX);
destY = getDouble(nbt, player, "DestY", destY);
destZ = getDouble(nbt, player, "DestZ", destZ);
angle = getDouble(nbt, player, "Angle", angle);
updateListeners();
}

private static double getDouble(CompoundTag nbt, ServerPlayer player, String key, double defaultValue) throws CommandRuntimeException {
if (!nbt.contains(key, Tag.TAG_DOUBLE)) {
return defaultValue;
}
final double value = nbt.getDouble(key);
if (!Double.isFinite(value)) {
throw new CommandRuntimeException(Component.translatable("portalcubed.catapult.invalidDouble", value));
}
return value;
}

public void updateListeners() {
setChanged();
assert level != null;
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), Block.UPDATE_ALL);
}

@NotNull
@Override
public CompoundTag getUpdateTag() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.fusionflux.portalcubed.listeners;

import net.minecraft.commands.CommandRuntimeException;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerPlayer;

public interface NbtSyncable {
void syncNbt(CompoundTag nbt, ServerPlayer player) throws CommandRuntimeException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
import com.fusionflux.portalcubed.entity.CorePhysicsEntity;
import com.fusionflux.portalcubed.entity.TurretEntity;
import com.fusionflux.portalcubed.items.PortalCubedItems;
import com.fusionflux.portalcubed.listeners.NbtSyncable;
import com.fusionflux.portalcubed.sound.PortalCubedSounds;
import com.fusionflux.portalcubed.util.AdvancedEntityRaycast;
import com.fusionflux.portalcubed.util.ClickHandlingItem;
import com.fusionflux.portalcubed.util.PortalCubedComponents;
import com.fusionflux.portalcubed.util.PortalDirectionUtils;
import net.minecraft.commands.CommandRuntimeException;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
Expand Down Expand Up @@ -52,6 +56,7 @@ public class PortalCubedServerPackets {
public static final ResourceLocation LEFT_CLICK = id("left_click");
public static final ResourceLocation RIGHT_CLICK = id("right_click");
public static final ResourceLocation SYNC_SHOOTER_ROT = id("sync_shooter_rot");
public static final ResourceLocation NBT_SYNC = id("nbt_sync");

public static void onGrabKeyPressed(MinecraftServer server, ServerPlayer player, @SuppressWarnings("unused") ServerGamePacketListenerImpl handler, @SuppressWarnings("unused") FriendlyByteBuf buf, @SuppressWarnings("unused") PacketSender sender) {

Expand Down Expand Up @@ -189,5 +194,32 @@ public static void registerPackets() {
player.yHeadRotO = yRot;
});
});
ServerPlayNetworking.registerGlobalReceiver(NBT_SYNC, (server, player, handler, buf, responseSender) -> {
final BlockPos pos = buf.readBlockPos();
final CompoundTag nbt = buf.readNbt();
server.execute(() -> {
final Vec3 originPos = Vec3.atCenterOf(pos);
if (player.position().distanceToSqr(originPos) > 100) {
PortalCubed.LOGGER.warn(
"Player {} tried to update distant block entity ({})",
player, player.position().distanceTo(originPos)
);
return;
}
if (player.level().getBlockState(pos) instanceof NbtSyncable syncable) {
try {
syncable.syncNbt(nbt, player);
} catch (CommandRuntimeException e) {
player.connection.disconnect(e.getComponent());
PortalCubed.LOGGER.warn("Player {} sent invalid sync NBT: {}", player, e.getLocalizedMessage());
} catch (Exception e) {
player.connection.disconnect(Component.translatable("disconnect.genericReason", e.getLocalizedMessage()));
PortalCubed.LOGGER.error("Error in handling nbt_sync", e);
}
} else {
PortalCubed.LOGGER.warn("{} failed to update non-existent block entity at {}", player, pos);
}
});
});
}
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/portalcubed/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -494,5 +494,6 @@
"portalcubed.catapult.destX": "Destination X",
"portalcubed.catapult.destY": "Destination Y",
"portalcubed.catapult.destZ": "Destination Z",
"portalcubed.catapult.angle": "Launch Angle"
"portalcubed.catapult.angle": "Launch Angle",
"portalcubed.catapult.invalidDouble": "Invalid double: %s"
}

0 comments on commit 1b933e7

Please sign in to comment.