Skip to content

Commit

Permalink
Refactor interaction handling in trap blocks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Alatyami committed Jul 14, 2024
1 parent f372aa4 commit 2bcb630
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
24 changes: 16 additions & 8 deletions src/main/java/growthcraft/trapper/block/FishtrapBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/growthcraft/trapper/block/SpawnEggTrapBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 2bcb630

Please sign in to comment.