-
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.
Merge pull request #3 from EngineerSmith/main
Feat; Added Barrel and Crate
- Loading branch information
Showing
51 changed files
with
1,325 additions
and
4 deletions.
There are no files selected for viewing
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
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
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,19 @@ | ||
package com.kikis.ptdyeplus; | ||
|
||
import net.minecraft.world.phys.shapes.BooleanOp; | ||
import net.minecraft.world.phys.shapes.Shapes; | ||
import net.minecraft.world.phys.shapes.VoxelShape; | ||
|
||
public class Shape { | ||
public VoxelShape shape; | ||
public Shape() { | ||
this.shape = Shapes.empty(); | ||
} | ||
|
||
public Shape box(int x0, int y0, int z0, int x1, int y1, int z1) { | ||
float x0f = (float) x0 / 16.0F, y0f = (float) y0 / 16.F, z0f = (float) z0 / 16.F; | ||
float x1f = (float) x1 / 16.0F, y1f = (float) y1 / 16.F, z1f = (float) z1 / 16.F; | ||
this.shape = Shapes.join(shape, Shapes.box(x0f, y0f, z0f, x1f, y1f, z1f), BooleanOp.OR); | ||
return this; | ||
} | ||
} |
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,139 @@ | ||
package com.kikis.ptdyeplus.block; | ||
|
||
import com.kikis.ptdyeplus.block.entity.BarrelEntity; | ||
import com.kikis.ptdyeplus.block.property.fullness; | ||
import com.kikis.ptdyeplus.Shape; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.stats.Stats; | ||
import net.minecraft.world.Container; | ||
import net.minecraft.world.Containers; | ||
import net.minecraft.world.InteractionHand; | ||
import net.minecraft.world.InteractionResult; | ||
import net.minecraft.world.entity.LivingEntity; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.level.BlockGetter; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.block.BaseEntityBlock; | ||
import net.minecraft.world.level.block.Block; | ||
import net.minecraft.world.level.block.RenderShape; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.state.BlockBehaviour; | ||
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.IntegerProperty; | ||
import net.minecraft.world.phys.BlockHitResult; | ||
import net.minecraft.world.phys.shapes.CollisionContext; | ||
import net.minecraft.world.phys.shapes.VoxelShape; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class Barrel extends BaseEntityBlock { | ||
public static final IntegerProperty FULLNESS; | ||
public static final IntegerProperty POWER; | ||
|
||
private static final VoxelShape hitbox = new Shape() | ||
.box(1,0,1, 15,16,15) | ||
.box(3,0,0, 13,16,16) | ||
.box(0,0,3, 16,16,13) | ||
.shape; | ||
|
||
public Barrel(BlockBehaviour.Properties properties) { | ||
super(properties); | ||
this.registerDefaultState(this.stateDefinition.any() | ||
.setValue(fullness.PROPERTY, fullness.MIN) | ||
.setValue(BlockStateProperties.POWER, 0) | ||
); | ||
} | ||
|
||
private void updatePower(BlockState blockState, Level level, BlockPos blockPos) { | ||
if (!level.isClientSide) { | ||
int power = level.getBestNeighborSignal(blockPos); | ||
level.setBlock(blockPos, blockState.setValue(POWER, Math.max(0, Math.min(power, 15))), 2); | ||
} | ||
} | ||
|
||
@Override | ||
public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand hand, BlockHitResult hitResult) { | ||
if (level.isClientSide) | ||
return InteractionResult.SUCCESS; | ||
BlockEntity entity = level.getBlockEntity(blockPos); | ||
if (entity instanceof BarrelEntity) { | ||
player.openMenu((BarrelEntity) entity); | ||
player.awardStat(Stats.OPEN_BARREL); | ||
} | ||
return InteractionResult.CONSUME; | ||
} | ||
|
||
@Override | ||
public void onRemove(BlockState blockState, Level level, BlockPos blockPos, BlockState blockState2, boolean p_60519_) { | ||
if (!blockState.is(blockState2.getBlock())) { | ||
BlockEntity entity = level.getBlockEntity(blockPos); | ||
if (entity instanceof Container container) { | ||
Containers.dropContents(level, blockPos, container); | ||
level.updateNeighbourForOutputSignal(blockPos, this); | ||
} | ||
|
||
super.onRemove(blockState, level, blockPos, blockState, p_60519_); | ||
} | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) { | ||
return new BarrelEntity(blockPos, blockState); | ||
} | ||
|
||
@Override | ||
public RenderShape getRenderShape(BlockState p_49232_) { | ||
return RenderShape.MODEL; | ||
} | ||
|
||
@Override | ||
public VoxelShape getShape(BlockState blockState, BlockGetter blockGetter, BlockPos blockPos, CollisionContext collisionContext) { | ||
return Barrel.hitbox; | ||
} | ||
|
||
@Override | ||
public void setPlacedBy(Level level, BlockPos blockPos, BlockState blockState, @Nullable LivingEntity livingEntity, ItemStack itemStack) { | ||
if (itemStack.hasCustomHoverName()) { | ||
BlockEntity entity = level.getBlockEntity(blockPos); | ||
if (entity instanceof BarrelEntity barrelEntity) | ||
barrelEntity.setCustomName(itemStack.getHoverName()); | ||
} | ||
this.updatePower(blockState, level, blockPos); | ||
} | ||
|
||
@Override | ||
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateBuilder) { | ||
stateBuilder.add(FULLNESS).add(POWER); | ||
} | ||
|
||
@Override | ||
public void neighborChanged(BlockState blockState, Level level, BlockPos blockPos, Block neighborBlock, BlockPos fromPos, boolean moving) { | ||
super.neighborChanged(blockState, level, blockPos, neighborBlock, fromPos, moving); | ||
this.updatePower(blockState, level, blockPos); | ||
} | ||
|
||
@Override | ||
public boolean hasAnalogOutputSignal(BlockState p_49058_) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public int getAnalogOutputSignal(BlockState blockState, Level level, BlockPos blockPos) { | ||
// edge case that 'should' never happen | ||
if (!(level.getBlockEntity(blockPos) instanceof BarrelEntity entity)) | ||
return 0; | ||
|
||
int power = blockState.getValue(POWER); | ||
if (power == 0) | ||
return Math.min(entity.getItemCount(null), 15); | ||
return Math.min(entity.getItemCount(entity.getItem(power - 1)), 15); | ||
} | ||
|
||
static { | ||
FULLNESS = fullness.PROPERTY; | ||
POWER = BlockStateProperties.POWER; | ||
} | ||
} |
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,140 @@ | ||
package com.kikis.ptdyeplus.block; | ||
|
||
import com.kikis.ptdyeplus.block.entity.CrateEntity; | ||
import com.kikis.ptdyeplus.block.property.fullness; | ||
import com.kikis.ptdyeplus.Shape; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.stats.Stats; | ||
import net.minecraft.world.Container; | ||
import net.minecraft.world.Containers; | ||
import net.minecraft.world.InteractionHand; | ||
import net.minecraft.world.InteractionResult; | ||
import net.minecraft.world.entity.LivingEntity; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.level.BlockGetter; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.block.BaseEntityBlock; | ||
import net.minecraft.world.level.block.Block; | ||
import net.minecraft.world.level.block.RenderShape; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.state.BlockBehaviour; | ||
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.IntegerProperty; | ||
import net.minecraft.world.phys.BlockHitResult; | ||
import net.minecraft.world.phys.shapes.CollisionContext; | ||
import net.minecraft.world.phys.shapes.VoxelShape; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class Crate extends BaseEntityBlock { | ||
public static final IntegerProperty FULLNESS; | ||
public static final IntegerProperty POWER; | ||
|
||
|
||
private static final VoxelShape hitbox = new Shape() | ||
.box(0,0,0,16,2,16) | ||
.box(0,14,0, 16,16,16) | ||
.box(1,0,1, 15,16,15) | ||
.shape; | ||
|
||
public Crate(BlockBehaviour.Properties properties) { | ||
super(properties); | ||
this.registerDefaultState(this.stateDefinition.any() | ||
.setValue(FULLNESS, fullness.MIN) | ||
.setValue(POWER, 0) | ||
); | ||
} | ||
|
||
private void updatePower(BlockState blockState, Level level, BlockPos blockPos) { | ||
if (!level.isClientSide) { | ||
int power = level.getBestNeighborSignal(blockPos); | ||
level.setBlock(blockPos, blockState.setValue(POWER, Math.max(0, Math.min(power, 15))), 2); | ||
} | ||
} | ||
|
||
@Override | ||
public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand hand, BlockHitResult hitResult) { | ||
if (level.isClientSide) | ||
return InteractionResult.SUCCESS; | ||
BlockEntity entity = level.getBlockEntity(blockPos); | ||
if (entity instanceof CrateEntity) { | ||
player.openMenu((CrateEntity) entity); | ||
player.awardStat(Stats.OPEN_BARREL); | ||
} | ||
return InteractionResult.CONSUME; | ||
} | ||
|
||
@Override | ||
public void onRemove(BlockState blockState, Level level, BlockPos blockPos, BlockState blockState2, boolean p_60519_) { | ||
if (!blockState.is(blockState2.getBlock())) { | ||
BlockEntity entity = level.getBlockEntity(blockPos); | ||
if (entity instanceof Container container) { | ||
Containers.dropContents(level, blockPos, container); | ||
level.updateNeighbourForOutputSignal(blockPos, this); | ||
} | ||
|
||
super.onRemove(blockState, level, blockPos, blockState, p_60519_); | ||
} | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) { | ||
return new CrateEntity(blockPos, blockState); | ||
} | ||
|
||
@Override | ||
public RenderShape getRenderShape(BlockState p_49232_) { | ||
return RenderShape.MODEL; | ||
} | ||
|
||
@Override | ||
public VoxelShape getShape(BlockState blockState, BlockGetter blockGetter, BlockPos blockPos, CollisionContext collisionContext) { | ||
return Crate.hitbox; | ||
} | ||
|
||
@Override | ||
public void setPlacedBy(Level level, BlockPos blockPos, BlockState blockState, @Nullable LivingEntity livingEntity, ItemStack itemStack) { | ||
if (itemStack.hasCustomHoverName()) { | ||
BlockEntity entity = level.getBlockEntity(blockPos); | ||
if (entity instanceof CrateEntity crateEntity) | ||
crateEntity.setCustomName(itemStack.getHoverName()); | ||
} | ||
this.updatePower(blockState, level, blockPos); | ||
} | ||
|
||
@Override | ||
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateBuilder) { | ||
stateBuilder.add(FULLNESS).add(POWER); | ||
} | ||
|
||
@Override | ||
public void neighborChanged(BlockState blockState, Level level, BlockPos blockPos, Block neighborBlock, BlockPos fromPos, boolean moving) { | ||
super.neighborChanged(blockState, level, blockPos, neighborBlock, fromPos, moving); | ||
this.updatePower(blockState, level, blockPos); | ||
} | ||
|
||
@Override | ||
public boolean hasAnalogOutputSignal(BlockState p_49058_) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public int getAnalogOutputSignal(BlockState blockState, Level level, BlockPos blockPos) { | ||
// edge case that 'should' never happen | ||
if (!(level.getBlockEntity(blockPos) instanceof CrateEntity entity)) | ||
return 0; | ||
|
||
int power = blockState.getValue(POWER); | ||
if (power == 0) | ||
return Math.min(entity.getItemCount(null), 15); | ||
return Math.min(entity.getItemCount(entity.getItem(power - 1)), 15); | ||
} | ||
|
||
static { | ||
FULLNESS = fullness.PROPERTY; | ||
POWER = BlockStateProperties.POWER; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/kikis/ptdyeplus/block/entity/BarrelEntity.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,12 @@ | ||
package com.kikis.ptdyeplus.block.entity; | ||
|
||
import com.kikis.ptdyeplus.init.BlockEntityInit; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
|
||
public class BarrelEntity extends BaseContainerEntity { | ||
|
||
public BarrelEntity(BlockPos blockPos, BlockState blockState) { | ||
super(blockPos, blockState, 9, 2, BlockEntityInit.BARREL.get(), "ptdyeplus.container.barrel"); | ||
} | ||
} |
Oops, something went wrong.