|
| 1 | +# TOC |
| 2 | +* [About](#about) |
| 3 | +* [Utilities](#utilities) |
| 4 | + + [Item NBT](#item-nbt) |
| 5 | + + [Entity NBT](#entity-nbt) |
| 6 | + + [Tile entity NBT](#tile-entity-nbt) |
| 7 | + + [NBT parser](#nbt-parser) |
| 8 | +* [Usage](#usage) |
| 9 | +* [Examples](#examples) |
| 10 | + - [Mark an item](#mark-an-item) |
| 11 | + - [When pigs can fly](#when-pigs-can-fly) |
| 12 | + - [Locking chests](#locking-chests) |
| 13 | +- [Changelog](#changelog) |
| 14 | + * [Breaking changes in 1.0.2](#breaking-changes-in-102) |
| 15 | + + [All utils](#all-utils) |
| 16 | + + [ItemNBTUtil](#itemnbtutil) |
| 17 | + |
| 18 | + |
1 | 19 | # MiniNBT |
2 | | -A small NBT library to allow the modification of NBT tags across different versions |
| 20 | +A small NBT library to allow the modification of NBT tags across different Minecraft versions. |
| 21 | +Currently supported are 1.8.8 up to 1.14.4, but very likely also future and *maybe* lower versions. |
3 | 22 |
|
4 | 23 | ## About |
5 | | -This is a small library, originally written for [PercieveCore](https://github.com/PerceiveDev/PerceiveCore) and adapted to be a standalone utility. |
6 | | -It provides an easy way to interact with NBT (Notch's Binary Format), but only the interaction with item NBT is really tested ;) |
| 24 | +This is a small standalone library providing an easy way to interact with NBT (Notch's Binary Format). |
| 25 | +It is currently capable of reading and modifying `Item`, `Entity` and `TileEntity` NBT. |
| 26 | + |
| 27 | + |
| 28 | +## Utilities |
7 | 29 |
|
8 | 30 | ### Item NBT |
9 | 31 | You can modify item NBT with the `ItemNBTUtil`. |
10 | 32 | Supported are: |
11 | 33 | - Setting a tag |
12 | 34 | - Getting a tag |
13 | 35 |
|
14 | | -*The custom NBT on items is preserved.* |
| 36 | +*Custom NBT on items is preserved, so you can store whatever you want.* |
15 | 37 |
|
16 | 38 | ### Entity NBT |
17 | | -You can also modify entity NBT (`EntityNBTUtil`), but I think there are methods for that in Spigot now. |
18 | | - |
19 | | -*Custom tags are never set and therefore impossible.* |
| 39 | +You can edit entity NBT (`NoGravity` and whatever other tag you like). Many of those settings are now exposed through Bukkit methods though. |
| 40 | +*Custom tags are never read by Minecraft and therefore impossible.* |
20 | 41 |
|
21 | 42 | ### Tile entity NBT |
22 | | -This allows you to modify the NBT of `TileEntities` (`TileEntityNBTUtil`), like a chest. |
23 | | -It can be used to change the name of a furnace/chest or similiar after it was placed, or do some other trickery. |
| 43 | +`TileEntities` are blocks with some special data, like furnaces, chests, and so on. |
| 44 | +It can be used to change the name of a furnace/chest or similar after it was placed, or do some other trickery. |
24 | 45 |
|
25 | | -*Custom tags are never set and therefore impossible.* |
| 46 | +*Custom tags are never read by Minecraft and therefore impossible.* |
26 | 47 |
|
27 | 48 | ### NBT parser |
28 | 49 | This allows you to parse a String to a NBTTagCompound. It uses the `MojangsonParser` internally, but you will need to use the `NbtParser` class. |
29 | 50 |
|
30 | 51 | ## Usage |
31 | | -Quite easy. I have _nearly_ exactly recreated the original NBT classes, so you can use it in the same way you would use the original. |
| 52 | +This utility follows the exact same structure as the Minecraft tags do, so you can just change your imports and things might work. If not, it shouldn't be hard to figure out the small differences. |
| 53 | +The `NBTTagCompound` can serve as a good starting point. |
| 54 | + |
| 55 | +## Examples |
| 56 | +#### Mark an item |
| 57 | +```java |
| 58 | +ItemStack item = player.getInventory().getItemInMainHand(); |
| 59 | + |
| 60 | +NBTTagCompound compound = ItemNBTUtil.getTag(item); // fetch the data |
| 61 | +String dataKey = "My custom data"; // the key to store it under |
| 62 | +if(compound.hasKey(dataKey)){ |
| 63 | + player.sendMessage(ChatColor.GOLD + "Got: " + ChatColor.GREEN + compound.getString(dataKey)); |
| 64 | +} else { |
| 65 | + compound.setString(dataKey, String.join(" ", args)); // set it to command arguments |
| 66 | + ItemNBTUtil.setNBTTag(compound, item); |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +#### When pigs can fly |
| 71 | +```java |
| 72 | +Entity pig = player.getNearbyEntities(5, 5, 5).stream() |
| 73 | + .filter(it -> it instanceof Pig) |
| 74 | + .findFirst().get(); |
| 75 | +NBTTagCompound compound = EntityNBTUtil.getNbtTag(pig); |
| 76 | +System.out.println(compound); // prints all data |
| 77 | +compound.setBoolean("NoGravity", !compound.getBoolean("NoGravity")); |
| 78 | +EntityNBTUtil.setNbtTag(pig, compound); // overwrites the entity's data |
| 79 | +``` |
| 80 | + |
| 81 | +#### Locking chests |
| 82 | +```java |
| 83 | +Block block = player.getLocation().getBlock(); |
| 84 | +if(block.getType() == Material.AIR){ |
| 85 | + block = block.getRelative(BlockFace.DOWN); |
| 86 | +} |
| 87 | +NBTTagCompound compound = TileEntityNBTUtil.getNbtTag(block.getState()); |
| 88 | +System.out.println(compound); // prints all data |
| 89 | +compound.setString("Lock", "Test"); // locks it to "Test" |
| 90 | +TileEntityNBTUtil.setNbtTag(block.getState(), compound); // applies the changes |
| 91 | +``` |
| 92 | + |
| 93 | +# Changelog |
32 | 94 |
|
33 | | -You can refer to the format on the MinecraftWiki pages and you will find any class you find there here too. With the same name. |
| 95 | +## Breaking changes in 1.0.2 |
| 96 | +It should be ABI/API compatible except for the changes outlined below. |
| 97 | +### All utils |
| 98 | +* `ReflectionException` will now be thrown if initialization or any other operation fails. |
34 | 99 |
|
35 | | -## Breaking changes in 1.0.1 |
36 | 100 | ### ItemNBTUtil |
37 | | -* `getTag` now throws an exception if the tag was no compound or is unknown instead of returning an empty tag |
| 101 | +* `getTag` now throws an exception if the tag was no compound or is unknown instead of returning an empty tag. |
| 102 | + This will probably not impact you at all, but it still is a change in behaviour. |
0 commit comments