mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2026-01-04 15:41:38 +00:00
添加映射
This commit is contained in:
@@ -3,10 +3,7 @@ package net.momirealms.craftengine.bukkit.item;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
|
||||
import net.momirealms.craftengine.bukkit.util.MaterialUtils;
|
||||
import net.momirealms.craftengine.core.item.CustomItem;
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.item.ItemBuildContext;
|
||||
import net.momirealms.craftengine.core.item.ItemSettings;
|
||||
import net.momirealms.craftengine.core.item.*;
|
||||
import net.momirealms.craftengine.core.item.behavior.ItemBehavior;
|
||||
import net.momirealms.craftengine.core.item.modifier.ItemDataModifier;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
@@ -22,13 +19,15 @@ public class BukkitCustomItem implements CustomItem<ItemStack> {
|
||||
private final Key id;
|
||||
private final Key materialKey;
|
||||
private final Material material;
|
||||
private final List<ItemDataModifier<ItemStack>> modifiers;
|
||||
private final ItemDataModifier<ItemStack>[] modifiers;
|
||||
private final Map<String, ItemDataModifier<ItemStack>> modifierMap;
|
||||
private final List<ItemDataModifier<ItemStack>> clientBoundModifiers;
|
||||
private final ItemDataModifier<ItemStack>[] clientBoundModifiers;
|
||||
private final Map<String, ItemDataModifier<ItemStack>> clientBoundModifierMap;
|
||||
private final NetworkItemDataProcessor<ItemStack>[] networkItemDataProcessors;
|
||||
private final List<ItemBehavior> behaviors;
|
||||
private final ItemSettings settings;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public BukkitCustomItem(Key id,
|
||||
Key materialKey,
|
||||
Material material,
|
||||
@@ -39,8 +38,10 @@ public class BukkitCustomItem implements CustomItem<ItemStack> {
|
||||
this.id = id;
|
||||
this.material = material;
|
||||
this.materialKey = materialKey;
|
||||
this.modifiers = List.copyOf(modifiers);
|
||||
this.clientBoundModifiers = List.copyOf(clientBoundModifiers);
|
||||
// unchecked cast
|
||||
this.modifiers = modifiers.toArray(new ItemDataModifier[0]);
|
||||
// unchecked cast
|
||||
this.clientBoundModifiers = clientBoundModifiers.toArray(new ItemDataModifier[0]);
|
||||
this.behaviors = List.copyOf(behaviors);
|
||||
this.settings = settings;
|
||||
ImmutableMap.Builder<String, ItemDataModifier<ItemStack>> modifierMapBuilder = ImmutableMap.builder();
|
||||
@@ -49,10 +50,19 @@ public class BukkitCustomItem implements CustomItem<ItemStack> {
|
||||
}
|
||||
this.modifierMap = modifierMapBuilder.build();
|
||||
ImmutableMap.Builder<String, ItemDataModifier<ItemStack>> clientSideModifierMapBuilder = ImmutableMap.builder();
|
||||
List<NetworkItemDataProcessor<ItemStack>> networkItemDataProcessors = new ArrayList<>();
|
||||
for (ItemDataModifier<ItemStack> modifier : clientBoundModifiers) {
|
||||
clientSideModifierMapBuilder.put(modifier.name(), modifier);
|
||||
String name = modifier.name();
|
||||
clientSideModifierMapBuilder.put(name, modifier);
|
||||
if (this.modifierMap.containsKey(name)) {
|
||||
networkItemDataProcessors.add(NetworkItemDataProcessor.both(this.modifierMap.get(name), modifier));
|
||||
} else {
|
||||
networkItemDataProcessors.add(NetworkItemDataProcessor.clientOnly(modifier));
|
||||
}
|
||||
}
|
||||
this.clientBoundModifierMap = clientSideModifierMapBuilder.build();
|
||||
// unchecked cast
|
||||
this.networkItemDataProcessors = networkItemDataProcessors.toArray(new NetworkItemDataProcessor[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -66,7 +76,12 @@ public class BukkitCustomItem implements CustomItem<ItemStack> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ItemDataModifier<ItemStack>> dataModifiers() {
|
||||
public NetworkItemDataProcessor<ItemStack>[] networkItemDataProcessors() {
|
||||
return this.networkItemDataProcessors;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemDataModifier<ItemStack>[] dataModifiers() {
|
||||
return this.modifiers;
|
||||
}
|
||||
|
||||
@@ -77,11 +92,11 @@ public class BukkitCustomItem implements CustomItem<ItemStack> {
|
||||
|
||||
@Override
|
||||
public boolean hasClientBoundDataModifier() {
|
||||
return !this.clientBoundModifiers.isEmpty();
|
||||
return this.clientBoundModifiers.length != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ItemDataModifier<ItemStack>> clientBoundDataModifiers() {
|
||||
public ItemDataModifier<ItemStack>[] clientBoundDataModifiers() {
|
||||
return this.clientBoundModifiers;
|
||||
}
|
||||
|
||||
@@ -93,9 +108,6 @@ public class BukkitCustomItem implements CustomItem<ItemStack> {
|
||||
@Override
|
||||
public ItemStack buildItemStack(ItemBuildContext context, int count) {
|
||||
ItemStack item = new ItemStack(this.material);
|
||||
if (this.modifiers.isEmpty()) {
|
||||
return item;
|
||||
}
|
||||
Item<ItemStack> wrapped = BukkitCraftEngine.instance().itemManager().wrap(item);
|
||||
wrapped.count(count);
|
||||
for (ItemDataModifier<ItemStack> modifier : this.modifiers) {
|
||||
|
||||
@@ -201,6 +201,10 @@ public class ComponentItemFactory extends BukkitItemFactory {
|
||||
|
||||
@Override
|
||||
protected void enchantments(ItemWrapper<ItemStack> item, List<Enchantment> enchantments) {
|
||||
if (enchantments == null || enchantments.isEmpty()) {
|
||||
item.removeComponent(ComponentKeys.ENCHANTMENTS);
|
||||
return;
|
||||
}
|
||||
Map<String, Integer> enchants = new HashMap<>();
|
||||
for (Enchantment enchantment : enchantments) {
|
||||
enchants.put(enchantment.id().toString(), enchantment.level());
|
||||
@@ -210,6 +214,10 @@ public class ComponentItemFactory extends BukkitItemFactory {
|
||||
|
||||
@Override
|
||||
protected void storedEnchantments(ItemWrapper<ItemStack> item, List<Enchantment> enchantments) {
|
||||
if (enchantments == null || enchantments.isEmpty()) {
|
||||
item.removeComponent(ComponentKeys.STORED_ENCHANTMENTS);
|
||||
return;
|
||||
}
|
||||
Map<String, Integer> enchants = new HashMap<>();
|
||||
for (Enchantment enchantment : enchantments) {
|
||||
enchants.put(enchantment.id().toString(), enchantment.level());
|
||||
|
||||
@@ -142,6 +142,10 @@ public class UniversalItemFactory extends BukkitItemFactory {
|
||||
|
||||
@Override
|
||||
protected void enchantments(ItemWrapper<ItemStack> item, List<Enchantment> enchantments) {
|
||||
if (enchantments == null || enchantments.isEmpty()) {
|
||||
item.remove("Enchantments");
|
||||
return;
|
||||
}
|
||||
ArrayList<Object> tags = new ArrayList<>();
|
||||
for (Enchantment enchantment : enchantments) {
|
||||
tags.add((Map.of("id", enchantment.id().toString(), "lvl", (short) enchantment.level())));
|
||||
@@ -151,6 +155,10 @@ public class UniversalItemFactory extends BukkitItemFactory {
|
||||
|
||||
@Override
|
||||
protected void storedEnchantments(ItemWrapper<ItemStack> item, List<Enchantment> enchantments) {
|
||||
if (enchantments == null || enchantments.isEmpty()) {
|
||||
item.remove("StoredEnchantments");
|
||||
return;
|
||||
}
|
||||
ArrayList<Object> tags = new ArrayList<>();
|
||||
for (Enchantment enchantment : enchantments) {
|
||||
tags.add((Map.of("id", enchantment.id().toString(), "lvl", (short) enchantment.level())));
|
||||
|
||||
@@ -16,13 +16,15 @@ public interface CustomItem<I> extends BuildableItem<I> {
|
||||
|
||||
Key material();
|
||||
|
||||
List<ItemDataModifier<I>> dataModifiers();
|
||||
NetworkItemDataProcessor<I>[] networkItemDataProcessors();
|
||||
|
||||
ItemDataModifier<I>[] dataModifiers();
|
||||
|
||||
Map<String, ItemDataModifier<I>> dataModifierMap();
|
||||
|
||||
boolean hasClientBoundDataModifier();
|
||||
|
||||
List<ItemDataModifier<I>> clientBoundDataModifiers();
|
||||
ItemDataModifier<I>[] clientBoundDataModifiers();
|
||||
|
||||
Map<String, ItemDataModifier<I>> clientBoundDataModifierMap();
|
||||
|
||||
@@ -33,7 +35,7 @@ public interface CustomItem<I> extends BuildableItem<I> {
|
||||
}
|
||||
|
||||
default Item<I> buildItem(Player player) {
|
||||
return buildItem(new ItemBuildContext(player, ContextHolder.EMPTY));
|
||||
return buildItem(ItemBuildContext.of(player, ContextHolder.EMPTY));
|
||||
}
|
||||
|
||||
Item<I> buildItem(ItemBuildContext context);
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package net.momirealms.craftengine.core.item;
|
||||
|
||||
import net.momirealms.craftengine.core.item.modifier.ItemDataModifier;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public record NetworkItemDataProcessor<I>(@Nullable ItemDataModifier<I> server, @NotNull ItemDataModifier<I> client) {
|
||||
|
||||
public static <I> NetworkItemDataProcessor<I> clientOnly(ItemDataModifier<I> client) {
|
||||
return new NetworkItemDataProcessor<I>(null, client);
|
||||
}
|
||||
|
||||
public static <I> NetworkItemDataProcessor<I> both(ItemDataModifier<I> server, ItemDataModifier<I> client) {
|
||||
return new NetworkItemDataProcessor<I>(server, client);
|
||||
}
|
||||
|
||||
public void toClient(Item<I> item, ItemBuildContext context) {
|
||||
this.client.apply(item, context);
|
||||
}
|
||||
|
||||
public void toServer(Item<I> item, ItemBuildContext context) {
|
||||
this.client.remove(item);
|
||||
if (this.server != null) {
|
||||
this.server.apply(item, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,14 +2,21 @@ package net.momirealms.craftengine.core.item.modifier;
|
||||
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.item.ItemBuildContext;
|
||||
import net.momirealms.craftengine.core.util.Pair;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ComponentModifier<I> implements ItemDataModifier<I> {
|
||||
private final Map<String, Object> arguments;
|
||||
private final List<Pair<String, Object>> arguments;
|
||||
|
||||
public ComponentModifier(Map<String, Object> arguments) {
|
||||
this.arguments = arguments;
|
||||
List<Pair<String, Object>> pairs = new ArrayList<>(arguments.size());
|
||||
for (Map.Entry<String, Object> entry : arguments.entrySet()) {
|
||||
pairs.add(new Pair<>(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
this.arguments = pairs;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -19,10 +26,15 @@ public class ComponentModifier<I> implements ItemDataModifier<I> {
|
||||
|
||||
@Override
|
||||
public void apply(Item<I> item, ItemBuildContext context) {
|
||||
for (Map.Entry<String, Object> entry : arguments.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
item.setComponent(key, value);
|
||||
for (Pair<String, Object> entry : this.arguments) {
|
||||
item.setComponent(entry.left(), entry.right());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Item<I> item) {
|
||||
for (Pair<String, Object> entry : this.arguments) {
|
||||
item.removeComponent(entry.left());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,4 +19,9 @@ public class CustomModelDataModifier<I> implements ItemDataModifier<I> {
|
||||
public void apply(Item<I> item, ItemBuildContext context) {
|
||||
item.customModelData(argument);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Item<I> item) {
|
||||
item.customModelData(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,4 +21,9 @@ public class DisplayNameModifier<I> implements ItemDataModifier<I> {
|
||||
public void apply(Item<I> item, ItemBuildContext context) {
|
||||
item.customName(AdventureHelper.componentToJson(AdventureHelper.miniMessage().deserialize(this.argument, context.tagResolvers())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Item<I> item) {
|
||||
item.customName(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,4 +24,10 @@ public class EnchantmentModifier<I> implements ItemDataModifier<I> {
|
||||
if (item.vanillaId().equals(ItemKeys.ENCHANTED_BOOK)) item.setStoredEnchantments(enchantments);
|
||||
else item.setEnchantments(enchantments);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Item<I> item) {
|
||||
if (item.vanillaId().equals(ItemKeys.ENCHANTED_BOOK)) item.setStoredEnchantments(null);
|
||||
else item.setEnchantments(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,4 +21,9 @@ public class EquippableModifier<I> implements ItemDataModifier<I> {
|
||||
public void apply(Item<I> item, ItemBuildContext context) {
|
||||
item.setComponent(ComponentKeys.EQUIPPABLE, this.data.toMap());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Item<I> item) {
|
||||
item.removeComponent(ComponentKeys.EQUIPPABLE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,4 +30,9 @@ public class ExternalModifier<I> implements ItemDataModifier<I> {
|
||||
Item<I> anotherWrapped = (Item<I>) CraftEngine.instance().itemManager().wrap(another);
|
||||
item.merge(anotherWrapped);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Item<I> item) {
|
||||
// cannot remove
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,4 +21,9 @@ public class IdModifier<I> implements ItemDataModifier<I> {
|
||||
public void apply(Item<I> item, ItemBuildContext context) {
|
||||
item.setTag(argument.toString(), CRAFT_ENGINE_ID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Item<I> item) {
|
||||
item.removeTag(CRAFT_ENGINE_ID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,4 +8,6 @@ public interface ItemDataModifier<I> {
|
||||
String name();
|
||||
|
||||
void apply(Item<I> item, ItemBuildContext context);
|
||||
|
||||
void remove(Item<I> item);
|
||||
}
|
||||
|
||||
@@ -19,6 +19,11 @@ public class ItemModelModifier<I> implements ItemDataModifier<I> {
|
||||
|
||||
@Override
|
||||
public void apply(Item<I> item, ItemBuildContext context) {
|
||||
item.setComponent(ComponentKeys.ITEM_MODEL, this.data.toString());
|
||||
item.setComponent(ComponentKeys.ITEM_MODEL, this.data.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Item<I> item) {
|
||||
item.removeComponent(ComponentKeys.ITEM_MODEL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,4 +21,9 @@ public class ItemNameModifier<I> implements ItemDataModifier<I> {
|
||||
public void apply(Item<I> item, ItemBuildContext context) {
|
||||
item.itemName(AdventureHelper.componentToJson(AdventureHelper.miniMessage().deserialize(this.argument, context.tagResolvers())));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Item<I> item) {
|
||||
item.itemName(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,4 +31,9 @@ public class JukeboxSongModifier<I> implements ItemDataModifier<I> {
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Item<I> item) {
|
||||
item.removeComponent(ComponentKeys.JUKEBOX_PLAYABLE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,4 +24,9 @@ public class LoreModifier<I> implements ItemDataModifier<I> {
|
||||
item.lore(argument.stream().map(it -> AdventureHelper.componentToJson(AdventureHelper.miniMessage().deserialize(
|
||||
it, context.tagResolvers()))).toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Item<I> item) {
|
||||
item.lore(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,13 @@ public class TagsModifier<I> implements ItemDataModifier<I> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Item<I> item) {
|
||||
for (Map.Entry<String, Object> entry : arguments.entrySet()) {
|
||||
item.removeTag(entry.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, Object> mapToMap(final Map<String, Object> source) {
|
||||
Map<String, Object> resultMap = new LinkedHashMap<>();
|
||||
recursiveMapProcessing(source, resultMap);
|
||||
|
||||
@@ -21,4 +21,9 @@ public class TooltipStyleModifier<I> implements ItemDataModifier<I> {
|
||||
public void apply(Item<I> item, ItemBuildContext context) {
|
||||
item.setComponent(ComponentKeys.TOOLTIP_STYLE, argument.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Item<I> item) {
|
||||
item.removeComponent(ComponentKeys.TOOLTIP_STYLE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,4 +33,13 @@ public class TrimModifier<I> implements ItemDataModifier<I> {
|
||||
item.setTag(this.pattern, "Trim", "pattern");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Item<I> item) {
|
||||
if (VersionHelper.isVersionNewerThan1_20_5()) {
|
||||
item.removeComponent(ComponentKeys.TRIM);
|
||||
} else {
|
||||
item.removeTag("Trim");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,13 @@ public class UnbreakableModifier<I> implements ItemDataModifier<I> {
|
||||
|
||||
@Override
|
||||
public void apply(Item<I> item, ItemBuildContext context) {
|
||||
item.unbreakable(argument);
|
||||
item.unbreakable(this.argument);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Item<I> item) {
|
||||
if (this.argument) {
|
||||
item.unbreakable(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package net.momirealms.craftengine.core.util;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class ArrayUtils {
|
||||
@@ -59,4 +60,14 @@ public class ArrayUtils {
|
||||
public static boolean isEmpty(Object[] array) {
|
||||
return array == null || array.length == 0;
|
||||
}
|
||||
|
||||
public static <T> T[] collectionToArray(Collection<T> array, Class<T> clazz) {
|
||||
@SuppressWarnings("unchecked")
|
||||
T[] res = (T[]) Array.newInstance(clazz, array.size());
|
||||
int i = 0;
|
||||
for (T item : array) {
|
||||
res[i++] = item;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user