Skip to content

Commit ecc36eb

Browse files
authored
Merge pull request #26 from ethanicusss/feature/custom-hoppers
Feature/custom hoppers
2 parents 0474272 + 23b2be4 commit ecc36eb

File tree

11 files changed

+595
-27
lines changed

11 files changed

+595
-27
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.github.ethanicuss.astraladditions.blocks.customhopper;
2+
3+
import com.github.ethanicuss.astraladditions.AstralAdditions;
4+
import com.github.ethanicuss.astraladditions.registry.ModBlocks;
5+
import net.minecraft.block.BlockState;
6+
import net.minecraft.block.HopperBlock;
7+
import net.minecraft.block.entity.BlockEntity;
8+
import net.minecraft.block.entity.BlockEntityTicker;
9+
import net.minecraft.block.entity.BlockEntityType;
10+
import net.minecraft.entity.LivingEntity;
11+
import net.minecraft.entity.player.PlayerEntity;
12+
import net.minecraft.item.ItemStack;
13+
import net.minecraft.stat.Stats;
14+
import net.minecraft.util.ActionResult;
15+
import net.minecraft.util.Hand;
16+
import net.minecraft.util.Identifier;
17+
import net.minecraft.util.ItemScatterer;
18+
import net.minecraft.util.hit.BlockHitResult;
19+
import net.minecraft.util.math.BlockPos;
20+
import net.minecraft.util.registry.Registry;
21+
import net.minecraft.world.World;
22+
import org.jetbrains.annotations.Nullable;
23+
24+
public class CustomHopperBlock extends HopperBlock {
25+
26+
private final int cooldown;
27+
private final int itemRate;
28+
private final String blockEntityTypeId;
29+
30+
public CustomHopperBlock(Settings settings, String _blockEntityTypeId, int _cooldown, int _itemRate) {
31+
super(settings);
32+
this.cooldown = _cooldown;
33+
this.itemRate = _itemRate;
34+
this.blockEntityTypeId = _blockEntityTypeId;
35+
}
36+
37+
@Override
38+
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
39+
return new CustomHopperBlockEntity(pos, state, this.blockEntityTypeId, this.cooldown, this.itemRate);
40+
}
41+
42+
@Nullable
43+
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
44+
BlockEntityType<CustomHopperBlockEntity> blockEntityType = (BlockEntityType<CustomHopperBlockEntity>) Registry.BLOCK_ENTITY_TYPE.get(new Identifier(AstralAdditions.MOD_ID, blockEntityTypeId));
45+
return world.isClient ? null : checkType(type, blockEntityType, CustomHopperBlockEntity::serverTick);
46+
}
47+
48+
@Override
49+
public void onPlaced(World world, BlockPos pos, BlockState state, LivingEntity placer, ItemStack itemStack) {
50+
if (itemStack.hasCustomName()) {
51+
BlockEntity blockEntity = world.getBlockEntity(pos);
52+
if (blockEntity instanceof CustomHopperBlockEntity) {
53+
((CustomHopperBlockEntity)blockEntity).setCustomName(itemStack.getName());
54+
}
55+
}
56+
57+
}
58+
59+
@Override
60+
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
61+
if (world.isClient) {
62+
return ActionResult.SUCCESS;
63+
} else {
64+
BlockEntity blockEntity = world.getBlockEntity(pos);
65+
if (blockEntity instanceof CustomHopperBlockEntity) {
66+
player.openHandledScreen((CustomHopperBlockEntity)blockEntity);
67+
player.incrementStat(Stats.INSPECT_HOPPER);
68+
}
69+
70+
return ActionResult.CONSUME;
71+
}
72+
}
73+
74+
@Override
75+
public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) {
76+
if (!state.isOf(newState.getBlock())) {
77+
BlockEntity blockEntity = world.getBlockEntity(pos);
78+
if (blockEntity instanceof CustomHopperBlockEntity) {
79+
ItemScatterer.spawn(world, pos, (CustomHopperBlockEntity)blockEntity);
80+
world.updateComparators(pos, this);
81+
}
82+
83+
super.onStateReplaced(state, world, pos, newState, moved);
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)