Advertisement
CoolLord22

Untitled

Aug 21st, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.97 KB | None | 0 0
  1. package com.gmail.coollord14.inventorycommands;
  2.  
  3. import java.lang.reflect.Array;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Map.Entry;
  11. import java.util.Set;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14.  
  15. import org.bukkit.Bukkit;
  16. import org.bukkit.ChatColor;
  17. import org.bukkit.Material;
  18. import org.bukkit.command.CommandSender;
  19. import org.bukkit.enchantments.Enchantment;
  20. import org.bukkit.entity.Player;
  21. import org.bukkit.inventory.ItemStack;
  22. import org.bukkit.inventory.PlayerInventory;
  23. import org.bukkit.inventory.meta.ItemMeta;
  24.  
  25. import net.md_5.bungee.api.ChatMessageType;
  26. import net.md_5.bungee.api.chat.TextComponent;
  27.  
  28. public class Methods {
  29. public static String prefix = "&f[&bInventoryCommands&f] ";
  30. public static HashMap<Player, HashMap<String, String>> playerStatus = new HashMap<Player, HashMap<String, String>>();
  31. public static HashMap<Player, HashMap<String, String>> playerGroupStatus = new HashMap<Player, HashMap<String, String>>();
  32.  
  33. public static void sendMessage(boolean onlyActionBar, boolean usePrefix, boolean actionBar, CommandSender sender, String msg) {
  34. if(usePrefix && !onlyActionBar)
  35. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', prefix + msg));
  36. if(!usePrefix && !onlyActionBar)
  37. sender.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
  38.  
  39. if(sender instanceof Player && actionBar)
  40. ((Player) sender).spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', msg)));
  41. }
  42.  
  43. public static void checkItems(PlayerInventory inv, Player p, InventoryCommands plugin) {
  44. if(plugin.getConfig().getConfigurationSection("items") != null && plugin.getConfig().getConfigurationSection("items").getKeys(false).size() > 0) {
  45. HashMap<String, String> returnedItemStats = new HashMap<String, String>();
  46. for(String itemInConfig : plugin.getConfig().getConfigurationSection("items").getKeys(false)) {
  47. //creating which commands need to be run for this item
  48. List<String> commandsPresent = new ArrayList<String>();
  49. List<String> commandsAbsent = new ArrayList<String>();
  50.  
  51. //Getting config values
  52. String data = plugin.getConfig().getString("items." + itemInConfig + ".data");
  53. List<String> commandsPresentConfig = plugin.getConfig().getStringList("items." + itemInConfig + ".present_commands");
  54. List<String> commandsAbsentConfig = plugin.getConfig().getStringList("items." + itemInConfig + ".absent_commands");
  55.  
  56. //parsing item to see if it is in the inventory
  57. ItemStack item = parseItem(data);
  58. boolean contains = contains(inv, item);
  59. boolean changed = false;
  60.  
  61. if(playerStatus.containsKey(p)) {
  62. returnedItemStats = playerStatus.get(p);
  63. if(returnedItemStats.containsKey(itemInConfig)) {
  64. String status = returnedItemStats.get(itemInConfig);
  65. if(status.equalsIgnoreCase("absent") && contains) {
  66. commandsPresent.addAll(commandsPresentConfig);
  67. returnedItemStats.put(itemInConfig, "contains");
  68. changed = true;
  69. } else if(status.equalsIgnoreCase("contains") && !contains) {
  70. commandsAbsent.addAll(commandsAbsentConfig);
  71. returnedItemStats.put(itemInConfig, "absent");
  72. changed = true;
  73. }
  74.  
  75. } else if(!returnedItemStats.containsKey(itemInConfig)) {
  76. if(contains) {
  77. commandsPresent.addAll(commandsPresentConfig);
  78. returnedItemStats.put(itemInConfig, "contains");
  79. changed = true;
  80. } else if(!contains) {
  81. commandsAbsent.addAll(commandsAbsentConfig);
  82. returnedItemStats.put(itemInConfig, "absent");
  83. changed = true;
  84. }
  85. }
  86. } else {
  87. if(contains) {
  88. commandsPresent.addAll(commandsPresentConfig);
  89. returnedItemStats.put(itemInConfig, "contains");
  90. changed = true;
  91. } else if(!contains) {
  92. commandsAbsent.addAll(commandsAbsentConfig);
  93. returnedItemStats.put(itemInConfig, "absent");
  94. changed = true;
  95. }
  96. }
  97.  
  98. if(changed) {
  99. for(String cmd : commandsPresent) {
  100. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replaceAll("%p", p.getName()));
  101. }
  102. for(String cmd : commandsAbsent) {
  103. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replaceAll("%p", p.getName()));
  104. }
  105. }
  106. }
  107. playerStatus.put(p, returnedItemStats);
  108. }
  109.  
  110. if(plugin.getConfig().getConfigurationSection("item_groups") != null && plugin.getConfig().getConfigurationSection("item_groups").getKeys(false).size() > 0) {
  111. //getting the item group names, and if they contain more keys inside, proceed onwards!
  112. HashMap<String, String> returnedItemStats = new HashMap<String, String>();
  113. for(String groupID : plugin.getConfig().getConfigurationSection("item_groups").getKeys(false)) {
  114. String slotLocation = "LOWEST";
  115. HashMap<ItemStack, String> toSearchFor = new HashMap<ItemStack, String>();
  116. //getting the items in each group
  117. if(plugin.getConfig().getConfigurationSection("item_groups." + groupID) != null && plugin.getConfig().getConfigurationSection("item_groups." + groupID).getKeys(false).size() > 0) {
  118. for(String itemID : plugin.getConfig().getConfigurationSection("item_groups." + groupID).getKeys(false)) {
  119. //if its slot specification
  120. if(itemID.equalsIgnoreCase("slot"))
  121. slotLocation = plugin.getConfig().getString("item_groups." + groupID + ".slot");
  122. else {
  123. //Getting config values
  124. String data = plugin.getConfig().getString("item_groups." + groupID + "." + itemID + ".data");
  125. toSearchFor.put(parseItem(data), itemID);
  126. }
  127. }
  128. }
  129. String successful = toSearchFor.get(contains(inv, toSearchFor.keySet(), slotLocation));
  130. boolean changed = false;
  131. boolean contains = false;
  132. if(successful != null)
  133. contains = true;
  134. //creating which commands need to be run for this item
  135. List<String> commandsPresent = new ArrayList<String>();
  136. List<String> commandsAbsent = new ArrayList<String>();
  137.  
  138. if(playerGroupStatus.containsKey(p) && playerGroupStatus.get(p).containsKey(groupID)) {
  139. returnedItemStats = playerGroupStatus.get(p);
  140. String lastSuccessfulItem = returnedItemStats.get(groupID);
  141. if(successful == null) {
  142. commandsAbsent.addAll(plugin.getConfig().getStringList("item_groups." + groupID + "." + lastSuccessfulItem + ".absent_commands"));
  143. returnedItemStats.remove(groupID);
  144. changed = true;
  145. }
  146. else if(!lastSuccessfulItem.equals(successful)) {
  147. commandsPresent.addAll(plugin.getConfig().getStringList("item_groups." + groupID + "." + successful + ".present_commands"));
  148. commandsAbsent.addAll(plugin.getConfig().getStringList("item_groups." + groupID + "." + lastSuccessfulItem + ".absent_commands"));
  149. returnedItemStats.put(groupID, successful);
  150. changed = true;
  151. }
  152. } else {
  153. if(contains) {
  154. commandsPresent.addAll(plugin.getConfig().getStringList("item_groups." + groupID + "." + successful + ".present_commands"));
  155. returnedItemStats.put(groupID, successful);
  156. changed = true;
  157. }
  158. }
  159.  
  160. if(changed) {
  161. for(String cmd : commandsPresent) {
  162. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replaceAll("%p", p.getName()));
  163. }
  164. for(String cmd : commandsAbsent) {
  165. Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd.replaceAll("%p", p.getName()));
  166. }
  167. }
  168. }
  169. if(returnedItemStats.isEmpty())
  170. playerGroupStatus.remove(p);
  171. else playerGroupStatus.put(p, returnedItemStats);
  172. }
  173. }
  174.  
  175. public static ItemStack contains(PlayerInventory inv, Set<ItemStack> toSearch, String slot) {
  176. try {
  177. int slotPosition = Integer.parseInt(slot);
  178. for(ItemStack requirement : toSearch) {
  179. ArrayList<ItemStack> potential = possibleMatches(inv, requirement);
  180. for(ItemStack potentialMatch : potential) {
  181. if(inv.getItem(slotPosition).equals(potentialMatch))
  182. return potentialMatch;
  183. }
  184. }
  185. } catch(Exception e) {
  186. ArrayList<ItemStack> allMatches = new ArrayList<ItemStack>();
  187. for(ItemStack requirement : toSearch) {
  188. allMatches.addAll(possibleMatches(inv, requirement));
  189. }
  190. int min = Integer.MAX_VALUE;
  191. int max = 0;
  192. boolean ignoreArmor = true;
  193. for(ItemStack foundItem : allMatches) {
  194. if(inv.first(foundItem) <= min && inv.first(foundItem) >= 0)
  195. min = inv.first(foundItem);
  196. if(inv.first(foundItem) >= max)
  197. max = inv.first(foundItem);
  198. }
  199. for(ItemStack foundItem : allMatches) {
  200. if(Arrays.asList(inv.getExtraContents()).contains(foundItem) && slot.equalsIgnoreCase("lowest"))
  201. return foundItem;
  202. if(Arrays.asList(inv.getArmorContents()).contains(foundItem) && slot.equalsIgnoreCase("highest")) {
  203. ignoreArmor = false;
  204. if(max > 3)
  205. max = Arrays.asList(inv.getArmorContents()).indexOf(foundItem);
  206. else if(max < Arrays.asList(inv.getArmorContents()).indexOf(foundItem))
  207. max = Arrays.asList(inv.getArmorContents()).indexOf(foundItem);
  208. }
  209. }
  210. if(slot.equalsIgnoreCase("lowest")) {
  211. if(min < 0)
  212. return null;
  213. return inv.getItem(min);
  214. }
  215. if(!ignoreArmor)
  216. return Arrays.asList(inv.getArmorContents()).get(max);
  217. if(ignoreArmor && slot.equalsIgnoreCase("highest")) {
  218. if(max < 0)
  219. return null;
  220. return inv.getItem(max);
  221. }
  222. }
  223. return null;
  224. }
  225.  
  226. public static boolean contains(PlayerInventory inv, ItemStack req) {
  227. if(possibleMatches(inv, req).size() > 0)
  228. return true;
  229. else return false;
  230. }
  231.  
  232. public static ArrayList<ItemStack> possibleMatches(PlayerInventory inv, ItemStack req) {
  233. ArrayList<ItemStack> matchedItems = new ArrayList<ItemStack>();
  234. for(ItemStack item : inv.getContents()) {
  235. if(item != null && item.getType().equals(req.getType()))
  236. matchedItems.add(item);
  237. }
  238. Iterator<ItemStack> iterator = matchedItems.iterator();
  239. while(iterator.hasNext()) {
  240. ItemStack item = iterator.next();
  241. if (req.getItemMeta().getDisplayName() != null)
  242. if (!req.getItemMeta().getDisplayName().equals(item.getItemMeta().getDisplayName()))
  243. iterator.remove();
  244.  
  245. if (req.getItemMeta().getLore() != null && !req.getItemMeta().getLore().isEmpty())
  246. if (!req.getItemMeta().getLore().equals(item.getItemMeta().getLore()))
  247. iterator.remove();
  248.  
  249. if (!req.getEnchantments().isEmpty())
  250. if(item.getEnchantments().isEmpty())
  251. iterator.remove();
  252. else if(!matches(req.getEnchantments(), item.getEnchantments()))
  253. iterator.remove();
  254. }
  255. return matchedItems;
  256. }
  257.  
  258. public static ArrayList<HashMap<Enchantment, Integer>> parseEnchantments(String enchantments) {
  259. ArrayList<HashMap<Enchantment, Integer>> enchList = new ArrayList<HashMap<Enchantment, Integer>>();
  260.  
  261. if (!enchantments.isEmpty()) {
  262. String[] split3 = enchantments.split("!");
  263. for (String loopEnchantment : split3) {
  264. HashMap<Enchantment, Integer> ench = parseFromString(loopEnchantment);
  265. if (ench != null)
  266. enchList.add(ench);
  267. }
  268. }
  269.  
  270. return enchList;
  271. }
  272.  
  273. public static boolean matches(Map<Enchantment, Integer> customEnchs, Map<Enchantment, Integer> toolEnchs) {
  274. int matchCount = 0;
  275. if(customEnchs.size() == 0 && toolEnchs.size() == 0)
  276. return true;
  277. for (Entry<Enchantment, Integer> customEnchEntry : customEnchs.entrySet()) {
  278. for (Entry<Enchantment, Integer> toolEnchEntry : toolEnchs.entrySet()) {
  279. if(customEnchEntry.getKey().equals(toolEnchEntry.getKey()))
  280. if(customEnchEntry.getValue().equals(toolEnchEntry.getValue()))
  281. matchCount++;
  282. }
  283. }
  284.  
  285. if (matchCount != customEnchs.size())
  286. return false;
  287.  
  288. return true;
  289. }
  290.  
  291. public static HashMap<Enchantment, Integer> parseFromString(String input) {
  292. HashMap<Enchantment, Integer> enchant = new HashMap<Enchantment, Integer>();
  293. String[] enchSplit = input.split("#");
  294. String enchString = enchSplit[0].trim().toLowerCase();
  295.  
  296. String enchLevel = "";
  297. if (enchSplit.length > 1)
  298. enchLevel = enchSplit[1];
  299. int enchLevelInt = 0;
  300.  
  301. try {
  302. if (!enchLevel.isEmpty() && enchLevel.matches("[0-9-~]*"))
  303. enchLevelInt = Integer.parseInt(enchLevel);
  304. } catch (NumberFormatException x) {
  305. return null;
  306. }
  307.  
  308. Enchantment ench = Enchantment.getByName(enchString);
  309.  
  310. if (ench == null || enchLevelInt == 0) {
  311. return null;
  312.  
  313. }
  314. enchant.put(ench, enchLevelInt);
  315. return enchant;
  316. }
  317.  
  318. public static ItemStack parseItem(String input) {
  319. String dataString = null;
  320. ArrayList<HashMap<Enchantment, Integer>> enchantments = new ArrayList<HashMap<Enchantment, Integer>>();
  321. String displayname = null;
  322. List<String> lore = new ArrayList<String>();
  323. Material material;
  324.  
  325. String[] firstSplit = input.split("[@:;~]", 2);
  326. if (firstSplit.length > 1) {
  327. // if extra fields are found, parse them - firstly separating out the type of "thing" this is
  328. material = Material.getMaterial(firstSplit[0]);
  329. String firstChar = input.substring(firstSplit[0].length(), firstSplit[0].length() + 1);
  330. if (firstChar.matches("[^~]")) {
  331. // only want to use a semi-colon rather than @ or : but preserve the ~
  332. firstChar = ";";
  333. } else if (firstChar.matches("~")) {
  334. displayname = "";
  335. }
  336. input = firstChar + firstSplit[1];
  337.  
  338. // check for initial data value and enchantment to support old format
  339. if (input.matches("([;])([^;!]+)!.*")) {
  340. String[] dataEnchSplit = input.split("!", 2);
  341. dataString = dataEnchSplit[0].substring(1);
  342. input = ";" + dataEnchSplit[1];
  343.  
  344. }
  345.  
  346. // then, loop through each ";<value>" or "~<value>" pair and parse accordingly
  347. Pattern p = Pattern.compile("([~;])([^~;]+)");
  348. Matcher m = p.matcher(input);
  349. while (m.find()) {
  350. String key = m.group(1);
  351. String value = m.group(2);
  352. value = value.replaceAll("slashCharPlaceholder", "/");
  353. if (key != null && value != null) {
  354. if (key.equals("~")) {
  355. displayname = ChatColor.translateAlternateColorCodes('&', value);
  356. } else if (displayname != null && !displayname.isEmpty()) {
  357. // displayname found, treat next as lore
  358. value = ChatColor.translateAlternateColorCodes('&', value);
  359. lore.add(value);
  360. } else {
  361. // first check for enchantment
  362. ArrayList<HashMap<Enchantment, Integer>> ench = parseEnchantments(value);
  363. if (ench == null || ench.isEmpty()) {
  364. // otherwise assume data
  365. dataString = value;
  366. } else {
  367. enchantments.addAll(ench);
  368. }
  369. }
  370. }
  371. }
  372. } else {
  373. material = Material.getMaterial(input);
  374. }
  375. if(material != null) {
  376. ItemStack item = new ItemStack(material);
  377. ItemMeta itemMeta = item.getItemMeta();
  378. if(displayname != null)
  379. itemMeta.setDisplayName(displayname);
  380. if(!lore.isEmpty())
  381. itemMeta.setLore(lore);
  382. for(HashMap<Enchantment, Integer> ench : enchantments) {
  383. for(Enchantment enchant : ench.keySet()) {
  384. itemMeta.addEnchant(enchant, ench.get(enchant), true);
  385. }
  386. }
  387. item.setItemMeta(itemMeta);
  388. return item;
  389. }
  390. return null;
  391. }
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement