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

重构方块实体显示注册

This commit is contained in:
XiaoMoMi
2025-12-27 23:21:52 +08:00
parent 988f709c57
commit c90fad69f1
8 changed files with 47 additions and 33 deletions

View File

@@ -1,15 +1,14 @@
package net.momirealms.craftengine.bukkit.block.entity.renderer.element;
import net.momirealms.craftengine.core.block.entity.render.element.BlockEntityElementConfigType;
import net.momirealms.craftengine.core.block.entity.render.element.BlockEntityElementConfigs;
import net.momirealms.craftengine.core.util.Key;
public class BukkitBlockEntityElementConfigs extends BlockEntityElementConfigs {
static {
register(ITEM_DISPLAY, ItemDisplayBlockEntityElementConfig.FACTORY);
register(TEXT_DISPLAY, TextDisplayBlockEntityElementConfig.FACTORY);
register(ITEM, ItemBlockEntityElementConfig.FACTORY);
register(ARMOR_STAND, ArmorStandBlockEntityElementConfig.FACTORY);
}
public final class BukkitBlockEntityElementConfigs extends BlockEntityElementConfigs {
public static final BlockEntityElementConfigType<ItemDisplayBlockEntityElement> ITEM_DISPLAY = register(Key.ce("item_display"), ItemDisplayBlockEntityElementConfig.FACTORY);
public static final BlockEntityElementConfigType<TextDisplayBlockEntityElement> TEXT_DISPLAY = register(Key.ce("text_display"), TextDisplayBlockEntityElementConfig.FACTORY);
public static final BlockEntityElementConfigType<ItemBlockEntityElement> ITEM = register(Key.ce("item"), ItemBlockEntityElementConfig.FACTORY);
public static final BlockEntityElementConfigType<ArmorStandBlockEntityElement> ARMOR_STAND = register(Key.ce("armor_stand"), ArmorStandBlockEntityElementConfig.FACTORY);
private BukkitBlockEntityElementConfigs() {}

View File

@@ -6,7 +6,7 @@ import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.plugin.entityculling.CullingData;
import org.jetbrains.annotations.Nullable;
public class ConstantBlockEntityRenderer implements Cullable {
public final class ConstantBlockEntityRenderer implements Cullable {
private final BlockEntityElement[] elements;
public final CullingData cullingData;

View File

@@ -0,0 +1,6 @@
package net.momirealms.craftengine.core.block.entity.render.element;
import net.momirealms.craftengine.core.util.Key;
public record BlockEntityElementConfigType<E extends BlockEntityElement>(Key id, BlockEntityElementConfigFactory<E> factory) {
}

View File

@@ -11,30 +11,33 @@ import java.util.Map;
import java.util.Optional;
public abstract class BlockEntityElementConfigs {
public static final Key ITEM_DISPLAY = Key.of("craftengine:item_display");
public static final Key TEXT_DISPLAY = Key.of("craftengine:text_display");
public static final Key ITEM = Key.of("craftengine:item");
public static final Key ARMOR_STAND = Key.of("craftengine:armor_stand");
public static void register(Key key, BlockEntityElementConfigFactory<?> type) {
((WritableRegistry<BlockEntityElementConfigFactory<?>>) BuiltInRegistries.BLOCK_ENTITY_ELEMENT_TYPE)
protected BlockEntityElementConfigs() {}
public static <E extends BlockEntityElement> BlockEntityElementConfigType<E> register(Key key, BlockEntityElementConfigFactory<E> factory) {
BlockEntityElementConfigType<E> type = new BlockEntityElementConfigType<>(key, factory);
((WritableRegistry<BlockEntityElementConfigType<?>>) BuiltInRegistries.BLOCK_ENTITY_ELEMENT_TYPE)
.register(ResourceKey.create(Registries.BLOCK_ENTITY_ELEMENT_TYPE.location(), key), type);
return type;
}
public static <E extends BlockEntityElement> BlockEntityElementConfig<E> fromMap(Map<String, Object> arguments) {
Key type = Optional.ofNullable(arguments.get("type")).map(String::valueOf).map(it -> Key.withDefaultNamespace(it, "craftengine")).orElse(null);
if (type == null) {
if (arguments.containsKey("text")) {
type = TEXT_DISPLAY;
} else {
type = ITEM_DISPLAY;
}
}
Key type = guessType(arguments);
@SuppressWarnings("unchecked")
BlockEntityElementConfigFactory<E> factory = (BlockEntityElementConfigFactory<E>) BuiltInRegistries.BLOCK_ENTITY_ELEMENT_TYPE.getValue(type);
if (factory == null) {
BlockEntityElementConfigType<E> configType = (BlockEntityElementConfigType<E>) BuiltInRegistries.BLOCK_ENTITY_ELEMENT_TYPE.getValue(type);
if (configType == null) {
throw new LocalizedResourceConfigException("warning.config.block.state.entity_renderer.invalid_type", type.toString());
}
return factory.create(arguments);
return configType.factory().create(arguments);
}
private static Key guessType(Map<String, Object> arguments) {
return Key.ce(Optional.ofNullable(arguments.get("type")).map(String::valueOf).orElseGet(() -> {
if (arguments.containsKey("text")) {
return "text_display";
} else {
return "item_display";
}
}));
}
}

View File

@@ -18,12 +18,11 @@ public class ItemModelProcessor<I> implements SimpleNetworkItemProcessor<I> {
}
public Key data() {
return data;
return this.data;
}
@Override
public Item<I> apply(Item<I> item, ItemBuildContext context) {
if (!VersionHelper.isOrAbove1_21_2()) return item;
return item.itemModel(this.data.asString());
}

View File

@@ -4,6 +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.entity.render.element.BlockEntityElementConfigType;
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;
@@ -76,7 +77,7 @@ public final class BuiltInRegistries {
public static final Registry<ItemUpdaterType<?>> ITEM_UPDATER_TYPE = createConstantBoundRegistry(Registries.ITEM_UPDATER_TYPE, 16);
public static final Registry<NetworkCodec<FriendlyByteBuf, ? extends ModPacket>> MOD_PACKET = createConstantBoundRegistry(Registries.MOD_PACKET, 16);
public static final Registry<BlockEntityType<?>> BLOCK_ENTITY_TYPE = createConstantBoundRegistry(Registries.BLOCK_ENTITY_TYPE, 64);
public static final Registry<BlockEntityElementConfigFactory<?>> BLOCK_ENTITY_ELEMENT_TYPE = createConstantBoundRegistry(Registries.BLOCK_ENTITY_ELEMENT_TYPE, 16);
public static final Registry<BlockEntityElementConfigType<?>> BLOCK_ENTITY_ELEMENT_TYPE = createConstantBoundRegistry(Registries.BLOCK_ENTITY_ELEMENT_TYPE, 16);
public static final Registry<CraftRemainderFactory<?>> CRAFT_REMAINDER_FACTORY = createConstantBoundRegistry(Registries.CRAFT_REMAINDER_FACTORY, 16);
public static final Registry<FurnitureElementConfigFactory<?>> FURNITURE_ELEMENT_TYPE = createConstantBoundRegistry(Registries.FURNITURE_ELEMENT_TYPE, 16);
public static final Registry<FurnitureHitBoxConfigFactory<?>> FURNITURE_HITBOX_TYPE = createConstantBoundRegistry(Registries.FURNITURE_HITBOX_TYPE, 16);

View File

@@ -4,6 +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.entity.render.element.BlockEntityElementConfigType;
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;
@@ -80,7 +81,7 @@ public final class Registries {
public static final ResourceKey<Registry<ItemUpdaterType<?>>> ITEM_UPDATER_TYPE = ResourceKey.create(ROOT_REGISTRY, Key.withDefaultNamespace("item_updater_type"));
public static final ResourceKey<Registry<NetworkCodec<FriendlyByteBuf, ? extends ModPacket>>> MOD_PACKET = ResourceKey.create(ROOT_REGISTRY, Key.withDefaultNamespace("mod_packet_type"));
public static final ResourceKey<Registry<BlockEntityType<?>>> BLOCK_ENTITY_TYPE = ResourceKey.create(ROOT_REGISTRY, Key.withDefaultNamespace("block_entity_type"));
public static final ResourceKey<Registry<BlockEntityElementConfigFactory<?>>> BLOCK_ENTITY_ELEMENT_TYPE = ResourceKey.create(ROOT_REGISTRY, Key.withDefaultNamespace("block_entity_element_type"));
public static final ResourceKey<Registry<BlockEntityElementConfigType<?>>> BLOCK_ENTITY_ELEMENT_TYPE = ResourceKey.create(ROOT_REGISTRY, Key.withDefaultNamespace("block_entity_element_type"));
public static final ResourceKey<Registry<CraftRemainderFactory<?>>> CRAFT_REMAINDER_FACTORY = ResourceKey.create(ROOT_REGISTRY, Key.withDefaultNamespace("craft_remainder_factory"));
public static final ResourceKey<Registry<FurnitureElementConfigFactory<?>>> FURNITURE_ELEMENT_TYPE = ResourceKey.create(ROOT_REGISTRY, Key.withDefaultNamespace("furniture_element_type"));
public static final ResourceKey<Registry<FurnitureHitBoxConfigFactory<?>>> FURNITURE_HITBOX_TYPE = ResourceKey.create(ROOT_REGISTRY, Key.withDefaultNamespace("furniture_hitbox_type"));

View File

@@ -4,6 +4,7 @@ import org.jetbrains.annotations.NotNull;
public record Key(String namespace, String value) {
public static final String DEFAULT_NAMESPACE = "craftengine";
public static final String MINECRAFT_NAMESPACE = "minecraft";
public static Key withDefaultNamespace(String value) {
return new Key(DEFAULT_NAMESPACE, value);
@@ -22,11 +23,15 @@ public record Key(String namespace, String value) {
}
public static Key of(String namespacedId) {
return of(decompose(namespacedId, "minecraft"));
return of(decompose(namespacedId, MINECRAFT_NAMESPACE));
}
public static Key ce(String namespacedId) {
return of(decompose(namespacedId, DEFAULT_NAMESPACE));
}
public static Key from(String namespacedId) {
return of(decompose(namespacedId, "minecraft"));
return of(decompose(namespacedId, MINECRAFT_NAMESPACE));
}
public static Key fromNamespaceAndPath(String namespace, String path) {
@@ -65,7 +70,7 @@ public record Key(String namespace, String value) {
}
public String asMinimalString() {
if (this.namespace.equals("minecraft")) {
if (this.namespace.equals(MINECRAFT_NAMESPACE)) {
return this.value;
}
return asString();