block state added
Some checks failed
build / build (push) Has been cancelled

This commit is contained in:
Jottyfan
2025-12-27 14:26:47 +01:00
parent d4654623f4
commit 042df08b9b
2 changed files with 41 additions and 37 deletions

View File

@@ -6,6 +6,8 @@ import net.minecraft.block.BlockState;
import net.minecraft.block.SweetBerryBushBlock;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.state.StateManager.Builder;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.util.ActionResult;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
@@ -14,9 +16,17 @@ import net.minecraft.world.WorldView;
import net.minecraft.world.event.GameEvent;
public class ChristmasTree extends SweetBerryBushBlock {
public static final BooleanProperty ACTIVATED = BooleanProperty.of("activated");
public ChristmasTree(Settings settings) {
super(settings);
super(settings.ticksRandomly().noCollision().luminance(state -> state.get(ACTIVATED) ? 15 : 0));
setDefaultState(getDefaultState().with(ACTIVATED, false));
}
@Override
protected void appendProperties(Builder<Block, BlockState> builder) {
builder.add(ACTIVATED);
super.appendProperties(builder);
}
@Override
@@ -26,14 +36,14 @@ public class ChristmasTree extends SweetBerryBushBlock {
@Override
protected ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
if (state.get(AGE) > 1) {
if (state.get(AGE) > 1 && state.get(ACTIVATED)) {
dropStack(world, pos, new ItemStack(ModItems.GINGERBREAD));
BlockState blockState = state.with(AGE, 1);
BlockState blockState = state.with(AGE, 0).cycle(ACTIVATED);
world.setBlockState(pos, blockState, Block.NOTIFY_LISTENERS);
world.emitGameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Emitter.of(player, blockState));
} else if (state.get(AGE) <= 1) {
world.setBlockState(pos, state.cycle(ACTIVATED));
}
return ActionResult.SUCCESS;
} else {
return super.onUse(state, world, pos, player, hit);
}
}
}

View File

@@ -14,28 +14,23 @@ import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.util.Identifier;
public class ModBlocks {
public static final Block RUBY_BLOCK = registerBlock(Identifier.of(Gtamfmd.MOD_ID, "ruby_block"),
AbstractBlock.Settings.create().strength(4f).requiresTool().sounds(BlockSoundGroup.AMETHYST_BLOCK));
public static final Block RUBY_BLOCK = registerBlock(Identifier.of(Gtamfmd.MOD_ID, "ruby_block"), x -> new Block(x));
public static final Block RUBY_ORE = registerBlock(Identifier.of(Gtamfmd.MOD_ID, "ruby_ore"),
AbstractBlock.Settings.create().strength(4f).requiresTool());
public static final Block CHRISTMASTREE = registerSpecialBlock(Identifier.of(Gtamfmd.MOD_ID, "christmastree"),
AbstractBlock.Settings.create().ticksRandomly().noCollision().luminance(state -> 15),
x -> new Block(x.strength(4).requiresTool()));
public static final Block CHRISTMASTREE = registerBlock(Identifier.of(Gtamfmd.MOD_ID, "christmastree"),
x -> new ChristmasTree(x));
// functional interface
private static Block registerSpecialBlock(Identifier identifier, Settings settings, Function<Settings, Block> function) {
Block block = function.apply(settings.registryKey(RegistryKey.of(RegistryKeys.BLOCK, identifier)));
registerBlockItem(identifier, block, new Item.Settings());
return Registry.register(Registries.BLOCK, identifier, block);
private static Block registerBlock(Identifier identifier, Function<Settings, Block> function) {
return registerBlock(identifier, AbstractBlock.Settings.create(), function);
}
private static Block registerBlock(Identifier identifier, Block.Settings settings) {
Block block = new Block(settings.registryKey(RegistryKey.of(RegistryKeys.BLOCK, identifier)));
// functional interface
private static Block registerBlock(Identifier identifier, Settings settings, Function<Settings, Block> function) {
Block block = function.apply(settings.registryKey(RegistryKey.of(RegistryKeys.BLOCK, identifier)));
registerBlockItem(identifier, block, new Item.Settings());
return Registry.register(Registries.BLOCK, identifier, block);
}
@@ -47,8 +42,7 @@ public class ModBlocks {
public static void registerModBlocks() {
Gtamfmd.LOGGER.info("Registering Mod Blocks for {}", Gtamfmd.MOD_ID);
ItemGroupEvents.modifyEntriesEvent(ItemGroups.BUILDING_BLOCKS)
.register(entries -> {
ItemGroupEvents.modifyEntriesEvent(ItemGroups.BUILDING_BLOCKS).register(entries -> {
entries.add(RUBY_BLOCK);
entries.add(RUBY_ORE);
});