From 2bcb6303306592cdfb0399fbb40949a6c6151e10 Mon Sep 17 00:00:00 2001 From: Alatyami Date: Sat, 13 Jul 2024 19:37:59 -0600 Subject: [PATCH] Refactor interaction handling in trap blocks The interaction handling for SpawnEggTrapBlock and FishTrapBlock has been revised. They now perform entity type checking before attempting to open their respective GUIs, limiting potential errors. Also, the logged error message in case of failure now includes the player's name and block position for ease of debugging. --- .../trapper/block/FishtrapBlock.java | 24 ++++++++++++------- .../trapper/block/SpawnEggTrapBlock.java | 23 ++++++++++++------ 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/main/java/growthcraft/trapper/block/FishtrapBlock.java b/src/main/java/growthcraft/trapper/block/FishtrapBlock.java index 7525148..fc1423c 100644 --- a/src/main/java/growthcraft/trapper/block/FishtrapBlock.java +++ b/src/main/java/growthcraft/trapper/block/FishtrapBlock.java @@ -44,19 +44,27 @@ public FishtrapBlock() { @Override public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand interactionHand, BlockHitResult blockHitResult) { - if (!level.isClientSide) { + BlockEntity blockEntity = level.getBlockEntity(blockPos); + + if(!(blockEntity instanceof FishtrapBlockEntity fishtrapBlockEntity)) + return InteractionResult.PASS; + + if(level.isClientSide) + return InteractionResult.SUCCESS; + + try { // Play sound level.playSound(player, blockPos, SoundEvents.BARREL_OPEN, SoundSource.BLOCKS, 1.0F, 1.0F); - // Open the menu container - try { - this.openContainer(level, blockPos, player); - } catch (Exception ex) { - GrowthcraftTrapper.LOGGER.error(String.format("%s unable to open FishtrapBlockEntity GUI at %s.", player.getDisplayName().getString(), blockPos)); - GrowthcraftTrapper.LOGGER.error(ex.getMessage()); + // Open the Menu Container + if(player instanceof ServerPlayer serverPlayer) { + serverPlayer.openMenu(fishtrapBlockEntity, blockPos); } + } catch (Exception ex) { + GrowthcraftTrapper.LOGGER.error(String.format("%s unable to open FishTrapBlockEntity GUI at %s.", player.getDisplayName().getString(), blockPos)); + GrowthcraftTrapper.LOGGER.error(ex.getMessage()); } - return InteractionResult.SUCCESS; + return InteractionResult.CONSUME; } /** diff --git a/src/main/java/growthcraft/trapper/block/SpawnEggTrapBlock.java b/src/main/java/growthcraft/trapper/block/SpawnEggTrapBlock.java index e7e50fd..6b872c9 100644 --- a/src/main/java/growthcraft/trapper/block/SpawnEggTrapBlock.java +++ b/src/main/java/growthcraft/trapper/block/SpawnEggTrapBlock.java @@ -43,18 +43,27 @@ public SpawnEggTrapBlock() { @Override public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand interactionHand, BlockHitResult blockHitResult) { - if (!level.isClientSide) { + BlockEntity blockEntity = level.getBlockEntity(blockPos); + + if(!(blockEntity instanceof SpawnEggTrapBlockEntity spawnEggTrapBlockEntity)) + return InteractionResult.PASS; + + if(level.isClientSide) + return InteractionResult.SUCCESS; + + try { // Play sound level.playSound(player, blockPos, SoundEvents.BARREL_OPEN, SoundSource.BLOCKS, 1.0F, 1.0F); - // Open the menu container - try { - this.openContainer(level, blockPos, player); - } catch (Exception ex) { - GrowthcraftTrapper.LOGGER.error(ex.getMessage()); + // Open the Menu Container + if(player instanceof ServerPlayer serverPlayer) { + serverPlayer.openMenu(spawnEggTrapBlockEntity, blockPos); } + } catch (Exception ex) { + GrowthcraftTrapper.LOGGER.error(String.format("%s unable to open SpawnEggTrapBlockEntity GUI at %s.", player.getDisplayName().getString(), blockPos)); + GrowthcraftTrapper.LOGGER.error(ex.getMessage()); } - return InteractionResult.SUCCESS; + return InteractionResult.CONSUME; } /**