Skip to content

Commit

Permalink
Starts refactoring the documentation for NBT arguments. The aim is to…
Browse files Browse the repository at this point in the history
… create a separate page for NBT compound arguments with tutorials using both the NBTAPI and Rtag. We want to include Rtag because hopefully that gives us some insight into a possible implementation of the NBT path argument for #386
  • Loading branch information
JorelAli committed Dec 28, 2023
1 parent 30538cd commit c2168d9
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,18 @@ void argument_nbt2() {
}
}

class argument_nbt_rtag1 extends JavaPlugin {
/* ANCHOR: argumentNBTRtag1 */
@Override
public void onLoad() {
CommandAPI.onLoad(new CommandAPIBukkitConfig(this)
.initializeNBTAPI(Object.class, o -> o)
);
}
/* ANCHOR_END: argumentNBTRtag1 */

}

void argument_objectives() {
/* ANCHOR: argumentObjectives1 */
new CommandAPICommand("sidebar")
Expand Down
3 changes: 2 additions & 1 deletion docssrc/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ level = 2
# Should we check links on the internet? Enabling this option adds a
# non-negligible performance impact
follow-web-links = true
optional = true

# Are we allowed to link to files outside of the book's root directory? This
# may help prevent linking to sensitive files (e.g. "../../../../etc/shadow")
Expand All @@ -57,4 +58,4 @@ warning-policy = "error"
#
# Hint: you can use TOML's raw strings (single quote) to avoid needing to
# escape things twice.
exclude = [ 'spigotmc\.org', 'bukkit\.org' ]
exclude = [ 'spigotmc\.org', 'bukkit\.org' ]
1 change: 1 addition & 0 deletions docssrc/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
- [Block predicate arguments](./argument_blockpredicate.md)
- [ItemStack predicate arguments](./argument_itemstackpredicate.md)
- [NBT arguments](./argument_nbt.md)
- [NBT Compound argument](./argument_nbt_compound.md)
- [Literal arguments](./category_literal_arguments.md)
- [Literal arguments](./argument_literal.md)
- [Multi literal arguments](./argument_multiliteral.md)
Expand Down
4 changes: 4 additions & 0 deletions docssrc/src/argument_nbt.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# NBT arguments

The CommandAPI has support for arguments that interact with Minecraft's [NBT format](https://minecraft.wiki/w/NBT_format). Since Minecraft's NBT format is not directly accessible via Spigot (it is instead implemented via Spigot's APIs when necessary), using NBT-based arguments requires an implementation of an NBT API that is capable of interpreting NMS NBT objects.


Check failure on line 5 in docssrc/src/argument_nbt.md

View workflow job for this annotation

GitHub Actions / build (17, ubuntu-20.04)

Multiple consecutive blank lines [Expected: 1; Actual: 2]

docssrc/src/argument_nbt.md:5 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2]

Check failure on line 6 in docssrc/src/argument_nbt.md

View workflow job for this annotation

GitHub Actions / build (17, ubuntu-20.04)

Multiple consecutive blank lines [Expected: 1; Actual: 3]

docssrc/src/argument_nbt.md:6 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 3]
The CommandAPI includes support for NBT compound arguments using an NBT API. The usage for the `NBTCompoundArgument` depends on whether you are using the CommandAPI plugin (using a `CommandAPI.jar` file in your `plugins/` folder), or are shading the CommandAPI (including the compiled CommandAPI code in your own plugin).

-----
Expand Down
118 changes: 118 additions & 0 deletions docssrc/src/argument_nbt_compound.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# NBT Compound argument

### Using Minecraft's NBT objects

|Spigot-mapped name|Mojang-mapped name|
|------------------|------------------|
|`NBTTagCompound`|`CompoundTag`|
|`NBTBase`|`Tag`|

NBTTagCompound

The CommandAPI allows you to access the NMS `NBTTagCompound` class via `net.minecraft.nbt.NBTTagCompound` directly without requiring any additional APIs to access NBT. This is especially useful for Mojang-mapped servers.

```java
@Override
public void onLoad() {
CommandAPI.onLoad(new CommandAPIBukkitConfig(this)
.initializeNBTAPI(NBTTagCompound.class, o -> o)
);
}
```

------

## Using the NBT API

-----

Check failure on line 27 in docssrc/src/argument_nbt_compound.md

View workflow job for this annotation

GitHub Actions / build (17, ubuntu-20.04)

Horizontal rule style [Expected: ------; Actual: -----]

docssrc/src/argument_nbt_compound.md:27 MD035/hr-style Horizontal rule style [Expected: ------; Actual: -----]

## Using Rtag

[Rtag](https://www.spigotmc.org/resources/100694/) is an easily shadeable API that allows you to easily interact with NBT and is highly compatible with the CommandAPI! There are multiple ways to deconstruct NBT arguments using Rtag:

### Accessing individual paths using `Rtag.INSTANCE.get()`

The [`Rtag#get`](https://javadoc.saicone.com/rtag/com/saicone/rtag/Rtag.html#get(java.lang.Object,java.lang.Object...)) method allows you to retrieve objects directly from an NMS `NBTTagCompound` object without requiring you to import `net.minecraft.net.NBTTagCompound`

```java
@Override
public void onLoad() {
CommandAPI.onLoad(new CommandAPIBukkitConfig(this)
.initializeNBTAPI(Object.class, o -> o)
);
}
```

<div class="example">

For example, if you have an NBT compound tag with this data:

```json
{
"some": {
"path": {
"here": "hello"
}
}
}
```

You can access the string value `hello` using `Rtag.INSTANCE.get()`:

```java
new CommandAPICommand("test")
.withArguments(new NBTCompoundArgument<Object>("nbt"))
.executes((sender, args) -> {
String result = Rtag.INSTANCE.get(args.get("nbt"), "some", "path", "here");
})
.register();
```

</div>

### Converting the whole compound tag into standard Java objects

The easiest way to interact with Rtag is to deconstruct a compound tag into a `Map` of Java objects. Rtag is able to do this easily using `TagCompound#getValue`, with the `Rtag.INSTANCE` field:

```java
@Override
public void onLoad() {
CommandAPI.onLoad(new CommandAPIBukkitConfig(this)
.initializeNBTAPI(Map.class, o -> TagCompound.getValue(Rtag.INSTANCE, o))
);
}
```

<div class="example">

Say you have an NBT compound tag that looks like this:

```json
{
name: "Notch",
exp: 20s,
armor: ["diamond_helmet", "elytra", "diamond_leggings", "leather_boots"],
hand_item: {
type: "diamond_pickaxe",
durability: 123
}
}
```

Using Rtag's representation of an NBT compound tag using standard Java objects would convert the above NBT compound tag into the following object:

```java
Map<String, Object> {
"name": String "Notch",
"exp": Short 20,
"armor": List<String> ["diamond_helmet", "elytra", "diamond_leggings", "leather_boots"],
"hand_item": Map<String, Object> {
"type": String "diamond_pickaxe",
"durability": Integer 123
}
}
```

This

Check failure on line 116 in docssrc/src/argument_nbt_compound.md

View workflow job for this annotation

GitHub Actions / build (17, ubuntu-20.04)

Trailing spaces [Expected: 0 or 2; Actual: 1]

docssrc/src/argument_nbt_compound.md:116:5 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 1]

</div>

0 comments on commit c2168d9

Please sign in to comment.