diff --git a/build.gradle.kts b/build.gradle.kts index 0ed4090..33f0f8a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -19,7 +19,9 @@ repositories { mavenCentral() maven("https://papermc.io/repo/repository/maven-public/") maven("https://repo.thbn.me/releases/") - maven("https://repo.thbn.me/snapshots/") + maven("https://repo.tehbrian.dev/snapshots/") + maven("https://repo.codemc.io/repository/maven-public/") + maven("https://repo.broccol.ai/snapshots/") } dependencies { @@ -34,7 +36,8 @@ dependencies { implementation("com.google.inject:guice:7.0.0") implementation("org.spongepowered:configurate-yaml:4.1.2") implementation("dev.tehbrian:tehlib-paper:0.6.0") - implementation("dev.tehbrian.restrictionhelper:restrictionhelper-spigot:0.4.1") + implementation("dev.tehbrian:restrictionhelper-spigot:0.5.0") + implementation("de.tr7zw:item-nbt-api:2.15.0") } tasks { @@ -67,7 +70,7 @@ tasks { "love.broccolai.corn", "cloud.commandframework", "com.google", - "dev.tehbrian.restrictionhelper", + "dev.tehbrian.restrictionhelper-spigot", "dev.tehbrian.tehlib", "io.leangen", "jakarta.inject", @@ -77,6 +80,7 @@ tasks { "org.checkerframework", "org.spongepowered", "org.yaml", + "de.tr7zw.changeme.nbtapi" ) } diff --git a/src/main/java/dev/tehbrian/buildersutilities/config/SpecialConfig.java b/src/main/java/dev/tehbrian/buildersutilities/config/SpecialConfig.java index fe943d8..c60fc1c 100644 --- a/src/main/java/dev/tehbrian/buildersutilities/config/SpecialConfig.java +++ b/src/main/java/dev/tehbrian/buildersutilities/config/SpecialConfig.java @@ -2,11 +2,11 @@ import com.google.inject.Inject; import com.google.inject.name.Named; +import de.tr7zw.changeme.nbtapi.NBT; +import de.tr7zw.changeme.nbtapi.iface.ReadableNBT; import dev.tehbrian.tehlib.configurate.AbstractConfig; import org.bukkit.Material; -import org.bukkit.block.data.type.Light; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.BlockDataMeta; import org.checkerframework.checker.nullness.qual.Nullable; import org.slf4j.Logger; import org.spongepowered.configurate.CommentedConfigurationNode; @@ -15,7 +15,6 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.List; -import java.util.Locale; public final class SpecialConfig extends AbstractConfig { @@ -43,50 +42,11 @@ public void load() throws ConfigurateException { } for (final String dirtyItemName : itemNames) { - final var itemName = dirtyItemName.strip().toUpperCase(Locale.ROOT); - - // special case for light levels. - if (itemName.startsWith("LIGHT") && !itemName.equals("LIGHT")) { - int level; - try { - level = Integer.parseInt(itemName.split("-")[1]); - } catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { - this.logger.warn("Invalid light data {}.", itemName); - this.logger.warn("Skipping this item. Please check your {}", fileName); - this.logger.warn("Printing stack trace:", e); - continue; - } - - if (level > 15) { - level = 15; - } - if (level < 0) { - level = 0; - } - - // create a light item with that light level. - final ItemStack item = new ItemStack(Material.LIGHT); - final BlockDataMeta meta = (BlockDataMeta) item.getItemMeta(); - final Light data = (Light) Material.LIGHT.createBlockData(); - data.setLevel(level); - meta.setBlockData(data); - item.setItemMeta(meta); - + final String configItem = dirtyItemName.strip(); + ItemStack item = this.itemFromString(configItem, fileName); + if (item != null) { this.items.add(item); - continue; } - - final Material itemMaterial; - try { - itemMaterial = Material.valueOf(itemName); - } catch (final IllegalArgumentException e) { - this.logger.warn("The material {} does not exist.", itemName); - this.logger.warn("Skipping this item. Please check your {}", fileName); - this.logger.warn("Printing stack trace:", e); - continue; - } - - this.items.add(new ItemStack(itemMaterial)); } } @@ -94,4 +54,36 @@ public List items() { return this.items; } + /** + * This method return the ItemStack object based on the configuration following + * the syntax '%Material name%, %JSON values%'. + * @param configItem The item string + * @param fileName The name of the configuration file for logging purposes + * @return The ItemStack object or null if the item is invalid + */ + private @Nullable ItemStack itemFromString(String configItem, String fileName) { + try { + String[] itemData = configItem.strip().split(", ", 2); + Material material = Material.valueOf(itemData[0].toUpperCase()); + ItemStack item = new ItemStack(material); + + if (itemData.length > 1 && !itemData[1].isBlank()) { + try { + ReadableNBT itemNBT = NBT.parseNBT(itemData[1]); + NBT.modifyComponents(item, nbt -> { + nbt.mergeCompound(itemNBT); + }); + } catch (Exception e) { + this.logger.warn("Invalid NBT data for item {}: {}", material, itemData[1], e); + return null; + } + } + return item; + } catch (IllegalArgumentException e) { + this.logger.warn("The material {} does not exist.", configItem); + this.logger.warn("Skipping this item. Please check your {}", fileName); + this.logger.warn("Printing stack trace:", e); + return null; + } + } } diff --git a/src/main/resources/META-INF/.mojang-mapped b/src/main/resources/META-INF/.mojang-mapped new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/special.yml b/src/main/resources/special.yml index 88641b7..d216d2f 100644 --- a/src/main/resources/special.yml +++ b/src/main/resources/special.yml @@ -17,22 +17,22 @@ items: - STRUCTURE_VOID - BARRIER - DEBUG_STICK - - LIGHT-15 - - LIGHT-14 - - LIGHT-13 - - LIGHT-12 - - LIGHT-11 - - LIGHT-10 - - LIGHT-9 - - LIGHT-8 - - LIGHT-7 - - LIGHT-6 - - LIGHT-5 - - LIGHT-4 - - LIGHT-3 - - LIGHT-2 - - LIGHT-1 - - LIGHT-0 + - 'LIGHT, {"minecraft:block_state": {level: "15"}}' + - 'LIGHT, {"minecraft:block_state": {level: "14"}} ' + - 'LIGHT, {"minecraft:block_state": {level: "13"}}' + - 'LIGHT, {"minecraft:block_state": {level: "12"}}' + - 'LIGHT, {"minecraft:block_state": {level: "11"}}' + - 'LIGHT, {"minecraft:block_state": {level: "10"}}' + - 'LIGHT, {"minecraft:block_state": {level: "9"}}' + - 'LIGHT, {"minecraft:block_state": {level: "8"}}' + - 'LIGHT, {"minecraft:block_state": {level: "7"}}' + - 'LIGHT, {"minecraft:block_state": {level: "6"}}' + - 'LIGHT, {"minecraft:block_state": {level: "5"}}' + - 'LIGHT, {"minecraft:block_state": {level: "4"}}' + - 'LIGHT, {"minecraft:block_state": {level: "3"}}' + - 'LIGHT, {"minecraft:block_state": {level: "2"}}' + - 'LIGHT, {"minecraft:block_state": {level: "1"}}' + - 'LIGHT, {"minecraft:block_state": {level: "0"}}' # Items below are not in the operator items menu. - KNOWLEDGE_BOOK @@ -45,5 +45,8 @@ items: - TIPPED_ARROW - PETRIFIED_OAK_SLAB + # Normal items with special NBT + - 'ITEM_FRAME, {"minecraft:entity_data": {id: "minecraft:item_frame", Invisible: 1b}, "minecraft:item_name":''{text:"Invisible Item Frame"}''}' + # No touch! version: 1