Skip to content

Commit c875eff

Browse files
committed
block entity practice
1 parent 3799b3b commit c875eff

File tree

4 files changed

+97
-32
lines changed

4 files changed

+97
-32
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package jiink.smeltinginapinch;
2+
3+
import net.minecraft.block.Block;
4+
import net.minecraft.block.BlockEntityProvider;
5+
import net.minecraft.block.BlockState;
6+
import net.minecraft.block.entity.BlockEntity;
7+
import net.minecraft.entity.player.PlayerEntity;
8+
import net.minecraft.text.Text;
9+
import net.minecraft.util.ActionResult;
10+
import net.minecraft.util.Hand;
11+
import net.minecraft.util.hit.BlockHitResult;
12+
import net.minecraft.util.math.BlockPos;
13+
import net.minecraft.world.World;
14+
15+
public class DemoBlock extends Block implements BlockEntityProvider {
16+
public DemoBlock(Settings settings) {
17+
super(settings);
18+
}
19+
20+
@Override
21+
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
22+
return new DemoBlockEntity(pos, state);
23+
}
24+
25+
@Override
26+
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
27+
if (!world.isClient && hand == Hand.MAIN_HAND) {
28+
BlockEntity blockEntity = world.getBlockEntity(pos);
29+
if (blockEntity instanceof DemoBlockEntity) {
30+
DemoBlockEntity demoBlockEntity = (DemoBlockEntity)blockEntity;
31+
demoBlockEntity.number++;
32+
demoBlockEntity.markDirty();
33+
player.sendMessage(Text.literal("Number is " + demoBlockEntity.number), false);
34+
return ActionResult.SUCCESS;
35+
}
36+
}
37+
return ActionResult.PASS;
38+
}
39+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package jiink.smeltinginapinch;
2+
3+
import net.minecraft.block.BlockState;
4+
import net.minecraft.block.entity.BlockEntity;
5+
import net.minecraft.nbt.NbtCompound;
6+
import net.minecraft.util.math.BlockPos;
7+
8+
public class DemoBlockEntity extends BlockEntity {
9+
10+
public int number = 7;
11+
private static final String NBT_ATTACHMENT_KEY = "number";
12+
13+
public DemoBlockEntity(BlockPos pos, BlockState state) {
14+
super(SmeltingInAPinch.DEMO_BLOCK_ENTITY, pos, state);
15+
SmeltingInAPinch.LOGGER.info("I am a new demoblockentity!");
16+
}
17+
18+
// Call markDirty() whenever changing the saved variable so this gets called.
19+
@Override
20+
public void writeNbt(NbtCompound nbt) {
21+
nbt.putInt(NBT_ATTACHMENT_KEY, number);
22+
super.writeNbt(nbt);
23+
}
24+
25+
@Override
26+
public void readNbt(NbtCompound nbt) {
27+
super.readNbt(nbt);
28+
number = nbt.getInt(NBT_ATTACHMENT_KEY);
29+
}
30+
}

