9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-31 21:06:31 +00:00

重构方块状态属性注册表

This commit is contained in:
XiaoMoMi
2025-12-27 20:55:34 +08:00
parent 5c6b90eb18
commit ece2afed60
13 changed files with 74 additions and 67 deletions

View File

@@ -1,5 +1,6 @@
package net.momirealms.craftengine.core.block.properties;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.sparrow.nbt.ByteTag;
import net.momirealms.sparrow.nbt.Tag;
@@ -8,8 +9,9 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
public class BooleanProperty extends Property<Boolean> {
public final static Factory FACTORY = new Factory();
public final class BooleanProperty extends Property<Boolean> {
public static final Key ID = Key.of("craftengine:boolean");
public static final PropertyFactory<Boolean> FACTORY = new Factory();
private static final List<Boolean> VALUES = List.of(true, false);
private static final Boolean[] BY_ID = new Boolean[]{ Boolean.FALSE, Boolean.TRUE };
private static final ByteTag TRUE = new ByteTag((byte) 1);
@@ -81,9 +83,10 @@ public class BooleanProperty extends Property<Boolean> {
return new BooleanProperty(name, defaultValue);
}
public static class Factory implements PropertyFactory {
private static class Factory implements PropertyFactory<Boolean> {
@Override
public Property<?> create(String name, Map<String, Object> arguments) {
public Property<Boolean> create(String name, Map<String, Object> arguments) {
boolean bool = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("default", false), "default");
return BooleanProperty.create(name, bool);
}

View File

@@ -7,7 +7,7 @@ import net.momirealms.sparrow.nbt.Tag;
import java.lang.reflect.Array;
import java.util.*;
public class EnumProperty<T extends Enum<T>> extends Property<T> {
public final class EnumProperty<T extends Enum<T>> extends Property<T> {
private final List<T> values;
private final Map<String, T> names;
private final int[] ordinalToIndex;
@@ -106,7 +106,11 @@ public class EnumProperty<T extends Enum<T>> extends Property<T> {
return new EnumProperty<>(name, type, values, defaultValue);
}
public static class Factory<A extends Enum<A>> implements PropertyFactory {
public static <T extends Enum<T>> PropertyFactory<T> factory(Class<T> enumClass) {
return new Factory<>(enumClass);
}
private static class Factory<A extends Enum<A>> implements PropertyFactory<A> {
private final Class<A> enumClass;
public Factory(Class<A> enumClass) {
@@ -114,7 +118,7 @@ public class EnumProperty<T extends Enum<T>> extends Property<T> {
}
@Override
public Property<?> create(String name, Map<String, Object> arguments) {
public Property<A> create(String name, Map<String, Object> arguments) {
List<A> enums = Arrays.asList(enumClass.getEnumConstants());
String defaultValueName = arguments.getOrDefault("default", "").toString().toLowerCase(Locale.ENGLISH);
A defaultValue = enums.stream()

View File

@@ -2,6 +2,7 @@ package net.momirealms.craftengine.core.block.properties;
import it.unimi.dsi.fastutil.ints.IntImmutableList;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.sparrow.nbt.IntTag;
import net.momirealms.sparrow.nbt.NumericTag;
@@ -12,8 +13,9 @@ import java.util.Map;
import java.util.Optional;
import java.util.stream.IntStream;
public class IntegerProperty extends Property<Integer> {
public static final Factory FACTORY = new Factory();
public final class IntegerProperty extends Property<Integer> {
public static final Key ID = Key.of("craftengine:int");
public static final PropertyFactory<Integer> FACTORY = new Factory();
private final IntImmutableList values;
public final int min;
public final int max;
@@ -95,9 +97,10 @@ public class IntegerProperty extends Property<Integer> {
return new IntegerProperty(name, min, max, defaultValue);
}
public static class Factory implements PropertyFactory {
private static class Factory implements PropertyFactory<Integer> {
@Override
public Property<?> create(String name, Map<String, Object> arguments) {
public Property<Integer> create(String name, Map<String, Object> arguments) {
String range = arguments.getOrDefault("range", "1~1").toString();
String[] split = range.split("~");
if (split.length != 2) {

View File

@@ -10,49 +10,39 @@ import net.momirealms.craftengine.core.util.*;
import java.util.Map;
public final class Properties {
public static final Key BOOLEAN = Key.of("craftengine:boolean");
public static final Key INT = Key.of("craftengine:int");
public static final Key STRING = Key.of("craftengine:string");
public static final Key AXIS = Key.of("craftengine:axis");
public static final Key HORIZONTAL_DIRECTION = Key.of("craftengine:horizontal_direction");
public static final Key DIRECTION = Key.of("craftengine:direction");
public static final Key SINGLE_BLOCK_HALF = Key.of("craftengine:single_block_half");
public static final Key DOUBLE_BLOCK_HALF = Key.of("craftengine:double_block_half");
public static final Key HINGE = Key.of("craftengine:hinge");
public static final Key STAIRS_SHAPE = Key.of("craftengine:stairs_shape");
public static final Key SLAB_TYPE = Key.of("craftengine:slab_type");
public static final Key SOFA_SHAPE = Key.of("craftengine:sofa_shape");
public static final Key ANCHOR_TYPE = Key.of("craftengine:anchor_type");
public static final PropertyType<?> BOOLEAN = register(BooleanProperty.ID, BooleanProperty.FACTORY);
public static final PropertyType<?> INT = register(IntegerProperty.ID, IntegerProperty.FACTORY);
public static final PropertyType<?> STRING = register(StringProperty.ID, StringProperty.FACTORY);
public static final PropertyType<?> AXIS = register(Key.of("craftengine:axis"), EnumProperty.factory(Direction.Axis.class));
public static final PropertyType<?> HORIZONTAL_DIRECTION = register(Key.of("craftengine:horizontal_direction"), EnumProperty.factory(HorizontalDirection.class));
public static final PropertyType<?> FOUR_DIRECTION = register(Key.of("craftengine:4-direction"), EnumProperty.factory(HorizontalDirection.class));
public static final PropertyType<?> DIRECTION = register(Key.of("craftengine:direction"), EnumProperty.factory(Direction.class));
public static final PropertyType<?> SIX_DIRECTION = register(Key.of("craftengine:6-direction"), EnumProperty.factory(Direction.class));
public static final PropertyType<?> SINGLE_BLOCK_HALF = register(Key.of("craftengine:single_block_half"), EnumProperty.factory(SingleBlockHalf.class));
public static final PropertyType<?> DOUBLE_BLOCK_HALF = register(Key.of("craftengine:double_block_half"), EnumProperty.factory(DoubleBlockHalf.class));
public static final PropertyType<?> HINGE = register(Key.of("craftengine:hinge"), EnumProperty.factory(DoorHinge.class));
public static final PropertyType<?> STAIRS_SHAPE = register(Key.of("craftengine:stairs_shape"), EnumProperty.factory(StairsShape.class));
public static final PropertyType<?> SLAB_TYPE = register(Key.of("craftengine:slab_type"), EnumProperty.factory(SlabType.class));
public static final PropertyType<?> SOFA_SHAPE = register(Key.of("craftengine:sofa_shape"), EnumProperty.factory(SofaShape.class));
public static final PropertyType<?> ANCHOR_TYPE = register(Key.of("craftengine:anchor_type"), EnumProperty.factory(AnchorType.class));
static {
register(BOOLEAN, BooleanProperty.FACTORY);
register(INT, IntegerProperty.FACTORY);
register(STRING, StringProperty.FACTORY);
register(AXIS, new EnumProperty.Factory<>(Direction.Axis.class));
register(DIRECTION, new EnumProperty.Factory<>(Direction.class));
register(Key.of("craftengine:6-direction"), new EnumProperty.Factory<>(Direction.class));
register(HORIZONTAL_DIRECTION, new EnumProperty.Factory<>(HorizontalDirection.class));
register(Key.of("craftengine:4-direction"), new EnumProperty.Factory<>(HorizontalDirection.class));
register(SINGLE_BLOCK_HALF, new EnumProperty.Factory<>(SingleBlockHalf.class));
register(DOUBLE_BLOCK_HALF, new EnumProperty.Factory<>(DoubleBlockHalf.class));
register(HINGE, new EnumProperty.Factory<>(DoorHinge.class));
register(STAIRS_SHAPE, new EnumProperty.Factory<>(StairsShape.class));
register(SLAB_TYPE, new EnumProperty.Factory<>(SlabType.class));
register(SOFA_SHAPE, new EnumProperty.Factory<>(SofaShape.class));
register(ANCHOR_TYPE, new EnumProperty.Factory<>(AnchorType.class));
private Properties() {}
public static <T extends Comparable<T>> PropertyType<T> register(Key key, PropertyFactory<T> factory) {
PropertyType<T> type = new PropertyType<>(key, factory);
((WritableRegistry<PropertyType<?>>) BuiltInRegistries.PROPERTY_TYPE)
.register(ResourceKey.create(Registries.PROPERTY_TYPE.location(), key), type);
return type;
}
public static void register(Key key, PropertyFactory factory) {
((WritableRegistry<PropertyFactory>) BuiltInRegistries.PROPERTY_FACTORY).register(ResourceKey.create(Registries.PROPERTY_FACTORY.location(), key), factory);
}
public static Property<?> fromMap(String name, Map<String, Object> map) {
@SuppressWarnings("unchecked")
public static <T extends Comparable<T>> Property<T> fromMap(String name, Map<String, Object> map) {
String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("type"), "warning.config.block.state.property.missing_type");
Key key = Key.withDefaultNamespace(type, Key.DEFAULT_NAMESPACE);
PropertyFactory factory = BuiltInRegistries.PROPERTY_FACTORY.getValue(key);
if (factory == null) {
PropertyType<T> propertyType = (PropertyType<T>) BuiltInRegistries.PROPERTY_TYPE.getValue(key);
if (propertyType == null) {
throw new LocalizedResourceConfigException("warning.config.block.state.property.invalid_type", key.toString(), name);
}
return factory.create(name, map);
return propertyType.factory().create(name, map);
}
}

View File

@@ -2,7 +2,7 @@ package net.momirealms.craftengine.core.block.properties;
import java.util.Map;
public interface PropertyFactory {
public interface PropertyFactory<T extends Comparable<T>> {
Property<?> create(String name, Map<String, Object> arguments);
Property<T> create(String name, Map<String, Object> arguments);
}

View File

@@ -0,0 +1,6 @@
package net.momirealms.craftengine.core.block.properties;
import net.momirealms.craftengine.core.util.Key;
public record PropertyType<T extends Comparable<T>>(Key id, PropertyFactory<T> factory) {
}

View File

@@ -1,6 +1,7 @@
package net.momirealms.craftengine.core.block.properties;
import com.google.common.collect.ImmutableMap;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
import net.momirealms.sparrow.nbt.StringTag;
import net.momirealms.sparrow.nbt.Tag;
@@ -9,8 +10,9 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
public class StringProperty extends Property<String> {
public static final Factory FACTORY = new Factory();
public final class StringProperty extends Property<String> {
public static final Key ID = Key.of("craftengine:string");
public static final PropertyFactory<String> FACTORY = new Factory();
private final List<String> values;
private final ImmutableMap<String, String> names;
@@ -90,10 +92,10 @@ public class StringProperty extends Property<String> {
return new StringProperty(name, values, defaultValue);
}
public static class Factory implements PropertyFactory {
private static class Factory implements PropertyFactory<String> {
@Override
public Property<?> create(String name, Map<String, Object> arguments) {
public Property<String> create(String name, Map<String, Object> arguments) {
List<String> values = MiscUtils.getAsStringList(arguments.get("values"))
.stream()
.toList();

View File

@@ -9,11 +9,11 @@ import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.List;
import java.util.Map;
public class AlternativesLootEntryContainer<T> extends AbstractCompositeLootEntryContainer<T> {
public final class AlternativesLootEntryContainer<T> extends AbstractCompositeLootEntryContainer<T> {
public static final Key ID = Key.from("craftengine:alternatives");
public static final LootEntryContainerFactory<?> FACTORY = new Factory<>();
protected AlternativesLootEntryContainer(List<Condition<LootContext>> conditions, List<LootEntryContainer<T>> children) {
private AlternativesLootEntryContainer(List<Condition<LootContext>> conditions, List<LootEntryContainer<T>> children) {
super(conditions, children);
}

View File

@@ -11,11 +11,11 @@ import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
public class EmptyLoopEntryContainer<T> extends AbstractSingleLootEntryContainer<T> {
public final class EmptyLoopEntryContainer<T> extends AbstractSingleLootEntryContainer<T> {
public static final Key ID = Key.from("craftengine:empty");
public static final LootEntryContainerFactory<?> FACTORY = new Factory<>();
protected EmptyLoopEntryContainer(List<Condition<LootContext>> conditions, int weight, int quality) {
private EmptyLoopEntryContainer(List<Condition<LootContext>> conditions, int weight, int quality) {
super(conditions, null, weight, quality);
}

View File

@@ -13,12 +13,12 @@ import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
public class ExpLootEntryContainer<T> extends AbstractLootEntryContainer<T> {
public final class ExpLootEntryContainer<T> extends AbstractLootEntryContainer<T> {
public static final Key ID = Key.from("craftengine:exp");
public static final Factory<?> FACTORY = new Factory<>();
private final NumberProvider value;
protected ExpLootEntryContainer(NumberProvider value, List<Condition<LootContext>> conditions) {
private ExpLootEntryContainer(NumberProvider value, List<Condition<LootContext>> conditions) {
super(conditions);
this.value = value;
}

View File

@@ -16,11 +16,11 @@ import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
public class FurnitureItemLootEntryContainer<T> extends SingleItemLootEntryContainer<T> {
public final class FurnitureItemLootEntryContainer<T> extends SingleItemLootEntryContainer<T> {
public static final LootEntryContainerFactory<?> FACTORY = new Factory<>();
private final boolean hasFallback;
protected FurnitureItemLootEntryContainer(@Nullable Key item, List<Condition<LootContext>> conditions, List<LootFunction<T>> lootFunctions, int weight, int quality) {
private FurnitureItemLootEntryContainer(@Nullable Key item, List<Condition<LootContext>> conditions, List<LootFunction<T>> lootFunctions, int weight, int quality) {
super(item, conditions, lootFunctions, weight, quality);
this.hasFallback = item != null;
}

View File

@@ -4,7 +4,7 @@ import net.momirealms.craftengine.core.block.CustomBlock;
import net.momirealms.craftengine.core.block.behavior.BlockBehaviorType;
import net.momirealms.craftengine.core.block.entity.BlockEntityType;
import net.momirealms.craftengine.core.block.entity.render.element.BlockEntityElementConfigFactory;
import net.momirealms.craftengine.core.block.properties.PropertyFactory;
import net.momirealms.craftengine.core.block.properties.PropertyType;
import net.momirealms.craftengine.core.entity.furniture.behavior.FurnitureBehaviorType;
import net.momirealms.craftengine.core.entity.furniture.element.FurnitureElementConfigFactory;
import net.momirealms.craftengine.core.entity.furniture.hitbox.FurnitureHitBoxConfigFactory;
@@ -20,7 +20,6 @@ import net.momirealms.craftengine.core.item.recipe.network.modern.display.slot.S
import net.momirealms.craftengine.core.item.recipe.remainder.CraftRemainderFactory;
import net.momirealms.craftengine.core.item.recipe.result.PostProcessor;
import net.momirealms.craftengine.core.item.updater.ItemUpdaterType;
import net.momirealms.craftengine.core.loot.LootContext;
import net.momirealms.craftengine.core.loot.entry.LootEntryContainerType;
import net.momirealms.craftengine.core.loot.function.ApplyBonusCountFunction;
import net.momirealms.craftengine.core.loot.function.LootFunctionType;
@@ -49,7 +48,7 @@ public final class BuiltInRegistries {
public static final Registry<BlockBehaviorType> BLOCK_BEHAVIOR_TYPE = createConstantBoundRegistry(Registries.BLOCK_BEHAVIOR_TYPE, 64);
public static final Registry<ItemProcessorType<?>> ITEM_PROCESSOR_TYPE = createConstantBoundRegistry(Registries.ITEM_PROCESSOR_TYPE, 64);
public static final Registry<ItemBehaviorType> ITEM_BEHAVIOR_TYPE = createConstantBoundRegistry(Registries.ITEM_BEHAVIOR_TYPE, 64);
public static final Registry<PropertyFactory> PROPERTY_FACTORY = createConstantBoundRegistry(Registries.PROPERTY_FACTORY, 16);
public static final Registry<PropertyType<?>> PROPERTY_TYPE = createConstantBoundRegistry(Registries.PROPERTY_TYPE, 16);
public static final Registry<LootFunctionType<?>> LOOT_FUNCTION_TYPE = createConstantBoundRegistry(Registries.LOOT_FUNCTION_TYPE, 32);
public static final Registry<LootEntryContainerType<?>> LOOT_ENTRY_CONTAINER_TYPE = createConstantBoundRegistry(Registries.LOOT_ENTRY_CONTAINER_TYPE, 16);
public static final Registry<NumberProviderFactory> NUMBER_PROVIDER_FACTORY = createConstantBoundRegistry(Registries.NUMBER_PROVIDER_FACTORY, 16);

View File

@@ -4,7 +4,7 @@ import net.momirealms.craftengine.core.block.CustomBlock;
import net.momirealms.craftengine.core.block.behavior.BlockBehaviorType;
import net.momirealms.craftengine.core.block.entity.BlockEntityType;
import net.momirealms.craftengine.core.block.entity.render.element.BlockEntityElementConfigFactory;
import net.momirealms.craftengine.core.block.properties.PropertyFactory;
import net.momirealms.craftengine.core.block.properties.PropertyType;
import net.momirealms.craftengine.core.entity.furniture.behavior.FurnitureBehaviorType;
import net.momirealms.craftengine.core.entity.furniture.element.FurnitureElementConfigFactory;
import net.momirealms.craftengine.core.entity.furniture.hitbox.FurnitureHitBoxConfigFactory;
@@ -50,7 +50,7 @@ public final class Registries {
public static final Key ROOT_REGISTRY = Key.withDefaultNamespace("root");
public static final ResourceKey<Registry<CustomBlock>> BLOCK = ResourceKey.create(ROOT_REGISTRY, Key.withDefaultNamespace("block"));
public static final ResourceKey<Registry<ItemProcessorType<?>>> ITEM_PROCESSOR_TYPE = ResourceKey.create(ROOT_REGISTRY, Key.withDefaultNamespace("item_processor_type"));
public static final ResourceKey<Registry<PropertyFactory>> PROPERTY_FACTORY = ResourceKey.create(ROOT_REGISTRY, Key.withDefaultNamespace("property_factory"));
public static final ResourceKey<Registry<PropertyType<?>>> PROPERTY_TYPE = ResourceKey.create(ROOT_REGISTRY, Key.withDefaultNamespace("property_type"));
public static final ResourceKey<Registry<BlockBehaviorType>> BLOCK_BEHAVIOR_TYPE = ResourceKey.create(ROOT_REGISTRY, Key.withDefaultNamespace("block_behavior_type"));
public static final ResourceKey<Registry<ItemBehaviorType>> ITEM_BEHAVIOR_TYPE = ResourceKey.create(ROOT_REGISTRY, Key.withDefaultNamespace("item_behavior_type"));
public static final ResourceKey<Registry<LootFunctionType<?>>> LOOT_FUNCTION_TYPE = ResourceKey.create(ROOT_REGISTRY, Key.withDefaultNamespace("loot_function_type"));