Skip to content

Commit

Permalink
fix: add proper handling for name function (#18)
Browse files Browse the repository at this point in the history
* fix: add proper handling for name function

* chore: update version
  • Loading branch information
Szczurowsky authored Jul 27, 2024
1 parent af8af91 commit 40c1245
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 12 deletions.
11 changes: 8 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@ plugins {
}

group = "pl.szczurowsky"
version = "1.0.1"
version = "1.0.2"

repositories {
mavenCentral()
maven {
url = uri("https://repo.papermc.io/repository/maven-public/")
}
maven {
url = uri("https://repo.szczurowsky.pl/releases")
}
maven {
name = "CodeMC"
url = uri("https://repo.codemc.io/repository/maven-public/")
}
mavenLocal()
}

dependencies {
implementation("com.google.code.gson:gson:2.11.0")
implementation("de.tr7zw:item-nbt-api:2.13.1")
compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT")
compileOnly("io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT")
compileOnly("org.jetbrains:annotations:24.1.0")

testImplementation(platform("org.junit:junit-bom:5.10.3"))
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT")
testImplementation("io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT")
testImplementation("com.github.seeseemelk:MockBukkit-v1.20:3.68.0")
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import pl.szczurowsky.loottableparser.pojo.LootEntry;
import pl.szczurowsky.loottableparser.pojo.integer.LootInteger;
import pl.szczurowsky.loottableparser.pojo.integer.LootUniformInteger;

import java.util.ArrayList;
Expand Down Expand Up @@ -68,10 +67,24 @@ public static void handleFunction(ItemStack itemStack, LootEntry lootEntry) {
});
}
case "minecraft:set_name" -> {
JsonArray name = jsonObject.getAsJsonArray("name");
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.displayName(JSONComponentSerializer.json().deserialize(name.toString()));
itemStack.setItemMeta(itemMeta);
if (jsonObject.get("name").isJsonArray()) {
JsonArray name = jsonObject.getAsJsonArray("name");
List<Component> textComponents = new ArrayList<>();
name.forEach(jsonElement1 -> textComponents.add(JSONComponentSerializer.json().deserialize(jsonElement1.toString())));
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.displayName(textComponents.get(0));
itemStack.setItemMeta(itemMeta);
} else if (jsonObject.get("name").isJsonObject()) {
Component textComponent = JSONComponentSerializer.json().deserialize(jsonObject.getAsJsonObject("name").toString());
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.displayName(textComponent);
itemStack.setItemMeta(itemMeta);
} else {
String name = jsonObject.get("name").getAsString();
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.displayName(JSONComponentSerializer.json().deserialize("{\"text\":\"" + name + "\"}"));
itemStack.setItemMeta(itemMeta);
}
}
case "minecraft:set_lore" -> {
JsonArray lore = jsonObject.getAsJsonArray("lore");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package pl.szczurowsky.loottableparser.util.paper;


import be.seeseemelk.mockbukkit.MockBukkit;
import be.seeseemelk.mockbukkit.ServerMock;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import pl.szczurowsky.loottableparser.exception.NotALootTableException;
Expand All @@ -17,10 +22,12 @@

class HandleFunctionUtilTest {

LootTableObject lootTableObject;
private ServerMock serverMock;
private LootTableObject lootTableObject;

@BeforeEach
void setUp() throws NotALootTableException {
this.serverMock = MockBukkit.mock();
String simpleLootTable = """
{
"type": "minecraft:chest",
Expand All @@ -35,28 +42,77 @@ void setUp() throws NotALootTableException {
{
"function": "minecraft:set_count",
"count": 3
},
{
"function": "minecraft:set_name",
"name": {
"text": "Test",
"bold": true
}
}
]
}
]
},
{
"rolls": 1,
"entries": [
{
"type": "minecraft:item",
"name": "minecraft:cobblestone",
"functions": [
{
"function": "minecraft:set_name",
"name": [
{
"text": "test",
"bold": true,
"italic": false
},
{
"text": "xd",
"bold": true,
"italic": false,
"underlined": true
}
]
}
]
}
]
}
]
}
""";
JsonObject parsedLootTable = new Gson().fromJson(simpleLootTable, JsonObject.class);
lootTableObject = LootTableParserUtil.parseJsonObject(parsedLootTable);
}

@AfterEach
public void tearDown()
{
MockBukkit.unmock();
}

@Test
void handleFunction() {
LootEntry entry = lootTableObject.getPools().get(0).getEntries().get(0);
Optional<Material> material = Optional.ofNullable(Material.getMaterial(entry.getName().replace("minecraft:", "").toUpperCase()));
assertTrue(material.isPresent());
assertEquals(Material.STONE, material.get());
assertEquals(1, entry.getLootFunction().get().size());
assertEquals(2, entry.getLootFunction().get().size());
ItemStack itemStack = new ItemStack(material.get(), 1);
HandleFunctionUtil.handleFunction(itemStack, entry);

assertEquals(3, itemStack.getAmount());
assertTrue(itemStack.getItemMeta().displayName().toString().contains("bold=true"));
LootEntry secondEntry = lootTableObject.getPools().get(1).getEntries().get(0);
Optional<Material> secondMaterial = Optional.ofNullable(Material.getMaterial(secondEntry.getName().replace("minecraft:", "").toUpperCase()));
assertTrue(secondMaterial.isPresent());
assertEquals(Material.COBBLESTONE, secondMaterial.get());
assertEquals(1, secondEntry.getLootFunction().get().size());
ItemStack secondItemStack = new ItemStack(secondMaterial.get(), 1);
HandleFunctionUtil.handleFunction(secondItemStack, secondEntry);
assertTrue(secondItemStack.getItemMeta().displayName().toString().contains("bold=true"));
}
}

0 comments on commit 40c1245

Please sign in to comment.