mirror of
https://github.com/Xiao-MoMi/Custom-Crops.git
synced 2025-12-29 20:09:20 +00:00
增强兼容
This commit is contained in:
@@ -45,9 +45,12 @@ public abstract class BukkitItemFactory extends ItemFactory<CustomCropsPlugin, R
|
||||
return new UniversalItemFactory(plugin);
|
||||
}
|
||||
case "1.20.5", "1.20.6",
|
||||
"1.21", "1.21.1", "1.21.2", "1.21.3", "1.21.4", "1.21.5" -> {
|
||||
"1.21", "1.21.1", "1.21.2", "1.21.3", "1.21.4" -> {
|
||||
return new ComponentItemFactory(plugin);
|
||||
}
|
||||
case "1.21.5" -> {
|
||||
return new ComponentItemFactory1_21_5(plugin);
|
||||
}
|
||||
default -> throw new IllegalStateException("Unsupported server version: " + plugin.getServerVersion());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package net.momirealms.customcrops.bukkit.item;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.saicone.rtag.RtagItem;
|
||||
import com.saicone.rtag.data.ComponentType;
|
||||
import com.saicone.rtag.tag.TagList;
|
||||
import com.saicone.rtag.util.ChatComponent;
|
||||
import net.momirealms.customcrops.common.helper.GsonHelper;
|
||||
import net.momirealms.customcrops.common.item.ComponentKeys;
|
||||
import net.momirealms.customcrops.common.plugin.CustomCropsPlugin;
|
||||
import net.momirealms.sparrow.heart.SparrowHeart;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@SuppressWarnings("UnstableApiUsage")
|
||||
public class ComponentItemFactory1_21_5 extends ComponentItemFactory {
|
||||
|
||||
public ComponentItemFactory1_21_5(CustomCropsPlugin plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Optional<List<String>> lore(RtagItem item) {
|
||||
if (!item.hasComponent(ComponentKeys.LORE)) return Optional.empty();
|
||||
return ComponentType.encodeJson(
|
||||
ComponentKeys.LORE,
|
||||
item.getComponent(ComponentKeys.LORE)
|
||||
).map(list -> {
|
||||
List<String> lore = new ArrayList<>();
|
||||
for (JsonElement jsonElement : (JsonArray) list) {
|
||||
lore.add(GsonHelper.get().toJson(jsonElement));
|
||||
}
|
||||
return lore;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void lore(RtagItem item, List<String> lore) {
|
||||
if (lore == null || lore.isEmpty()) {
|
||||
item.removeComponent(ComponentKeys.LORE);
|
||||
} else {
|
||||
List<Object> loreTags = new ArrayList<>();
|
||||
for (String json : lore) {
|
||||
loreTags.add(ChatComponent.toTag(SparrowHeart.getInstance().getMinecraftComponent(json)));
|
||||
}
|
||||
item.setComponent(ComponentKeys.LORE, TagList.newTag(loreTags));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user