mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-28 11:29:17 +00:00
添加block-state data类型
This commit is contained in:
@@ -7,6 +7,8 @@ import net.momirealms.craftengine.core.block.ImmutableBlockState;
|
||||
import net.momirealms.craftengine.core.block.properties.Property;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class BukkitCustomBlockStateWrapper extends AbstractBlockStateWrapper {
|
||||
@@ -51,6 +53,12 @@ public class BukkitCustomBlockStateWrapper extends AbstractBlockStateWrapper {
|
||||
return getImmutableBlockState().map(state -> state.owner().value().getProperty(propertyName) != null).orElse(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> getPropertyNames() {
|
||||
Optional<ImmutableBlockState> immutableBlockState = getImmutableBlockState();
|
||||
return immutableBlockState.<Collection<String>>map(state -> state.getProperties().stream().map(Property::name).toList()).orElseGet(List::of);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsString() {
|
||||
return getImmutableBlockState().map(ImmutableBlockState::toString).orElseGet(() -> BlockStateUtils.fromBlockData(super.blockState).getAsString());
|
||||
|
||||
@@ -8,6 +8,8 @@ import net.momirealms.craftengine.core.block.BlockStateWrapper;
|
||||
import net.momirealms.craftengine.core.block.StatePropertyAccessor;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class BukkitVanillaBlockStateWrapper extends AbstractBlockStateWrapper {
|
||||
private final StatePropertyAccessor accessor;
|
||||
|
||||
@@ -31,6 +33,11 @@ public class BukkitVanillaBlockStateWrapper extends AbstractBlockStateWrapper {
|
||||
return this.accessor.hasProperty(propertyName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> getPropertyNames() {
|
||||
return this.accessor.getPropertyNames();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAsString() {
|
||||
return BlockStateUtils.fromBlockData(super.blockState).getAsString();
|
||||
|
||||
@@ -18,7 +18,6 @@ import net.momirealms.sparrow.nbt.CompoundTag;
|
||||
import net.momirealms.sparrow.nbt.ListTag;
|
||||
import net.momirealms.sparrow.nbt.StringTag;
|
||||
import net.momirealms.sparrow.nbt.Tag;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -609,4 +609,14 @@ public class ComponentItemFactory1_20_5 extends BukkitItemFactory<ComponentItemW
|
||||
}
|
||||
item.setSparrowNBTComponent(ComponentKeys.ATTRIBUTE_MODIFIERS, compoundTag);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Optional<Map<String, String>> blockState(ComponentItemWrapper item) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void blockState(ComponentItemWrapper item, Map<String, String> state) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.sparrow.nbt.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
public interface BlockStateWrapper extends Comparable<BlockStateWrapper> {
|
||||
@@ -18,6 +19,8 @@ public interface BlockStateWrapper extends Comparable<BlockStateWrapper> {
|
||||
|
||||
boolean hasProperty(String propertyName);
|
||||
|
||||
Collection<String> getPropertyNames();
|
||||
|
||||
BlockStateWrapper withProperty(String propertyName, String propertyValue);
|
||||
|
||||
String getAsString();
|
||||
|
||||
@@ -17,6 +17,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class AbstractItem<W extends ItemWrapper<I>, I> implements Item<I> {
|
||||
@@ -110,6 +111,17 @@ public class AbstractItem<W extends ItemWrapper<I>, I> implements Item<I> {
|
||||
return this.factory.maxDamage(this.item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item<I> blockState(Map<String, String> state) {
|
||||
this.factory.blockState(this.item, state);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Map<String, String>> blockState() {
|
||||
return this.factory.blockState(this.item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item<I> dyedColor(Color data) {
|
||||
this.factory.dyedColor(this.item, data);
|
||||
|
||||
@@ -18,6 +18,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
@@ -76,6 +77,10 @@ public interface Item<I> {
|
||||
|
||||
int maxDamage();
|
||||
|
||||
Item<I> blockState(Map<String, String> state);
|
||||
|
||||
Optional<Map<String, String>> blockState();
|
||||
|
||||
// todo 考虑部分版本的show in tooltip保留
|
||||
Item<I> dyedColor(Color data);
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import net.momirealms.craftengine.core.util.UniqueKey;
|
||||
import net.momirealms.sparrow.nbt.Tag;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -216,4 +217,8 @@ public abstract class ItemFactory<W extends ItemWrapper<I>, I> {
|
||||
protected abstract UniqueKey recipeIngredientID(W item);
|
||||
|
||||
protected abstract void attributeModifiers(W item, List<AttributeModifier> modifiers);
|
||||
|
||||
protected abstract Optional<Map<String, String>> blockState(W item);
|
||||
|
||||
protected abstract void blockState(W item, Map<String, String> state);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import java.util.*;
|
||||
public class AttributeModifiersModifier<I> implements SimpleNetworkItemDataModifier<I> {
|
||||
public static final Factory<?> FACTORY = new Factory<>();
|
||||
public static final Map<Key, Key> CONVERTOR = new HashMap<>();
|
||||
private static final Object[] NBT_PATH = new Object[]{"AttributeModifiers"};
|
||||
|
||||
static {
|
||||
if (VersionHelper.isOrAbove1_21_2()) {
|
||||
@@ -119,7 +120,7 @@ public class AttributeModifiersModifier<I> implements SimpleNetworkItemDataModif
|
||||
|
||||
@Override
|
||||
public @Nullable Object[] nbtPath(Item<I> item, ItemBuildContext context) {
|
||||
return new Object[]{"AttributeModifiers"};
|
||||
return NBT_PATH;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package net.momirealms.craftengine.core.item.modifier;
|
||||
|
||||
import net.momirealms.craftengine.core.block.BlockStateWrapper;
|
||||
import net.momirealms.craftengine.core.item.ComponentKeys;
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.item.ItemBuildContext;
|
||||
import net.momirealms.craftengine.core.item.ItemDataModifierFactory;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.LazyReference;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class BlockStateModifier<I> implements SimpleNetworkItemDataModifier<I> {
|
||||
public static final Factory<?> FACTORY = new Factory<>();
|
||||
private static final Object[] NBT_PATH = new Object[]{"BlockStateTag"};
|
||||
private final LazyReference<Map<String, String>> wrapper;
|
||||
|
||||
public BlockStateModifier(LazyReference<Map<String, String>> wrapper) {
|
||||
this.wrapper = wrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key type() {
|
||||
return ItemDataModifiers.BLOCK_STATE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item<I> apply(Item<I> item, ItemBuildContext context) {
|
||||
return item.blockState(this.wrapper.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Object[] nbtPath(Item<I> item, ItemBuildContext context) {
|
||||
return NBT_PATH;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String nbtPathString(Item<I> item, ItemBuildContext context) {
|
||||
return "BlockStateTag";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key componentType(Item<I> item, ItemBuildContext context) {
|
||||
return ComponentKeys.BLOCK_STATE;
|
||||
}
|
||||
|
||||
public static class Factory<I> implements ItemDataModifierFactory<I> {
|
||||
|
||||
@Override
|
||||
public ItemDataModifier<I> create(Object arg) {
|
||||
if (arg instanceof Map<?, ?> map) {
|
||||
Map<String, String> properties = new HashMap<>();
|
||||
for (Map.Entry<?, ?> entry : map.entrySet()) {
|
||||
properties.put(entry.getKey().toString(), entry.getValue().toString());
|
||||
}
|
||||
return new BlockStateModifier<>(LazyReference.lazyReference(() -> properties));
|
||||
} else {
|
||||
String stateString = arg.toString();
|
||||
return new BlockStateModifier<>(LazyReference.lazyReference(() -> {
|
||||
BlockStateWrapper blockState = CraftEngine.instance().blockManager().createBlockState(stateString);
|
||||
if (blockState != null) {
|
||||
Map<String, String> properties = new HashMap<>(4);
|
||||
for (String property : blockState.getPropertyNames()) {
|
||||
properties.put(property, blockState.getProperty(property));
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
return Collections.emptyMap();
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class CustomNameModifier<I> implements SimpleNetworkItemDataModifier<I> {
|
||||
public static final Factory<?> FACTORY = new Factory<>();
|
||||
private static final Object[] NBT_PATH = new Object[]{"display", "Name"};
|
||||
private final String argument;
|
||||
private final FormattedLine line;
|
||||
|
||||
@@ -49,7 +50,7 @@ public class CustomNameModifier<I> implements SimpleNetworkItemDataModifier<I> {
|
||||
|
||||
@Override
|
||||
public @Nullable Object[] nbtPath(Item<I> item, ItemBuildContext context) {
|
||||
return new Object[]{"display", "Name"};
|
||||
return NBT_PATH;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.joml.Vector3f;
|
||||
|
||||
public class DyedColorModifier<I> implements SimpleNetworkItemDataModifier<I> {
|
||||
public static final Factory<?> FACTORY = new Factory<>();
|
||||
private static final Object[] NBT_PATH = new Object[]{"display", "color"};
|
||||
private final Color color;
|
||||
|
||||
public DyedColorModifier(Color color) {
|
||||
@@ -39,7 +40,7 @@ public class DyedColorModifier<I> implements SimpleNetworkItemDataModifier<I> {
|
||||
|
||||
@Override
|
||||
public @Nullable Object[] nbtPath(Item<I> item, ItemBuildContext context) {
|
||||
return new Object[]{"display", "color"};
|
||||
return NBT_PATH;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -12,6 +12,8 @@ import java.util.Map;
|
||||
|
||||
public class EnchantmentsModifier<I> implements SimpleNetworkItemDataModifier<I> {
|
||||
public static final Factory<?> FACTORY = new Factory<>();
|
||||
private static final Object[] STORED_ENCHANTMENTS = new Object[] {"StoredEnchantments"};
|
||||
private static final Object[] ENCHANTMENTS = new Object[] {"Enchantments"};
|
||||
private final List<Enchantment> enchantments;
|
||||
|
||||
public EnchantmentsModifier(List<Enchantment> enchantments) {
|
||||
@@ -43,7 +45,7 @@ public class EnchantmentsModifier<I> implements SimpleNetworkItemDataModifier<I>
|
||||
|
||||
@Override
|
||||
public @Nullable Object[] nbtPath(Item<I> item, ItemBuildContext context) {
|
||||
return item.vanillaId().equals(ItemKeys.ENCHANTED_BOOK) ? new Object[]{"StoredEnchantments"} : new Object[]{"Enchantments"};
|
||||
return item.vanillaId().equals(ItemKeys.ENCHANTED_BOOK) ? STORED_ENCHANTMENTS : ENCHANTMENTS;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -46,6 +46,7 @@ public final class ItemDataModifiers {
|
||||
public static final Key DYNAMIC_LORE = Key.of("craftengine:dynamic-lore");
|
||||
public static final Key OVERWRITABLE_LORE = Key.of("craftengine:overwritable-lore");
|
||||
public static final Key MAX_DAMAGE = Key.of("craftengine:max-damage");
|
||||
public static final Key BLOCK_STATE = Key.of("craftengine:block-state");
|
||||
|
||||
public static <T> void register(Key key, ItemDataModifierFactory<T> factory) {
|
||||
((WritableRegistry<ItemDataModifierFactory<?>>) BuiltInRegistries.ITEM_DATA_MODIFIER_FACTORY)
|
||||
@@ -77,6 +78,7 @@ public final class ItemDataModifiers {
|
||||
register(ARGUMENTS, ArgumentsModifier.FACTORY);
|
||||
register(OVERWRITABLE_ITEM_NAME, OverwritableItemNameModifier.FACTORY);
|
||||
register(PDC, PDCModifier.FACTORY);
|
||||
register(BLOCK_STATE, BlockStateModifier.FACTORY);
|
||||
if (VersionHelper.isOrAbove1_20_5()) {
|
||||
register(CUSTOM_NAME, CustomNameModifier.FACTORY);
|
||||
register(ITEM_NAME, ItemNameModifier.FACTORY);
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ItemNameModifier<I> implements SimpleNetworkItemDataModifier<I> {
|
||||
public static final Factory<?> FACTORY = new Factory<>();
|
||||
private static final Object[] NBT_PATH = new Object[]{"display", "Name"};
|
||||
private final String argument;
|
||||
private final FormattedLine line;
|
||||
|
||||
@@ -40,7 +41,7 @@ public class ItemNameModifier<I> implements SimpleNetworkItemDataModifier<I> {
|
||||
|
||||
@Override
|
||||
public @Nullable Object[] nbtPath(Item<I> item, ItemBuildContext context) {
|
||||
return new Object[]{"display", "Name"};
|
||||
return NBT_PATH;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.util.Map;
|
||||
|
||||
public class TrimModifier<I> implements SimpleNetworkItemDataModifier<I> {
|
||||
public static final Factory<?> FACTORY = new Factory<>();
|
||||
private static final Object[] NBT_PATH = new Object[] {"Trim"};
|
||||
private final Key material;
|
||||
private final Key pattern;
|
||||
|
||||
@@ -47,7 +48,7 @@ public class TrimModifier<I> implements SimpleNetworkItemDataModifier<I> {
|
||||
|
||||
@Override
|
||||
public @Nullable Object[] nbtPath(Item<I> item, ItemBuildContext context) {
|
||||
return new Object[]{"Trim"};
|
||||
return NBT_PATH;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class UnbreakableModifier<I> implements SimpleNetworkItemDataModifier<I> {
|
||||
public static final Factory<?> FACTORY = new Factory<>();
|
||||
private static final Object[] NBT_PATH = new Object[]{"Unbreakable"};
|
||||
private final boolean argument;
|
||||
|
||||
public UnbreakableModifier(boolean argument) {
|
||||
@@ -38,7 +39,7 @@ public class UnbreakableModifier<I> implements SimpleNetworkItemDataModifier<I>
|
||||
|
||||
@Override
|
||||
public @Nullable Object[] nbtPath(Item<I> item, ItemBuildContext context) {
|
||||
return new Object[]{"Unbreakable"};
|
||||
return NBT_PATH;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user