Skip to content

Commit

Permalink
use normal colour/glow effects for potions in shop
Browse files Browse the repository at this point in the history
  • Loading branch information
steve4744 committed Mar 9, 2022
1 parent 4ffdf6d commit 999aa47
Showing 1 changed file with 51 additions and 25 deletions.
76 changes: 51 additions & 25 deletions src/main/java/tntrun/utils/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.Map;

import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.command.ConsoleCommandSender;
Expand Down Expand Up @@ -176,14 +175,9 @@ private ItemStack getItem(Material material, int amount, String displayname, Lis

private void addEnchantmentsToMeta(Material material, List<String> enchantments, ItemMeta meta) {
for (String enchs : enchantments) {
String[] array = enchs.split("#");
String ench = array[0].toUpperCase();
String ench = getEnchantmentName(enchs);
int level = getEnchantmentLevel(enchs);

// get the enchantment level
int level = 1;
if (array.length > 1 && Utils.isNumber(array[1])) {
level = Integer.valueOf(array[1]).intValue();
}
Enchantment realEnch = getEnchantmentFromString(ench);
if (realEnch != null) {
meta.addEnchant(realEnch, level, true);
Expand All @@ -194,21 +188,32 @@ private void addEnchantmentsToMeta(Material material, List<String> enchantments,
}
}

/**
* Get the splash potion item to give to the player.
*
* @param material
* @param amount
* @param displayname
* @param lore
* @param enchantments
* @return
*/
private ItemStack getPotionItem(Material material, int amount, String displayname, List<String> lore, List<String> enchantments) {
ItemStack item = new ItemStack(material, amount);
PotionMeta potionmeta = (PotionMeta) item.getItemMeta();

potionmeta.setDisplayName(displayname);
potionmeta.setColor(Color.RED);

if (lore != null && !lore.isEmpty()) {
potionmeta.setLore(getFormattedLore(lore));
}

if (enchantments != null && !enchantments.isEmpty()) {
for (String peffects : enchantments) {
PotionEffect effect = createPotionEffect(peffects);
if (effect != null) {
potionmeta.addCustomEffect(effect, true);
potionmeta.setColor(PotionEffectType.getByName(getEnchantmentName(peffects)).getColor());
}
}
}
Expand All @@ -217,16 +222,10 @@ private ItemStack getPotionItem(Material material, int amount, String displaynam
}

private PotionEffect createPotionEffect(String effect) {
String[] array = effect.split("#");
String name = array[0].toUpperCase();
int duration = 30;
if (array.length > 1 && Utils.isNumber(array[1])) {
duration = Integer.valueOf(array[1]).intValue();
}
int amplifier = 1;
if (array.length > 2 && Utils.isNumber(array[2])) {
amplifier = Integer.valueOf(array[2]).intValue();
}
String name = getEnchantmentName(effect);
int duration = getEnchantmentDuration(effect);
int amplifier = getEnchantmentAmplifier(effect);

PotionEffect peffect = new PotionEffect(PotionEffectType.getByName(name), duration * 20, amplifier);
return peffect;
}
Expand All @@ -253,12 +252,16 @@ public void setItems(Inventory inventory, Player player) {
for (String kitCounter : cfg.getConfigurationSection("").getKeys(false)) {
String title = FormattingCodesParser.parseFormattingCodes(cfg.getString(kitCounter + ".name"));
List<String> lore = cfg.getStringList(kitCounter + ".lore");

List<String> enchantments = cfg.getStringList(kitCounter + ".items.1.enchantments");
String firstEnchantment = (enchantments != null && !enchantments.isEmpty()) ? enchantments.get(0) : "";

boolean isGlowing = cfg.getBoolean(kitCounter + ".glow");
Material material = Material.getMaterial(cfg.getString(kitCounter + ".material"));
int amount = cfg.getInt(kitCounter + ".amount");

if (material.toString().equalsIgnoreCase("POTION") || material.toString().equalsIgnoreCase("SPLASH_POTION")) {
inventory.setItem(slot, getShopPotionItem(material, title, lore, amount));
inventory.setItem(slot, getShopPotionItem(material, title, lore, amount, firstEnchantment));
} else {
inventory.setItem(slot, getShopItem(material, title, lore, amount, isGlowing));
}
Expand Down Expand Up @@ -297,16 +300,19 @@ private ItemStack getShopItem(Material material, String title, List<String> lore
return item;
}

private ItemStack getShopPotionItem(Material material, String title, List<String> lore, int amount) {
private ItemStack getShopPotionItem(Material material, String title, List<String> lore, int amount, String enchantment) {
ItemStack item = new ItemStack(material, amount);
PotionMeta potionmeta = (PotionMeta) item.getItemMeta();

potionmeta.setDisplayName(title);
potionmeta.setColor(Color.BLUE);
if (material.toString().equalsIgnoreCase("SPLASH_POTION")) {
potionmeta.setColor(Color.RED);

PotionEffect effect = createPotionEffect(enchantment);
if (effect != null) {
potionmeta.addCustomEffect(effect, true);
potionmeta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
potionmeta.setColor(PotionEffectType.getByName(getEnchantmentName(enchantment)).getColor());
}
potionmeta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);

if ((lore != null) && (!lore.isEmpty())) {
potionmeta.setLore(getFormattedLore(lore));
}
Expand Down Expand Up @@ -416,4 +422,24 @@ private List<String> getFormattedLore(List<String> lore) {
}
return formattedLore;
}

private String getEnchantmentName(String enchant) {
String[] array = enchant.split("#");
return array[0].toUpperCase();
}

private int getEnchantmentLevel(String enchant) {
String[] array = enchant.split("#");
return (array.length > 1 && Utils.isNumber(array[1])) ? Integer.valueOf(array[1]) : 1;
}

private int getEnchantmentDuration(String enchant) {
String[] array = enchant.split("#");
return (array.length > 1 && Utils.isNumber(array[1])) ? Integer.valueOf(array[1]) : 30;
}

private int getEnchantmentAmplifier(String enchant) {
String[] array = enchant.split("#");
return (array.length > 2 && Utils.isNumber(array[2])) ? Integer.valueOf(array[2]) : 1;
}
}

0 comments on commit 999aa47

Please sign in to comment.