[Tutorial][Bukkit]如何在Bukkit服务器上读取玩家的NBT

目录

  • 导读
  • 如何读取
  • 读取离线玩家的背包
  • 将修改后的NBT保存回playerdata

导读

本教程需要玩家拥有关于 NMS 里的 NBT 的相关知识

本教程使用 Paper 1.15.2 的版本作为演示

如何读取

其实这个功能在古老的 1.12 之前的版本就已经有了, 只不过一直懒得去做这方面的东西
在我在写这一篇文章之前我通过反编译知道, 早在 1.7.10 就已经拥有了这个方法, 所以本方法适用于目前的所有办法, 并且该方法应该不会经常改动

总所周知的是, 在 MinecraftServer 中, 所有玩家的数据都会被保存在 world\playerdata 里面, 并且以 玩家uuid.dat 的方式来命名
所以我们只需要读取这些 dat 文件就可以得到相对应的玩家数据


首先我们需要得到玩家对应的 dat 文件

// playerdata
File playerDataFolder = new File(getDataFolder().getParentFile().getParentFile(), "world\\playerdata\\");
// uuid
String uuid = player.getUniqueId().toString();
// 玩家的nbt数据文件
File playerDat = new File(playerDataFolder, uuid + ".dat");

之后我们将其丢入 FileInputStream 当中

FileInputStream inputStream = new FileInputStream(playerDat);

开始读取!

NBTTagCompound nbt = NBTCompressedStreamTools.a(inputStream);

