├── .gitignore ├── README.md ├── pom.xml └── src └── com └── extendedclip └── papi └── expansion └── checkitem └── CheckItemExpansion.java /.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | /target/ 3 | /.settings/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CheckItem-Expansion 2 | 3 | **Placeholder:** 4 | - `%checkitem_,,<...>%` - Returns if user has the item 5 | - `%checkitem_amount_,,<...>%` - Returns amount of items the user has 6 | - `%checkitem_remove_,,<...>%` - Removes the items from the players inventory - Can be used with amount, it just has to be after. (Ex. `%checkitem_amount_remove_<...>%`) 7 | - `%checkitem_give_,,<...>%` - Gives the player an item. Returns yes if successful, returns amount of items NOT given if unsuccessful. 8 | - `%checkitem_getinfo:_,,<...>%` - Returns information about an item in a slot. Returns information in the same order listed on this wiki. List is seperated via ` &r` (Ex. `%checkitem_getinfo:0_mat:`) 9 | 10 | *Notes:* 11 | *- `mainhand` and `offhand` work in `getinfo:`* 12 | *- `give` and `remove` placeholders are **DISABLED** by default. See PlaceholderAPI `config.yml` file to enable.* 13 | 14 | **Modifiers:** 15 | - `namecontains:`~ 16 | - `namestartswith:`~ 17 | - `nameequals:`*~ 18 | - `mat:`*~ 19 | - `amt:`*~ 20 | - `data:`*~ 21 | - `custommodeldata:`*~ 22 | - `lorecontains:`~ 23 | - `loreequals:` (Separate lines with `|`)*~ 24 | - `matcontains:` 25 | - `enchantments:;` (`=lvl` is optional)*~ 26 | - `enchanted`~ 27 | - `potiontype:`*~ 28 | - `potionextended:`*` 29 | - `potionupgraded:`*~ 30 | - `strict` 31 | - `inhand<:hand>` (`inhand` will check both hands, you can add `:main` or `:off` to check specific hands) 32 | - `inslot:` [Valid Slots](https://i.imgur.com/3YCrfC8.png) 33 | - `nbtstrings:=;=`*~ 34 | - `nbtints:=;=`*~ 35 | 36 | *For nbt data you can use compounds by putting `..` inside your string Example (`%checkitem_nbtstrings:PublicBukkitValues..executableitems:ei-id=Free_Money%`)** 37 | 38 | 39 | Ex: `%checkitem_mat:STONE,amt:1,data:2,nameequals:&6Test%` 40 | 41 | To use commas in strings, escape them with `\` (Ex: `nameequals:This\, is a test`) 42 | 43 | 44 | To use semicolon in lists, escape them with `\` (Ex: `nbtints:ae_enchantment\;mirror=1`) 45 | 46 | **PAPI Placeholders work, you just need to put them in `{}` instead of `%%`** 47 | 48 | *Works with `give` placeholder. 49 | 50 | ~Works with the `getinfo` placeholder 51 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | Expansion-CheckItem 6 | Expansion-CheckItem 7 | 2.7.8 8 | Expansion-CheckItem 9 | 10 | ${project.artifactId} 11 | src 12 | 13 | 14 | maven-compiler-plugin 15 | 3.8.0 16 | 17 | 1.8 18 | 1.8 19 | 20 | 21 | 22 | org.apache.maven.plugins 23 | maven-shade-plugin 24 | 3.4.1 25 | 26 | 27 | shade 28 | package 29 | 30 | shade 31 | 32 | 33 | 34 | 35 | false 36 | 37 | 38 | de.tr7zw.changeme.nbtapi 39 | de.shaded.checkitem.nbtapi 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | spigot-repo 49 | https://hub.spigotmc.org/nexus/content/repositories/snapshots/ 50 | 51 | 52 | placeholderapi 53 | https://repo.extendedclip.com/content/repositories/placeholderapi/ 54 | 55 | 56 | codemc-repo 57 | https://repo.codemc.org/repository/maven-public/ 58 | default 59 | 60 | 61 | 62 | 63 | org.spigotmc 64 | spigot-api 65 | 1.20-R0.1-SNAPSHOT 66 | provided 67 | 68 | 69 | me.clip 70 | placeholderapi 71 | 2.11.2 72 | provided 73 | 74 | 75 | de.tr7zw 76 | item-nbt-api 77 | 2.13.2 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /src/com/extendedclip/papi/expansion/checkitem/CheckItemExpansion.java: -------------------------------------------------------------------------------- 1 | package com.extendedclip.papi.expansion.checkitem; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | import java.util.Map.Entry; 8 | import java.util.Set; 9 | import java.util.logging.Level; 10 | import java.util.stream.Collectors; 11 | import java.util.stream.Stream; 12 | 13 | import de.tr7zw.changeme.nbtapi.NBT; 14 | import de.tr7zw.changeme.nbtapi.iface.ReadableNBT; 15 | import org.bukkit.Bukkit; 16 | import org.bukkit.ChatColor; 17 | import org.bukkit.Material; 18 | import org.bukkit.NamespacedKey; 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.Damageable; 24 | import org.bukkit.inventory.meta.EnchantmentStorageMeta; 25 | import org.bukkit.inventory.meta.ItemMeta; 26 | import org.bukkit.inventory.meta.PotionMeta; 27 | import org.bukkit.potion.PotionData; 28 | import org.bukkit.potion.PotionType; 29 | 30 | import de.tr7zw.changeme.nbtapi.NBTItem; 31 | import de.tr7zw.changeme.nbtapi.NBTType; 32 | import me.clip.placeholderapi.PlaceholderAPI; 33 | import me.clip.placeholderapi.PlaceholderAPIPlugin; 34 | import me.clip.placeholderapi.expansion.Configurable; 35 | import me.clip.placeholderapi.expansion.PlaceholderExpansion; 36 | 37 | public class CheckItemExpansion extends PlaceholderExpansion implements Configurable { 38 | 39 | // Minecraft data version for components 40 | private static final boolean USE_COMPONENTS; 41 | 42 | static { 43 | boolean temp = false; 44 | String version = Bukkit.getServer().getBukkitVersion(); 45 | String[] versionParsing = version.split("\\.|-"); 46 | if (versionParsing.length == 5) { 47 | try { 48 | temp = Integer.parseInt(versionParsing[1]) > 20; 49 | } catch (NumberFormatException ignored) {} 50 | } else if (versionParsing.length > 5) { 51 | try { 52 | int major = Integer.parseInt(versionParsing[1]); 53 | int minor = Integer.parseInt(versionParsing[2]); 54 | temp = (major == 20 && minor >= 5) || major > 20; 55 | } catch (NumberFormatException ignored) {} 56 | } 57 | 58 | USE_COMPONENTS = temp; 59 | } 60 | 61 | public boolean canRegister() { 62 | return true; 63 | } 64 | 65 | public String getAuthor() { 66 | return "cj89898"; 67 | } 68 | 69 | public String getIdentifier() { 70 | return "checkitem"; 71 | } 72 | 73 | public String getVersion() { 74 | return "2.7.8"; 75 | } 76 | 77 | public class ItemWrapper { 78 | 79 | private boolean checkNameContains; 80 | private boolean checkNameStartsWith; 81 | private boolean checkNameEquals; 82 | private boolean checkLoreContains; 83 | private boolean checkLoreEquals; 84 | private boolean checkMaterialContains; 85 | private boolean checkDurability; 86 | private boolean checkCustomData; 87 | private boolean checkAmount; 88 | private boolean checkType; 89 | private boolean checkMainHand; 90 | private boolean checkOffHand; 91 | private boolean checkEnchantments; 92 | private boolean checkEnchanted; 93 | private boolean checkPotionType; 94 | private boolean checkPotionExtended; 95 | private boolean checkPotionUpgraded; 96 | private boolean checkNbtStrings; 97 | private boolean checkNbtInts; 98 | private boolean isStrict; 99 | private boolean hdbItem; 100 | private boolean remove; 101 | private String material; 102 | private short data; 103 | private int customData; 104 | private int amount; 105 | private String name; 106 | private String lore; 107 | private String materialString; 108 | private HashMap enchantments; 109 | private PotionType potionType; 110 | private boolean potionExtended; 111 | private boolean potionUpgraded; 112 | private int hdbId; 113 | private int slot; 114 | private Map nbtStrings; 115 | private Map nbtInts; 116 | 117 | public ItemWrapper(String material, short data, int amt) { 118 | this.material = material.toUpperCase(); 119 | this.data = data; 120 | this.amount = amt; 121 | slot = -1; 122 | } 123 | 124 | public ItemWrapper() { 125 | slot = -1; 126 | } 127 | 128 | @Override 129 | public String toString() { 130 | return "ItemWrapper [checkNameContains=" 131 | + checkNameContains 132 | + ", checkNameStartsWith=" 133 | + checkNameStartsWith 134 | + ", checkNameEquals=" 135 | + checkNameEquals 136 | + ", checkLoreContains=" 137 | + checkLoreContains 138 | + ", checkLoreEquals=" 139 | + checkLoreEquals 140 | + ", checkMaterialContains=" 141 | + checkMaterialContains 142 | + ", checkDurability=" 143 | + checkDurability 144 | + ", checkCustomData=" 145 | + checkCustomData 146 | + ", checkAmount=" 147 | + checkAmount 148 | + ", checkType=" 149 | + checkType 150 | + ", checkMainHand=" 151 | + checkMainHand 152 | + ", checkOffHand=" 153 | + checkOffHand 154 | + ", checkEnchantments=" 155 | + checkEnchantments 156 | + ", checkEnchanted=" 157 | + checkEnchanted 158 | + ", checkNbtStrings=" 159 | + checkNbtStrings 160 | + ", checkNbtInts=" 161 | + checkNbtInts 162 | + ", isStrict=" 163 | + isStrict 164 | + ", hdbItem=" 165 | + hdbItem 166 | + ", remove=" 167 | + remove 168 | + ", material=" 169 | + material 170 | + ", data=" 171 | + data 172 | + ", customData=" 173 | + customData 174 | + ", amount=" 175 | + amount 176 | + ", name=" 177 | + name 178 | + ", lore=" 179 | + lore 180 | + ", materialString=" 181 | + materialString 182 | + ", enchantments=" 183 | + enchantments 184 | + ", potionType=" 185 | + potionType 186 | + ", checkPotionExtended=" 187 | + checkPotionExtended 188 | + ", potionExtended=" 189 | + potionExtended 190 | + ", checkPotionUpgraded=" 191 | + checkPotionUpgraded 192 | + ", potionUpgraded=" 193 | + potionUpgraded 194 | + ", hdbId=" 195 | + hdbId 196 | + ", slot=" 197 | + slot 198 | + ", nbtStrings=" 199 | + nbtStrings 200 | + ", nbtInts=" 201 | + nbtInts 202 | + "]"; 203 | } 204 | 205 | public String getType() { 206 | return this.material; 207 | } 208 | 209 | protected void setType(String material) { 210 | this.material = material.toUpperCase(); 211 | } 212 | 213 | public short getDurability() { 214 | return this.data; 215 | } 216 | 217 | protected void setDurability(short durability) { 218 | this.data = durability; 219 | } 220 | 221 | public int getCustomData() { 222 | return this.customData; 223 | } 224 | 225 | protected void setCustomData(int customData) { 226 | this.customData = customData; 227 | } 228 | 229 | public int getAmount() { 230 | return this.amount; 231 | } 232 | 233 | protected void setAmount(int amount) { 234 | this.amount = amount; 235 | } 236 | 237 | public String getName() { 238 | return this.name; 239 | } 240 | 241 | protected void setName(String name) { 242 | this.name = name; 243 | } 244 | 245 | public String getLore() { 246 | return this.lore; 247 | } 248 | 249 | protected void setLore(String lore) { 250 | this.lore = lore; 251 | } 252 | 253 | public String getMaterialString() { 254 | return this.materialString; 255 | } 256 | 257 | protected void setMaterialString(String materialString) { 258 | this.materialString = materialString; 259 | } 260 | 261 | protected void setEnchantments(HashMap enchantments) { 262 | this.enchantments = enchantments; 263 | } 264 | 265 | public HashMap getEnchantments() { 266 | return this.enchantments; 267 | } 268 | 269 | protected void setPotionType(PotionType potionType) { 270 | this.potionType = potionType; 271 | } 272 | 273 | public PotionType getPotionType() { 274 | return this.potionType; 275 | } 276 | 277 | protected void setPotionExtended(boolean potionExtended) { 278 | this.potionExtended = potionExtended; 279 | } 280 | 281 | public boolean getPotionExtended() { 282 | return this.potionExtended; 283 | } 284 | 285 | protected void setPotionUpgraded(boolean potionUpgraded) { 286 | this.potionUpgraded = potionUpgraded; 287 | } 288 | 289 | public boolean getPotionUpgraded() { 290 | return this.potionUpgraded; 291 | } 292 | 293 | public void setNbtStrings(Map nbtStrings) { 294 | this.nbtStrings = nbtStrings; 295 | } 296 | 297 | public Map getNbtStrings() { 298 | return this.nbtStrings; 299 | } 300 | 301 | public void setNbtInts(Map nbtInts) { 302 | this.nbtInts = nbtInts; 303 | } 304 | 305 | public Map getNbtInts() { 306 | return this.nbtInts; 307 | } 308 | 309 | protected void setHdbId(int id) { 310 | this.hdbId = id; 311 | } 312 | 313 | public int getHdbId() { 314 | return this.hdbId; 315 | } 316 | 317 | protected void setCheckDurability(boolean checkDurability) { 318 | this.checkDurability = checkDurability; 319 | } 320 | 321 | public boolean shouldCheckDurability() { 322 | return this.checkDurability; 323 | } 324 | 325 | protected void setCheckCustomData(boolean checkCustomData) { 326 | this.checkCustomData = checkCustomData; 327 | } 328 | 329 | public boolean shouldCheckCustomData() { 330 | return this.checkCustomData; 331 | } 332 | 333 | protected void setCheckAmount(boolean checkAmount) { 334 | this.checkAmount = checkAmount; 335 | } 336 | 337 | public boolean shouldCheckAmount() { 338 | return this.checkAmount; 339 | } 340 | 341 | protected void setCheckNameContains(boolean checkNameContains) { 342 | this.checkNameContains = checkNameContains; 343 | } 344 | 345 | public boolean shouldCheckNameContains() { 346 | return this.checkNameContains; 347 | } 348 | 349 | protected void setCheckNameStartsWith(boolean checkNameStartsWith) { 350 | this.checkNameStartsWith = checkNameStartsWith; 351 | } 352 | 353 | public boolean shouldCheckNameStartsWith() { 354 | return this.checkNameStartsWith; 355 | } 356 | 357 | protected void setCheckNameEquals(boolean checkNameEquals) { 358 | this.checkNameEquals = checkNameEquals; 359 | } 360 | 361 | public boolean shouldCheckNameEquals() { 362 | return this.checkNameEquals; 363 | } 364 | 365 | protected void setCheckLoreContains(boolean checkLoreContains) { 366 | this.checkLoreContains = checkLoreContains; 367 | } 368 | 369 | public boolean shouldCheckLoreContains() { 370 | return this.checkLoreContains; 371 | } 372 | 373 | protected void setCheckLoreEquals(boolean checkLoreEquals) { 374 | this.checkLoreEquals = checkLoreEquals; 375 | } 376 | 377 | public boolean shouldCheckLoreEquals() { 378 | return this.checkLoreEquals; 379 | } 380 | 381 | protected void setCheckMaterialContains(boolean checkMaterialContains) { 382 | this.checkMaterialContains = checkMaterialContains; 383 | } 384 | 385 | public boolean shouldCheckMaterialContains() { 386 | return this.checkMaterialContains; 387 | } 388 | 389 | protected void setCheckType(boolean checkType) { 390 | this.checkType = checkType; 391 | } 392 | 393 | public boolean shouldCheckType() { 394 | return this.checkType; 395 | } 396 | 397 | protected void setCheckMainHand(boolean checkMainHand) { 398 | this.checkMainHand = checkMainHand; 399 | } 400 | 401 | public boolean shouldCheckMainHand() { 402 | return this.checkMainHand; 403 | } 404 | 405 | protected void setCheckOffHand(boolean checkOffHand) { 406 | this.checkOffHand = checkOffHand; 407 | } 408 | 409 | public boolean shouldCheckOffHand() { 410 | return this.checkOffHand; 411 | } 412 | 413 | protected void setIsStrict(boolean isStrict) { 414 | this.isStrict = isStrict; 415 | } 416 | 417 | public boolean isStrict() { 418 | return this.isStrict; 419 | } 420 | 421 | protected void setCheckEnchantments(boolean checkEnchantments) { 422 | this.checkEnchantments = checkEnchantments; 423 | } 424 | 425 | public boolean shouldCheckEnchantments() { 426 | return this.checkEnchantments; 427 | } 428 | 429 | protected void setCheckEnchanted(boolean checkEnchanted) { 430 | this.checkEnchanted = checkEnchanted; 431 | } 432 | 433 | public boolean shouldCheckEnchanted() { 434 | return this.checkEnchanted; 435 | } 436 | 437 | protected void setCheckPotionType(boolean checkPotionType) { 438 | this.checkPotionType = checkPotionType; 439 | } 440 | 441 | public boolean shouldCheckPotionType() { 442 | return this.checkPotionType; 443 | } 444 | 445 | protected void setCheckPotionExtended(boolean checkPotionExtended) { 446 | this.checkPotionExtended = checkPotionExtended; 447 | } 448 | 449 | public boolean shouldCheckPotionExtended() { 450 | return this.checkPotionExtended; 451 | } 452 | 453 | protected void setCheckPotionUpgraded(boolean checkPotionUpgraded) { 454 | this.checkPotionUpgraded = checkPotionUpgraded; 455 | } 456 | 457 | public boolean shouldCheckPotionUpgraded() { 458 | return this.checkPotionUpgraded; 459 | } 460 | 461 | protected void setCheckNbtStrings(boolean checkNbtStrings) { 462 | this.checkNbtStrings = checkNbtStrings; 463 | } 464 | 465 | public boolean shouldCheckNbtStrings() { 466 | return this.checkNbtStrings; 467 | } 468 | 469 | protected void setCheckNbtInts(boolean checkNbtInts) { 470 | this.checkNbtInts = checkNbtInts; 471 | } 472 | 473 | public boolean shouldCheckNbtInts() { 474 | return this.checkNbtInts; 475 | } 476 | 477 | protected void setRemove(boolean remove) { 478 | this.remove = remove; 479 | } 480 | 481 | public boolean shouldRemove() { 482 | return remove; 483 | } 484 | 485 | protected void setSlot(int slot) { 486 | this.slot = slot; 487 | } 488 | 489 | public int getSlot() { 490 | return slot; 491 | } 492 | 493 | } 494 | 495 | @SuppressWarnings("deprecation") 496 | public String onPlaceholderRequest(Player p, String args) { 497 | if (p == null) 498 | return "%" + getIdentifier() + "_" + args + "%"; 499 | ItemWrapper wrapper = new ItemWrapper(); 500 | ItemStack[] itemsToCheck; 501 | boolean amount = false; 502 | args = PlaceholderAPI.setBracketPlaceholders(p, args); 503 | if (args.startsWith("give_")) { 504 | if (!((boolean) get("give_enabled", true))) { 505 | return "Give placeholders have been disabled. Check PlaceholderAPI Config."; 506 | } 507 | args = args.replace("give_", ""); 508 | wrapper = getWrapper(wrapper, ChatColor.translateAlternateColorCodes('&', args), p); 509 | if (wrapper == null) { 510 | return null; 511 | } 512 | return giveItem(wrapper, p); 513 | } 514 | if (args.split("_")[0].startsWith("getinfo:")) { 515 | args = args.replace("getinfo:", ""); 516 | String[] argsSplit = args.split("_", 2); 517 | int slot = 0; 518 | switch (argsSplit[0]) { 519 | case "mainhand": 520 | slot = p.getInventory().getHeldItemSlot(); 521 | break; 522 | case "offhand": 523 | slot = 40; 524 | break; 525 | default: 526 | try { 527 | slot = Integer.parseInt(argsSplit[0]); 528 | } catch (NumberFormatException e) { 529 | return "Invalid number for slot"; 530 | } 531 | break; 532 | } 533 | boolean multiMod = false; 534 | if (argsSplit.length == 2) { 535 | wrapper = getWrapper(wrapper, ChatColor.translateAlternateColorCodes('&', argsSplit[1]), p); 536 | multiMod = argsSplit[1].split(",").length > 1; 537 | } else { 538 | wrapper.setCheckNameContains(true); 539 | wrapper.setCheckType(true); 540 | wrapper.setCheckAmount(true); 541 | wrapper.setCheckDurability(true); 542 | try { 543 | Class.forName("org.bukkit.inventory.meta.ItemMeta").getMethod("hasCustomModelData", null); 544 | wrapper.setCheckCustomData(true); 545 | } catch (Exception e) { 546 | } 547 | wrapper.setCheckLoreContains(true); 548 | wrapper.setCheckEnchantments(true); 549 | wrapper.setCheckEnchanted(true); 550 | wrapper.setCheckPotionType(true); 551 | wrapper.setCheckPotionExtended(true); 552 | wrapper.setCheckPotionUpgraded(true); 553 | wrapper.setCheckNbtStrings(true); 554 | wrapper = getWrapper(wrapper, "", p); 555 | multiMod = true; 556 | } 557 | ItemStack item = p.getInventory().getItem(slot); 558 | if (item == null) { 559 | return ""; 560 | } 561 | String data = ""; 562 | if ((wrapper.shouldCheckNameContains() || wrapper.shouldCheckNameEquals() || wrapper.shouldCheckNameStartsWith()) 563 | && (item.hasItemMeta() && item.getItemMeta().hasDisplayName())) { 564 | data = multiMod ? data += "name:" : ""; 565 | data += item.getItemMeta().getDisplayName() + " &r"; 566 | } 567 | if (wrapper.shouldCheckType()) { 568 | data = multiMod ? data += "mat:" : ""; 569 | data += item.getType() + " &r"; 570 | } 571 | if (wrapper.shouldCheckAmount()) { 572 | data = multiMod ? data += "amt:" : ""; 573 | data += item.getAmount() + " &r"; 574 | } 575 | if (wrapper.shouldCheckDurability()) { 576 | data = multiMod ? data += "data:" : ""; 577 | data += item.getDurability() + " &r"; 578 | } 579 | if (wrapper.shouldCheckCustomData() && item.hasItemMeta() && item.getItemMeta().hasCustomModelData()) { 580 | data = multiMod ? data += "custommodeldata:" : ""; 581 | data += item.getItemMeta().getCustomModelData() + " &r"; 582 | } 583 | if ((wrapper.shouldCheckLoreContains() || wrapper.shouldCheckLoreEquals()) 584 | && (item.hasItemMeta() && item.getItemMeta().hasLore())) { 585 | data = multiMod ? data += "lore:" : ""; 586 | int line = -1; 587 | try { 588 | line = Integer.parseInt(wrapper.getLore()); 589 | } catch (Exception e) { 590 | } 591 | if (line != -1) { 592 | data += item.getItemMeta().getLore().get(line); 593 | } else { 594 | for (String s : item.getItemMeta().getLore()) { 595 | data += s + "|"; 596 | } 597 | data = data.substring(0, data.length() - 1); 598 | } 599 | data = data + " &r"; 600 | } 601 | if (wrapper.shouldCheckEnchantments()) { 602 | if (!multiMod && wrapper.getEnchantments().size() == 1) { 603 | data = "0"; 604 | if ((item.hasItemMeta() && item.getItemMeta().hasEnchants()) 605 | || (item.hasItemMeta() 606 | && (item.getItemMeta() instanceof EnchantmentStorageMeta) 607 | && ((EnchantmentStorageMeta) item.getItemMeta()).hasStoredEnchants())) { 608 | Map toCheckEnchants; 609 | if (item.getItemMeta() instanceof EnchantmentStorageMeta) 610 | toCheckEnchants = ((EnchantmentStorageMeta) item.getItemMeta()).getStoredEnchants(); 611 | else 612 | toCheckEnchants = item.getItemMeta().getEnchants(); 613 | for (Entry e : wrapper.getEnchantments().entrySet()) { 614 | if (toCheckEnchants.containsKey(e.getKey())) { 615 | data = "" + toCheckEnchants.get(e.getKey()); 616 | break; 617 | } 618 | } 619 | } 620 | } else if ((item.hasItemMeta() && item.getItemMeta().hasEnchants()) 621 | || (item.hasItemMeta() 622 | && item.getItemMeta() instanceof EnchantmentStorageMeta 623 | && ((EnchantmentStorageMeta) item.getItemMeta()).hasStoredEnchants())) { 624 | data = multiMod ? data += "enchantments:" : ""; 625 | Set> enchantSet; 626 | if (item.getItemMeta() instanceof EnchantmentStorageMeta) 627 | enchantSet = ((EnchantmentStorageMeta) item.getItemMeta()).getStoredEnchants().entrySet(); 628 | else 629 | enchantSet = item.getItemMeta().getEnchants().entrySet(); 630 | for (Entry entry : enchantSet) { 631 | data += entry.getKey().getKey() + ":" + entry.getValue() + "|"; 632 | } 633 | data = data.substring(0, data.length() - 1) + " &r"; 634 | } 635 | } 636 | if (wrapper.shouldCheckEnchanted() && item.hasItemMeta()) { 637 | data = multiMod ? data += "enchanted:" : ""; 638 | data += (item.getItemMeta().hasEnchants() 639 | || (item.getItemMeta() instanceof EnchantmentStorageMeta 640 | && ((EnchantmentStorageMeta) item.getItemMeta()).hasStoredEnchants())) 641 | + " &r"; 642 | } 643 | if (wrapper.shouldCheckPotionType() && item.hasItemMeta() && item.getItemMeta() instanceof PotionMeta) { 644 | data = multiMod ? data += "potiontype:" : ""; 645 | PotionData potionData = ((PotionMeta) item.getItemMeta()).getBasePotionData(); 646 | data += potionData.getType() + " &r"; 647 | } 648 | if (wrapper.shouldCheckPotionExtended() && item.hasItemMeta() && item.getItemMeta() instanceof PotionMeta) { 649 | data = multiMod ? data += "potionextended:" : ""; 650 | PotionData potionData = ((PotionMeta) item.getItemMeta()).getBasePotionData(); 651 | data += potionData.isExtended() + " &r"; 652 | } 653 | if (wrapper.shouldCheckPotionUpgraded() && item.hasItemMeta() && item.getItemMeta() instanceof PotionMeta) { 654 | data = multiMod ? data += "potionupgraded:" : ""; 655 | PotionData potionData = ((PotionMeta) item.getItemMeta()).getBasePotionData(); 656 | data += potionData.isUpgraded() + " &r"; 657 | } 658 | if (wrapper.shouldCheckNbtStrings() || wrapper.shouldCheckNbtInts()) { 659 | NBTItem nbtItem = new NBTItem(item); 660 | int size = 0; 661 | if (wrapper.shouldCheckNbtStrings() && wrapper.getNbtStrings() != null) 662 | size += wrapper.getNbtStrings().size(); 663 | if (wrapper.shouldCheckNbtInts() && wrapper.getNbtInts() != null) 664 | size += wrapper.getNbtInts().size(); 665 | if (!multiMod && size == 1) { 666 | if (wrapper.shouldCheckNbtStrings()) { 667 | for (Entry entry : wrapper.getNbtStrings().entrySet()) { 668 | if (entry.getKey().contains("..")) { 669 | String[] entrySplit = entry.getKey().split("\\.\\."); 670 | data = nbtItem.getCompound(entrySplit[0]).getString(entrySplit[1]); 671 | } else { 672 | data = nbtItem.getString(entry.getKey()); 673 | } 674 | } 675 | } else { 676 | for (Entry entry : wrapper.getNbtInts().entrySet()) { 677 | if (entry.getKey().contains("..")) { 678 | String[] entrySplit = entry.getKey().split("\\.\\."); 679 | data = "" + nbtItem.getCompound(entrySplit[0]).getInteger(entrySplit[1]); 680 | } else { 681 | data = "" + nbtItem.getInteger(entry.getKey()); 682 | } 683 | } 684 | } 685 | 686 | } else if (!nbtItem.getKeys().isEmpty()) { 687 | data += "nbt:"; 688 | for (String entry : nbtItem.getKeys()) { 689 | if (nbtItem.getType(entry).equals(NBTType.NBTTagString)) 690 | data += "STRING:" + entry + ":" + nbtItem.getString(entry) + "|"; 691 | else if (nbtItem.getType(entry).equals(NBTType.NBTTagInt)) 692 | data += "INTEGER:" + entry + ":" + nbtItem.getInteger(entry) + "|"; 693 | else if (nbtItem.getType(entry).equals(NBTType.NBTTagCompound)) { 694 | if (!entry.equalsIgnoreCase("display")) 695 | data += "NBTTagCompound:" + entry + ":" + nbtItem.getCompound(entry) + "|"; 696 | } 697 | } 698 | data = data.endsWith("|") ? data.substring(0, data.length() - 1) + " &r" : data + " &r"; 699 | } 700 | } 701 | return data.endsWith(" &r") ? data.substring(0, data.length() - 3) : data; 702 | } 703 | if (args.startsWith("amount_")) 704 | 705 | { 706 | args = args.replace("amount_", ""); 707 | amount = true; 708 | } 709 | if (args.startsWith("remove_")) { 710 | if (!((boolean) get("remove_enabled", true))) { 711 | return "Remove placeholders have been disabled. Check PlaceholderAPI Config."; 712 | } 713 | wrapper.setRemove(true); 714 | args = args.replace("remove_", ""); 715 | } 716 | wrapper = 717 | 718 | getWrapper(wrapper, ChatColor.translateAlternateColorCodes('&', args), p); 719 | 720 | if (wrapper == null) { 721 | return null; 722 | } 723 | if (wrapper.shouldCheckMainHand() || wrapper.shouldCheckOffHand()) { 724 | try { 725 | Class.forName("org.bukkit.inventory.PlayerInventory").getMethod("getItemInMainHand", null); 726 | if (wrapper.shouldCheckMainHand() && wrapper.shouldCheckOffHand()) { 727 | itemsToCheck = new ItemStack[2]; 728 | itemsToCheck[0] = (p.getInventory().getItem(p.getInventory().getHeldItemSlot())); 729 | itemsToCheck[1] = (p.getInventory().getItem(40)); 730 | } else if (wrapper.shouldCheckMainHand()) { 731 | itemsToCheck = new ItemStack[1]; 732 | itemsToCheck[0] = (p.getInventory().getItem(p.getInventory().getHeldItemSlot())); 733 | } else { 734 | itemsToCheck = new ItemStack[1]; 735 | itemsToCheck[0] = (p.getInventory().getItem(40)); 736 | } 737 | 738 | } catch (NoSuchMethodException e) { 739 | itemsToCheck = new ItemStack[1]; 740 | itemsToCheck[0] = p.getInventory().getItem(p.getInventory().getHeldItemSlot()); 741 | } catch (Exception e) { 742 | e.printStackTrace(); 743 | return "error"; 744 | } 745 | } else if (wrapper.getSlot() != -1) { 746 | itemsToCheck = new ItemStack[1]; 747 | itemsToCheck[0] = p.getInventory().getItem(wrapper.getSlot()); 748 | } else { 749 | if (wrapper.shouldCheckType() && wrapper.getType().equals("AIR")) { 750 | if (amount) { 751 | int slots = 0; 752 | PlayerInventory inv = p.getInventory(); 753 | for (ItemStack slot : inv.getContents()) { 754 | if (slot == null) 755 | slots++; 756 | } 757 | if (!Bukkit.getBukkitVersion().contains("1.7") && !Bukkit.getBukkitVersion().contains("1.8")) { 758 | if (inv.getItemInOffHand() == null || inv.getItemInOffHand().getType() == Material.AIR) 759 | slots--; 760 | if (inv.getHelmet() == null) 761 | slots--; 762 | if (inv.getChestplate() == null) 763 | slots--; 764 | if (inv.getLeggings() == null) 765 | slots--; 766 | if (inv.getBoots() == null) 767 | slots--; 768 | } 769 | return slots + ""; 770 | } 771 | return p.getInventory().firstEmpty() == -1 ? PlaceholderAPIPlugin.booleanFalse() 772 | : PlaceholderAPIPlugin.booleanTrue(); 773 | } 774 | itemsToCheck = p.getInventory().getContents(); 775 | } 776 | 777 | if (amount) { 778 | return String.valueOf(getItemAmount(wrapper, p, itemsToCheck)); 779 | } else { 780 | return checkItem(wrapper, p, itemsToCheck) ? PlaceholderAPIPlugin.booleanTrue() 781 | : PlaceholderAPIPlugin.booleanFalse(); 782 | } 783 | } 784 | 785 | @SuppressWarnings("deprecation") 786 | private String giveItem(ItemWrapper wrapper, Player p) { 787 | ItemStack item = new ItemStack(Material.getMaterial(wrapper.getType())); 788 | ItemMeta meta = item.getItemMeta(); 789 | if (wrapper.shouldCheckDurability()) { 790 | try { 791 | Class.forName("org.bukkit.inventory.meta.Damageable"); 792 | if (meta instanceof Damageable) { 793 | Damageable dmg = (Damageable) meta; 794 | dmg.setDamage(wrapper.getDurability()); 795 | } 796 | } catch (ClassNotFoundException e) { 797 | item.setDurability(wrapper.getDurability()); 798 | } catch (Exception e) { 799 | e.printStackTrace(); 800 | return "error"; 801 | } 802 | } 803 | if (wrapper.shouldCheckCustomData()) { 804 | meta.setCustomModelData(wrapper.getCustomData()); 805 | } 806 | if (wrapper.shouldCheckNameEquals()) 807 | meta.setDisplayName(wrapper.getName()); 808 | if (wrapper.shouldCheckLoreEquals()) { 809 | List lore = Stream.of(wrapper.getLore().split("\\|", -1)).collect(Collectors.toList()); 810 | meta.setLore(lore); 811 | } 812 | if (wrapper.shouldCheckEnchantments()) { 813 | for (Entry e : wrapper.getEnchantments().entrySet()) { 814 | meta.addEnchant(e.getKey(), (e.getValue() == -1 ? 1 : e.getValue()), true); 815 | } 816 | } 817 | if (wrapper.shouldCheckPotionType()) { 818 | if (meta instanceof PotionMeta) { 819 | PotionMeta potionMeta = (PotionMeta) meta; 820 | potionMeta.setBasePotionData( 821 | new PotionData(wrapper.getPotionType(), wrapper.getPotionExtended(), wrapper.getPotionUpgraded())); 822 | } 823 | } 824 | item.setItemMeta(meta); 825 | 826 | if (wrapper.shouldCheckNbtStrings() || wrapper.shouldCheckNbtInts()) { 827 | NBTItem nbtItem = new NBTItem(item); 828 | if (wrapper.shouldCheckNbtStrings()) { 829 | for (Entry entry : wrapper.getNbtStrings().entrySet()) { 830 | if (entry.getKey().contains("..")) { 831 | String[] entrySplit = entry.getKey().split("\\.\\."); 832 | nbtItem.addCompound(entrySplit[0]).setString(entrySplit[1], entry.getValue()); 833 | } else { 834 | nbtItem.setString(entry.getKey(), entry.getValue()); 835 | } 836 | } 837 | } 838 | 839 | if (wrapper.shouldCheckNbtInts()) { 840 | for (Entry entry : wrapper.getNbtInts().entrySet()) { 841 | 842 | if (entry.getKey().contains("..")) { 843 | String[] entrySplit = entry.getKey().split("\\.\\."); 844 | nbtItem.addCompound(entrySplit[0]).setInteger(entrySplit[1], entry.getValue()); 845 | } else { 846 | nbtItem.setInteger(entry.getKey(), entry.getValue()); 847 | } 848 | } 849 | } 850 | item = nbtItem.getItem(); 851 | } 852 | 853 | if (wrapper.shouldCheckAmount()) { 854 | int remaining = wrapper.getAmount(); 855 | int maxStack = item.getMaxStackSize(); 856 | while (remaining > 0) { 857 | HashMap returned; 858 | if (remaining > maxStack) { 859 | item.setAmount(maxStack); 860 | returned = p.getInventory().addItem(item); 861 | remaining -= maxStack; 862 | } else { 863 | item.setAmount(remaining); 864 | returned = p.getInventory().addItem(item); 865 | remaining = 0; 866 | } 867 | if (!returned.isEmpty()) { 868 | for (Entry e : returned.entrySet()) { 869 | return (e.getValue().getAmount() + remaining) + ""; 870 | } 871 | } 872 | } 873 | } else { 874 | p.getInventory().addItem(item); 875 | } 876 | return "yes"; 877 | } 878 | 879 | private boolean checkItem(ItemWrapper wrapper, Player p, ItemStack... items) { 880 | int total = getItemAmount(wrapper, p, items); 881 | if (wrapper.shouldCheckAmount()) { 882 | if (wrapper.isStrict()) { 883 | return total == wrapper.getAmount(); 884 | } 885 | return total >= wrapper.getAmount(); 886 | } 887 | 888 | return total >= 1; 889 | } 890 | 891 | @SuppressWarnings("deprecation") 892 | private int getItemAmount(ItemWrapper wrapper, Player p, ItemStack... items) { 893 | int total = 0; 894 | List matched = new ArrayList(); 895 | itemsLoop: for (ItemStack toCheck : items) { 896 | if (toCheck == null && wrapper.shouldCheckType() && wrapper.getType().equals("AIR")) { 897 | return Integer.MAX_VALUE; 898 | } 899 | if (toCheck != null && toCheck.getType() != Material.AIR) { 900 | if (wrapper.shouldCheckType() && !(wrapper.getType().equals(toCheck.getType().name()))) { 901 | continue; 902 | } 903 | if (wrapper.shouldCheckDurability() && !(wrapper.getDurability() == toCheck.getDurability())) { 904 | continue; 905 | } 906 | ItemMeta toCheckMeta = toCheck.getItemMeta(); 907 | if (wrapper.shouldCheckCustomData()) { 908 | try { 909 | if (!toCheckMeta.hasCustomModelData()) 910 | continue; 911 | if (wrapper.getCustomData() != toCheckMeta.getCustomModelData()) { 912 | continue; 913 | } 914 | } catch (NoSuchMethodError e) { 915 | PlaceholderAPIPlugin.getInstance().getLogger().log(Level.WARNING, 916 | "[CheckItem Expansion] CustomModelData doesn't exist before 1.14!"); 917 | } 918 | } 919 | if (wrapper.shouldCheckLoreContains()) { 920 | if (!toCheckMeta.hasLore()) 921 | continue; 922 | boolean loreContains = false; 923 | for (String line : toCheckMeta.getLore()) { 924 | if (line.contains(wrapper.getLore())) { 925 | loreContains = true; 926 | break; 927 | } 928 | } 929 | if (!loreContains) { 930 | continue; 931 | } 932 | } 933 | if (wrapper.shouldCheckLoreEquals()) { 934 | if (!toCheckMeta.hasLore()) 935 | continue; 936 | String lore = String.join("|", toCheckMeta.getLore()); 937 | if (!wrapper.getLore().equals(lore)) 938 | continue; 939 | } 940 | if (wrapper.shouldCheckNameContains()) { 941 | if (!(toCheckMeta.hasDisplayName() && toCheckMeta.getDisplayName().contains(wrapper.getName()))) { 942 | continue; 943 | } 944 | } else if (wrapper.shouldCheckNameStartsWith()) { 945 | if (!(toCheckMeta.hasDisplayName() && toCheckMeta.getDisplayName().startsWith(wrapper.getName()))) { 946 | continue; 947 | } 948 | } else if (wrapper.shouldCheckNameEquals()) { 949 | if (!(toCheckMeta.hasDisplayName() && toCheckMeta.getDisplayName().equals(wrapper.getName()))) { 950 | continue; 951 | } 952 | } 953 | if (wrapper.shouldCheckMaterialContains()) { 954 | if (!toCheck.getType().name().contains(wrapper.getMaterialString())) { 955 | continue; 956 | } 957 | } 958 | 959 | if (wrapper.shouldCheckEnchantments()) { 960 | if (toCheckMeta.getEnchants().isEmpty() 961 | && (toCheckMeta instanceof EnchantmentStorageMeta 962 | && ((EnchantmentStorageMeta) toCheckMeta).getStoredEnchants().isEmpty())) 963 | continue; 964 | Map toCheckEnchants; 965 | if (toCheckMeta instanceof EnchantmentStorageMeta) 966 | toCheckEnchants = ((EnchantmentStorageMeta) toCheckMeta).getStoredEnchants(); 967 | else 968 | toCheckEnchants = toCheckMeta.getEnchants(); 969 | for (Entry e : wrapper.getEnchantments().entrySet()) { 970 | if (!toCheckEnchants.containsKey(e.getKey())) { 971 | continue itemsLoop; 972 | } 973 | if (e.getValue() != -1 && !(toCheckEnchants.get(e.getKey()) == e.getValue())) { 974 | continue itemsLoop; 975 | } 976 | } 977 | } 978 | 979 | if (wrapper.shouldCheckPotionType() 980 | || wrapper.shouldCheckPotionExtended() 981 | || wrapper.shouldCheckPotionUpgraded()) { 982 | if (!(toCheckMeta instanceof PotionMeta)) 983 | continue; 984 | PotionData potionData = ((PotionMeta) toCheckMeta).getBasePotionData(); 985 | if (wrapper.shouldCheckPotionType() 986 | && (potionData.getType() == null || potionData.getType() != wrapper.getPotionType())) 987 | continue; 988 | if (wrapper.shouldCheckPotionExtended() && potionData.isExtended() != wrapper.getPotionExtended()) 989 | continue; 990 | if (wrapper.shouldCheckPotionUpgraded() && potionData.isUpgraded() != wrapper.getPotionUpgraded()) 991 | continue; 992 | } 993 | 994 | if (wrapper.shouldCheckEnchanted() && toCheckMeta.getEnchants().isEmpty()) { 995 | if (toCheckMeta instanceof EnchantmentStorageMeta) { 996 | if (((EnchantmentStorageMeta) toCheckMeta).getStoredEnchants().isEmpty()) { 997 | continue; 998 | } 999 | } else { 1000 | continue; 1001 | } 1002 | } 1003 | 1004 | if (wrapper.shouldCheckNbtStrings() || wrapper.shouldCheckNbtInts()) { 1005 | ReadableNBT nbtItem; 1006 | if (USE_COMPONENTS) { 1007 | nbtItem = NBT.modifyComponents(toCheck, nbt -> {return nbt.getCompound("minecraft:custom_data");}); 1008 | if (nbtItem == null) continue; 1009 | } else { 1010 | nbtItem = new NBTItem(toCheck); 1011 | } 1012 | 1013 | if (wrapper.shouldCheckNbtStrings()) { 1014 | for (Entry entry : wrapper.getNbtStrings().entrySet()) { 1015 | if (entry.getKey().contains("..")) { 1016 | String[] entrySplit = entry.getKey().split("\\.\\."); 1017 | if (nbtItem.getCompound(entrySplit[0]) == null) { 1018 | continue itemsLoop; 1019 | } 1020 | if (!checkNbtValue(entrySplit[1], entry.getValue(), nbtItem.getCompound(entrySplit[0]))) { 1021 | continue itemsLoop; 1022 | } 1023 | } else { 1024 | if (!nbtItem.hasTag(entry.getKey())) { 1025 | continue itemsLoop; 1026 | } 1027 | if (!nbtItem.getString(entry.getKey()).equals(entry.getValue())) { 1028 | continue itemsLoop; 1029 | } 1030 | } 1031 | } 1032 | } 1033 | if (wrapper.shouldCheckNbtInts()) { 1034 | for (Entry entry : wrapper.getNbtInts().entrySet()) { 1035 | if (entry.getKey().contains("..")) { 1036 | String[] entrySplit = entry.getKey().split("\\.\\."); 1037 | if (nbtItem.getCompound(entrySplit[0]) == null) { 1038 | continue itemsLoop; 1039 | } 1040 | if (!checkNbtValue(entrySplit[1], entry.getValue(), nbtItem.getCompound(entrySplit[0]))) { 1041 | continue itemsLoop; 1042 | } 1043 | } else { 1044 | if (!nbtItem.hasTag(entry.getKey())) { 1045 | continue itemsLoop; 1046 | } 1047 | if (!(nbtItem.getInteger(entry.getKey()) == entry.getValue())) { 1048 | continue itemsLoop; 1049 | } 1050 | } 1051 | } 1052 | } 1053 | 1054 | } 1055 | 1056 | if (wrapper.isStrict() && wrapper.shouldCheckType()) { 1057 | if (!wrapper.shouldCheckNameContains() 1058 | && !wrapper.shouldCheckNameEquals() 1059 | && !wrapper.shouldCheckNameStartsWith() 1060 | && toCheckMeta.hasDisplayName()) { 1061 | continue; 1062 | } 1063 | if (!wrapper.shouldCheckLoreContains() && toCheckMeta.hasLore()) { 1064 | continue; 1065 | } 1066 | if (!wrapper.shouldCheckDurability() && toCheck.getDurability() != 0) { 1067 | continue; 1068 | } 1069 | if (!wrapper.shouldCheckEnchantments() && !toCheck.getEnchantments().isEmpty()) { 1070 | continue; 1071 | } 1072 | } 1073 | total += toCheck.getAmount(); 1074 | matched.add(toCheck); 1075 | } 1076 | } 1077 | if (wrapper.shouldRemove()) 1078 | 1079 | { 1080 | boolean remove = true; 1081 | if (wrapper.shouldCheckAmount()) { 1082 | remove = total >= wrapper.getAmount(); 1083 | if (remove) 1084 | total = wrapper.getAmount(); 1085 | } 1086 | if (remove) { 1087 | ItemStack[] matchedArr = new ItemStack[matched.size()]; 1088 | if (wrapper.shouldCheckAmount()) { 1089 | int remaining = wrapper.getAmount(); 1090 | ItemStack[] armor = p.getInventory().getArmorContents(); 1091 | for (int i = 0; i < armor.length; i++) { 1092 | if (matched.contains(armor[i])) { 1093 | if (armor[i].getAmount() > remaining) { 1094 | armor[i].setAmount(armor[i].getAmount() - remaining); 1095 | remaining = 0; 1096 | break; 1097 | } else { 1098 | remaining -= armor[i].getAmount(); 1099 | armor[i] = null; 1100 | } 1101 | } 1102 | } 1103 | p.getInventory().setArmorContents(armor); 1104 | 1105 | try { 1106 | Class.forName("org.bukkit.inventory.PlayerInventory").getMethod("getItemInOffHand", null); 1107 | 1108 | ItemStack offhand = p.getInventory().getItemInOffHand(); 1109 | if (matched.contains(offhand)) { 1110 | if (offhand.getAmount() > remaining) { 1111 | offhand.setAmount(offhand.getAmount() - remaining); 1112 | remaining = 0; 1113 | } else { 1114 | remaining -= offhand.getAmount(); 1115 | offhand = null; 1116 | } 1117 | } 1118 | p.getInventory().setItemInOffHand(offhand); 1119 | 1120 | } catch (NoSuchMethodException e) { 1121 | 1122 | } catch (Exception e) { 1123 | e.printStackTrace(); 1124 | } 1125 | 1126 | for (int i = 0; i < matched.size() && remaining > 0; i++) { 1127 | ItemStack item = matched.get(i); 1128 | int match = p.getInventory().first(item); 1129 | if (match == -1) { 1130 | break; 1131 | } 1132 | ItemStack matchedItem = p.getInventory().getItem(match); 1133 | if (matchedItem.getAmount() > remaining) { 1134 | matchedItem.setAmount(matchedItem.getAmount() - remaining); 1135 | remaining = 0; 1136 | break; 1137 | } else { 1138 | remaining -= matchedItem.getAmount(); 1139 | p.getInventory().clear(match); 1140 | } 1141 | } 1142 | } else { 1143 | p.getInventory().removeItem(matched.toArray(matchedArr)); 1144 | ItemStack[] armor = p.getInventory().getArmorContents(); 1145 | for (int i = 0; i < armor.length; i++) { 1146 | if (matched.contains(armor[i])) { 1147 | armor[i] = null; 1148 | } 1149 | } 1150 | p.getInventory().setArmorContents(armor); 1151 | try { 1152 | Class.forName("org.bukkit.inventory.PlayerInventory").getMethod("getItemInOffHand", null); 1153 | 1154 | ItemStack offhand = p.getInventory().getItemInOffHand(); 1155 | if (matched.contains(offhand)) { 1156 | offhand = null; 1157 | } 1158 | p.getInventory().setItemInOffHand(offhand); 1159 | 1160 | } catch (NoSuchMethodException e) { 1161 | 1162 | } catch (Exception e) { 1163 | e.printStackTrace(); 1164 | } 1165 | } 1166 | } 1167 | } 1168 | return total; 1169 | 1170 | } 1171 | 1172 | private int getInt(String s) { 1173 | try { 1174 | return Integer.parseInt(s); 1175 | } catch (NumberFormatException ex) { 1176 | return -1; 1177 | } 1178 | } 1179 | 1180 | @SuppressWarnings("deprecation") 1181 | private ItemWrapper getWrapper(ItemWrapper wrapper, String input, Player p) { 1182 | input = input.replaceAll("__", " "); 1183 | String[] arrayOfString; 1184 | int j = (arrayOfString = input.split("(? enchantments = new HashMap<>(); 1254 | String[] enchArray = part.split("(? 1) { 1260 | NamespacedKey key = NamespacedKey 1261 | .minecraft(PlaceholderAPI.setBracketPlaceholders(p, ench[0]).toLowerCase()); 1262 | enchantments.put(Enchantment.getByKey(key), 1263 | Integer.valueOf(PlaceholderAPI.setBracketPlaceholders(p, ench[1]))); 1264 | } else { 1265 | NamespacedKey key = NamespacedKey.minecraft(PlaceholderAPI.setBracketPlaceholders(p, s).toLowerCase()); 1266 | enchantments.put(Enchantment.getByKey(key), -1); 1267 | } 1268 | } 1269 | } catch (NoSuchMethodException | NoClassDefFoundError e) { 1270 | for (String s : enchArray) { 1271 | String[] ench; 1272 | if ((ench = s.split("=")).length > 1) { 1273 | enchantments.put(Enchantment.getByName(PlaceholderAPI.setBracketPlaceholders(p, ench[0]).toUpperCase()), 1274 | Integer.valueOf(PlaceholderAPI.setBracketPlaceholders(p, ench[1]))); 1275 | } else { 1276 | enchantments.put(Enchantment.getByName(PlaceholderAPI.setBracketPlaceholders(p, s).toUpperCase()), -1); 1277 | } 1278 | } 1279 | } catch (IllegalArgumentException e) { 1280 | log(Level.WARNING, "Invalid Key for enchantment(s). -- Ignore if enchantment is blank on purpose"); 1281 | 1282 | } catch (Exception e) { 1283 | e.printStackTrace(); 1284 | } 1285 | wrapper.setEnchantments(enchantments); 1286 | wrapper.setCheckEnchantments(true); 1287 | continue; 1288 | } 1289 | if (part.startsWith("potiontype:")) { 1290 | part = part.replace("potiontype:", ""); 1291 | try { 1292 | wrapper.setPotionType(PotionType.valueOf(part.toUpperCase())); 1293 | } catch (IllegalArgumentException e) { 1294 | } 1295 | wrapper.setCheckPotionType(true); 1296 | continue; 1297 | } 1298 | if (part.startsWith("potionextended:")) { 1299 | part = part.replace("potionextended:", ""); 1300 | wrapper.setPotionExtended(Boolean.parseBoolean(part)); 1301 | wrapper.setCheckPotionExtended(true); 1302 | continue; 1303 | } 1304 | if (part.startsWith("potionupgraded:")) { 1305 | part = part.replace("potionupgraded:", ""); 1306 | wrapper.setCheckPotionUpgraded(true); 1307 | wrapper.setPotionUpgraded(Boolean.parseBoolean(part)); 1308 | continue; 1309 | } 1310 | if (part.startsWith("nbtstrings:")) { 1311 | part = part.replace("nbtstrings:", ""); 1312 | HashMap nbtStrings = new HashMap<>(); 1313 | String[] nbtArray = part.split("(? 1) { 1317 | nbtStrings.put(PlaceholderAPI.setBracketPlaceholders(p, nbt[0]), 1318 | PlaceholderAPI.setBracketPlaceholders(p, nbt[1])); 1319 | } 1320 | } 1321 | wrapper.setNbtStrings(nbtStrings); 1322 | wrapper.setCheckNbtStrings(true); 1323 | continue; 1324 | } 1325 | if (part.startsWith("nbtints:")) { 1326 | part = part.replace("nbtints:", ""); 1327 | HashMap nbtInts = new HashMap<>(); 1328 | String[] nbtArray = part.split("(? 1) { 1332 | try { 1333 | nbtInts.put(PlaceholderAPI.setBracketPlaceholders(p, nbt[0]), 1334 | Integer.parseInt(PlaceholderAPI.setBracketPlaceholders(p, nbt[1]))); 1335 | } catch (NumberFormatException e) { 1336 | } 1337 | } 1338 | } 1339 | wrapper.setNbtInts(nbtInts); 1340 | wrapper.setCheckNbtInts(true); 1341 | continue; 1342 | } 1343 | if (part.startsWith("inslot:")) { 1344 | part = part.replace("inslot:", ""); 1345 | wrapper.setSlot(getInt(PlaceholderAPI.setBracketPlaceholders(p, part))); 1346 | continue; 1347 | } 1348 | if (part.startsWith("inhand")) { 1349 | if (part.startsWith("inhand:")) { 1350 | part = part.replace("inhand:", ""); 1351 | if (part.equals("main")) 1352 | wrapper.setCheckMainHand(true); 1353 | else if (part.equals("off")) 1354 | wrapper.setCheckOffHand(true); 1355 | } else { 1356 | wrapper.setCheckMainHand(true); 1357 | wrapper.setCheckOffHand(true); 1358 | } 1359 | 1360 | continue; 1361 | } 1362 | if (part.equals("strict")) { 1363 | wrapper.setIsStrict(true); 1364 | continue; 1365 | } 1366 | if (part.equals("enchanted")) { 1367 | wrapper.setCheckEnchanted(true); 1368 | continue; 1369 | } 1370 | 1371 | } 1372 | return wrapper; 1373 | } 1374 | 1375 | @Override 1376 | public Map getDefaults() { 1377 | Map defaults = new HashMap<>(); 1378 | defaults.put("give_enabled", false); 1379 | defaults.put("remove_enabled", false); 1380 | return defaults; 1381 | } 1382 | 1383 | private boolean checkNbtValue(String key, String value, ReadableNBT nbtCompound) { 1384 | String[] keySplit = key.split("\\.\\."); 1385 | if (keySplit.length > 1) { 1386 | if (nbtCompound.getCompound(keySplit[0]) != null) 1387 | return checkNbtValue(keySplit[1], value, nbtCompound.getCompound(keySplit[0])); 1388 | return false; 1389 | } 1390 | String nbtValue = nbtCompound.getString(key); 1391 | return nbtValue == null ? false : nbtValue.toString().equals(value); 1392 | } 1393 | 1394 | private boolean checkNbtValue(String key, int value, ReadableNBT nbtCompound) { 1395 | String[] keySplit = key.split("\\.\\."); 1396 | if (keySplit.length > 1) { 1397 | if (nbtCompound.getCompound(keySplit[0]) != null) 1398 | return checkNbtValue(keySplit[1], value, nbtCompound.getCompound(keySplit[0])); 1399 | return false; 1400 | } 1401 | Integer nbtValue = nbtCompound.getInteger(key); 1402 | return nbtValue == null ? false : nbtValue == value; 1403 | } 1404 | } 1405 | --------------------------------------------------------------------------------