Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gmail.coollord14.inventorycommands;
- import java.lang.reflect.Array;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Set;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.Material;
- import org.bukkit.command.CommandSender;
- import org.bukkit.enchantments.Enchantment;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.PlayerInventory;
- import org.bukkit.inventory.meta.ItemMeta;
- import net.md_5.bungee.api.ChatMessageType;
- import net.md_5.bungee.api.chat.TextComponent;
- public class Methods {
- public static String prefix = "&f[&bInventoryCommands&f] ";
- public static HashMap<Player, HashMap<String, String>> playerStatus = new HashMap<Player, HashMap<String, String>>();
- public static HashMap<Player, HashMap<String, String>> playerGroupStatus = new HashMap<Player, HashMap<String, String>>();
- public static void sendMessage(boolean onlyActionBar, boolean usePrefix, boolean actionBar, CommandSender sender, String msg) {
- if(usePrefix && !onlyActionBar)
- sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + msg));
- if(!usePrefix && !onlyActionBar)
- sender.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
- if(sender instanceof Player && actionBar)
- ((Player) sender).spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', msg)));
- }
- public static void checkItems(PlayerInventory inv, Player p, InventoryCommands plugin) {
- if(plugin.getConfig().getConfigurationSection("items") != null && plugin.getConfig().getConfigurationSection("items").getKeys(false).size() > 0) {
- HashMap<String, String> returnedItemStats = new HashMap<String, String>();
- for(String itemInConfig : plugin.getConfig().getConfigurationSection("items").getKeys(false)) {
- //creating which commands need to be run for this item
- List<String> commandsPresent = new ArrayList<String>();
- List<String> commandsAbsent = new ArrayList<String>();
- //Getting config values
- String data = plugin.getConfig().getString("items." + itemInConfig + ".data");
- List<String> commandsPresentConfig = plugin.getConfig().getStringList("items." + itemInConfig + ".present_commands");
- List<String> commandsAbsentConfig = plugin.getConfig().getStringList("items." + itemInConfig + ".absent_commands");
- //parsing item to see if it is in the inventory
- ItemStack item = parseItem(data);
- boolean contains = contains(inv, item);
- boolean changed = false;
- if(playerStatus.containsKey(p)) {
- returnedItemStats = playerStatus.get(p);
- if(returnedItemStats.containsKey(itemInConfig)) {
- String status = returnedItemStats.get(itemInConfig);
- if(status.equalsIgnoreCase("absent") && contains) {
- commandsPresent.addAll(commandsPresentConfig);
- returnedItemStats.put(itemInConfig, "contains");
- changed = true;
- } else if(status.equalsIgnoreCase("contains") && !contains) {
- commandsAbsent.addAll(commandsAbsentConfig);
- returnedItemStats.put(itemInConfig, "absent");
- changed = true;
- }
- } else if(!returnedItemStats.containsKey(itemInConfig)) {
- if(contains) {
- commandsPresent.addAll(commandsPresentConfig);
- returnedItemStats.put(itemInConfig, "contains");
- changed = true;
- } else if(!contains) {
- commandsAbsent.addAll(commandsAbsentConfig);
- returnedItemStats.put(itemInConfig, "absent");
- changed = true;
- }
- }
- } else {
- if(contains) {
- commandsPresent.addAll(commandsPresentConfig);
- returnedItemStats.put(itemInConfig, "contains");
- changed = true;
- } else if(!contains) {
- commandsAbsent.addAll(commandsAbsentConfig);
- returnedItemStats.put(itemInConfig, "absent");
- changed = true;
- }
- }
- if(changed) {
- for(String cmd : commandsPresent) {
- Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replaceAll("%p", p.getName()));
- }
- for(String cmd : commandsAbsent) {
- Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replaceAll("%p", p.getName()));
- }
- }
- }
- playerStatus.put(p, returnedItemStats);
- }
- if(plugin.getConfig().getConfigurationSection("item_groups") != null && plugin.getConfig().getConfigurationSection("item_groups").getKeys(false).size() > 0) {
- //getting the item group names, and if they contain more keys inside, proceed onwards!
- HashMap<String, String> returnedItemStats = new HashMap<String, String>();
- for(String groupID : plugin.getConfig().getConfigurationSection("item_groups").getKeys(false)) {
- String slotLocation = "LOWEST";
- HashMap<ItemStack, String> toSearchFor = new HashMap<ItemStack, String>();
- //getting the items in each group
- if(plugin.getConfig().getConfigurationSection("item_groups." + groupID) != null && plugin.getConfig().getConfigurationSection("item_groups." + groupID).getKeys(false).size() > 0) {
- for(String itemID : plugin.getConfig().getConfigurationSection("item_groups." + groupID).getKeys(false)) {
- //if its slot specification
- if(itemID.equalsIgnoreCase("slot"))
- slotLocation = plugin.getConfig().getString("item_groups." + groupID + ".slot");
- else {
- //Getting config values
- String data = plugin.getConfig().getString("item_groups." + groupID + "." + itemID + ".data");
- toSearchFor.put(parseItem(data), itemID);
- }
- }
- }
- String successful = toSearchFor.get(contains(inv, toSearchFor.keySet(), slotLocation));
- boolean changed = false;
- boolean contains = false;
- if(successful != null)
- contains = true;
- //creating which commands need to be run for this item
- List<String> commandsPresent = new ArrayList<String>();
- List<String> commandsAbsent = new ArrayList<String>();
- if(playerGroupStatus.containsKey(p) && playerGroupStatus.get(p).containsKey(groupID)) {
- returnedItemStats = playerGroupStatus.get(p);
- String lastSuccessfulItem = returnedItemStats.get(groupID);
- if(successful == null) {
- commandsAbsent.addAll(plugin.getConfig().getStringList("item_groups." + groupID + "." + lastSuccessfulItem + ".absent_commands"));
- returnedItemStats.remove(groupID);
- changed = true;
- }
- else if(!lastSuccessfulItem.equals(successful)) {
- commandsPresent.addAll(plugin.getConfig().getStringList("item_groups." + groupID + "." + successful + ".present_commands"));
- commandsAbsent.addAll(plugin.getConfig().getStringList("item_groups." + groupID + "." + lastSuccessfulItem + ".absent_commands"));
- returnedItemStats.put(groupID, successful);
- changed = true;
- }
- } else {
- if(contains) {
- commandsPresent.addAll(plugin.getConfig().getStringList("item_groups." + groupID + "." + successful + ".present_commands"));
- returnedItemStats.put(groupID, successful);
- changed = true;
- }
- }
- if(changed) {
- for(String cmd : commandsPresent) {
- Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replaceAll("%p", p.getName()));
- }
- for(String cmd : commandsAbsent) {
- Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replaceAll("%p", p.getName()));
- }
- }
- }
- if(returnedItemStats.isEmpty())
- playerGroupStatus.remove(p);
- else playerGroupStatus.put(p, returnedItemStats);
- }
- }
- public static ItemStack contains(PlayerInventory inv, Set<ItemStack> toSearch, String slot) {
- try {
- int slotPosition = Integer.parseInt(slot);
- for(ItemStack requirement : toSearch) {
- ArrayList<ItemStack> potential = possibleMatches(inv, requirement);
- for(ItemStack potentialMatch : potential) {
- if(inv.getItem(slotPosition).equals(potentialMatch))
- return potentialMatch;
- }
- }
- } catch(Exception e) {
- ArrayList<ItemStack> allMatches = new ArrayList<ItemStack>();
- for(ItemStack requirement : toSearch) {
- allMatches.addAll(possibleMatches(inv, requirement));
- }
- int min = Integer.MAX_VALUE;
- int max = 0;
- boolean ignoreArmor = true;
- for(ItemStack foundItem : allMatches) {
- if(inv.first(foundItem) <= min && inv.first(foundItem) >= 0)
- min = inv.first(foundItem);
- if(inv.first(foundItem) >= max)
- max = inv.first(foundItem);
- }
- for(ItemStack foundItem : allMatches) {
- if(Arrays.asList(inv.getExtraContents()).contains(foundItem) && slot.equalsIgnoreCase("lowest"))
- return foundItem;
- if(Arrays.asList(inv.getArmorContents()).contains(foundItem) && slot.equalsIgnoreCase("highest")) {
- ignoreArmor = false;
- if(max > 3)
- max = Arrays.asList(inv.getArmorContents()).indexOf(foundItem);
- else if(max < Arrays.asList(inv.getArmorContents()).indexOf(foundItem))
- max = Arrays.asList(inv.getArmorContents()).indexOf(foundItem);
- }
- }
- if(slot.equalsIgnoreCase("lowest")) {
- if(min < 0)
- return null;
- return inv.getItem(min);
- }
- if(!ignoreArmor)
- return Arrays.asList(inv.getArmorContents()).get(max);
- if(ignoreArmor && slot.equalsIgnoreCase("highest")) {
- if(max < 0)
- return null;
- return inv.getItem(max);
- }
- }
- return null;
- }
- public static boolean contains(PlayerInventory inv, ItemStack req) {
- if(possibleMatches(inv, req).size() > 0)
- return true;
- else return false;
- }
- public static ArrayList<ItemStack> possibleMatches(PlayerInventory inv, ItemStack req) {
- ArrayList<ItemStack> matchedItems = new ArrayList<ItemStack>();
- for(ItemStack item : inv.getContents()) {
- if(item != null && item.getType().equals(req.getType()))
- matchedItems.add(item);
- }
- Iterator<ItemStack> iterator = matchedItems.iterator();
- while(iterator.hasNext()) {
- ItemStack item = iterator.next();
- if (req.getItemMeta().getDisplayName() != null)
- if (!req.getItemMeta().getDisplayName().equals(item.getItemMeta().getDisplayName()))
- iterator.remove();
- if (req.getItemMeta().getLore() != null && !req.getItemMeta().getLore().isEmpty())
- if (!req.getItemMeta().getLore().equals(item.getItemMeta().getLore()))
- iterator.remove();
- if (!req.getEnchantments().isEmpty())
- if(item.getEnchantments().isEmpty())
- iterator.remove();
- else if(!matches(req.getEnchantments(), item.getEnchantments()))
- iterator.remove();
- }
- return matchedItems;
- }
- public static ArrayList<HashMap<Enchantment, Integer>> parseEnchantments(String enchantments) {
- ArrayList<HashMap<Enchantment, Integer>> enchList = new ArrayList<HashMap<Enchantment, Integer>>();
- if (!enchantments.isEmpty()) {
- String[] split3 = enchantments.split("!");
- for (String loopEnchantment : split3) {
- HashMap<Enchantment, Integer> ench = parseFromString(loopEnchantment);
- if (ench != null)
- enchList.add(ench);
- }
- }
- return enchList;
- }
- public static boolean matches(Map<Enchantment, Integer> customEnchs, Map<Enchantment, Integer> toolEnchs) {
- int matchCount = 0;
- if(customEnchs.size() == 0 && toolEnchs.size() == 0)
- return true;
- for (Entry<Enchantment, Integer> customEnchEntry : customEnchs.entrySet()) {
- for (Entry<Enchantment, Integer> toolEnchEntry : toolEnchs.entrySet()) {
- if(customEnchEntry.getKey().equals(toolEnchEntry.getKey()))
- if(customEnchEntry.getValue().equals(toolEnchEntry.getValue()))
- matchCount++;
- }
- }
- if (matchCount != customEnchs.size())
- return false;
- return true;
- }
- public static HashMap<Enchantment, Integer> parseFromString(String input) {
- HashMap<Enchantment, Integer> enchant = new HashMap<Enchantment, Integer>();
- String[] enchSplit = input.split("#");
- String enchString = enchSplit[0].trim().toLowerCase();
- String enchLevel = "";
- if (enchSplit.length > 1)
- enchLevel = enchSplit[1];
- int enchLevelInt = 0;
- try {
- if (!enchLevel.isEmpty() && enchLevel.matches("[0-9-~]*"))
- enchLevelInt = Integer.parseInt(enchLevel);
- } catch (NumberFormatException x) {
- return null;
- }
- Enchantment ench = Enchantment.getByName(enchString);
- if (ench == null || enchLevelInt == 0) {
- return null;
- }
- enchant.put(ench, enchLevelInt);
- return enchant;
- }
- public static ItemStack parseItem(String input) {
- String dataString = null;
- ArrayList<HashMap<Enchantment, Integer>> enchantments = new ArrayList<HashMap<Enchantment, Integer>>();
- String displayname = null;
- List<String> lore = new ArrayList<String>();
- Material material;
- String[] firstSplit = input.split("[@:;~]", 2);
- if (firstSplit.length > 1) {
- // if extra fields are found, parse them - firstly separating out the type of "thing" this is
- material = Material.getMaterial(firstSplit[0]);
- String firstChar = input.substring(firstSplit[0].length(), firstSplit[0].length() + 1);
- if (firstChar.matches("[^~]")) {
- // only want to use a semi-colon rather than @ or : but preserve the ~
- firstChar = ";";
- } else if (firstChar.matches("~")) {
- displayname = "";
- }
- input = firstChar + firstSplit[1];
- // check for initial data value and enchantment to support old format
- if (input.matches("([;])([^;!]+)!.*")) {
- String[] dataEnchSplit = input.split("!", 2);
- dataString = dataEnchSplit[0].substring(1);
- input = ";" + dataEnchSplit[1];
- }
- // then, loop through each ";<value>" or "~<value>" pair and parse accordingly
- Pattern p = Pattern.compile("([~;])([^~;]+)");
- Matcher m = p.matcher(input);
- while (m.find()) {
- String key = m.group(1);
- String value = m.group(2);
- value = value.replaceAll("slashCharPlaceholder", "/");
- if (key != null && value != null) {
- if (key.equals("~")) {
- displayname = ChatColor.translateAlternateColorCodes('&', value);
- } else if (displayname != null && !displayname.isEmpty()) {
- // displayname found, treat next as lore
- value = ChatColor.translateAlternateColorCodes('&', value);
- lore.add(value);
- } else {
- // first check for enchantment
- ArrayList<HashMap<Enchantment, Integer>> ench = parseEnchantments(value);
- if (ench == null || ench.isEmpty()) {
- // otherwise assume data
- dataString = value;
- } else {
- enchantments.addAll(ench);
- }
- }
- }
- }
- } else {
- material = Material.getMaterial(input);
- }
- if(material != null) {
- ItemStack item = new ItemStack(material);
- ItemMeta itemMeta = item.getItemMeta();
- if(displayname != null)
- itemMeta.setDisplayName(displayname);
- if(!lore.isEmpty())
- itemMeta.setLore(lore);
- for(HashMap<Enchantment, Integer> ench : enchantments) {
- for(Enchantment enchant : ench.keySet()) {
- itemMeta.addEnchant(enchant, ench.get(enchant), true);
- }
- }
- item.setItemMeta(itemMeta);
- return item;
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement