Skip to content

Instantly share code, notes, and snippets.

@DarkSeraphim
Last active January 3, 2022 23:58
Show Gist options
  • Save DarkSeraphim/9217330e81540f6da07f to your computer and use it in GitHub Desktop.
Save DarkSeraphim/9217330e81540f6da07f to your computer and use it in GitHub Desktop.
Utility to extract kits from Essentials, as ItemStack[]
import com.earth2me.essentials.Essentials;
import com.earth2me.essentials.Kit;
import com.earth2me.essentials.MetaItemStack;
import com.earth2me.essentials.textreader.IText;
import com.earth2me.essentials.textreader.KeywordReplacer;
import com.earth2me.essentials.textreader.SimpleTextInput;
import net.ess3.api.IEssentials;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
/**
* @Author DarkSeraphim
*/
public class KitExtractor
{
public static ItemStack[] getKit(String kitname)
{
return getKit(kitname, Bukkit.getConsoleSender());
}
public static ItemStack[] getKit(String kitname, CommandSender nameHolder)
{
ItemStack[] iss;
try
{
IEssentials ess = JavaPlugin.getPlugin(Essentials.class);
List<String> list = Kit.getItems(ess, null, kitname, ess.getSettings().getKit(kitname));
iss = parse(ess, list, nameHolder);
}
catch (Exception ex)
{
ex.printStackTrace();
return new ItemStack[0];
}
return iss;
}
@SuppressWarnings("Deprecated")
public static ItemStack[] parse(IEssentials ess, List<String> items, CommandSender nameHolder) throws Exception
{
List<ItemStack> iss = new ArrayList<ItemStack>();
IText input = new SimpleTextInput(items);
IText output = new KeywordReplacer(input, nameHolder, ess);
boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments();
for (String kitItem : output.getLines())
{
if (kitItem.startsWith(ess.getSettings().getCurrencySymbol()))
{
BigDecimal value = new BigDecimal(kitItem.substring(ess.getSettings().getCurrencySymbol().length()).trim());
iss.add(fromCurrency(value.doubleValue()));
}
else
{
String[] parts = kitItem.split(" +");
ItemStack parseStack = ess.getItemDb().get(parts[0], parts.length > 1 ? Integer.parseInt(parts[1]) : 1);
if (parseStack.getType() != Material.AIR)
{
MetaItemStack metaStack = new MetaItemStack(parseStack);
if (parts.length > 2)
{
metaStack.parseStringMeta(null, allowUnsafe, parts, 2, ess);
}
iss.add(metaStack.getItemStack());
}
}
}
return iss.toArray(new ItemStack[iss.size()]);
}
public static ItemStack fromCurrency(double amount)
{
ItemStack is = new ItemStack(Material.PAPER);
ItemMeta meta = is.getItemMeta();
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
meta.setDisplayName(ChatColor.GOLD + nf.format(amount));
is.setItemMeta(meta);
return is;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment