-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #16 Added Animal Traps that result in trapping entity drops.
- Loading branch information
Showing
42 changed files
with
6,286 additions
and
13 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
src/main/java/growthcraft/trapper/block/AnimalTrapBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package growthcraft.trapper.block; | ||
|
||
import growthcraft.trapper.GrowthcraftTrapper; | ||
import growthcraft.trapper.block.entity.AnimalTrapBlockEntity; | ||
import growthcraft.trapper.init.GrowthcraftTrapperBlockEntities; | ||
import growthcraft.trapper.utils.BlockPropertiesUtils; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.Direction; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.sounds.SoundEvents; | ||
import net.minecraft.sounds.SoundSource; | ||
import net.minecraft.world.InteractionHand; | ||
import net.minecraft.world.InteractionResult; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.item.context.BlockPlaceContext; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.block.*; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.entity.BlockEntityTicker; | ||
import net.minecraft.world.level.block.entity.BlockEntityType; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.minecraft.world.level.block.state.StateDefinition; | ||
import net.minecraft.world.level.block.state.properties.BlockStateProperties; | ||
import net.minecraft.world.level.block.state.properties.BooleanProperty; | ||
import net.minecraft.world.level.block.state.properties.DirectionProperty; | ||
import net.minecraft.world.level.material.FluidState; | ||
import net.minecraft.world.level.material.Fluids; | ||
import net.minecraft.world.level.material.PushReaction; | ||
import net.minecraft.world.phys.BlockHitResult; | ||
import net.minecraftforge.network.NetworkHooks; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class AnimalTrapBlock extends BaseEntityBlock implements SimpleWaterloggedBlock { | ||
|
||
public static final DirectionProperty FACING = DirectionalBlock.FACING; | ||
public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED; | ||
|
||
private int processingFactor = 1; | ||
|
||
public AnimalTrapBlock() { | ||
this(1); | ||
} | ||
|
||
public AnimalTrapBlock(int processingFactor) { | ||
super(BlockPropertiesUtils.getInitProperties("animal_trap", Blocks.IRON_BLOCK)); | ||
this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(WATERLOGGED, Boolean.valueOf(false))); | ||
this.processingFactor = processingFactor; | ||
} | ||
|
||
@Override | ||
public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand interactionHand, BlockHitResult blockHitResult) { | ||
if (!level.isClientSide) { | ||
// Play sound | ||
level.playSound(player, blockPos, SoundEvents.BARREL_OPEN, SoundSource.BLOCKS, 1.0F, 1.0F); | ||
// Open the menu container | ||
try { | ||
AnimalTrapBlockEntity blockEntity = (AnimalTrapBlockEntity) level.getBlockEntity(blockPos); | ||
NetworkHooks.openScreen(((ServerPlayer) player), blockEntity, blockPos); | ||
} catch (Exception ex) { | ||
GrowthcraftTrapper.LOGGER.error(String.format("%s unable to open AnimalTrapBlockEntity GUI at %s.", player.getDisplayName().getString(), blockPos)); | ||
GrowthcraftTrapper.LOGGER.error(ex.getMessage()); | ||
} | ||
|
||
} else { | ||
|
||
} | ||
return InteractionResult.SUCCESS; | ||
} | ||
|
||
@Override | ||
public RenderShape getRenderShape(BlockState p_49232_) { | ||
return RenderShape.MODEL; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public BlockState getStateForPlacement(BlockPlaceContext context) { | ||
FluidState fluidState = context.getLevel().getFluidState(context.getClickedPos()); | ||
BlockState blockState = this.defaultBlockState(); | ||
|
||
return blockState.setValue(FACING, context.getClickedFace()).setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER); | ||
} | ||
|
||
@Override | ||
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> blockStateBuilder) { | ||
blockStateBuilder.add(FACING, WATERLOGGED); | ||
} | ||
|
||
@Override | ||
public PushReaction getPistonPushReaction(BlockState p_60584_) { | ||
return PushReaction.DESTROY; | ||
} | ||
|
||
@Override | ||
public FluidState getFluidState(BlockState blockState) { | ||
return Boolean.TRUE.equals(blockState.getValue(WATERLOGGED)) | ||
? Fluids.WATER.getSource(false) | ||
: super.getFluidState(blockState); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) { | ||
return GrowthcraftTrapperBlockEntities.ANIMAL_TRAP_BLOCK_ENTITY.get().create(blockPos, blockState); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState blockState, BlockEntityType<T> blockEntityType) { | ||
return createTickerHelper( | ||
blockEntityType, | ||
GrowthcraftTrapperBlockEntities.ANIMAL_TRAP_BLOCK_ENTITY.get(), | ||
(worldLevel, pos, state, blockEntity) -> blockEntity.tick() | ||
); | ||
} | ||
|
||
@Override | ||
public void onRemove(BlockState blockState, Level level, BlockPos blockPos, BlockState newBlockState, boolean isMoving) { | ||
if (blockState.getBlock() != newBlockState.getBlock()) { | ||
try { | ||
AnimalTrapBlockEntity blockEntity = (AnimalTrapBlockEntity) level.getBlockEntity(blockPos); | ||
blockEntity.dropItems(); | ||
} catch (Exception ex) { | ||
GrowthcraftTrapper.LOGGER.error(String.format("Invalid blockEntity type at %s, expected AnimalTrapBlockEntity", blockPos)); | ||
} | ||
} | ||
super.onRemove(blockState, level, blockPos, newBlockState, isMoving); | ||
} | ||
|
||
public int getProcessingFactor() { | ||
return processingFactor; | ||
} | ||
} |
Oops, something went wrong.