ticking block entity

This commit is contained in:
Jenny 2025-01-06 03:16:00 +01:00
parent a12320a58a
commit 0c39ffe330
Signed by: Jenny
GPG Key ID: 2072A14E40940632
6 changed files with 73 additions and 0 deletions

View File

@ -14,4 +14,10 @@ public class BlockEntityInit {
() -> BlockEntityType.Builder.of(ExampleAdvancedBlockEntity::new, BlockInit.EXAMPLE_BLOCK_ENTITY.get()) () -> BlockEntityType.Builder.of(ExampleAdvancedBlockEntity::new, BlockInit.EXAMPLE_BLOCK_ENTITY.get())
.build(null) .build(null)
); );
public static final RegistryObject<BlockEntityType<ExampleTickingBlockEntity>> EXAMPLE_TICKING_BLOCK_ENTITY =
BLOCK_ENTITIES.register("example_ticking_block_entity",
() -> BlockEntityType.Builder.of(ExampleTickingBlockEntity::new, BlockInit.EXAMPLE_TICKING_BLOCK.get())
.build(null)
);
} }

View File

@ -13,4 +13,6 @@ public class BlockInit {
public static final RegistryObject<ExampleAdvancedBlock> EXAMPLE_BLOCK_ENTITY = BLOCKS.register("example_block_entity", public static final RegistryObject<ExampleAdvancedBlock> EXAMPLE_BLOCK_ENTITY = BLOCKS.register("example_block_entity",
() -> new ExampleAdvancedBlock(BlockBehaviour.Properties.copy(Blocks.CRAFTING_TABLE))); () -> new ExampleAdvancedBlock(BlockBehaviour.Properties.copy(Blocks.CRAFTING_TABLE)));
public static final RegistryObject<ExampleTickingBlock> EXAMPLE_TICKING_BLOCK = BLOCKS.register("example_ticking_block",
() -> new ExampleTickingBlock(BlockBehaviour.Properties.copy(Blocks.CRAFTING_TABLE)));
} }

View File

@ -0,0 +1,30 @@
package com.jenny.potionpills;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
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 org.jetbrains.annotations.NotNull;
import javax.annotation.Nullable;
public class ExampleTickingBlock extends Block implements EntityBlock {
public ExampleTickingBlock(Properties properties) {
super(properties);
}
@Override
public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState state) {
return BlockEntityInit.EXAMPLE_TICKING_BLOCK_ENTITY.get().create(pos, state);
}
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(@NotNull Level level, @NotNull BlockState state, @NotNull BlockEntityType<T> type) {
return TickableBlockEntity.getTickerHelper(level);
}
}

View File

@ -0,0 +1,19 @@
package com.jenny.potionpills;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
public class ExampleTickingBlockEntity extends BlockEntity implements TickableBlockEntity {
public ExampleTickingBlockEntity(BlockPos pos, BlockState state) {
super(BlockEntityInit.EXAMPLE_TICKING_BLOCK_ENTITY.get(), pos, state);
}
@Override
public void tick() {
if (this.level == null || this.level.isClientSide())
return;
System.out.println("Hello from \"tick\"!");
}
}

View File

@ -48,8 +48,10 @@ public class PotionPills
public static final RegistryObject<Block> EXAMPLE_BLOCK = BLOCKS.register("example_block", () -> new Block(BlockBehaviour.Properties.of().mapColor(MapColor.STONE))); public static final RegistryObject<Block> EXAMPLE_BLOCK = BLOCKS.register("example_block", () -> new Block(BlockBehaviour.Properties.of().mapColor(MapColor.STONE)));
// Creates a new BlockItem with the id "examplemod:example_block", combining the namespace and path // Creates a new BlockItem with the id "examplemod:example_block", combining the namespace and path
public static final RegistryObject<Item> EXAMPLE_BLOCK_ITEM = ITEMS.register("example_block", () -> new BlockItem(EXAMPLE_BLOCK.get(), new Item.Properties())); public static final RegistryObject<Item> EXAMPLE_BLOCK_ITEM = ITEMS.register("example_block", () -> new BlockItem(EXAMPLE_BLOCK.get(), new Item.Properties()));
public static final RegistryObject<Item> EXAMPLE_BLOCK_ENTITY_ITEM = ITEMS.register("example_block_entity", () -> new BlockItem(BlockInit.EXAMPLE_BLOCK_ENTITY.get(), new Item.Properties())); public static final RegistryObject<Item> EXAMPLE_BLOCK_ENTITY_ITEM = ITEMS.register("example_block_entity", () -> new BlockItem(BlockInit.EXAMPLE_BLOCK_ENTITY.get(), new Item.Properties()));
public static final RegistryObject<Item> EXAMPLE_TICKING_BLOCK_ITEM = ITEMS.register("example_ticking_block", () -> new BlockItem(BlockInit.EXAMPLE_TICKING_BLOCK.get(), new Item.Properties()));
// Creates a new food item with the id "examplemod:example_id", nutrition 1 and saturation 2 // Creates a new food item with the id "examplemod:example_id", nutrition 1 and saturation 2
public static final RegistryObject<Item> EXAMPLE_ITEM = ITEMS.register("example_item", () -> new Item(new Item.Properties().food(new FoodProperties.Builder() public static final RegistryObject<Item> EXAMPLE_ITEM = ITEMS.register("example_item", () -> new Item(new Item.Properties().food(new FoodProperties.Builder()
.alwaysEat().nutrition(1).saturationMod(2f).build()))); .alwaysEat().nutrition(1).saturationMod(2f).build())));
@ -61,6 +63,7 @@ public class PotionPills
.displayItems((parameters, output) -> { .displayItems((parameters, output) -> {
output.accept(EXAMPLE_ITEM.get()); output.accept(EXAMPLE_ITEM.get());
output.accept(EXAMPLE_BLOCK_ENTITY_ITEM.get()); output.accept(EXAMPLE_BLOCK_ENTITY_ITEM.get());
output.accept(EXAMPLE_TICKING_BLOCK_ITEM.get());
}).build()); }).build());
public PotionPills() public PotionPills()

View File

@ -0,0 +1,13 @@
package com.jenny.potionpills;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
public interface TickableBlockEntity {
void tick();
static <T extends BlockEntity> BlockEntityTicker<T> getTickerHelper(Level level) {
return level.isClientSide() ? null : (level0, pos0, state0, blockEntity) -> ((TickableBlockEntity)blockEntity).tick();
}
}