Memorial Edition

查看: 337|回复: 7

ItemStack(序列化版)

[复制链接]

Lv.5 农夫

人气
17 点
金粒
132 粒
宝石
1 颗
爱心
0 颗
钻石
87 颗
贡献
0 点

石镐矿工勋章

发表于 2024-8-26 01:58:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 AcesDephtPest 于 2024-8-28 21:24 编辑

兼容版本:目前最高版本-1.17.1


介绍:这是一个可以序列化的ItemStack对象,基本上可以兼容,也给那些想把ItemStack序列化储存进文件的后来人一点经验吧
本人新手,代码中的许多错误无法发现,如果你有更好的建议,希望可以分享分享awa
这是基于1.17.1的基础上做的改动,所以高版本的那些属性不可能完全的
以后的想法:使用Class<?>来执行/判断是否存在的接口来实现全版本通用
如果可以,请看第二页,那里是测试代码,但坑定是无法正常运行的,现在进度:映射Class实现全版本通用,
如果你可以花点时间看看第二页,顺便给点评价,那真是太好了doge

  1. public static class ItemStack implements Serializable {
  2.     @Serial
  3.     private static final long serialVersionUID = 1L;
  4.     private Material type = Material.AIR;
  5.     private int amount = 0;
  6.     private List<String> lore = new ArrayList<>();
  7.     private String displayName = null;
  8.     private Map<String, Integer> enchantments = new HashMap<>(); // 存储附魔名称和级别
  9.     private PotionType potionType = null; // 药水类型
  10.     private boolean isSplash = false; // 是否为喷溅药水
  11.     private String Variant = null; // 代表一桶蝾螈
  12.     private List<Pattern> BannerMeta = null; // 旗帜的图案的列表
  13.     private Location CompassMeta = null; // 代表能追踪指定位置的指南针
  14.     private short Damageable = 0; // 代表有耐久度,可损耗的物品
  15.     private Map<String, Integer> EnchantmentStorageMeta = new HashMap<>(); // 特定于专门存储附魔的物品,而非被附魔的物品. 附魔书 就是一个可存储附魔的物品的例子.
  16.     private FireworkEffect FireworkEffectMeta = null; // 烟花效果
  17.     private List<FireworkEffect> FireworkMeta = new ArrayList<>(); // 烟花火箭及其效果
  18.     private List<NamespacedKey> KnowledgeBookMeta = new ArrayList<>(); //与知识之书有关的元数据
  19.     private Color LeatherArmorMeta = null; // 下文中的"盔甲"可以是皮革靴子,外套,帽子,裤子任意一种,因为这个类代表了四种嘛
  20.     private Color MapMetaColor = null; // 伸缩的地图
  21.     private MapView MapMetaMapView = null; // 伸缩的地图
  22.     private int Repairable = 0; //修复此物品所需的额外经验等级
  23.     private OfflinePlayer SkullMetaOfflinePlayer = null; //头颅的主人
  24.     private List<PotionEffect> SuspiciousStewMeta = new ArrayList<>(); //自定义药水效果的迷之炖菜
  25.     private DyeColor TropicalFishBucketMetaBodyColor = null; //热带鱼桶
  26.     private TropicalFish.Pattern TropicalFishBucketMetaPattern = null; //热带鱼桶
  27.     private DyeColor TropicalFishBucketMetaPatternColor = null; // 热带鱼桶
  28.     private String BookMetaAuthor = null; // 热带鱼桶
  29.     private BookMeta.Generation BookMetaGeneration = null; // 热带鱼桶
  30.     private List<String> BookMetaPages = null; // 热带鱼桶
  31.     private String BookMetaTitle = null;

  32.     public ItemStack(org.bukkit.inventory.ItemStack itemStack) {
  33.         if (itemStack != null && itemStack.getType() != Material.AIR) {
  34.             this.type = itemStack.getType();
  35.             this.amount = itemStack.getAmount();
  36.             getMeta(itemStack.getItemMeta());
  37.             this.enchantments = new HashMap<>();
  38.             for (Map.Entry<Enchantment, Integer> entry : itemStack.getEnchantments().entrySet()) {
  39.                 this.enchantments.put(entry.getKey().getKey().toString(), entry.getValue());
  40.             }
  41.         }
  42.     }
  43.     public org.bukkit.inventory.ItemStack toItemStack() {
  44.         org.bukkit.inventory.ItemStack itemStack = new org.bukkit.inventory.ItemStack(this.type, this.amount);
  45.         itemStack.setItemMeta(setMeta(itemStack.getItemMeta()));
  46.         return itemStack;
  47.     }
  48.     public void getMeta(ItemMeta meta) {
  49.         if (meta != null) {
  50.             if (meta instanceof Damageable) this.Damageable = (short) ((Damageable) meta).getDamage();
  51.             if (meta instanceof AxolotlBucketMeta) this.Variant = ((AxolotlBucketMeta) meta).getVariant().toString();
  52.             if (meta instanceof BannerMeta) this.BannerMeta = ((BannerMeta) meta).getPatterns();
  53.             if (meta instanceof CompassMeta && ((CompassMeta) meta).getLodestone() != null) this.CompassMeta = new Location(((CompassMeta) meta).getLodestone());
  54.             if (meta instanceof EnchantmentStorageMeta) {
  55.                 this.EnchantmentStorageMeta = new HashMap<>();
  56.                 Map<Enchantment, Integer> list = ((EnchantmentStorageMeta) meta).getStoredEnchants();
  57.                 for (Map.Entry<Enchantment, Integer> entry : list.entrySet()) {
  58.                     this.EnchantmentStorageMeta.put(entry.getKey().getKey().toString(), entry.getValue());
  59.                 }
  60.             }
  61.             if (meta instanceof FireworkEffectMeta) this.FireworkEffectMeta = ((FireworkEffectMeta) meta).getEffect();
  62.             if (meta instanceof FireworkMeta) this.FireworkMeta = ((FireworkMeta) meta).getEffects();
  63.             if (meta instanceof KnowledgeBookMeta) this.KnowledgeBookMeta = ((KnowledgeBookMeta) meta).getRecipes();
  64.             if (meta instanceof LeatherArmorMeta) this.LeatherArmorMeta = ((LeatherArmorMeta) meta).getColor();
  65.             if (meta instanceof MapMeta) {
  66.                 this.MapMetaColor = ((MapMeta) meta).getColor();
  67.                 this.MapMetaMapView = ((MapMeta) meta).getMapView();
  68.             }
  69.             if (meta instanceof Repairable) this.Repairable = ((Repairable) meta).getRepairCost();
  70.             if (meta instanceof SkullMeta) this.SkullMetaOfflinePlayer = ((SkullMeta) meta).getOwningPlayer();
  71.             if (meta instanceof SuspiciousStewMeta) this.SuspiciousStewMeta = ((SuspiciousStewMeta) meta).getCustomEffects();
  72.             if (meta instanceof TropicalFishBucketMeta) {
  73.                 this.TropicalFishBucketMetaPattern = ((TropicalFishBucketMeta) meta).getPattern();
  74.                 this.TropicalFishBucketMetaBodyColor = ((TropicalFishBucketMeta) meta).getBodyColor();
  75.                 this.TropicalFishBucketMetaPatternColor = ((TropicalFishBucketMeta) meta).getPatternColor();
  76.             }
  77.             if (meta instanceof BookMeta) {
  78.                 this.BookMetaAuthor = ((BookMeta) meta).getAuthor();
  79.                 this.BookMetaGeneration = ((BookMeta) meta).getGeneration();
  80.                 this.BookMetaPages = ((BookMeta) meta).getPages();
  81.                 this.BookMetaTitle = ((BookMeta) meta).getTitle();
  82.             }
  83.             this.lore = Objects.requireNonNull(meta).hasLore() ? new ArrayList<>(Objects.requireNonNull(meta.getLore())) : null;
  84.             this.displayName = meta.hasDisplayName() ? meta.getDisplayName() : null;
  85.             if (this.type.equals(Material.POTION) || this.type.equals(Material.SPLASH_POTION) || this.type.equals(Material.LINGERING_POTION)) {
  86.                 PotionMeta potionMeta = (PotionMeta) meta;
  87.                 this.potionType = potionMeta.getBasePotionData().getType();
  88.                 this.isSplash = this.type == Material.SPLASH_POTION;
  89.             }
  90.         }
  91.     }
  92.     public ItemMeta setMeta(ItemMeta meta) {
  93.         if (meta != null) {
  94.             if (this.displayName != null) meta.setDisplayName(this.displayName);
  95.             if (this.lore != null) meta.setLore(this.lore);
  96.             if (this.enchantments != null && !this.enchantments.isEmpty()) for (Map.Entry<String, Integer> entry : this.enchantments.entrySet()) {
  97.                 Enchantment enchantment = Enchantment.getByKey(NamespacedKey.fromString(entry.getKey()));
  98.                 if (enchantment != null) meta.addEnchant(enchantment, entry.getValue(), true);
  99.             }
  100.             if (meta instanceof Damageable && this.Damageable > 0) ((Damageable) meta).setDamage(this.Damageable);
  101.             if (meta instanceof PotionMeta && this.potionType != null) {
  102.                 if (this.type.equals(Material.POTION) || this.type.equals(Material.LINGERING_POTION) || this.type.equals(Material.SPLASH_POTION)) {
  103.                     PotionData potionData = new PotionData(this.potionType,
  104.                             !this.isSplash && this.potionType.isExtendable(),
  105.                             this.isSplash && this.potionType.isUpgradeable());
  106.                     ((PotionMeta) meta).setBasePotionData(potionData);
  107.                 }
  108.             }
  109.             if (meta instanceof AxolotlBucketMeta && this.Variant != null) {
  110.                 ((AxolotlBucketMeta) meta).setVariant(Axolotl.Variant.valueOf(this.Variant));
  111.             }
  112.             if (meta instanceof BannerMeta && this.BannerMeta != null) {
  113.                 ((BannerMeta) meta).setPatterns(this.BannerMeta);
  114.             }
  115.             if (meta instanceof CompassMeta && this.CompassMeta != null) {
  116.                 ((CompassMeta) meta).setLodestone(this.CompassMeta.toLocation());
  117.             }
  118.             if (meta instanceof EnchantmentStorageMeta && this.EnchantmentStorageMeta != null && !this.EnchantmentStorageMeta.isEmpty()) for (Map.Entry<String, Integer> entry : this.EnchantmentStorageMeta.entrySet()) {
  119.                 Enchantment enchantment = Enchantment.getByKey(NamespacedKey.fromString(entry.getKey()));
  120.                 if (enchantment != null) ((EnchantmentStorageMeta) meta).addStoredEnchant(enchantment, entry.getValue(), true);
  121.             }
  122.             if (meta instanceof FireworkEffectMeta && this.FireworkEffectMeta != null) {
  123.                 ((FireworkEffectMeta) meta).setEffect(this.FireworkEffectMeta);
  124.             }
  125.             if (meta instanceof FireworkMeta && this.FireworkMeta != null) {
  126.                 ((FireworkMeta) meta).addEffects(this.FireworkMeta);
  127.             }
  128.             if (meta instanceof KnowledgeBookMeta && this.KnowledgeBookMeta != null) {
  129.                 ((KnowledgeBookMeta) meta).setRecipes(this.KnowledgeBookMeta);
  130.             }
  131.             if (meta instanceof LeatherArmorMeta && this.LeatherArmorMeta != null) {
  132.                 ((LeatherArmorMeta) meta).setColor(this.LeatherArmorMeta);
  133.             }
  134.             if (meta instanceof MapMeta) {
  135.                 if (this.MapMetaColor != null) ((MapMeta) meta).setColor(this.MapMetaColor);
  136.                 if (this.MapMetaMapView != null) ((MapMeta) meta).setMapView(this.MapMetaMapView);
  137.             }
  138.             if (meta instanceof Repairable && this.Repairable > 0) {
  139.                 ((Repairable) meta).setRepairCost(this.Repairable);
  140.             }
  141.             if (meta instanceof SkullMeta && this.SkullMetaOfflinePlayer != null) {
  142.                 ((SkullMeta) meta).setOwningPlayer(this.SkullMetaOfflinePlayer);
  143.             }
  144.             if (meta instanceof SuspiciousStewMeta && this.SuspiciousStewMeta != null) {
  145.                 for (PotionEffect data : this.SuspiciousStewMeta) ((SuspiciousStewMeta) meta).addCustomEffect(data,true);
  146.             }
  147.             if (meta instanceof TropicalFishBucketMeta) {
  148.                 if (this.TropicalFishBucketMetaBodyColor != null) ((TropicalFishBucketMeta) meta).setBodyColor(this.TropicalFishBucketMetaBodyColor);
  149.                 if (this.TropicalFishBucketMetaPattern != null) ((TropicalFishBucketMeta) meta).setPattern(this.TropicalFishBucketMetaPattern);
  150.                 if (this.TropicalFishBucketMetaPatternColor != null) ((TropicalFishBucketMeta) meta).setPatternColor(this.TropicalFishBucketMetaPatternColor);
  151.             }
  152.             if (meta instanceof BookMeta) {
  153.                 if (this.BookMetaAuthor != null) ((BookMeta) meta).setAuthor(this.BookMetaAuthor);
  154.                 if (this.BookMetaGeneration != null) ((BookMeta) meta).setGeneration(this.BookMetaGeneration);
  155.                 if (this.BookMetaPages != null) ((BookMeta) meta).setPages(this.BookMetaPages);
  156.                 if (this.BookMetaTitle != null) ((BookMeta) meta).setTitle(this.BookMetaTitle);
  157.             }
  158.         }
  159.         return meta;
  160.     }
  161.     public static class Location implements Serializable {
  162.         private final double x;
  163.         private final double y;
  164.         private final double z;
  165.         private final World world;
  166.         public Location(org.bukkit.Location location) {
  167.             this.world = location.getWorld();
  168.             this.x = location.getX();
  169.             this.y = location.getY();
  170.             this.z = location.getZ();
  171.         }
  172.         public org.bukkit.Location toLocation() {
  173.             return new org.bukkit.Location(this.world,this.x,this.y,this.z);
  174.         }
  175.     }
  176. }
复制代码






Lv.4 矿工

人气
12 点
金粒
978 粒
宝石
1 颗
爱心
1 颗
钻石
57 颗
贡献
0 点

Java正版勋章Windows 10正版勋章石镐矿工勋章

发表于 2024-8-26 13:21:51 | 显示全部楼层
关于itemstack的各种属性可查看api:https://bukkit.windit.net/javado ... tory/ItemStack.html

旗帜图案一般用 BannerMeta

以下是chatgpt给予的示例代码:
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BannerMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.BannerMeta.Pattern;
import org.bukkit.Color;

public class BannerItemExample {

    public static ItemStack createCustomBanner() {
        // 创建一个新的物品堆栈,这里我们使用白色旗帜(Banner)
        ItemStack banner = new ItemStack(Material.WHITE_BANNER);

        // 获取物品的 ItemMeta 对象
        BannerMeta bannerMeta = (BannerMeta) banner.getItemMeta();
        
        // 检查是否成功获取到 BannerMeta
        if (bannerMeta != null) {
            // 创建并添加自定义图案
            bannerMeta.addPattern(new Pattern(Color.RED, PatternType.STRIPE_TOP));
            bannerMeta.addPattern(new Pattern(Color.BLUE, PatternType.CIRCLE));

            // 将修改后的 ItemMeta 设置回物品堆栈
            banner.setItemMeta(bannerMeta);
        }

        return banner;
    }
}

