9
0
mirror of https://github.com/HibiscusMC/HibiscusCommons.git synced 2025-12-31 04:46:37 +00:00

feat: bring builder more inline with paper implementation

This commit is contained in:
LoJoSho
2024-02-24 13:59:23 -06:00
parent 933f95fb6a
commit 7ab66c85ce
2 changed files with 83 additions and 61 deletions

View File

@@ -64,32 +64,32 @@ public class ItemBuilderSerializer implements TypeSerializer<ItemBuilder> {
if (materialNode.virtual()) return null;
ItemBuilder builder = new ItemBuilder(materialNode.getString("AIR"));
if (!amountNode.virtual()) builder.setAmount(amountNode.getInt(1));
if (!nameNode.virtual()) builder.setDisplayName(nameNode.getString(""));
if (!unbreakableNode.virtual()) builder.setUnbreakable(unbreakableNode.getBoolean());
if (!glowingNode.virtual()) builder.setGlowing(glowingNode.getBoolean());
if (!amountNode.virtual()) builder.amount(amountNode.getInt(1));
if (!nameNode.virtual()) builder.name(nameNode.getString(""));
if (!unbreakableNode.virtual()) builder.unbreakable(unbreakableNode.getBoolean());
if (!glowingNode.virtual()) builder.glowing(glowingNode.getBoolean());
if (!loreNode.virtual()) builder.setLoreUsingStrings(new ArrayList<>(loreNode.getList(String.class, new ArrayList<>())));
if (!appendLoreNode.virtual()) {
String loreAppendMode = appendLoreNode.getString("").toUpperCase();
if (EnumUtils.isValidEnum(LoreAppendMode.class, loreAppendMode)) builder.setLoreAppendMode(LoreAppendMode.valueOf(loreAppendMode));
if (EnumUtils.isValidEnum(LoreAppendMode.class, loreAppendMode)) builder.loreAppendMode(LoreAppendMode.valueOf(loreAppendMode));
}
if (!modelDataNode.virtual()) builder.setModelData(modelDataNode.getInt());
if (!modelDataNode.virtual()) builder.model(modelDataNode.getInt());
if (!nbtNode.virtual()) {
for (ConfigurationNode nbtNodes : nbtNode.childrenMap().values()) {
builder.addNBTData(NamespacedKey.minecraft(nbtNodes.key().toString()), nbtNodes.getString());
builder.NBTData(NamespacedKey.minecraft(nbtNodes.key().toString()), nbtNodes.getString());
}
}
if (!enchantsNode.virtual()) {
for (ConfigurationNode enchantNode : enchantsNode.childrenMap().values()) {
if (Enchantment.getByKey(NamespacedKey.minecraft(enchantNode.key().toString())) == null) continue;
builder.addEnchantment(enchantNode.key().toString(), enchantNode.getInt(1));
builder.enchant(enchantNode.key().toString(), enchantNode.getInt(1));
}
}
try {
if (!itemFlagsNode.virtual()) {
for (String itemFlag : itemFlagsNode.getList(String.class)) {
builder.addItemFlag(ItemFlag.valueOf(itemFlag));
builder.itemFlag(ItemFlag.valueOf(itemFlag));
}
}
} catch (Exception e) {
@@ -100,25 +100,25 @@ public class ItemBuilderSerializer implements TypeSerializer<ItemBuilder> {
String ownerString = ownerNode.getString();
if (ownerString.contains("%")) {
// This means it has PAPI placeholders in it
builder.addNBTData(InventoryUtils.getSkullOwner(), ownerString);
builder.NBTData(InventoryUtils.getSkullOwner(), ownerString);
}
builder.setSkullOwner(ownerString);
builder.skullOwner(ownerString);
}
if (!textureNode.virtual()) {
String textureString = textureNode.getString();
if (textureString.contains("%")) {
// This means it has PAPI placeholders in it
builder.addNBTData(InventoryUtils.getSkullTexture(), textureString);
builder.NBTData(InventoryUtils.getSkullTexture(), textureString);
}
builder.setTexture(textureString);
builder.texture(textureString);
}
if (!colorNode.virtual()) {
if (!redNode.virtual()) {
builder.setColor(Color.fromRGB(redNode.getInt(0), greenNode.getInt(0), blueNode.getInt(0)));
builder.color(Color.fromRGB(redNode.getInt(0), greenNode.getInt(0), blueNode.getInt(0)));
} else {
builder.setColor(ServerUtils.hex2Rgb(colorNode.getString("#FFFFFF")));
builder.color(ServerUtils.hex2Rgb(colorNode.getString("#FFFFFF")));
}
}

View File

@@ -1,8 +1,6 @@
package me.lojosho.hibiscuscommons.items;
import lombok.Getter;
import me.lojosho.hibiscuscommons.hooks.Hooks;
import me.lojosho.hibiscuscommons.util.AdventureUtils;
import me.lojosho.hibiscuscommons.util.InventoryUtils;
import me.lojosho.hibiscuscommons.util.StringUtils;
import net.kyori.adventure.text.Component;
@@ -19,7 +17,6 @@ import org.bukkit.persistence.PersistentDataType;
import org.bukkit.profile.PlayerProfile;
import org.bukkit.profile.PlayerTextures;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.net.URL;
import java.util.*;
@@ -27,37 +24,27 @@ import java.util.stream.Collectors;
public class ItemBuilder {
@Getter
private String material;
@Getter
private String display;
@Getter
private String texture;
@Getter
private String skullOwner;
private List<String> lore = new ArrayList<>();
private final ArrayList<String> itemFlags = new ArrayList<>();
private final HashMap<String, Integer> enchantments = new HashMap<>();
private final HashMap<NamespacedKey, String> nbtData = new HashMap<>();
@Getter
private int amount = 1;
@Getter
private int model = -1;
@Getter
private LoreAppendMode loreAppendMode;
@Getter
private boolean unbreakable = false;
@Getter
private boolean glowing = false;
@Getter
private Color color;
public ItemBuilder(String material) {
setMaterial(material);
material(material);
}
public ItemBuilder(Material material) {
setMaterial(material.toString());
material(material.toString());
}
public ItemBuilder(@NotNull ItemStack itemStack) {
@@ -100,57 +87,80 @@ public class ItemBuilder {
* @param material
* @return
*/
public ItemBuilder setMaterial(@NotNull String material) {
public ItemBuilder material(@NotNull String material) {
this.material = material;
return this;
}
public ItemBuilder setDisplayName(@NotNull String display) {
public String material() {
return material;
}
public ItemBuilder name(@NotNull String display) {
this.display = display;
return this;
}
public ItemBuilder setDisplayName(@NotNull Component display) {
public String name() {
return display;
}
public ItemBuilder name(@NotNull Component display) {
this.display = MiniMessage.miniMessage().serialize(display);
return this;
}
public ItemBuilder setModelData(int modelData) {
public ItemBuilder model(int modelData) {
this.model = modelData;
return this;
}
public ItemBuilder setAmount(int amount) {
public int model() {
return model;
}
public ItemBuilder amount(int amount) {
this.amount = amount;
return this;
}
public ItemBuilder setUnbreakable(boolean unbreakable) {
public int amount() {
return amount;
}
public ItemBuilder unbreakable(boolean unbreakable) {
this.unbreakable = unbreakable;
return this;
}
public ItemBuilder setTexture(@NotNull String texture) {
public boolean unbreakable() {
return unbreakable;
}
public ItemBuilder texture(@NotNull String texture) {
this.texture = texture;
return this;
}
public ItemBuilder setSkullOwner(@NotNull String username) {
public String texture() {
return texture;
}
public ItemBuilder skullOwner(@NotNull String username) {
this.skullOwner = username;
return this;
}
/**
* Should try to use #{@link #setLore(List)} instead, as this is just sending strings.
* @param lore The lore of the item
* @return
*/
public ItemBuilder setLoreUsingStrings(@NotNull List<String> lore) {
public String skullOwner() {
return skullOwner;
}
public ItemBuilder lore(@NotNull List<String> lore) {
this.lore = lore;
return this;
}
public ItemBuilder setLore(@NotNull List<Component> lore) {
public ItemBuilder loreUsingComponents(@NotNull List<Component> lore) {
this.lore = lore.stream()
.map(component -> MiniMessage.miniMessage().serialize(component))
.collect(Collectors.toList());
@@ -162,7 +172,7 @@ public class ItemBuilder {
* @param flags
* @return
*/
public ItemBuilder addItemFlag(ItemFlag... flags) {
public ItemBuilder itemFlag(ItemFlag... flags) {
for (ItemFlag flag : flags) this.itemFlags.add(flag.toString());
return this;
}
@@ -172,28 +182,36 @@ public class ItemBuilder {
* @param flags
* @return
*/
public ItemBuilder addItemFlag(String... flags) {
public ItemBuilder itemFlag(String... flags) {
this.itemFlags.addAll((Arrays.asList(flags)));
return this;
}
public List<String> itemFlag() {
return List.copyOf(itemFlags);
}
/**
* Sets the lore append mode on how to add extra lore to an existing item
* So if you get an item from Oraxen, where do you want the additional lore to be added
* @param mode
* @return
*/
public ItemBuilder setLoreAppendMode(@NotNull LoreAppendMode mode) {
public ItemBuilder loreAppendMode(@NotNull LoreAppendMode mode) {
this.loreAppendMode = mode;
return this;
}
public LoreAppendMode loreAppendMode() {
return loreAppendMode;
}
/**
* Sets the color of the item
* @param color
* @return
*/
public ItemBuilder setColor(Color color) {
public ItemBuilder color(Color color) {
this.color = color;
return this;
}
@@ -205,7 +223,7 @@ public class ItemBuilder {
* @param blue The blue value
* @return
*/
public ItemBuilder setColor(int red, int green, int blue) {
public ItemBuilder color(int red, int green, int blue) {
this.color = Color.fromRGB(red, green, blue);
return this;
}
@@ -215,17 +233,21 @@ public class ItemBuilder {
* @param color
* @return
*/
public ItemBuilder setColor(DyeColor color) {
public ItemBuilder color(DyeColor color) {
this.color = color.getColor();
return this;
}
public Color color() {
return color;
}
/**
* Adds an enchantment to the item. If the enchantment is not valid, it will be ignored. Default value is 1
* @param enchants
* @return
*/
public ItemBuilder addEnchantment(String... enchants) {
public ItemBuilder enchant(String... enchants) {
for (String enchant : enchants) enchantments.put(enchant, 1);
return this;
}
@@ -236,7 +258,7 @@ public class ItemBuilder {
* @param level
* @return
*/
public ItemBuilder addEnchantment(String enchant, int level) {
public ItemBuilder enchant(String enchant, int level) {
enchantments.put(enchant, level);
return this;
}
@@ -247,7 +269,7 @@ public class ItemBuilder {
* @param level
* @return
*/
public ItemBuilder addEnchantment(Enchantment enchant, int level) {
public ItemBuilder enchant(Enchantment enchant, int level) {
enchantments.put(enchant.getKey().getKey(), level);
return this;
}
@@ -257,7 +279,7 @@ public class ItemBuilder {
* @param glowing
* @return
*/
public ItemBuilder setGlowing(boolean glowing) {
public ItemBuilder glowing(boolean glowing) {
this.glowing = glowing;
return this;
}
@@ -268,7 +290,7 @@ public class ItemBuilder {
* @param value
* @return
*/
public ItemBuilder addNBTData(NamespacedKey key, String value) {
public ItemBuilder NBTData(NamespacedKey key, String value) {
nbtData.put(key, value);
return this;
}
@@ -297,7 +319,7 @@ public class ItemBuilder {
* Returns the lore of the item. Use #setLore to set the lore!
* @return
*/
public List<String> getLore() {
public List<String> lore() {
return List.copyOf(lore);
}
@@ -305,11 +327,11 @@ public class ItemBuilder {
* Returns all the item flags of the item. Use #addItemFlag to add item flags!
* @return
*/
public List<String> getItemFlags() {
public List<String> itemFlags() {
return List.copyOf(itemFlags);
}
public List<NamespacedKey> getNBTDataKeys() {
public List<NamespacedKey> NBTData() {
return List.copyOf(nbtData.keySet());
}
@@ -409,7 +431,7 @@ public class ItemBuilder {
}
}
}
if (itemFlags != null && !itemFlags.isEmpty()) {
if (!itemFlags.isEmpty()) {
for (String flag : itemFlags) {
if (!EnumUtils.isValidEnum(ItemFlag.class, flag)) continue;
ItemFlag iFlag = ItemFlag.valueOf(flag);
@@ -422,7 +444,7 @@ public class ItemBuilder {
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
}
if (enchantments != null && !enchantments.isEmpty()) {
if (!enchantments.isEmpty()) {
for (String enchantment : enchantments.keySet()) {
if (Enchantment.getByKey(NamespacedKey.minecraft(enchantment)) == null) continue;
meta.addEnchant(Enchantment.getByKey(NamespacedKey.minecraft(enchantment)), enchantments.get(enchantment), true);