From d99ba1105fa0d7a2baee7d2bb6d640050a306947 Mon Sep 17 00:00:00 2001 From: Up Date: Wed, 3 Jan 2024 11:07:18 +0100 Subject: [PATCH] add interaction permission checks (closes #39) --- .../common/blocks/BackpackBlock.java | 11 ++++++++++- .../common/network/EquipBackpackPacket.java | 19 +++++++++++++------ .../common/network/PlaceBackpackPacket.java | 9 +++++++++ .../assets/camsbackpacks/lang/en_us.json | 5 +++++ 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/main/java/dev/cammiescorner/camsbackpacks/common/blocks/BackpackBlock.java b/src/main/java/dev/cammiescorner/camsbackpacks/common/blocks/BackpackBlock.java index a870284..51dc3e3 100644 --- a/src/main/java/dev/cammiescorner/camsbackpacks/common/blocks/BackpackBlock.java +++ b/src/main/java/dev/cammiescorner/camsbackpacks/common/blocks/BackpackBlock.java @@ -2,9 +2,12 @@ import dev.cammiescorner.camsbackpacks.common.blocks.entities.BackpackBlockEntity; import dev.cammiescorner.camsbackpacks.core.BackpacksConfig; +import net.minecraft.ChatFormatting; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.Component; +import net.minecraft.server.level.ServerPlayer; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.world.*; @@ -68,6 +71,12 @@ public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable L @Override public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { if (!world.isClientSide()) { + if(!world.mayInteract(player, pos)) { + ((ServerPlayer) player).sendSystemMessage(Component.translatable("error.camsbackpacks.permission_use").withStyle(ChatFormatting.RED), true); + return InteractionResult.FAIL; + } + + if (BackpacksConfig.sneakPlaceBackpack && player.isShiftKeyDown() && player.getItemBySlot(EquipmentSlot.CHEST).isEmpty()) { if (world.getBlockEntity(pos) instanceof BackpackBlockEntity blockEntity) { ItemStack stack = new ItemStack(this); @@ -92,7 +101,7 @@ public InteractionResult use(BlockState state, Level world, BlockPos pos, Player } } - return InteractionResult.SUCCESS; + return InteractionResult.sidedSuccess(world.isClientSide()); } @Override diff --git a/src/main/java/dev/cammiescorner/camsbackpacks/common/network/EquipBackpackPacket.java b/src/main/java/dev/cammiescorner/camsbackpacks/common/network/EquipBackpackPacket.java index c69efe2..ea3ee86 100644 --- a/src/main/java/dev/cammiescorner/camsbackpacks/common/network/EquipBackpackPacket.java +++ b/src/main/java/dev/cammiescorner/camsbackpacks/common/network/EquipBackpackPacket.java @@ -4,12 +4,14 @@ import dev.cammiescorner.camsbackpacks.common.blocks.BackpackBlock; import dev.cammiescorner.camsbackpacks.common.blocks.entities.BackpackBlockEntity; import dev.cammiescorner.camsbackpacks.common.items.BackpackItem; -import dev.cammiescorner.camsbackpacks.common.screen.BackpackScreenHandler; import dev.cammiescorner.camsbackpacks.core.util.BackpackHelper; import io.netty.buffer.Unpooled; +import net.minecraft.ChatFormatting; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; @@ -22,7 +24,6 @@ import net.minecraft.world.level.Level; import net.minecraft.world.level.block.state.properties.BlockStateProperties; import org.quiltmc.qsl.networking.api.PacketSender; -import org.quiltmc.qsl.networking.api.PlayerLookup; import org.quiltmc.qsl.networking.api.client.ClientPlayNetworking; public class EquipBackpackPacket { @@ -42,6 +43,15 @@ public static void handle(MinecraftServer server, ServerPlayer player, ServerGam server.execute(() -> { Level world = player.level(); + if (!world.mayInteract(player, pos)) { + player.closeContainer(); + MutableComponent message = isBlockEntity + ? Component.translatable("error.camsbackpacks.permission_pickup_at") + : Component.translatable("error.camsbackpacks.permission_place_at"); + player.sendSystemMessage(message.withStyle(ChatFormatting.RED), true); + return; + } + if (isBlockEntity) { if (world.getBlockEntity(pos) instanceof BackpackBlockEntity blockEntity) { ItemStack stack = new ItemStack(world.getBlockState(pos).getBlock().asItem()); @@ -55,10 +65,7 @@ public static void handle(MinecraftServer server, ServerPlayer player, ServerGam stack.setHoverName(blockEntity.getName()); world.destroyBlock(pos, false, player); - PlayerLookup.tracking(blockEntity).forEach(playerEntity -> { - if (playerEntity.containerMenu instanceof BackpackScreenHandler handler && handler.blockPos.equals(pos)) - playerEntity.closeContainer(); - }); + } } else { ItemStack stack = player.getItemBySlot(EquipmentSlot.CHEST); diff --git a/src/main/java/dev/cammiescorner/camsbackpacks/common/network/PlaceBackpackPacket.java b/src/main/java/dev/cammiescorner/camsbackpacks/common/network/PlaceBackpackPacket.java index f5d0a11..43e2231 100644 --- a/src/main/java/dev/cammiescorner/camsbackpacks/common/network/PlaceBackpackPacket.java +++ b/src/main/java/dev/cammiescorner/camsbackpacks/common/network/PlaceBackpackPacket.java @@ -6,8 +6,10 @@ import dev.cammiescorner.camsbackpacks.common.items.BackpackItem; import dev.cammiescorner.camsbackpacks.core.util.BackpackHelper; import io.netty.buffer.Unpooled; +import net.minecraft.ChatFormatting; import net.minecraft.core.BlockPos; 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; @@ -38,6 +40,13 @@ public static void handle(MinecraftServer server, ServerPlayer player, ServerGam server.execute(() -> { Level world = player.level(); BlockPos pos = BackpackHelper.isReplaceable(world, hitResult.getBlockPos()) ? hitResult.getBlockPos() : hitResult.getBlockPos().relative(hitResult.getDirection()); + + if(!world.mayInteract(player, pos)) { + player.closeContainer(); + player.sendSystemMessage(Component.translatable("error.camsbackpacks.permission_place_at").withStyle(ChatFormatting.RED), true); + return; + } + ItemStack stack = player.getItemBySlot(EquipmentSlot.CHEST); if (BackpackHelper.isReplaceable(world, pos)) { diff --git a/src/main/resources/assets/camsbackpacks/lang/en_us.json b/src/main/resources/assets/camsbackpacks/lang/en_us.json index e10839e..80c911b 100644 --- a/src/main/resources/assets/camsbackpacks/lang/en_us.json +++ b/src/main/resources/assets/camsbackpacks/lang/en_us.json @@ -43,6 +43,11 @@ "container.camsbackpacks.player_inv": "Inventory", "container.camsbackpacks.backpack_inv": "Backpack", + // Error Messages + "error.camsbackpacks.permission_place_at": "You do not have permission to place a backpack at this location!", + "error.camsbackpacks.permission_pickup_at": "You do not have permission to pick up a backpack at this location!", + "error.camsbackpacks.permission_use": "You do not have permission to use this backpack!", + // Config "camsbackpacks.midnightconfig.title": "CamsBackpacks", "camsbackpacks.midnightconfig.sneakPlaceBackpack": "Enable Sneak-Placing Backpacks"