输出 nbt 可以得到以下的内容

     {
    seenCredits: 0b,
    DeathTime: 0 s,
    AbsorptionAmount: 0.0 f,
    SpawnWorld: "world",
    Bukkit.updateLevel: 2,
    foodTickTimer: 0,
    UUIDLeast: -7709988740583578109 L,
    recipeBook: {
        isGuiOpen: 0b,
        recipes: ["minecraft:chest", "minecraft:charcoal", "minecraft:dark_oak_button", "minecraft:stone_hoe", "minecraft:item_frame", "minecraft:green_concrete_powder", "minecraft:lever", "minecraft:pumpkin_pie", "minecraft:stone_button", "minecraft:granite_slab_from_granite_stonecutting", "minecraft:stone_pickaxe", "minecraft:stonecutter", "minecraft:dark_oak_fence", "minecraft:skull_banner_pattern", "minecraft:granite_stairs_from_granite_stonecutting", "minecraft:iron_door", "minecraft:shield", "minecraft:polished_granite", "minecraft:iron_nugget", "minecraft:sandstone", "minecraft:yellow_concrete_powder", "minecraft:cooked_porkchop_from_campfire_cooking", "minecraft:dark_oak_trapdoor", "minecraft:red_concrete_powder", "minecraft:cobblestone_stairs_from_cobblestone_stonecutting", "minecraft:iron_shovel", "minecraft:polished_diorite_slab", "minecraft:jack_o_lantern", "minecraft:hopper", "minecraft:black_dye_from_wither_rose", "minecraft:glass", "minecraft:stone_stairs_from_stone_stonecutting", "minecraft:crafting_table", "minecraft:stone_stairs", "minecraft:lantern", "minecraft:iron_leggings", "minecraft:cobblestone_wall", "minecraft:blue_concrete_powder", "minecraft:polished_diorite_slab_from_diorite_stonecutting", "minecraft:white_concrete_powder", "minecraft:stone_slab_from_stone_stonecutting", "minecraft:iron_bars", "minecraft:iron_boots", "minecraft:iron_trapdoor", "minecraft:gray_concrete_powder", "minecraft:dark_oak_sign", "minecraft:granite_slab", "minecraft:iron_helmet", "minecraft:stone_brick_slab_from_stone_stonecutting", "minecraft:stick", "minecraft:light_blue_concrete_powder", "minecraft:leather_chestplate", "minecraft:light_gray_concrete_powder", "minecraft:granite_wall_from_granite_stonecutting", "minecraft:leather_boots", "minecraft:anvil", "minecraft:dark_oak_stairs", "minecraft:furnace", "minecraft:iron_block", "minecraft:granite_stairs", "minecraft:stone_slab", "minecraft:dark_oak_door", "minecraft:cooked_porkchop", "minecraft:lime_concrete_powder", "minecraft:leather_leggings", "minecraft:dark_oak_pressure_plate", "minecraft:cooked_porkchop_from_smoking", "minecraft:stone_axe", "minecraft:leather_helmet", "minecraft:beacon", "minecraft:oak_wood", "minecraft:barrel", "minecraft:stone", "minecraft:polished_diorite_stairs_from_polished_diorite_stonecutting", "minecraft:polished_granite_from_granite_stonecutting", "minecraft:oak_planks", "minecraft:bone_meal", "minecraft:stone_brick_stairs_from_stone_stonecutting", "minecraft:polished_diorite_stairs_from_diorite_stonecutting", "minecraft:iron_sword", "minecraft:crossbow", "minecraft:chiseled_stone_bricks_stone_from_stonecutting", "minecraft:red_sandstone", "minecraft:iron_chestplate", "minecraft:pumpkin_seeds", "minecraft:stone_bricks", "minecraft:iron_pickaxe", "minecraft:smithing_table", "minecraft:black_concrete_powder", "minecraft:polished_granite_stairs_from_granite_stonecutting", "minecraft:purple_concrete_powder", "minecraft:smooth_stone", "minecraft:pink_concrete_powder", "minecraft:heavy_weighted_pressure_plate", "minecraft:stone_shovel", "minecraft:cobblestone_stairs", "minecraft:cobblestone_wall_from_cobblestone_stonecutting", "minecraft:leather_horse_armor", "minecraft:brown_concrete_powder", "minecraft:granite_wall", "minecraft:orange_concrete_powder", "minecraft:polished_diorite_stairs", "minecraft:magenta_concrete_powder", "minecraft:shears", "minecraft:stone_pressure_plate", "minecraft:polished_granite_slab_from_granite_stonecutting", "minecraft:stone_sword", "minecraft:dark_oak_slab", "minecraft:iron_axe", "minecraft:cobblestone_slab", "minecraft:iron_hoe", "minecraft:iron_ingot_from_iron_block", "minecraft:cyan_concrete_powder", "minecraft:minecart", "minecraft:coarse_dirt", "minecraft:dark_oak_fence_gate", "minecraft:stone_brick_walls_from_stone_stonecutting", "minecraft:bucket", "minecraft:cobblestone_slab_from_cobblestone_stonecutting", "minecraft:stone_bricks_from_stone_stonecutting", "minecraft:polished_diorite_slab_from_polished_diorite_stonecutting"],
        toBeDisplayed: ["minecraft:chest", "minecraft:charcoal", "minecraft:dark_oak_button", "minecraft:stone_hoe", "minecraft:item_frame", "minecraft:green_concrete_powder", "minecraft:lever", "minecraft:pumpkin_pie", "minecraft:stone_button", "minecraft:granite_slab_from_granite_stonecutting", "minecraft:stone_pickaxe", "minecraft:stonecutter", "minecraft:dark_oak_fence", "minecraft:skull_banner_pattern", "minecraft:granite_stairs_from_granite_stonecutting", "minecraft:iron_door", "minecraft:shield", "minecraft:polished_granite", "minecraft:iron_nugget", "minecraft:sandstone", "minecraft:yellow_concrete_powder", "minecraft:cooked_porkchop_from_campfire_cooking", "minecraft:dark_oak_trapdoor", "minecraft:red_concrete_powder", "minecraft:cobblestone_stairs_from_cobblestone_stonecutting", "minecraft:iron_shovel", "minecraft:polished_diorite_slab", "minecraft:jack_o_lantern", "minecraft:hopper", "minecraft:black_dye_from_wither_rose", "minecraft:glass", "minecraft:stone_stairs_from_stone_stonecutting", "minecraft:crafting_table", "minecraft:stone_stairs", "minecraft:lantern", "minecraft:iron_leggings", "minecraft:cobblestone_wall", "minecraft:blue_concrete_powder", "minecraft:polished_diorite_slab_from_diorite_stonecutting", "minecraft:white_concrete_powder", "minecraft:stone_slab_from_stone_stonecutting", "minecraft:iron_bars", "minecraft:iron_boots", "minecraft:iron_trapdoor", "minecraft:gray_concrete_powder", "minecraft:dark_oak_sign", "minecraft:granite_slab", "minecraft:iron_helmet", "minecraft:stone_brick_slab_from_stone_stonecutting", "minecraft:stick", "minecraft:light_blue_concrete_powder", "minecraft:leather_chestplate", "minecraft:light_gray_concrete_powder", "minecraft:granite_wall_from_granite_stonecutting", "minecraft:leather_boots", "minecraft:anvil", "minecraft:dark_oak_stairs", "minecraft:furnace", "minecraft:iron_block", "minecraft:granite_stairs", "minecraft:stone_slab", "minecraft:dark_oak_door", "minecraft:cooked_porkchop", "minecraft:lime_concrete_powder", "minecraft:leather_leggings", "minecraft:dark_oak_pressure_plate", "minecraft:cooked_porkchop_from_smoking", "minecraft:stone_axe", "minecraft:leather_helmet", "minecraft:beacon", "minecraft:oak_wood", "minecraft:barrel", "minecraft:stone", "minecraft:polished_diorite_stairs_from_polished_diorite_stonecutting", "minecraft:polished_granite_from_granite_stonecutting", "minecraft:oak_planks", "minecraft:bone_meal", "minecraft:stone_brick_stairs_from_stone_stonecutting", "minecraft:polished_diorite_stairs_from_diorite_stonecutting", "minecraft:iron_sword", "minecraft:crossbow", "minecraft:chiseled_stone_bricks_stone_from_stonecutting", "minecraft:red_sandstone", "minecraft:iron_chestplate", "minecraft:pumpkin_seeds", "minecraft:stone_bricks", "minecraft:iron_pickaxe", "minecraft:smithing_table", "minecraft:black_concrete_powder", "minecraft:polished_granite_stairs_from_granite_stonecutting", "minecraft:purple_concrete_powder", "minecraft:smooth_stone", "minecraft:pink_concrete_powder", "minecraft:heavy_weighted_pressure_plate", "minecraft:stone_shovel", "minecraft:cobblestone_stairs", "minecraft:cobblestone_wall_from_cobblestone_stonecutting", "minecraft:leather_horse_armor", "minecraft:brown_concrete_powder", "minecraft:granite_wall", "minecraft:orange_concrete_powder", "minecraft:polished_diorite_stairs", "minecraft:magenta_concrete_powder", "minecraft:shears", "minecraft:stone_pressure_plate", "minecraft:polished_granite_slab_from_granite_stonecutting", "minecraft:stone_sword", "minecraft:dark_oak_slab", "minecraft:iron_axe", "minecraft:cobblestone_slab", "minecraft:iron_hoe", "minecraft:iron_ingot_from_iron_block", "minecraft:cyan_concrete_powder", "minecraft:minecart", "minecraft:coarse_dirt", "minecraft:dark_oak_fence_gate", "minecraft:stone_brick_walls_from_stone_stonecutting", "minecraft:bucket", "minecraft:cobblestone_slab_from_cobblestone_stonecutting", "minecraft:stone_bricks_from_stone_stonecutting", "minecraft:polished_diorite_slab_from_polished_diorite_stonecutting"],
        isFurnaceGuiOpen: 0b,
        isFurnaceFilteringCraftable: 0b,
        isFilteringCraftable: 0b
    },
    OnGround: 1 b,
    XpTotal: 60,
    Attributes: [{
        Name: "generic.maxHealth",
        Base: 20.0 d
    }, {
        Name: "generic.knockbackResistance",
        Base: 0.0 d
    }, {
        Name: "generic.movementSpeed",
        Base: 0.10000000149011612 d
    }, {
        Name: "generic.armor",
        Base: 0.0 d
    }, {
        Name: "generic.armorToughness",
        Base: 0.0 d
    }, {
        Name: "generic.attackDamage",
        Base: 1.0 d
    }, {
        Name: "generic.attackSpeed",
        Base: 4.0 d
    }, {
        Name: "generic.luck",
        Base: 0.0 d
    }],
    playerGameType: 1,
    SelectedItemSlot: 4,
    Invulnerable: 0b,
    Brain: {
        memories: {}
    },
    bukkit: {
        newTotalExp: 0,
        newLevel: 0,
        newExp: 0,
        keepLevel: 0b,
        expToDrop: 0,
        lastPlayed: 1602948728077 L,
        firstPlayed: 1598409215186 L,
        lastKnownName: "Zoyn"
    },
    Dimension: 0,
    Paper.Origin: [-99.5 d, 70.0 d, -51.5 d],
    Score: 60,
    UUIDMost: -7592284996227679300 L,
    abilities: {
        walkSpeed: 0.1 f,
        flySpeed: 0.05 f,
        flying: 0b,
        instabuild: 1 b,
        invulnerable: 1 b,
        mayfly: 1 b,
        mayBuild: 1 b
    },
    Rotation: [-325.99268 f, 24.149998 f],
    HurtByTimestamp: 0,
    foodSaturationLevel: 6.8 f,
    WorldUUIDMost: -3093603006290312122 L,
    Motion: [0.0 d, -0.0784000015258789 d, 0.0 d],
    Air: 300 s,
    WorldUUIDLeast: -6703577456958716675 L,
    XpSeed: -903103853,
    DataVersion: 2230,
    Inventory: [{
        Slot: 0b,
        id: "minecraft:stone",
        Count: 64 b
    }, {
        Slot: 1 b,
        id: "minecraft:diamond_sword",
        Count: 1 b,
        tag: {
            Damage: 0
        }
    }, {
        Slot: 2 b,
        id: "minecraft:bone",
        Count: 1 b
    }, {
        Slot: 3 b,
        id: "minecraft:stone",
        Count: 64 b
    }, {
        Slot: 4 b,
        id: "minecraft:bee_nest",
        Count: 1 b
    }, {
        Slot: 5 b,
        id: "minecraft:stone",
        Count: 1 b
    }],
    XpLevel: 5,
    Spigot.ticksLived: 705164,
    FallDistance: 0.0 f,
    foodLevel: 20,
    SleepTimer: 0 s,
    EnderItems: [],
    XpP: 0.2941176 f,
    Paper: {
        LastLogin: 1602944296604 L,
        LastSeen: 1602948728077 L
    },
    FallFlying: 0b,
    HurtTime: 0 s,
    Health: 20.0 f,
    Pos: [200.30000001192093 d, 83.0 d, -296.69999998807907 d],
    Fire: -20 s,
    PortalCooldown: 0,
    foodExhaustionLevel: 0.06499999 f,
    Paper.SpawnReason: "DEFAULT"
}

