Skip to content

Commit

Permalink
optimize itembuilder, fix potion colour
Browse files Browse the repository at this point in the history
  • Loading branch information
Oribuin committed May 16, 2024
1 parent 34f5c2a commit 2967e37
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 82 deletions.
97 changes: 23 additions & 74 deletions src/main/java/xyz/oribuin/eternaltags/util/ItemBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
public class ItemBuilder {

private final ItemStack item;
private ItemMeta meta;

public ItemBuilder(Material material) {
this.item = new ItemStack(material);
this.meta = this.item.getItemMeta();
}

public ItemBuilder(ItemStack item) {
this.item = item.clone();
this.meta = this.item.getItemMeta();
}

public ItemBuilder setMaterial(Material material) {
Expand All @@ -45,13 +48,7 @@ public ItemBuilder setMaterial(Material material) {
* @return Item.Builder.
*/
public ItemBuilder name(@Nullable String text) {
final ItemMeta meta = this.item.getItemMeta();
if (meta == null || text == null)
return this;

meta.setDisplayName(text);
this.item.setItemMeta(meta);

this.meta.setDisplayName(text);
return this;
}

Expand All @@ -62,12 +59,7 @@ public ItemBuilder name(@Nullable String text) {
* @return Item.Builder.
*/
public ItemBuilder lore(@Nullable List<String> lore) {
final ItemMeta meta = this.item.getItemMeta();
if (meta == null || lore == null)
return this;

meta.setLore(lore);
this.item.setItemMeta(meta);
this.meta.setLore(lore);
return this;
}

Expand Down Expand Up @@ -100,12 +92,7 @@ public ItemBuilder amount(int amount) {
* @return Item.Builder
*/
public ItemBuilder enchant(Enchantment ench, int level) {
final ItemMeta meta = this.item.getItemMeta();
if (meta == null) return this;

meta.addEnchant(ench, level, true);
this.item.setItemMeta(meta);

this.meta.addEnchant(ench, level, true);
return this;
}

Expand All @@ -117,14 +104,7 @@ public ItemBuilder enchant(Enchantment ench, int level) {
*/

public ItemBuilder enchant(Map<Enchantment, Integer> enchantments) {
final ItemMeta meta = this.item.getItemMeta();
if (meta == null) return this;

for (Map.Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
meta.addEnchant(entry.getKey(), entry.getValue(), true);
}

this.item.setItemMeta(meta);
enchantments.forEach(this::enchant);
return this;
}

Expand All @@ -146,13 +126,7 @@ public ItemBuilder remove(Enchantment ench) {
* @return Item.Builder
*/
public ItemBuilder flags(ItemFlag[] flags) {
final ItemMeta meta = this.item.getItemMeta();
if (meta == null) return this;

meta.removeItemFlags(ItemFlag.values());
meta.addItemFlags(flags);
this.item.setItemMeta(meta);

this.meta.removeItemFlags(ItemFlag.values());
return this;
}

Expand All @@ -164,11 +138,7 @@ public ItemBuilder flags(ItemFlag[] flags) {
* @return Item.Builder
*/
public ItemBuilder unbreakable(boolean unbreakable) {
final ItemMeta meta = this.item.getItemMeta();
if (meta == null) return this;

meta.setUnbreakable(unbreakable);
item.setItemMeta(meta);
this.meta.setUnbreakable(unbreakable);
return this;
}

Expand All @@ -180,13 +150,8 @@ public ItemBuilder unbreakable(boolean unbreakable) {
public ItemBuilder glow(boolean b) {
if (!b) return this;

final ItemMeta meta = this.item.getItemMeta();
if (meta == null) return this;

meta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 1, true);
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
this.item.setItemMeta(meta);

this.meta.addEnchant(Enchantment.PROTECTION_ENVIRONMENTAL, 1, true);
this.meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
return this;
}

Expand All @@ -200,12 +165,8 @@ public ItemBuilder texture(@Nullable String texture) {
if (item.getType() != Material.PLAYER_HEAD || texture == null)
return this;

final SkullMeta skullMeta = (SkullMeta) item.getItemMeta();
if (skullMeta == null)
return this;

if (!(this.meta instanceof SkullMeta skullMeta)) return this;
SkullUtils.setSkullTexture(skullMeta, texture);
this.item.setItemMeta(skullMeta);
return this;
}

Expand All @@ -216,46 +177,33 @@ public ItemBuilder texture(@Nullable String texture) {
* @return Item.Builder
*/
public ItemBuilder owner(OfflinePlayer owner) {
if (item.getType() != Material.PLAYER_HEAD)
return this;

final SkullMeta skullMeta = (SkullMeta) item.getItemMeta();
if (skullMeta == null || owner == null || skullMeta.getOwningPlayer() != null)
return this;
if (item.getType() != Material.PLAYER_HEAD) return this;
if (owner == null) return this;
if (!(this.meta instanceof SkullMeta skullMeta)) return this;

skullMeta.setOwningPlayer(owner);
this.item.setItemMeta(skullMeta);
return this;
}

public ItemBuilder model(int model) {
final ItemMeta meta = this.item.getItemMeta();
if (meta == null || model <= 0)
return this;

meta.setCustomModelData(model);
this.item.setItemMeta(meta);
this.meta.setCustomModelData(model);
return this;
}

public ItemBuilder potion(PotionEffectType effectType, int duration, int amp) {
if (!(this.item.getItemMeta() instanceof PotionMeta meta))
return this;
if (!(this.meta instanceof PotionMeta potionMeta)) return this;

meta.addCustomEffect(new PotionEffect(effectType, duration, amp), true);
this.item.setItemMeta(meta);
potionMeta.addCustomEffect(new PotionEffect(effectType, duration, amp), true);
return this;
}

public ItemBuilder color(Color color) {
if (this.item.getItemMeta() instanceof PotionMeta meta) {
meta.setColor(color);
this.item.setItemMeta(meta);
if (this.meta instanceof PotionMeta potionMeta) {
potionMeta.setColor(color);
}

if (this.item.getItemMeta() instanceof LeatherArmorMeta meta) {
meta.setColor(color);
this.item.setItemMeta(meta);
if (this.item.getItemMeta() instanceof LeatherArmorMeta leatherArmorMeta) {
leatherArmorMeta.setColor(color);
}

return this;
Expand All @@ -267,6 +215,7 @@ public ItemBuilder color(Color color) {
* @return The ItemStack
*/
public ItemStack build() {
this.item.setItemMeta(this.meta);
return this.item;
}

Expand Down
14 changes: 6 additions & 8 deletions src/main/java/xyz/oribuin/eternaltags/util/TagsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,15 @@ public static Location center(Location location) {
* @return The bukkit color
*/
public static Color fromHex(String hex) {
if (hex == null)
return Color.BLACK;

java.awt.Color awtColor;
try {
awtColor = java.awt.Color.decode(hex);
if (hex == null || hex.isEmpty())
return Color.WHITE;

java.awt.Color decoded = java.awt.Color.decode(hex);
return Color.fromRGB(decoded.getRed(), decoded.getGreen(), decoded.getBlue());
} catch (NumberFormatException e) {
return Color.BLACK;
}

return Color.fromRGB(awtColor.getRed(), awtColor.getGreen(), awtColor.getBlue());
}

/**
Expand Down Expand Up @@ -194,7 +192,7 @@ public static ItemStack deserialize(
.model(toInt(locale.format(sender, section.getString(key + ".model-data", "0"), placeholders)))
.enchant(enchantments)
.texture(locale.format(sender, section.getString(key + ".texture"), placeholders))
.color(fromHex(locale.format(sender, section.getString(key + ".potion-color"), placeholders)))
.color(fromHex(section.getString(key + ".potion-color")))
.owner(offlinePlayer)
.build();
}
Expand Down

0 comments on commit 2967e37

Please sign in to comment.