mirror of
				https://github.com/mcMMO-Dev/mcMMO.git
				synced 2025-10-31 01:03:44 +01:00 
			
		
		
		
	some more refactoring
This commit is contained in:
		| @@ -35,10 +35,6 @@ import java.util.function.Predicate; | ||||
| import static java.util.Objects.requireNonNull; | ||||
|  | ||||
| public final class ItemUtils { | ||||
|     private ItemUtils() { | ||||
|         // private constructor | ||||
|     } | ||||
|  | ||||
|     // Reflection for setItemName only available in newer APIs | ||||
|     private static final Method setItemName; | ||||
|  | ||||
| @@ -46,6 +42,10 @@ public final class ItemUtils { | ||||
|         setItemName = getSetItemName(); | ||||
|     } | ||||
|  | ||||
|     private ItemUtils() { | ||||
|         // private constructor | ||||
|     } | ||||
|  | ||||
|     private static Method getSetItemName() { | ||||
|         try { | ||||
|             return ItemMeta.class.getMethod("setItemName", String.class); | ||||
| @@ -57,6 +57,7 @@ public final class ItemUtils { | ||||
|     /** | ||||
|      * Sets the item name using the new API if available | ||||
|      * or falls back to the old API. | ||||
|      * | ||||
|      * @param itemMeta The item meta to set the name on | ||||
|      * @param name     The name to set | ||||
|      */ | ||||
| @@ -93,6 +94,7 @@ public final class ItemUtils { | ||||
|      * <p> | ||||
|      * This method will first try a normal lookup, then a legacy lookup, then a lookup by ENUM name, | ||||
|      * and finally a lookup by ENUM name with legacy name. | ||||
|      * | ||||
|      * @param materialName The name of the material to lookup | ||||
|      * @return The Material if found, or null if not found | ||||
|      */ | ||||
| @@ -168,11 +170,6 @@ public final class ItemUtils { | ||||
|         return mcMMO.getMaterialMapStore().isCrossbow(item.getType().getKey().getKey()); | ||||
|     } | ||||
|  | ||||
|     // TODO: Unit tests | ||||
|     public static boolean isBowOrCrossbow(@NotNull ItemStack item) { | ||||
|         return isBow(item) || isCrossbow(item); | ||||
|     } | ||||
|  | ||||
|     // TODO: Unit tests | ||||
|     public static boolean isTrident(@NotNull ItemStack item) { | ||||
|         return mcMMO.getMaterialMapStore().isTrident(item.getType().getKey().getKey()); | ||||
| @@ -183,7 +180,8 @@ public final class ItemUtils { | ||||
|     } | ||||
|  | ||||
|     public static boolean hasItemInEitherHand(@NotNull Player player, Material material) { | ||||
|         return player.getInventory().getItemInMainHand().getType() == material || player.getInventory().getItemInOffHand().getType() == material; | ||||
|         return player.getInventory().getItemInMainHand().getType() == material | ||||
|                 || player.getInventory().getItemInOffHand().getType() == material; | ||||
|     } | ||||
|  | ||||
|     public static boolean doesPlayerHaveEnchantmentOnArmor(@NotNull Player player, @NotNull String enchantmentByName) { | ||||
| @@ -196,7 +194,7 @@ public final class ItemUtils { | ||||
|     } | ||||
|  | ||||
|     public static boolean doesPlayerHaveEnchantmentOnArmor(@NotNull Player player, @NotNull Enchantment enchantment) { | ||||
|         for(ItemStack itemStack : player.getInventory().getArmorContents()) { | ||||
|         for (ItemStack itemStack : player.getInventory().getArmorContents()) { | ||||
|             if (itemStack != null) { | ||||
|                 if (hasEnchantment(itemStack, enchantment)) | ||||
|                     return true; | ||||
| @@ -257,7 +255,7 @@ public final class ItemUtils { | ||||
|     } | ||||
|  | ||||
|     public static @Nullable Enchantment getEnchantment(@NotNull String enchantmentName) { | ||||
|         for(Enchantment enchantment : Enchantment.values()) { | ||||
|         for (Enchantment enchantment : Enchantment.values()) { | ||||
|             if (enchantment.getKey().getKey().equalsIgnoreCase(enchantmentName)) { | ||||
|                 return enchantment; | ||||
|             } | ||||
| @@ -509,7 +507,11 @@ public final class ItemUtils { | ||||
|             return false; | ||||
|         } | ||||
|  | ||||
|         return isMiningDrop(item) || isWoodcuttingDrop(item) || isMobDrop(item) || isHerbalismDrop(item) || isMiscDrop(item); | ||||
|         return isMiningDrop(item) | ||||
|                 || isWoodcuttingDrop(item) | ||||
|                 || isMobDrop(item) | ||||
|                 || isHerbalismDrop(item) | ||||
|                 || isMiscDrop(item); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -520,27 +522,12 @@ public final class ItemUtils { | ||||
|      */ | ||||
|     public static boolean isMiningDrop(ItemStack item) { | ||||
|         //TODO: 1.14 This needs to be updated | ||||
|         switch (item.getType()) { | ||||
|             case COAL: | ||||
|             case COAL_ORE: | ||||
|             case DIAMOND: | ||||
|             case DIAMOND_ORE: | ||||
|             case EMERALD: | ||||
|             case EMERALD_ORE: | ||||
|             case GOLD_ORE: | ||||
|             case IRON_ORE: | ||||
|             case LAPIS_ORE: | ||||
|             case REDSTONE_ORE: // Should we also have Glowing Redstone Ore here? | ||||
|             case REDSTONE: | ||||
|             case GLOWSTONE_DUST: // Should we also have Glowstone here? | ||||
|             case QUARTZ: | ||||
|             case NETHER_QUARTZ_ORE: | ||||
|             case LAPIS_LAZULI: | ||||
|                 return true; | ||||
|  | ||||
|             default: | ||||
|                 return false; | ||||
|         } | ||||
|         return switch (item.getType()) { // Should we also have Glowing Redstone Ore here? | ||||
|             // Should we also have Glowstone here? | ||||
|             case COAL, COAL_ORE, DIAMOND, DIAMOND_ORE, EMERALD, EMERALD_ORE, GOLD_ORE, IRON_ORE, LAPIS_ORE, | ||||
|                  REDSTONE_ORE, REDSTONE, GLOWSTONE_DUST, QUARTZ, NETHER_QUARTZ_ORE, LAPIS_LAZULI -> true; | ||||
|             default -> false; | ||||
|         }; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -551,36 +538,13 @@ public final class ItemUtils { | ||||
|      */ | ||||
|     public static boolean isHerbalismDrop(ItemStack item) { | ||||
|         //TODO: 1.14 This needs to be updated | ||||
|         switch (item.getType().getKey().getKey().toLowerCase()) { | ||||
|             case "wheat": | ||||
|             case "wheat_seeds": | ||||
|             case "carrot": | ||||
|             case "chorus_fruit": | ||||
|             case "chorus_flower": | ||||
|             case "potato": | ||||
|             case "beetroot": | ||||
|             case "beetroots": | ||||
|             case "beetroot_seeds": | ||||
|             case "nether_wart": | ||||
|             case "brown_mushroom": | ||||
|             case "red_mushroom": | ||||
|             case "rose_bush": | ||||
|             case "dandelion": | ||||
|             case "cactus": | ||||
|             case "sugar_cane": | ||||
|             case "melon": | ||||
|             case "melon_seeds": | ||||
|             case "pumpkin": | ||||
|             case "pumpkin_seeds": | ||||
|             case "lily_pad": | ||||
|             case "vine": | ||||
|             case "tall_grass": | ||||
|             case "cocoa_beans": | ||||
|                 return true; | ||||
|  | ||||
|             default: | ||||
|                 return false; | ||||
|         } | ||||
|         return switch (item.getType().getKey().getKey().toLowerCase()) { | ||||
|             case "wheat", "wheat_seeds", "carrot", "chorus_fruit", "chorus_flower", "potato", "beetroot", "beetroots", | ||||
|                  "beetroot_seeds", "nether_wart", "brown_mushroom", "red_mushroom", "rose_bush", "dandelion", "cactus", | ||||
|                  "sugar_cane", "melon", "melon_seeds", "pumpkin", "pumpkin_seeds", "lily_pad", "vine", "tall_grass", | ||||
|                  "cocoa_beans" -> true; | ||||
|             default -> false; | ||||
|         }; | ||||
|     } | ||||
|  | ||||
|  | ||||
| @@ -592,54 +556,14 @@ public final class ItemUtils { | ||||
|      */ | ||||
|     public static boolean isMobDrop(ItemStack item) { | ||||
|         //TODO: 1.14 This needs to be updated | ||||
|         switch (item.getType()) { | ||||
|             case STRING: | ||||
|             case FEATHER: | ||||
|             case CHICKEN: | ||||
|             case COOKED_CHICKEN: | ||||
|             case LEATHER: | ||||
|             case BEEF: | ||||
|             case COOKED_BEEF: | ||||
|             case PORKCHOP: | ||||
|             case COOKED_PORKCHOP: | ||||
|             case WHITE_WOOL: | ||||
|             case BLACK_WOOL: | ||||
|             case BLUE_WOOL: | ||||
|             case BROWN_WOOL: | ||||
|             case CYAN_WOOL: | ||||
|             case GRAY_WOOL: | ||||
|             case GREEN_WOOL: | ||||
|             case LIGHT_BLUE_WOOL: | ||||
|             case LIGHT_GRAY_WOOL: | ||||
|             case LIME_WOOL: | ||||
|             case MAGENTA_WOOL: | ||||
|             case ORANGE_WOOL: | ||||
|             case PINK_WOOL: | ||||
|             case PURPLE_WOOL: | ||||
|             case RED_WOOL: | ||||
|             case YELLOW_WOOL: | ||||
|             case IRON_INGOT: | ||||
|             case SNOWBALL: | ||||
|             case BLAZE_ROD: | ||||
|             case SPIDER_EYE: | ||||
|             case GUNPOWDER: | ||||
|             case ENDER_PEARL: | ||||
|             case GHAST_TEAR: | ||||
|             case MAGMA_CREAM: | ||||
|             case BONE: | ||||
|             case ARROW: | ||||
|             case SLIME_BALL: | ||||
|             case NETHER_STAR: | ||||
|             case ROTTEN_FLESH: | ||||
|             case GOLD_NUGGET: | ||||
|             case EGG: | ||||
|             case ROSE_BUSH: | ||||
|             case COAL: | ||||
|                 return true; | ||||
|  | ||||
|             default: | ||||
|                 return false; | ||||
|         } | ||||
|         return switch (item.getType()) { | ||||
|             case STRING, FEATHER, CHICKEN, COOKED_CHICKEN, LEATHER, BEEF, COOKED_BEEF, PORKCHOP, COOKED_PORKCHOP, | ||||
|                  WHITE_WOOL, BLACK_WOOL, BLUE_WOOL, BROWN_WOOL, CYAN_WOOL, GRAY_WOOL, GREEN_WOOL, LIGHT_BLUE_WOOL, | ||||
|                  LIGHT_GRAY_WOOL, LIME_WOOL, MAGENTA_WOOL, ORANGE_WOOL, PINK_WOOL, PURPLE_WOOL, RED_WOOL, YELLOW_WOOL, | ||||
|                  IRON_INGOT, SNOWBALL, BLAZE_ROD, SPIDER_EYE, GUNPOWDER, ENDER_PEARL, GHAST_TEAR, MAGMA_CREAM, BONE, | ||||
|                  ARROW, SLIME_BALL, NETHER_STAR, ROTTEN_FLESH, GOLD_NUGGET, EGG, ROSE_BUSH, COAL -> true; | ||||
|             default -> false; | ||||
|         }; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -649,39 +573,14 @@ public final class ItemUtils { | ||||
|      * @return true if the item is a woodcutting drop, false otherwise | ||||
|      */ | ||||
|     public static boolean isWoodcuttingDrop(ItemStack item) { | ||||
|         switch (item.getType().toString()) { | ||||
|             case "ACACIA_LOG": | ||||
|             case "BIRCH_LOG": | ||||
|             case "DARK_OAK_LOG": | ||||
|             case "JUNGLE_LOG": | ||||
|             case "OAK_LOG": | ||||
|             case "SPRUCE_LOG": | ||||
|             case "STRIPPED_ACACIA_LOG": | ||||
|             case "STRIPPED_BIRCH_LOG": | ||||
|             case "STRIPPED_DARK_OAK_LOG": | ||||
|             case "STRIPPED_JUNGLE_LOG": | ||||
|             case "STRIPPED_OAK_LOG": | ||||
|             case "STRIPPED_SPRUCE_LOG": | ||||
|             case "STRIPPED_MANGROVE_LOG": | ||||
|             case "ACACIA_SAPLING": | ||||
|             case "SPRUCE_SAPLING": | ||||
|             case "BIRCH_SAPLING": | ||||
|             case "DARK_OAK_SAPLING": | ||||
|             case "JUNGLE_SAPLING": | ||||
|             case "OAK_SAPLING": | ||||
|             case "ACACIA_LEAVES": | ||||
|             case "BIRCH_LEAVES": | ||||
|             case "DARK_OAK_LEAVES": | ||||
|             case "JUNGLE_LEAVES": | ||||
|             case "OAK_LEAVES": | ||||
|             case "SPRUCE_LEAVES": | ||||
|             case "BEE_NEST": | ||||
|             case "APPLE": | ||||
|                 return true; | ||||
|  | ||||
|             default: | ||||
|                 return false; | ||||
|         } | ||||
|         return switch (item.getType().toString()) { | ||||
|             case "ACACIA_LOG", "BIRCH_LOG", "DARK_OAK_LOG", "JUNGLE_LOG", "OAK_LOG", "SPRUCE_LOG", | ||||
|                  "STRIPPED_ACACIA_LOG", "STRIPPED_BIRCH_LOG", "STRIPPED_DARK_OAK_LOG", "STRIPPED_JUNGLE_LOG", | ||||
|                  "STRIPPED_OAK_LOG", "STRIPPED_SPRUCE_LOG", "STRIPPED_MANGROVE_LOG", "ACACIA_SAPLING", "SPRUCE_SAPLING", | ||||
|                  "BIRCH_SAPLING", "DARK_OAK_SAPLING", "JUNGLE_SAPLING", "OAK_SAPLING", "ACACIA_LEAVES", "BIRCH_LEAVES", | ||||
|                  "DARK_OAK_LEAVES", "JUNGLE_LEAVES", "OAK_LEAVES", "SPRUCE_LEAVES", "BEE_NEST", "APPLE" -> true; | ||||
|             default -> false; | ||||
|         }; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -721,23 +620,6 @@ public final class ItemUtils { | ||||
|         return itemMeta.hasDisplayName() && itemMeta.getDisplayName().equals(ChatColor.GOLD + LocaleLoader.getString("Item.ChimaeraWing.Name")); | ||||
|     } | ||||
|  | ||||
| //    public static void addAbilityLore(@NotNull ItemStack itemStack) { | ||||
| //        ItemMeta itemMeta = itemStack.getItemMeta(); | ||||
| //        List<String> itemLore = new ArrayList<>(); | ||||
| // | ||||
| //        if (itemMeta == null) | ||||
| //            return; | ||||
| // | ||||
| //        if (itemMeta.hasLore()) { | ||||
| //            itemLore = itemMeta.getLore(); | ||||
| //        } | ||||
| // | ||||
| //        itemLore.add("mcMMO Ability Tool"); | ||||
| // | ||||
| //        itemMeta.setLore(itemLore); | ||||
| //        itemStack.setItemMeta(itemMeta); | ||||
| //    } | ||||
|  | ||||
|     public static void removeAbilityLore(@NotNull ItemStack itemStack) { | ||||
|         ItemMeta itemMeta = itemStack.getItemMeta(); | ||||
|  | ||||
| @@ -757,7 +639,8 @@ public final class ItemUtils { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public static void addDigSpeedToItem(@NotNull ItemStack itemStack, int existingEnchantLevel) { | ||||
|     public static void addDigSpeedToItem(@NotNull ItemStack itemStack, | ||||
|                                          int existingEnchantLevel) { | ||||
|         ItemMeta itemMeta = itemStack.getItemMeta(); | ||||
|  | ||||
|         if (itemMeta == null) | ||||
| @@ -782,12 +665,16 @@ public final class ItemUtils { | ||||
|         } | ||||
|  | ||||
|         EnchantmentStorageMeta enchantmentStorageMeta = (EnchantmentStorageMeta) itemMeta; | ||||
|         enchantmentStorageMeta.addStoredEnchant(enchantmentWrapper.getEnchantment(), enchantmentWrapper.getEnchantmentLevel(), ExperienceConfig.getInstance().allowUnsafeEnchantments()); | ||||
|         enchantmentStorageMeta.addStoredEnchant( | ||||
|                 enchantmentWrapper.getEnchantment(), | ||||
|                 enchantmentWrapper.getEnchantmentLevel(), | ||||
|                 ExperienceConfig.getInstance().allowUnsafeEnchantments()); | ||||
|         itemStack.setItemMeta(enchantmentStorageMeta); | ||||
|         return itemStack; | ||||
|     } | ||||
|  | ||||
|     public static @NotNull EnchantmentWrapper getRandomEnchantment(@NotNull List<EnchantmentWrapper> enchantmentWrappers) { | ||||
|     public static @NotNull EnchantmentWrapper getRandomEnchantment( | ||||
|             @NotNull List<EnchantmentWrapper> enchantmentWrappers) { | ||||
|         Collections.shuffle(enchantmentWrappers, Misc.getRandom()); | ||||
|  | ||||
|         int randomIndex = Misc.getRandom().nextInt(enchantmentWrappers.size()); | ||||
| @@ -820,7 +707,11 @@ public final class ItemUtils { | ||||
|      * @param is       The items to drop | ||||
|      * @param quantity The amount of items to drop | ||||
|      */ | ||||
|     public static void spawnItems(@Nullable Player player, @NotNull Location location, @NotNull ItemStack is, int quantity, @NotNull ItemSpawnReason itemSpawnReason) { | ||||
|     public static void spawnItems(@Nullable Player player, | ||||
|                                   @NotNull Location location, | ||||
|                                   @NotNull ItemStack is, | ||||
|                                   int quantity, | ||||
|                                   @NotNull ItemSpawnReason itemSpawnReason) { | ||||
|         for (int i = 0; i < quantity; i++) { | ||||
|             spawnItem(player, location, is, itemSpawnReason); | ||||
|         } | ||||
| @@ -834,7 +725,10 @@ public final class ItemUtils { | ||||
|      * @param itemSpawnReason the reason for the item drop | ||||
|      * @return Dropped Item entity or null if invalid or cancelled | ||||
|      */ | ||||
|     public static @Nullable Item spawnItem(@Nullable Player player, @NotNull Location location, @NotNull ItemStack itemStack, @NotNull ItemSpawnReason itemSpawnReason) { | ||||
|     public static @Nullable Item spawnItem(@Nullable Player player, | ||||
|                                            @NotNull Location location, | ||||
|                                            @NotNull ItemStack itemStack, | ||||
|                                            @NotNull ItemSpawnReason itemSpawnReason) { | ||||
|         if (itemStack.getType() == Material.AIR || location.getWorld() == null) { | ||||
|             return null; | ||||
|         } | ||||
| @@ -858,7 +752,10 @@ public final class ItemUtils { | ||||
|      * @param itemSpawnReason the reason for the item drop | ||||
|      * @return Dropped Item entity or null if invalid or cancelled | ||||
|      */ | ||||
|     public static @Nullable Item spawnItemNaturally(@Nullable Player player, @NotNull Location location, @NotNull ItemStack itemStack, @NotNull ItemSpawnReason itemSpawnReason) { | ||||
|     public static @Nullable Item spawnItemNaturally(@Nullable Player player, | ||||
|                                                     @NotNull Location location, | ||||
|                                                     @NotNull ItemStack itemStack, | ||||
|                                                     @NotNull ItemSpawnReason itemSpawnReason) { | ||||
|         if (itemStack.getType() == Material.AIR || location.getWorld() == null) { | ||||
|             return null; | ||||
|         } | ||||
| @@ -882,7 +779,13 @@ public final class ItemUtils { | ||||
|      * @param speed        the speed that the item should travel | ||||
|      * @param quantity     The amount of items to drop | ||||
|      */ | ||||
|     public static void spawnItemsTowardsLocation(@Nullable Player player, @NotNull Location fromLocation, @NotNull Location toLocation, @NotNull ItemStack is, int quantity, double speed, @NotNull ItemSpawnReason itemSpawnReason) { | ||||
|     public static void spawnItemsTowardsLocation(@Nullable Player player, | ||||
|                                                  @NotNull Location fromLocation, | ||||
|                                                  @NotNull Location toLocation, | ||||
|                                                  @NotNull ItemStack is, | ||||
|                                                  int quantity, | ||||
|                                                  double speed, | ||||
|                                                  @NotNull ItemSpawnReason itemSpawnReason) { | ||||
|         for (int i = 0; i < quantity; i++) { | ||||
|             spawnItemTowardsLocation(player, fromLocation, toLocation, is, speed, itemSpawnReason); | ||||
|         } | ||||
| @@ -943,6 +846,7 @@ public final class ItemUtils { | ||||
|                                                 @NotNull Location location, | ||||
|                                                 @NotNull Collection<ItemStack> drops, | ||||
|                                                 @NotNull ItemSpawnReason itemSpawnReason) { | ||||
|         requireNonNull(drops, "drops cannot be null"); | ||||
|         for (ItemStack drop : drops) { | ||||
|             spawnItem(player, location, drop, itemSpawnReason); | ||||
|         } | ||||
| @@ -961,9 +865,10 @@ public final class ItemUtils { | ||||
|                                                 @NotNull Collection<ItemStack> drops, | ||||
|                                                 @NotNull ItemSpawnReason itemSpawnReason, | ||||
|                                                 int sizeLimit) { | ||||
|         ItemStack[] arrayDrops = drops.toArray(new ItemStack[0]); | ||||
|         // TODO: This doesn't make much sense, unit test time? | ||||
|         final ItemStack[] arrayDrops = drops.toArray(new ItemStack[0]); | ||||
|  | ||||
|         for(int i = 0; i < sizeLimit-1; i++) { | ||||
|         for (int i = 0; i < sizeLimit - 1; i++) { | ||||
|             spawnItem(player, location, arrayDrops[i], itemSpawnReason); | ||||
|         } | ||||
|     } | ||||
| @@ -979,7 +884,7 @@ public final class ItemUtils { | ||||
|      * @param predicate          The predicate to test the item against | ||||
|      * @param player             The player to spawn the item for | ||||
|      */ | ||||
|     public static void spawnItem(@NotNull Collection <ItemStack> potentialItemDrops, | ||||
|     public static void spawnItem(@NotNull Collection<ItemStack> potentialItemDrops, | ||||
|                                  @NotNull ItemSpawnReason itemSpawnReason, | ||||
|                                  @NotNull Location spawnLocation, | ||||
|                                  @NotNull Predicate<String> predicate, | ||||
|   | ||||
| @@ -17,6 +17,7 @@ import org.junit.jupiter.api.BeforeEach; | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.mockito.Mockito; | ||||
|  | ||||
| import java.util.Collection; | ||||
| import java.util.Collections; | ||||
| import java.util.List; | ||||
|  | ||||
| @@ -72,7 +73,7 @@ class WoodcuttingTest extends MMOTestEnvironment { | ||||
|         Mockito.when(blockState.getBlock()).thenReturn(block); | ||||
|  | ||||
|         // return empty collection if ItemStack | ||||
|         Mockito.when(blockState.getBlock().getDrops(any())).thenReturn(Collections.EMPTY_LIST); | ||||
|         Mockito.when(blockState.getBlock().getDrops(any())).thenReturn(Collections.emptyList()); | ||||
|         Mockito.when(blockState.getType()).thenReturn(Material.OAK_LOG); | ||||
|         woodcuttingManager.processBonusDropCheck(blockState); | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 nossr50
					nossr50