完整代码

File playerDataFolder = new File(getDataFolder().getParentFile().getParentFile(), "world\\playerdata\\");
File playerDat = new File(playerDataFolder, player.getUniqueId().toString() + ".dat");
try {
    FileInputStream inputStream = new FileInputStream(playerDat);
    NBTTagCompound nbt = NBTCompressedStreamTools.a(inputStream);
    NBTTagList tagList = nbt.getList("Inventory", 10);
    System.out.println(tagList);

} catch (Exception e) {
    e.printStackTrace();
}

所以其实我们看到, 真正的读取都是利用了 NBTCompressedStreamTools 这个已经在 NMS 写好的工具类
而我们只是利用轮子的人

读取离线玩家的背包

那么我们可以通过上面的 NBT样本 看到了其实里面保存有玩家的物品, 而这样我们可以利用这个NBT数据将其读取出来
看下面的代码

File playerDataFolder = new File(getDataFolder().getParentFile().getParentFile(), "world\\playerdata\\");
File playerDat = new File(playerDataFolder, player.getUniqueId().toString() + ".dat");
try {
    FileInputStream inputStream = new FileInputStream(playerDat);
    NBTTagCompound nbt = NBTCompressedStreamTools.a(inputStream);
    
    // 得到 玩家NBT里的 Inventory
    NBTTagList tagList = nbt.getList("Inventory", 10);
    System.out.println(tagList);
    
    // 转为Bukkit进行读取
    List itemStackList = new ArrayList<>();
    for (int i = 0; i < tagList.size(); i++) {
        NBTTagCompound compound = (NBTTagCompound) tagList.get(i);
        // 这里的ItemStack其实是NMS下的ItemStack!
        ItemStack nmsItem = ItemStack.a(compound);
        itemStackList.add(nmsItem.getBukkitStack());
    }
    
    Inventory inventory = Bukkit.createInventory(null, 54);
    inventory.addItem(itemStackList.toArray(new org.bukkit.inventory.ItemStack[0]));
    player.openInventory(inventory);
} catch (Exception e) {
    e.printStackTrace();
}

控制台输出:

[{
    Slot: 0b,
    id: "minecraft:stone",
    Count: 64b
}, {
    Slot: 1b,
    id: "minecraft:diamond_sword",
    Count: 1b,
    tag: {
        Damage: 0
    }
}, {
    Slot: 4b,
    id: "minecraft:bee_nest",
    Count: 1b
}, {
    Slot: 5b,
    id: "minecraft:stone",
    Count: 1b
}]

游戏内效果:


[Tutorial][Bukkit]如何在Bukkit服务器上读取玩家的NBT_第1张图片
playernbt.png

将修改后的NBT保存回playerdata

具体请看这里 net.minecraft.server.v1_xx_Rx.WorldNBTStorage
具体代码:

[Tutorial][Bukkit]如何在Bukkit服务器上读取玩家的NBT_第2张图片
nbtsave.png

你可能感兴趣的:([Tutorial][Bukkit]如何在Bukkit服务器上读取玩家的NBT)