From 88cec66abb9f9223abc793edde5148fbfb2add33 Mon Sep 17 00:00:00 2001 From: XiaoMoMi Date: Fri, 21 Mar 2025 03:39:30 +0800 Subject: [PATCH] refactor default state provider --- .../item/behavior/BlockItemBehavior.java | 1 + .../craftengine/core/block/CustomBlock.java | 41 ++++------------ .../core/block/properties/Property.java | 47 +++++++++++++++++-- .../shared/block/BlockBehavior.java | 4 ++ 4 files changed, 57 insertions(+), 36 deletions(-) diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/behavior/BlockItemBehavior.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/behavior/BlockItemBehavior.java index 8e3e38923..af2690183 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/behavior/BlockItemBehavior.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/item/behavior/BlockItemBehavior.java @@ -125,6 +125,7 @@ public class BlockItemBehavior extends ItemBehavior { return InteractionResult.SUCCESS; } + // for child class to override @Nullable public BlockPlaceContext updatePlacementContext(BlockPlaceContext context) { return context; diff --git a/core/src/main/java/net/momirealms/craftengine/core/block/CustomBlock.java b/core/src/main/java/net/momirealms/craftengine/core/block/CustomBlock.java index d63ca24fe..2879ab25b 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/block/CustomBlock.java +++ b/core/src/main/java/net/momirealms/craftengine/core/block/CustomBlock.java @@ -6,8 +6,6 @@ import net.momirealms.craftengine.core.item.context.BlockPlaceContext; import net.momirealms.craftengine.core.loot.LootTable; import net.momirealms.craftengine.core.plugin.CraftEngine; import net.momirealms.craftengine.core.registry.Holder; -import net.momirealms.craftengine.core.util.Direction; -import net.momirealms.craftengine.core.util.HorizontalDirection; import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.shared.block.BlockBehavior; import net.momirealms.sparrow.nbt.CompoundTag; @@ -19,6 +17,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.function.BiFunction; public abstract class CustomBlock { protected final Holder holder; @@ -29,6 +28,7 @@ public abstract class CustomBlock { protected BlockBehavior behavior; @Nullable protected LootTable lootTable; + protected List> placements; public CustomBlock( @NotNull Key id, @@ -45,6 +45,7 @@ public abstract class CustomBlock { this.id = id; this.lootTable = lootTable; this.properties = properties; + this.placements = new ArrayList<>(); this.variantProvider = new BlockStateVariantProvider(holder, ImmutableBlockState::new, properties); this.setDefaultState(this.variantProvider.getDefaultState()); this.behavior = BlockBehaviors.fromMap(this, behaviorSettings); @@ -76,6 +77,9 @@ public abstract class CustomBlock { } } this.applyPlatformSettings(); + for (Map.Entry> propertyEntry : this.properties.entrySet()) { + this.placements.add(Property.createStateForPlacement(propertyEntry.getKey(), propertyEntry.getValue())); + } } protected abstract void applyPlatformSettings(); @@ -148,39 +152,12 @@ public abstract class CustomBlock { return this.defaultState; } - // TODO refactor this method - @SuppressWarnings("unchecked") public ImmutableBlockState getStateForPlacement(BlockPlaceContext context) { ImmutableBlockState state = getDefaultState(); - Property axisProperty = (Property) this.variantProvider.getProperty("axis"); - if (axisProperty != null) { - state = state.with(axisProperty, context.getClickedFace().axis()); + for (BiFunction placement : this.placements) { + state = placement.apply(context, state); } - Property directionProperty = this.variantProvider.getProperty("facing"); - if (directionProperty != null) { - if (directionProperty.valueClass() == HorizontalDirection.class) { - state = state.with((Property) directionProperty, context.getHorizontalDirection().opposite().toHorizontalDirection()); - } else if (directionProperty.valueClass() == Direction.class) { - state = state.with((Property) directionProperty, context.getNearestLookingDirection().opposite()); - } - } - Property waterloggedProperty = (Property) this.variantProvider.getProperty("waterlogged"); - if (waterloggedProperty != null) { - if (context.isWaterSource()) { - state = state.with(waterloggedProperty, true); - } - } - - //TODO Use default values - Property persistentProperty = (Property) this.variantProvider.getProperty("persistent"); - if (persistentProperty != null) { - state = state.with(persistentProperty, true); - } - Property distanceProperty = (Property) this.variantProvider.getProperty("distance"); - if (distanceProperty != null) { - state = state.with(distanceProperty, distanceProperty.defaultValue()); - } - //TODO state = state.behavior().getStateForPlacement(state, context); + state = (ImmutableBlockState) this.behavior.updateStateForPlacement(context, state); return state; } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/block/properties/Property.java b/core/src/main/java/net/momirealms/craftengine/core/block/properties/Property.java index d85dc93d1..78391ee8c 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/block/properties/Property.java +++ b/core/src/main/java/net/momirealms/craftengine/core/block/properties/Property.java @@ -1,13 +1,46 @@ package net.momirealms.craftengine.core.block.properties; +import net.momirealms.craftengine.core.block.ImmutableBlockState; +import net.momirealms.craftengine.core.item.context.BlockPlaceContext; +import net.momirealms.craftengine.core.util.Direction; +import net.momirealms.craftengine.core.util.HorizontalDirection; import net.momirealms.sparrow.nbt.Tag; import javax.annotation.Nullable; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiFunction; +import java.util.function.Function; +@SuppressWarnings("unchecked") public abstract class Property> { + public static final Map, BiFunction>> HARD_CODED_PLACEMENTS = new HashMap<>(); + + static { + HARD_CODED_PLACEMENTS.put("axis", (property -> { + Property axisProperty = (Property) property; + return (context, state) -> state.with(axisProperty, context.getClickedFace().axis()); + })); + HARD_CODED_PLACEMENTS.put("facing", (property -> { + if (property.valueClass() == HorizontalDirection.class) { + Property directionProperty = (Property) property; + return (context, state) -> state.with(directionProperty, context.getHorizontalDirection().opposite().toHorizontalDirection()); + } else if (property.valueClass() == Direction.class) { + Property directionProperty = (Property) property; + return (context, state) -> state.with(directionProperty, context.getNearestLookingDirection().opposite()); + } else { + throw new IllegalArgumentException("Unsupported property type used in hard-coded `facing` property: " + property.valueClass()); + } + })); + HARD_CODED_PLACEMENTS.put("waterlogged", (property -> { + Property waterloggedProperty = (Property) property; + return (context, state) -> state.with(waterloggedProperty, context.isWaterSource()); + })); + } + private final Class clazz; private final String name; @Nullable @@ -83,10 +116,10 @@ public abstract class Property> { return this.clazz; } - @Override - public boolean equals(Object object) { - return this == object; - } +// @Override +// public boolean equals(Object object) { +// return this == object; +// } @Override public final int hashCode() { @@ -115,4 +148,10 @@ public abstract class Property> { return this.property.name + "=" + this.property.valueName(this.value); } } + + public static BiFunction createStateForPlacement(String id, Property property) { + return Optional.ofNullable(HARD_CODED_PLACEMENTS.get(id)) + .map(it -> it.apply(property)) + .orElse(((context, state) -> ImmutableBlockState.with(state, property, property.defaultValue()))); + } } diff --git a/shared/src/main/java/net/momirealms/craftengine/shared/block/BlockBehavior.java b/shared/src/main/java/net/momirealms/craftengine/shared/block/BlockBehavior.java index 8e808f319..305439451 100644 --- a/shared/src/main/java/net/momirealms/craftengine/shared/block/BlockBehavior.java +++ b/shared/src/main/java/net/momirealms/craftengine/shared/block/BlockBehavior.java @@ -46,4 +46,8 @@ public abstract class BlockBehavior { public void performBoneMeal(Object thisBlock, Object[] args) throws Exception { } + + public Object updateStateForPlacement(Object context, Object state) { + return state; + } }