Skip to content

Commit d99ba11

Browse files
committed
add interaction permission checks (closes #39)
1 parent b85d6b7 commit d99ba11

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

src/main/java/dev/cammiescorner/camsbackpacks/common/blocks/BackpackBlock.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import dev.cammiescorner.camsbackpacks.common.blocks.entities.BackpackBlockEntity;
44
import dev.cammiescorner.camsbackpacks.core.BackpacksConfig;
5+
import net.minecraft.ChatFormatting;
56
import net.minecraft.core.BlockPos;
67
import net.minecraft.core.Direction;
78
import net.minecraft.nbt.CompoundTag;
9+
import net.minecraft.network.chat.Component;
10+
import net.minecraft.server.level.ServerPlayer;
811
import net.minecraft.sounds.SoundEvents;
912
import net.minecraft.sounds.SoundSource;
1013
import net.minecraft.world.*;
@@ -68,6 +71,12 @@ public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable L
6871
@Override
6972
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
7073
if (!world.isClientSide()) {
74+
if(!world.mayInteract(player, pos)) {
75+
((ServerPlayer) player).sendSystemMessage(Component.translatable("error.camsbackpacks.permission_use").withStyle(ChatFormatting.RED), true);
76+
return InteractionResult.FAIL;
77+
}
78+
79+
7180
if (BackpacksConfig.sneakPlaceBackpack && player.isShiftKeyDown() && player.getItemBySlot(EquipmentSlot.CHEST).isEmpty()) {
7281
if (world.getBlockEntity(pos) instanceof BackpackBlockEntity blockEntity) {
7382
ItemStack stack = new ItemStack(this);
@@ -92,7 +101,7 @@ public InteractionResult use(BlockState state, Level world, BlockPos pos, Player
92101
}
93102
}
94103

95-
return InteractionResult.SUCCESS;
104+
return InteractionResult.sidedSuccess(world.isClientSide());
96105
}
97106

98107
@Override

src/main/java/dev/cammiescorner/camsbackpacks/common/network/EquipBackpackPacket.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import dev.cammiescorner.camsbackpacks.common.blocks.BackpackBlock;
55
import dev.cammiescorner.camsbackpacks.common.blocks.entities.BackpackBlockEntity;
66
import dev.cammiescorner.camsbackpacks.common.items.BackpackItem;
7-
import dev.cammiescorner.camsbackpacks.common.screen.BackpackScreenHandler;
87
import dev.cammiescorner.camsbackpacks.core.util.BackpackHelper;
98
import io.netty.buffer.Unpooled;
9+
import net.minecraft.ChatFormatting;
1010
import net.minecraft.core.BlockPos;
1111
import net.minecraft.nbt.CompoundTag;
1212
import net.minecraft.network.FriendlyByteBuf;
13+
import net.minecraft.network.chat.Component;
14+
import net.minecraft.network.chat.MutableComponent;
1315
import net.minecraft.resources.ResourceLocation;
1416
import net.minecraft.server.MinecraftServer;
1517
import net.minecraft.server.level.ServerPlayer;
@@ -22,7 +24,6 @@
2224
import net.minecraft.world.level.Level;
2325
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
2426
import org.quiltmc.qsl.networking.api.PacketSender;
25-
import org.quiltmc.qsl.networking.api.PlayerLookup;
2627
import org.quiltmc.qsl.networking.api.client.ClientPlayNetworking;
2728

2829
public class EquipBackpackPacket {
@@ -42,6 +43,15 @@ public static void handle(MinecraftServer server, ServerPlayer player, ServerGam
4243
server.execute(() -> {
4344
Level world = player.level();
4445

46+
if (!world.mayInteract(player, pos)) {
47+
player.closeContainer();
48+
MutableComponent message = isBlockEntity
49+
? Component.translatable("error.camsbackpacks.permission_pickup_at")
50+
: Component.translatable("error.camsbackpacks.permission_place_at");
51+
player.sendSystemMessage(message.withStyle(ChatFormatting.RED), true);
52+
return;
53+
}
54+
4555
if (isBlockEntity) {
4656
if (world.getBlockEntity(pos) instanceof BackpackBlockEntity blockEntity) {
4757
ItemStack stack = new ItemStack(world.getBlockState(pos).getBlock().asItem());
@@ -55,10 +65,7 @@ public static void handle(MinecraftServer server, ServerPlayer player, ServerGam
5565
stack.setHoverName(blockEntity.getName());
5666

5767
world.destroyBlock(pos, false, player);
58-
PlayerLookup.tracking(blockEntity).forEach(playerEntity -> {
59-
if (playerEntity.containerMenu instanceof BackpackScreenHandler handler && handler.blockPos.equals(pos))
60-
playerEntity.closeContainer();
61-
});
68+
6269
}
6370
} else {
6471
ItemStack stack = player.getItemBySlot(EquipmentSlot.CHEST);

src/main/java/dev/cammiescorner/camsbackpacks/common/network/PlaceBackpackPacket.java

+9
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import dev.cammiescorner.camsbackpacks.common.items.BackpackItem;
77
import dev.cammiescorner.camsbackpacks.core.util.BackpackHelper;
88
import io.netty.buffer.Unpooled;
9+
import net.minecraft.ChatFormatting;
910
import net.minecraft.core.BlockPos;
1011
import net.minecraft.network.FriendlyByteBuf;
12+
import net.minecraft.network.chat.Component;
1113
import net.minecraft.resources.ResourceLocation;
1214
import net.minecraft.server.MinecraftServer;
1315
import net.minecraft.server.level.ServerPlayer;
@@ -38,6 +40,13 @@ public static void handle(MinecraftServer server, ServerPlayer player, ServerGam
3840
server.execute(() -> {
3941
Level world = player.level();
4042
BlockPos pos = BackpackHelper.isReplaceable(world, hitResult.getBlockPos()) ? hitResult.getBlockPos() : hitResult.getBlockPos().relative(hitResult.getDirection());
43+
44+
if(!world.mayInteract(player, pos)) {
45+
player.closeContainer();
46+
player.sendSystemMessage(Component.translatable("error.camsbackpacks.permission_place_at").withStyle(ChatFormatting.RED), true);
47+
return;
48+
}
49+
4150
ItemStack stack = player.getItemBySlot(EquipmentSlot.CHEST);
4251

4352
if (BackpackHelper.isReplaceable(world, pos)) {

src/main/resources/assets/camsbackpacks/lang/en_us.json

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
"container.camsbackpacks.player_inv": "Inventory",
4444
"container.camsbackpacks.backpack_inv": "Backpack",
4545

46+
// Error Messages
47+
"error.camsbackpacks.permission_place_at": "You do not have permission to place a backpack at this location!",
48+
"error.camsbackpacks.permission_pickup_at": "You do not have permission to pick up a backpack at this location!",
49+
"error.camsbackpacks.permission_use": "You do not have permission to use this backpack!",
50+
4651
// Config
4752
"camsbackpacks.midnightconfig.title": "CamsBackpacks",
4853
"camsbackpacks.midnightconfig.sneakPlaceBackpack": "Enable Sneak-Placing Backpacks"

0 commit comments

Comments
 (0)