Skip to content

Commit

Permalink
fix: keys now work in the offhand
Browse files Browse the repository at this point in the history
  • Loading branch information
bconlon1 committed Apr 2, 2024
1 parent 554bc83 commit dd9e31b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,30 +112,29 @@ public void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource
@SuppressWarnings("deprecation")
@Override
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (level.isClientSide()) {
return InteractionResult.SUCCESS;
} else {
BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity instanceof TreasureChestBlockEntity treasureChestBlockEntity) {
MenuProvider menuProvider = this.getMenuProvider(state, level, pos);
if (treasureChestBlockEntity.getLocked()) {
ItemStack stack = player.getMainHandItem();
if (treasureChestBlockEntity.tryUnlock(player)) {
if (player instanceof ServerPlayer) {
player.awardStat(Stats.ITEM_USED.get(stack.getItem()));
}
if (!player.getAbilities().instabuild) {
stack.shrink(1);
}
BlockEntity blockEntity = level.getBlockEntity(pos);
if (blockEntity instanceof TreasureChestBlockEntity treasureChestBlockEntity) {
MenuProvider menuProvider = this.getMenuProvider(state, level, pos);
if (treasureChestBlockEntity.getLocked()) {
ItemStack stack = player.getItemInHand(hand);
if (treasureChestBlockEntity.tryUnlock(player, stack)) {
if (player instanceof ServerPlayer) {
player.awardStat(Stats.ITEM_USED.get(stack.getItem()));
}
if (!player.getAbilities().instabuild) {
stack.shrink(1);
}
} else if (!ChestBlock.isChestBlockedAt(level, pos) && menuProvider != null) {
player.openMenu(menuProvider);
player.awardStat(Stats.CUSTOM.get(Stats.OPEN_CHEST));
PiglinAi.angerNearbyPiglins(player, true);
return InteractionResult.sidedSuccess(level.isClientSide);
} else {
player.swing(InteractionHand.MAIN_HAND);
}
} else if (!ChestBlock.isChestBlockedAt(level, pos) && menuProvider != null) {
player.openMenu(menuProvider);
player.awardStat(Stats.CUSTOM.get(Stats.OPEN_CHEST));
PiglinAi.angerNearbyPiglins(player, true);
}
return InteractionResult.CONSUME;
}
return InteractionResult.PASS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,19 @@ protected TreasureChestBlockEntity(BlockEntityType<?> tileEntityType, BlockPos p
* @param player The {@link Player} attempting to unlock the Treasure Chest.
* @return Whether the Treasure Chest was unlocked.
*/
public boolean tryUnlock(Player player) {
ItemStack stack = player.getMainHandItem();
public boolean tryUnlock(Player player, ItemStack stack) {
boolean keyMatches = stack.getItem() instanceof DungeonKeyItem dungeonKeyItem && this.getKind().equals(dungeonKeyItem.getDungeonType());
boolean hasNoKeys = (!(player.getMainHandItem().getItem() instanceof DungeonKeyItem mainHandItem) || !this.getKind().equals(mainHandItem.getDungeonType()))
&& (!(player.getOffhandItem().getItem() instanceof DungeonKeyItem offhandItem) || !this.getKind().equals(offhandItem.getDungeonType()));
if (this.getLocked() && keyMatches && this.level != null) {
this.setLocked(false);
this.setChanged();
this.level.markAndNotifyBlock(this.worldPosition, this.level.getChunkAt(this.worldPosition), this.getBlockState(), this.getBlockState(), 2, 512);
return true;
} else {
} else if (hasNoKeys) {
player.displayClientMessage(Component.translatable(this.getKind().getNamespace() + "." + this.getKind().getPath() + "_treasure_chest_locked"), true);
return false;
}
return false;
}

@Override
Expand Down

0 comments on commit dd9e31b

Please sign in to comment.