src/main/java/jiink/smeltinginapinch/SmeltingInAPinch.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.fabricmc.api.ModInitializer;
44
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
5+
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
56
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
67
import net.minecraft.block.Block;
78
import net.minecraft.block.entity.BlockEntityType;
@@ -23,6 +24,22 @@ public class SmeltingInAPinch implements ModInitializer {
2324
// It is considered best practice to use your mod id as the logger's name.
2425
public static final Logger LOGGER = LoggerFactory.getLogger("modid");
2526

27+
public static final Block DEMO_BLOCK = Registry.register(
28+
Registries.BLOCK,
29+
new Identifier("smeltinginapinch", "demo_block"),
30+
new DemoBlock(FabricBlockSettings.create().strength(1.0f))
31+
);
32+
public static final Item DEMO_BLOCK_ITEM = Registry.register(
33+
Registries.ITEM,
34+
new Identifier("smeltinginapinch", "demo_block"),
35+
new BlockItem(DEMO_BLOCK, new Item.Settings())
36+
);
37+
public static final BlockEntityType<DemoBlockEntity> DEMO_BLOCK_ENTITY = Registry.register(
38+
Registries.BLOCK_ENTITY_TYPE,
39+
new Identifier("smeltinginapinch", "demo_block_entity"),
40+
FabricBlockEntityTypeBuilder.create(DemoBlockEntity::new, DEMO_BLOCK).build()
41+
);
42+
2643
public static final WoodenFurnaceBlock WOODEN_FURNACE_BLOCK = Registry.register(
2744
Registries.BLOCK,
2845
new Identifier("smeltinginapinch", "wooden_furnace"),
@@ -47,6 +64,7 @@ public void onInitialize() {
4764
LOGGER.info("Hello Fabric world!");
4865
ItemGroupEvents.modifyEntriesEvent(ItemGroups.FUNCTIONAL).register(content -> {
4966
content.addAfter(Items.BLAST_FURNACE, WOODEN_FURANCE_BLOCK_ITEM);
67+
content.addAfter(Items.TORCH, DEMO_BLOCK_ITEM);
5068
});
5169
}
5270
}

src/main/java/jiink/smeltinginapinch/WoodenFurnaceBlock.java

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.minecraft.block.AbstractFurnaceBlock;
44
import net.minecraft.block.BlockState;
5+
import net.minecraft.block.BlockWithEntity;
56
import net.minecraft.block.entity.AbstractFurnaceBlockEntity;
67
import net.minecraft.block.entity.BlockEntity;
78
import net.minecraft.block.entity.BlockEntityTicker;
@@ -15,17 +16,25 @@
1516
import net.minecraft.util.math.random.Random;
1617
import net.minecraft.world.World;
1718

19+
import java.util.function.BiConsumer;
20+
1821
import org.jetbrains.annotations.Nullable;
1922

2023
import com.mojang.serialization.MapCodec;
2124

2225
import net.minecraft.block.AbstractBlock;
2326

24-
public class WoodenFurnaceBlock extends AbstractFurnaceBlock {
27+
public class WoodenFurnaceBlock extends BlockWithEntity {
2528
public WoodenFurnaceBlock(AbstractBlock.Settings settings) {
2629
super(settings);
2730
}
2831

32+
// @Nullable
33+
// public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
34+
// return world.isClient ? null : checkType(type, SmeltingInAPinch.WOODEN_FURNACE_BLOCK_ENTITY, WoodenFurnaceBlockEntity::tick);
35+
// }
36+
37+
2938
@Override
3039
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
3140
return new WoodenFurnaceBlockEntity(pos, state);
@@ -36,35 +45,4 @@ public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
3645
protected MapCodec<WoodenFurnaceBlock> getCodec() {
3746
return CODEC;
3847
}
39-
40-
@Override
41-
protected void openScreen(World world, BlockPos pos, PlayerEntity player) {
42-
BlockEntity blockEntity = world.getBlockEntity(pos);
43-
if (blockEntity instanceof WoodenFurnaceBlockEntity) {
44-
player.openHandledScreen((NamedScreenHandlerFactory)((Object)blockEntity));
45-
}
46-
}
47-
48-
@Override
49-
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
50-
return checkType(type, SmeltingInAPinch.WOODEN_FURNACE_BLOCK_ENTITY, (world1, pos, state1, be) -> WoodenFurnaceBlockEntity.tick(world1, pos, state1, be));
51-
}
52-
53-
public static <T extends BlockEntity> BlockEntityTicker<T> checkType(BlockEntityType<T> givenType, BlockEntityType<WoodenFurnaceBlockEntity> expectedType, BlockEntityTicker<? super WoodenFurnaceBlockEntity> ticker) {
54-
return givenType == expectedType ? (BlockEntityTicker<T>) ticker : null;
55-
}
56-
57-
@Override
58-
public void randomDisplayTick(BlockState state, World world, BlockPos pos, Random random) {
59-
if (!state.get(LIT).booleanValue()) {
60-
return;
61-
}
62-
double d = (double)pos.getX() + 0.5;
63-
double e = pos.getY();
64-
double f = (double)pos.getZ() + 0.5;
65-
if (random.nextDouble() < 0.1) {
66-
world.playSound(d, e, f, SoundEvents.BLOCK_SMOKER_SMOKE, SoundCategory.BLOCKS, 1.0f, 1.0f, false);
67-
}
68-
world.addParticle(ParticleTypes.SMOKE, d, e + 1.1, f, 0.0, 0.0, 0.0);
69-
}
7048
}

0 commit comments

Comments
 (0)