mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-31 12:56:28 +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())));
|
||||
|
||||
Reference in New Issue
Block a user