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

优化component实现

This commit is contained in:
XiaoMoMi
2025-04-14 02:27:57 +08:00
parent b63625ba20
commit b327ed11d5
24 changed files with 433 additions and 129 deletions

View File

@@ -118,7 +118,7 @@ public class BukkitCustomItem implements CustomItem<ItemStack> {
@Override
public ItemSettings settings() {
return settings;
return this.settings;
}
@Override

View File

@@ -89,26 +89,6 @@ public abstract class BukkitItemFactory extends ItemFactory<CraftEngine, RTagIte
return item.remove(path);
}
@Override
protected void setComponent(ItemWrapper<ItemStack> item, String type, Object value) {
item.setComponent(type, value);
}
@Override
protected Object getComponent(ItemWrapper<ItemStack> item, String type) {
return item.getComponent(type);
}
@Override
protected boolean hasComponent(ItemWrapper<ItemStack> item, String type) {
return item.hasComponent(type);
}
@Override
protected void removeComponent(ItemWrapper<ItemStack> item, String type) {
item.removeComponent(type);
}
@Override
protected void update(ItemWrapper<ItemStack> item) {
item.update();

View File

@@ -1,14 +1,18 @@
package net.momirealms.craftengine.bukkit.item.factory;
import com.google.gson.JsonElement;
import com.saicone.rtag.RtagItem;
import com.saicone.rtag.data.ComponentType;
import com.saicone.rtag.item.ItemObject;
import net.momirealms.craftengine.bukkit.item.RTagItemWrapper;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.util.EnchantmentUtils;
import net.momirealms.craftengine.bukkit.util.KeyUtils;
import net.momirealms.craftengine.bukkit.util.Reflections;
import net.momirealms.craftengine.core.item.ComponentKeys;
import net.momirealms.craftengine.core.item.Enchantment;
import net.momirealms.craftengine.core.item.ItemWrapper;
import net.momirealms.craftengine.core.item.Trim;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.util.Key;
import org.bukkit.inventory.ItemStack;
@@ -26,17 +30,59 @@ public class ComponentItemFactory extends BukkitItemFactory {
super(plugin);
}
@Override
protected void setComponent(ItemWrapper<ItemStack> item, Key type, Object value) {
if (value instanceof JsonElement jsonElement) {
setJsonComponentDirectly(item, type, jsonElement);
} else {
setJavaComponentDirectly(item, type, value);
}
}
@Override
protected void resetComponent(ItemWrapper<ItemStack> item, Key type) {
FastNMS.INSTANCE.resetComponent(item.getLiteralObject(), KeyUtils.toResourceLocation(type));
}
@Override
protected Object getComponent(ItemWrapper<ItemStack> item, Key type) {
return item.getComponent(type);
}
@Override
protected boolean hasComponent(ItemWrapper<ItemStack> item, Key type) {
return item.hasComponent(type);
}
@Override
protected void removeComponent(ItemWrapper<ItemStack> item, Key type) {
FastNMS.INSTANCE.removeComponent(item.getLiteralObject(), KeyUtils.toResourceLocation(type));
}
protected void setJavaComponentDirectly(ItemWrapper<ItemStack> item, Key type, Object value) {
ComponentType.parseJava(type, value).ifPresent(it -> FastNMS.INSTANCE.setComponent(item.getLiteralObject(), KeyUtils.toResourceLocation(type), it));
}
protected void setJsonComponentDirectly(ItemWrapper<ItemStack> item, Key type, JsonElement value) {
ComponentType.parseJson(type, value).ifPresent(it -> FastNMS.INSTANCE.setComponent(item.getLiteralObject(), KeyUtils.toResourceLocation(type), it));
}
@Override
public Object encodeJava(Key componentType, @Nullable Object component) {
return ComponentType.encodeJava(componentType, component).orElse(null);
}
@Override
protected JsonElement encodeJson(Key type, Object component) {
return ComponentType.encodeJson(type, component).orElse(null);
}
@Override
protected void customModelData(ItemWrapper<ItemStack> item, Integer data) {
if (data == null) {
item.removeComponent(ComponentKeys.CUSTOM_MODEL_DATA);
resetComponent(item, ComponentKeys.CUSTOM_MODEL_DATA);
} else {
item.setComponent(ComponentKeys.CUSTOM_MODEL_DATA, data);
setJavaComponentDirectly(item, ComponentKeys.CUSTOM_MODEL_DATA, data);
}
}
@@ -53,9 +99,9 @@ public class ComponentItemFactory extends BukkitItemFactory {
@Override
protected void customName(ItemWrapper<ItemStack> item, String json) {
if (json == null) {
item.removeComponent(ComponentKeys.CUSTOM_NAME);
resetComponent(item, ComponentKeys.CUSTOM_NAME);
} else {
item.setComponent(ComponentKeys.CUSTOM_NAME, json);
setJavaComponentDirectly(item, ComponentKeys.CUSTOM_NAME, json);
}
}
@@ -73,9 +119,9 @@ public class ComponentItemFactory extends BukkitItemFactory {
@Override
protected void itemName(ItemWrapper<ItemStack> item, String json) {
if (json == null) {
item.removeComponent(ComponentKeys.ITEM_NAME);
resetComponent(item, ComponentKeys.ITEM_NAME);
} else {
item.setComponent(ComponentKeys.ITEM_NAME, json);
setJavaComponentDirectly(item, ComponentKeys.ITEM_NAME, json);
}
}
@@ -92,15 +138,12 @@ public class ComponentItemFactory extends BukkitItemFactory {
@Override
protected void skull(ItemWrapper<ItemStack> item, String skullData) {
final Map<String, Object> profile = Map.of(
"properties", List.of(
Map.of(
"name", "textures",
"value", skullData
)
)
);
item.setComponent("minecraft:profile", profile);
if (skullData == null) {
resetComponent(item, ComponentKeys.PROFILE);
} else {
Map<String, Object> profile = Map.of("properties", List.of(Map.of("name", "textures", "value", skullData)));
setJavaComponentDirectly(item, ComponentKeys.PROFILE, profile);
}
}
@SuppressWarnings("unchecked")
@@ -118,9 +161,9 @@ public class ComponentItemFactory extends BukkitItemFactory {
@Override
protected void lore(ItemWrapper<ItemStack> item, List<String> lore) {
if (lore == null || lore.isEmpty()) {
item.removeComponent(ComponentKeys.LORE);
resetComponent(item, ComponentKeys.LORE);
} else {
item.setComponent(ComponentKeys.LORE, lore);
setJavaComponentDirectly(item, ComponentKeys.LORE, lore);
}
}
@@ -132,9 +175,9 @@ public class ComponentItemFactory extends BukkitItemFactory {
@Override
protected void unbreakable(ItemWrapper<ItemStack> item, boolean unbreakable) {
if (unbreakable) {
item.removeComponent(ComponentKeys.UNBREAKABLE);
resetComponent(item, ComponentKeys.UNBREAKABLE);
} else {
item.setComponent(ComponentKeys.UNBREAKABLE, true);
setJavaComponentDirectly(item, ComponentKeys.UNBREAKABLE, true);
}
}
@@ -145,7 +188,11 @@ public class ComponentItemFactory extends BukkitItemFactory {
@Override
protected void glint(ItemWrapper<ItemStack> item, Boolean glint) {
item.setComponent(ComponentKeys.ENCHANTMENT_GLINT_OVERRIDE, glint);
if (glint == null) {
resetComponent(item, ComponentKeys.ENCHANTMENT_GLINT_OVERRIDE);
} else {
setJavaComponentDirectly(item, ComponentKeys.ENCHANTMENT_GLINT_OVERRIDE, glint);
}
}
@Override
@@ -161,8 +208,11 @@ public class ComponentItemFactory extends BukkitItemFactory {
@Override
protected void damage(ItemWrapper<ItemStack> item, Integer damage) {
if (damage == null) damage = 0;
item.setComponent(ComponentKeys.DAMAGE, damage);
if (damage == null) {
resetComponent(item, ComponentKeys.DAMAGE);
} else {
setJavaComponentDirectly(item, ComponentKeys.DAMAGE, damage);
}
}
@Override
@@ -179,9 +229,9 @@ public class ComponentItemFactory extends BukkitItemFactory {
@Override
protected void maxDamage(ItemWrapper<ItemStack> item, Integer damage) {
if (damage == null) {
item.removeComponent(ComponentKeys.MAX_DAMAGE);
resetComponent(item, ComponentKeys.MAX_DAMAGE);
} else {
item.setComponent(ComponentKeys.MAX_DAMAGE, damage);
setJavaComponentDirectly(item, ComponentKeys.MAX_DAMAGE, damage);
}
}
@@ -202,27 +252,27 @@ 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;
resetComponent(item, ComponentKeys.ENCHANTMENTS);
} else {
Map<String, Integer> enchants = new HashMap<>();
for (Enchantment enchantment : enchantments) {
enchants.put(enchantment.id().toString(), enchantment.level());
}
setJavaComponentDirectly(item, ComponentKeys.ENCHANTMENTS, enchants);
}
Map<String, Integer> enchants = new HashMap<>();
for (Enchantment enchantment : enchantments) {
enchants.put(enchantment.id().toString(), enchantment.level());
}
item.setComponent(ComponentKeys.ENCHANTMENTS, enchants);
}
@Override
protected void storedEnchantments(ItemWrapper<ItemStack> item, List<Enchantment> enchantments) {
if (enchantments == null || enchantments.isEmpty()) {
item.removeComponent(ComponentKeys.STORED_ENCHANTMENTS);
return;
resetComponent(item, ComponentKeys.STORED_ENCHANTMENTS);
} else {
Map<String, Integer> enchants = new HashMap<>();
for (Enchantment enchantment : enchantments) {
enchants.put(enchantment.id().toString(), enchantment.level());
}
setJavaComponentDirectly(item, ComponentKeys.STORED_ENCHANTMENTS, enchants);
}
Map<String, Integer> enchants = new HashMap<>();
for (Enchantment enchantment : enchantments) {
enchants.put(enchantment.id().toString(), enchantment.level());
}
item.setComponent(ComponentKeys.STORED_ENCHANTMENTS, enchants);
}
@Override
@@ -231,7 +281,7 @@ public class ComponentItemFactory extends BukkitItemFactory {
try {
Map<String, Integer> map = EnchantmentUtils.toMap(enchant);
map.put(enchantment.toString(), enchantment.level());
item.setComponent(ComponentKeys.ENCHANTMENTS, map);
setJavaComponentDirectly(item, ComponentKeys.ENCHANTMENTS, map);
} catch (ReflectiveOperationException e) {
plugin.logger().warn("Failed to add enchantment", e);
}
@@ -243,7 +293,7 @@ public class ComponentItemFactory extends BukkitItemFactory {
try {
Map<String, Integer> map = EnchantmentUtils.toMap(enchant);
map.put(enchantment.toString(), enchantment.level());
item.setComponent(ComponentKeys.STORED_ENCHANTMENTS, map);
setJavaComponentDirectly(item, ComponentKeys.STORED_ENCHANTMENTS, map);
} catch (ReflectiveOperationException e) {
plugin.logger().warn("Failed to add stored enchantment", e);
}
@@ -264,18 +314,18 @@ public class ComponentItemFactory extends BukkitItemFactory {
@Override
protected void maxStackSize(ItemWrapper<ItemStack> item, Integer maxStackSize) {
if (maxStackSize == null) {
item.removeComponent(ComponentKeys.MAX_STACK_SIZE);
resetComponent(item, ComponentKeys.MAX_STACK_SIZE);
} else {
item.setComponent(ComponentKeys.MAX_STACK_SIZE, maxStackSize);
setJavaComponentDirectly(item, ComponentKeys.MAX_STACK_SIZE, maxStackSize);
}
}
@Override
protected void repairCost(ItemWrapper<ItemStack> item, Integer data) {
if (data == null) {
item.removeComponent(ComponentKeys.REPAIR_COST);
resetComponent(item, ComponentKeys.REPAIR_COST);
} else {
item.setComponent(ComponentKeys.REPAIR_COST, data);
setJavaComponentDirectly(item, ComponentKeys.REPAIR_COST, data);
}
}
@@ -302,7 +352,6 @@ public class ComponentItemFactory extends BukkitItemFactory {
@Override
protected void merge(ItemWrapper<ItemStack> item1, ItemWrapper<ItemStack> item2) {
// load previous changes on nms items
item1.load();
Object itemStack1 = item1.getLiteralObject();
Object itemStack2 = item2.getLiteralObject();
try {
@@ -311,4 +360,28 @@ public class ComponentItemFactory extends BukkitItemFactory {
plugin.logger().warn("Failed to merge item", e);
}
}
@Override
protected void trim(ItemWrapper<ItemStack> item, Trim trim) {
if (trim == null) {
resetComponent(item, ComponentKeys.TRIM);
} else {
setJavaComponentDirectly(item, ComponentKeys.TRIM, Map.of(
"pattern", trim.pattern(),
"material", trim.material()
));
}
}
@Override
protected Optional<Trim> trim(ItemWrapper<ItemStack> item) {
if (!item.hasComponent(ComponentKeys.TRIM)) return Optional.empty();
Optional<Object> trim = ComponentType.encodeJava(ComponentKeys.TRIM, item.getComponent(ComponentKeys.TRIM));
if (trim.isEmpty()) {
return Optional.empty();
}
@SuppressWarnings("unchecked")
Map<String, String> trimMap = (Map<String, String>) trim.get();
return Optional.of(new Trim(trimMap.get("pattern"), trimMap.get("material")));
}
}

View File

@@ -33,9 +33,9 @@ public class ComponentItemFactory1_21_4 extends ComponentItemFactory {
@Override
protected void customModelData(ItemWrapper<ItemStack> item, Integer data) {
if (data == null) {
item.removeComponent(ComponentKeys.CUSTOM_MODEL_DATA);
resetComponent(item, ComponentKeys.CUSTOM_MODEL_DATA);
} else {
item.setComponent(ComponentKeys.CUSTOM_MODEL_DATA, Map.of("floats", List.of(data.floatValue())));
setComponent(item, ComponentKeys.CUSTOM_MODEL_DATA, Map.of("floats", List.of(data.floatValue())));
}
}
}

View File

@@ -26,9 +26,9 @@ public class ComponentItemFactory1_21_5 extends ComponentItemFactory1_21_4 {
@Override
protected void customName(ItemWrapper<ItemStack> item, String json) {
if (json == null) {
item.removeComponent(ComponentKeys.CUSTOM_NAME);
resetComponent(item, ComponentKeys.CUSTOM_NAME);
} else {
item.setComponent(ComponentKeys.CUSTOM_NAME, ChatComponent.toTag(ComponentUtils.jsonToMinecraft(json)));
setJavaComponentDirectly(item, ComponentKeys.CUSTOM_NAME, ChatComponent.toTag(ComponentUtils.jsonToMinecraft(json)));
}
}
@@ -41,9 +41,9 @@ public class ComponentItemFactory1_21_5 extends ComponentItemFactory1_21_4 {
@Override
protected void itemName(ItemWrapper<ItemStack> item, String json) {
if (json == null) {
item.removeComponent(ComponentKeys.ITEM_NAME);
resetComponent(item, ComponentKeys.ITEM_NAME);
} else {
item.setComponent(ComponentKeys.ITEM_NAME, ChatComponent.toTag(ComponentUtils.jsonToMinecraft(json)));
setJavaComponentDirectly(item, ComponentKeys.ITEM_NAME, ChatComponent.toTag(ComponentUtils.jsonToMinecraft(json)));
}
}
@@ -71,13 +71,13 @@ public class ComponentItemFactory1_21_5 extends ComponentItemFactory1_21_4 {
@Override
protected void lore(ItemWrapper<ItemStack> item, List<String> lore) {
if (lore == null || lore.isEmpty()) {
item.removeComponent(ComponentKeys.LORE);
resetComponent(item, ComponentKeys.LORE);
} else {
List<Object> loreTags = new ArrayList<>();
for (String json : lore) {
loreTags.add(ChatComponent.toTag(ComponentUtils.jsonToMinecraft(json)));
}
item.setComponent(ComponentKeys.LORE, TagList.newTag(loreTags));
setJavaComponentDirectly(item, ComponentKeys.LORE, TagList.newTag(loreTags));
}
}
}

View File

@@ -1,5 +1,6 @@
package net.momirealms.craftengine.bukkit.item.factory;
import com.google.gson.JsonElement;
import com.saicone.rtag.RtagItem;
import com.saicone.rtag.item.ItemObject;
import com.saicone.rtag.tag.TagBase;
@@ -8,6 +9,7 @@ import com.saicone.rtag.tag.TagList;
import net.momirealms.craftengine.bukkit.item.RTagItemWrapper;
import net.momirealms.craftengine.core.item.Enchantment;
import net.momirealms.craftengine.core.item.ItemWrapper;
import net.momirealms.craftengine.core.item.Trim;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.SkullUtils;
@@ -26,6 +28,36 @@ public class UniversalItemFactory extends BukkitItemFactory {
super(plugin);
}
@Override
protected void resetComponent(ItemWrapper<ItemStack> item, Key type) {
throw new UnsupportedOperationException("This feature is only available on 1.20.5+");
}
@Override
protected JsonElement encodeJson(Key type, Object component) {
throw new UnsupportedOperationException("This feature is only available on 1.20.5+");
}
@Override
protected void setComponent(ItemWrapper<ItemStack> item, Key type, Object value) {
throw new UnsupportedOperationException("This feature is only available on 1.20.5+");
}
@Override
protected Object getComponent(ItemWrapper<ItemStack> item, Key type) {
throw new UnsupportedOperationException("This feature is only available on 1.20.5+");
}
@Override
protected boolean hasComponent(ItemWrapper<ItemStack> item, Key type) {
throw new UnsupportedOperationException("This feature is only available on 1.20.5+");
}
@Override
protected void removeComponent(ItemWrapper<ItemStack> item, Key type) {
throw new UnsupportedOperationException("This feature is only available on 1.20.5+");
}
@Override
public Object encodeJava(Key componentType, @Nullable Object component) {
throw new UnsupportedOperationException("This feature is only available on 1.20.5+");
@@ -256,4 +288,22 @@ public class UniversalItemFactory extends BukkitItemFactory {
item1.load();
TagCompound.merge(ItemObject.getCustomDataTag(item1.getLiteralObject()), ItemObject.getCustomDataTag(item2.getLiteralObject()), true, true);
}
@Override
protected void trim(ItemWrapper<ItemStack> item, Trim trim) {
if (trim == null) {
item.remove("Trim");
return;
}
item.set(trim.material(), "Trim", "material");
item.set(trim.pattern(), "Trim", "pattern");
}
@Override
protected Optional<Trim> trim(ItemWrapper<ItemStack> item) {
String material = item.get("Trim", "material");
String pattern = item.get("Trim", "pattern");
if (material == null || pattern == null) return Optional.empty();
return Optional.of(new Trim(material, pattern));
}
}

View File

@@ -28,7 +28,8 @@ public class TestCommand extends BukkitCommandFeature<CommandSender> {
ItemStack itemStack = new ItemStack(Material.STONE);
RtagItem rtagItem = new RtagItem(itemStack);
rtagItem.setComponent(ComponentKeys.CUSTOM_DATA, Map.of("test1", "1"));
rtagItem.setComponent(ComponentKeys.CUSTOM_DATA, Map.of("test2", "2"));
rtagItem.removeComponent(ComponentKeys.CUSTOM_DATA);
rtagItem.removeComponent(ComponentKeys.LORE);
player.getInventory().addItem(rtagItem.load());
});
}