代码解析
ItemStack:创建一个白色旗帜物品(Material.WHITE_BANNER)
BannerMeta:用于设置旗帜的元数据。
Pattern:定义旗帜上的图案。PatternType 是预定义的图案类型,比如 STRIPE_TOP 和 CIRCLE
addPattern:将图案添加到旗帜上

PatternType 类提供了多种图案类型,您可以根据需求选择不同的图案。
确保您使用的 Bukkit API 版本支持这些功能,因为 API 可能会在不同的版本中有所变化

点评

刚更新了代码,你觉得咋样awa  详情 回复 发表于 2024-8-27 14:17
谢谢,可以了  详情 回复 发表于 2024-8-26 23:14
回复

使用道具 举报

Lv.5 农夫

人气
17 点
金粒
132 粒
宝石
1 颗
爱心
0 颗
钻石
87 颗
贡献
0 点

石镐矿工勋章

 楼主| 发表于 2024-8-26 23:14:13 | 显示全部楼层
爱玩游戏的阿坤 发表于 2024-8-26 13:21
关于itemstack的各种属性可查看api:https://bukkit.windit.net/javadoc/org/bukkit/inventory/ItemStack.ht ...

谢谢,可以了
回复

使用道具 举报

Lv.5 农夫

人气
17 点
金粒
132 粒
宝石
1 颗
爱心
0 颗
钻石
87 颗
贡献
0 点

