Skip to content

Commit

Permalink
New "modifyComponents" api
Browse files Browse the repository at this point in the history
  • Loading branch information
tr7zw committed Jun 13, 2024
1 parent cf30cf9 commit c16548f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
40 changes: 40 additions & 0 deletions item-nbt-api/src/main/java/de/tr7zw/changeme/nbtapi/NBT.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import de.tr7zw.changeme.nbtapi.wrapper.NBTProxy;
import de.tr7zw.changeme.nbtapi.wrapper.ProxyBuilder;
import de.tr7zw.changeme.nbtapi.iface.ReadableNBTList;
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;

/**
* General utility class for a clean and simple nbt access.
*
* @author tr7zw
*
*/
@SuppressWarnings("deprecation")
public class NBT {

private NBT() {
Expand Down Expand Up @@ -225,6 +227,44 @@ public static <T> T modify(Entity entity, Function<ReadWriteNBT, T> function) {
nbtEnt.setClosed();
return ret;
}

/**
* It takes an ItemStack and a Consumer&lt;ReadWriteNBT&gt;, and then applies
* the Consumer to the ItemStacks Components as NBT. This is for 1.20.5+ only.
* This method is quiet expensive, so don't overuse it.
*
* @param item The item you want to modify the components of
* @param consumer The consumer that will be used to modify the components.
*/
public static void modifyComponents(ItemStack item, Consumer<ReadWriteNBT> consumer) {
if(!MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
throw new NbtApiException("This method only works for 1.20.5+!");
}
ReadWriteNBT nbti = NBT.itemStackToNBT(item);
consumer.accept(nbti.getOrCreateCompound("components"));
ItemStack tmp = NBT.itemStackFromNBT(nbti);
item.setItemMeta(tmp.getItemMeta());
}

/**
* It takes an ItemStack and a Consumer&lt;ReadWriteNBT&gt;, and then applies
* the Consumer to the ItemStacks Components as NBT. This is for 1.20.5+ only.
* This method is quiet expensive, so don't overuse it.
*
* @param item The item you want to modify the components of
* @param function The consumer that will be used to modify the components.
* @return The return type is the same as the return type of the function.
*/
public static <T> T modifyComponents(ItemStack item, Function<ReadWriteNBT, T> function) {
if(!MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
throw new NbtApiException("This method only works for 1.20.5+!");
}
ReadWriteNBT nbti = NBT.itemStackToNBT(item);
T ret = function.apply(nbti.getOrCreateCompound("components"));
ItemStack tmp = NBT.itemStackFromNBT(nbti);
item.setItemMeta(tmp.getItemMeta());
return ret;
}

/**
* It takes an entity and a function that takes a ReadWriteNBT and applies the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import de.tr7zw.nbtapi.plugin.tests.injector.MergeTileSubCompoundTest;
import de.tr7zw.nbtapi.plugin.tests.injector.SpawnEntityCustomNbtInjectorTest;
import de.tr7zw.nbtapi.plugin.tests.injector.TilesCustomNBTInjectorTest;
import de.tr7zw.nbtapi.plugin.tests.items.ComponentsTest;
import de.tr7zw.nbtapi.plugin.tests.items.DirectApplyMetaTest;
import de.tr7zw.nbtapi.plugin.tests.items.DirectApplyTest;
import de.tr7zw.nbtapi.plugin.tests.items.EmptyItemTest;
Expand Down Expand Up @@ -132,6 +133,7 @@ public void onLoad() {
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
apiTests.add(new LegacyItemTest());
}
apiTests.add(new ComponentsTest());
apiTests.add(new EmptyItemTest());
apiTests.add(new SmuggleTest());
apiTests.add(new DataItemProxyTest());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package de.tr7zw.nbtapi.plugin.tests.items;

import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import de.tr7zw.changeme.nbtapi.NBT;
import de.tr7zw.changeme.nbtapi.NbtApiException;
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
import de.tr7zw.nbtapi.plugin.tests.Test;

public class ComponentsTest implements Test {

@Override
public void test() throws Exception {
if(!MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
return;
}
ItemStack item = new ItemStack(Material.STICK);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("test");
item.setItemMeta(meta);
String comp = NBT.modifyComponents(item, n -> {
return n.toString();
});
if(!comp.contains("test")) {
throw new NbtApiException("ReadComponent didn't work!");
}
NBT.modifyComponents(item, nbt -> {
nbt.setString("minecraft:custom_name", "{\"extra\":[\"foobar\"],\"text\":\"\"}");
});
if(!item.getItemMeta().getDisplayName().equals("foobar")) {
throw new NbtApiException("ModifyComponent didn't work!");
}
}

}

0 comments on commit c16548f

Please sign in to comment.