Skip to content

Commit

Permalink
add interaction permission checks (closes #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Jan 3, 2024
1 parent b85d6b7 commit d99ba11
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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);
Expand All @@ -92,7 +101,7 @@ public InteractionResult use(BlockState state, Level world, BlockPos pos, Player
}
}

return InteractionResult.SUCCESS;
return InteractionResult.sidedSuccess(world.isClientSide());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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());
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/assets/camsbackpacks/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit d99ba11

Please sign in to comment.