石镐矿工勋章

 楼主| 发表于 2024-8-27 14:17:08 | 显示全部楼层
爱玩游戏的阿坤 发表于 2024-8-26 13:21
关于itemstack的各种属性可查看api:https://bukkit.windit.net/javadoc/org/bukkit/inventory/ItemStack.ht ...

刚更新了代码,你觉得咋样awa
回复

使用道具 举报

Lv.5 农夫

人气
17 点
金粒
132 粒
宝石
1 颗
爱心
0 颗
钻石
87 颗
贡献
0 点

石镐矿工勋章

 楼主| 发表于 2024-8-27 14:32:32 | 显示全部楼层
6,修好了: 没加判断 Damageable != 0
回复

使用道具 举报

Lv.10 附魔师

人气
2097 点
金粒
23 粒
宝石
13 颗
爱心
587 颗
钻石
4119 颗
贡献
5 点

石镐矿工勋章铁镐矿工勋章钻镐矿工勋章青苹果勋章红苹果勋章金苹果勋章小麦种勋章苹果树勋章下界疣勋章论坛十周年纪念勋章骨灰勋章Java正版勋章Windows 10正版勋章金锭勋章金块勋章开发人员勋章捉虫专家勋章优秀小组勋章-EWO第12届创意赛参与奖

