|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 AcesDephtPest 于 2024-8-28 21:24 编辑
兼容版本:目前最高版本-1.17.1
介绍:这是一个可以序列化的ItemStack对象,基本上可以兼容,也给那些想把ItemStack序列化储存进文件的后来人一点经验吧
本人新手,代码中的许多错误无法发现,如果你有更好的建议,希望可以分享分享awa
这是基于1.17.1的基础上做的改动,所以高版本的那些属性不可能完全的
以后的想法:使用Class<?>来执行/判断是否存在的接口来实现全版本通用
如果可以,请看第二页,那里是测试代码,但坑定是无法正常运行的,现在进度:映射Class实现全版本通用,
如果你可以花点时间看看第二页,顺便给点评价,那真是太好了doge
- public static class ItemStack implements Serializable {
- @Serial
- private static final long serialVersionUID = 1L;
- private Material type = Material.AIR;
- private int amount = 0;
- private List<String> lore = new ArrayList<>();
- private String displayName = null;
- private Map<String, Integer> enchantments = new HashMap<>(); // 存储附魔名称和级别
- private PotionType potionType = null; // 药水类型
- private boolean isSplash = false; // 是否为喷溅药水
- private String Variant = null; // 代表一桶蝾螈
- private List<Pattern> BannerMeta = null; // 旗帜的图案的列表
- private Location CompassMeta = null; // 代表能追踪指定位置的指南针
- private short Damageable = 0; // 代表有耐久度,可损耗的物品
- private Map<String, Integer> EnchantmentStorageMeta = new HashMap<>(); // 特定于专门存储附魔的物品,而非被附魔的物品. 附魔书 就是一个可存储附魔的物品的例子.
- private FireworkEffect FireworkEffectMeta = null; // 烟花效果
- private List<FireworkEffect> FireworkMeta = new ArrayList<>(); // 烟花火箭及其效果
- private List<NamespacedKey> KnowledgeBookMeta = new ArrayList<>(); //与知识之书有关的元数据
- private Color LeatherArmorMeta = null; // 下文中的"盔甲"可以是皮革靴子,外套,帽子,裤子任意一种,因为这个类代表了四种嘛
- private Color MapMetaColor = null; // 伸缩的地图
- private MapView MapMetaMapView = null; // 伸缩的地图
- private int Repairable = 0; //修复此物品所需的额外经验等级
- private OfflinePlayer SkullMetaOfflinePlayer = null; //头颅的主人
- private List<PotionEffect> SuspiciousStewMeta = new ArrayList<>(); //自定义药水效果的迷之炖菜
- private DyeColor TropicalFishBucketMetaBodyColor = null; //热带鱼桶
- private TropicalFish.Pattern TropicalFishBucketMetaPattern = null; //热带鱼桶
- private DyeColor TropicalFishBucketMetaPatternColor = null; // 热带鱼桶
- private String BookMetaAuthor = null; // 热带鱼桶
- private BookMeta.Generation BookMetaGeneration = null; // 热带鱼桶
- private List<String> BookMetaPages = null; // 热带鱼桶
- private String BookMetaTitle = null;
- public ItemStack(org.bukkit.inventory.ItemStack itemStack) {
- if (itemStack != null && itemStack.getType() != Material.AIR) {
- this.type = itemStack.getType();
- this.amount = itemStack.getAmount();
- getMeta(itemStack.getItemMeta());
- this.enchantments = new HashMap<>();
- for (Map.Entry<Enchantment, Integer> entry : itemStack.getEnchantments().entrySet()) {
- this.enchantments.put(entry.getKey().getKey().toString(), entry.getValue());
- }
- }
- }
- public org.bukkit.inventory.ItemStack toItemStack() {
- org.bukkit.inventory.ItemStack itemStack = new org.bukkit.inventory.ItemStack(this.type, this.amount);
- itemStack.setItemMeta(setMeta(itemStack.getItemMeta()));
- return itemStack;
- }
- public void getMeta(ItemMeta meta) {
- if (meta != null) {
- if (meta instanceof Damageable) this.Damageable = (short) ((Damageable) meta).getDamage();
- if (meta instanceof AxolotlBucketMeta) this.Variant = ((AxolotlBucketMeta) meta).getVariant().toString();
- if (meta instanceof BannerMeta) this.BannerMeta = ((BannerMeta) meta).getPatterns();
- if (meta instanceof CompassMeta && ((CompassMeta) meta).getLodestone() != null) this.CompassMeta = new Location(((CompassMeta) meta).getLodestone());
- if (meta instanceof EnchantmentStorageMeta) {
- this.EnchantmentStorageMeta = new HashMap<>();
- Map<Enchantment, Integer> list = ((EnchantmentStorageMeta) meta).getStoredEnchants();
- for (Map.Entry<Enchantment, Integer> entry : list.entrySet()) {
- this.EnchantmentStorageMeta.put(entry.getKey().getKey().toString(), entry.getValue());
- }
- }
- if (meta instanceof FireworkEffectMeta) this.FireworkEffectMeta = ((FireworkEffectMeta) meta).getEffect();
- if (meta instanceof FireworkMeta) this.FireworkMeta = ((FireworkMeta) meta).getEffects();
- if (meta instanceof KnowledgeBookMeta) this.KnowledgeBookMeta = ((KnowledgeBookMeta) meta).getRecipes();
- if (meta instanceof LeatherArmorMeta) this.LeatherArmorMeta = ((LeatherArmorMeta) meta).getColor();
- if (meta instanceof MapMeta) {
- this.MapMetaColor = ((MapMeta) meta).getColor();
- this.MapMetaMapView = ((MapMeta) meta).getMapView();
- }
- if (meta instanceof Repairable) this.Repairable = ((Repairable) meta).getRepairCost();
- if (meta instanceof SkullMeta) this.SkullMetaOfflinePlayer = ((SkullMeta) meta).getOwningPlayer();
- if (meta instanceof SuspiciousStewMeta) this.SuspiciousStewMeta = ((SuspiciousStewMeta) meta).getCustomEffects();
- if (meta instanceof TropicalFishBucketMeta) {
- this.TropicalFishBucketMetaPattern = ((TropicalFishBucketMeta) meta).getPattern();
- this.TropicalFishBucketMetaBodyColor = ((TropicalFishBucketMeta) meta).getBodyColor();
- this.TropicalFishBucketMetaPatternColor = ((TropicalFishBucketMeta) meta).getPatternColor();
- }
- if (meta instanceof BookMeta) {
- this.BookMetaAuthor = ((BookMeta) meta).getAuthor();
- this.BookMetaGeneration = ((BookMeta) meta).getGeneration();
- this.BookMetaPages = ((BookMeta) meta).getPages();
- this.BookMetaTitle = ((BookMeta) meta).getTitle();
- }
- this.lore = Objects.requireNonNull(meta).hasLore() ? new ArrayList<>(Objects.requireNonNull(meta.getLore())) : null;
- this.displayName = meta.hasDisplayName() ? meta.getDisplayName() : null;
- if (this.type.equals(Material.POTION) || this.type.equals(Material.SPLASH_POTION) || this.type.equals(Material.LINGERING_POTION)) {
- PotionMeta potionMeta = (PotionMeta) meta;
- this.potionType = potionMeta.getBasePotionData().getType();
- this.isSplash = this.type == Material.SPLASH_POTION;
- }
- }
- }
- public ItemMeta setMeta(ItemMeta meta) {
- if (meta != null) {
- if (this.displayName != null) meta.setDisplayName(this.displayName);
- if (this.lore != null) meta.setLore(this.lore);
- if (this.enchantments != null && !this.enchantments.isEmpty()) for (Map.Entry<String, Integer> entry : this.enchantments.entrySet()) {
- Enchantment enchantment = Enchantment.getByKey(NamespacedKey.fromString(entry.getKey()));
- if (enchantment != null) meta.addEnchant(enchantment, entry.getValue(), true);
- }
- if (meta instanceof Damageable && this.Damageable > 0) ((Damageable) meta).setDamage(this.Damageable);
- if (meta instanceof PotionMeta && this.potionType != null) {
- if (this.type.equals(Material.POTION) || this.type.equals(Material.LINGERING_POTION) || this.type.equals(Material.SPLASH_POTION)) {
- PotionData potionData = new PotionData(this.potionType,
- !this.isSplash && this.potionType.isExtendable(),
- this.isSplash && this.potionType.isUpgradeable());
- ((PotionMeta) meta).setBasePotionData(potionData);
- }
- }
- if (meta instanceof AxolotlBucketMeta && this.Variant != null) {
- ((AxolotlBucketMeta) meta).setVariant(Axolotl.Variant.valueOf(this.Variant));
- }
- if (meta instanceof BannerMeta && this.BannerMeta != null) {
- ((BannerMeta) meta).setPatterns(this.BannerMeta);
- }
- if (meta instanceof CompassMeta && this.CompassMeta != null) {
- ((CompassMeta) meta).setLodestone(this.CompassMeta.toLocation());
- }
- if (meta instanceof EnchantmentStorageMeta && this.EnchantmentStorageMeta != null && !this.EnchantmentStorageMeta.isEmpty()) for (Map.Entry<String, Integer> entry : this.EnchantmentStorageMeta.entrySet()) {
- Enchantment enchantment = Enchantment.getByKey(NamespacedKey.fromString(entry.getKey()));
- if (enchantment != null) ((EnchantmentStorageMeta) meta).addStoredEnchant(enchantment, entry.getValue(), true);
- }
- if (meta instanceof FireworkEffectMeta && this.FireworkEffectMeta != null) {
- ((FireworkEffectMeta) meta).setEffect(this.FireworkEffectMeta);
- }
- if (meta instanceof FireworkMeta && this.FireworkMeta != null) {
- ((FireworkMeta) meta).addEffects(this.FireworkMeta);
- }
- if (meta instanceof KnowledgeBookMeta && this.KnowledgeBookMeta != null) {
- ((KnowledgeBookMeta) meta).setRecipes(this.KnowledgeBookMeta);
- }
- if (meta instanceof LeatherArmorMeta && this.LeatherArmorMeta != null) {
- ((LeatherArmorMeta) meta).setColor(this.LeatherArmorMeta);
- }
- if (meta instanceof MapMeta) {
- if (this.MapMetaColor != null) ((MapMeta) meta).setColor(this.MapMetaColor);
- if (this.MapMetaMapView != null) ((MapMeta) meta).setMapView(this.MapMetaMapView);
- }
- if (meta instanceof Repairable && this.Repairable > 0) {
- ((Repairable) meta).setRepairCost(this.Repairable);
- }
- if (meta instanceof SkullMeta && this.SkullMetaOfflinePlayer != null) {
- ((SkullMeta) meta).setOwningPlayer(this.SkullMetaOfflinePlayer);
- }
- if (meta instanceof SuspiciousStewMeta && this.SuspiciousStewMeta != null) {
- for (PotionEffect data : this.SuspiciousStewMeta) ((SuspiciousStewMeta) meta).addCustomEffect(data,true);
- }
- if (meta instanceof TropicalFishBucketMeta) {
- if (this.TropicalFishBucketMetaBodyColor != null) ((TropicalFishBucketMeta) meta).setBodyColor(this.TropicalFishBucketMetaBodyColor);
- if (this.TropicalFishBucketMetaPattern != null) ((TropicalFishBucketMeta) meta).setPattern(this.TropicalFishBucketMetaPattern);
- if (this.TropicalFishBucketMetaPatternColor != null) ((TropicalFishBucketMeta) meta).setPatternColor(this.TropicalFishBucketMetaPatternColor);
- }
- if (meta instanceof BookMeta) {
- if (this.BookMetaAuthor != null) ((BookMeta) meta).setAuthor(this.BookMetaAuthor);
- if (this.BookMetaGeneration != null) ((BookMeta) meta).setGeneration(this.BookMetaGeneration);
- if (this.BookMetaPages != null) ((BookMeta) meta).setPages(this.BookMetaPages);
- if (this.BookMetaTitle != null) ((BookMeta) meta).setTitle(this.BookMetaTitle);
- }
- }
- return meta;
- }
- public static class Location implements Serializable {
- private final double x;
- private final double y;
- private final double z;
- private final World world;
- public Location(org.bukkit.Location location) {
- this.world = location.getWorld();
- this.x = location.getX();
- this.y = location.getY();
- this.z = location.getZ();
- }
- public org.bukkit.Location toLocation() {
- return new org.bukkit.Location(this.world,this.x,this.y,this.z);
- }
- }
- }
复制代码
|
|