发表于 2024-8-27 15:54:14 | 显示全部楼层
使用默认序列化逻辑的话,应该只需继承Serializable,writeObject/readObject不是必要的吧?

点评

已修改,但感觉留空有更多的可能性(报错:开玩笑的)  详情 回复 发表于 2024-8-27 16:57
emmm,我试试  详情 回复 发表于 2024-8-27 16:04
回复

使用道具 举报

Lv.5 农夫

人气
17 点
金粒
132 粒
宝石
1 颗
爱心
0 颗
钻石
87 颗
贡献
0 点

石镐矿工勋章

 楼主| 发表于 2024-8-27 16:04:18 | 显示全部楼层
洞穴夜莺 发表于 2024-8-27 15:54
使用默认序列化逻辑的话,应该只需继承Serializable,writeObject/readObject不是必要的吧? ...

emmm,我试试
回复

使用道具 举报

Lv.5 农夫

人气
17 点
金粒
132 粒
宝石
1 颗
爱心
0 颗
钻石
87 颗
贡献
0 点

石镐矿工勋章

 楼主| 发表于 2024-8-27 16:57:55 | 显示全部楼层
洞穴夜莺 发表于 2024-8-27 15:54
使用默认序列化逻辑的话,应该只需继承Serializable,writeObject/readObject不是必要的吧? ...

已修改,但感觉留空有更多的可能性(报错:开玩笑的)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

彩虹不只是天上才有,也可以在心中架起。

Archiver|小黑屋| Mcbbs2.net | 隐私政策 | 手机版

GMT+8, 2024-9-19 09:01 , Processed in 0.413917 second(s), 18 queries , Redis On.

"Minecraft"以及"我的世界"为美国微软公司的商标 本站与微软公司没有从属关系

© 2010-2024 MCBBS纪念版 版权所有 本站内原创内容版权属于其原创作者,除作者或版规特别声明外未经许可不得转载

返回顶部