From 28836c11e4b59e17c4933618e7d0c2d165ff182e Mon Sep 17 00:00:00 2001 From: Enderaoe Date: Thu, 11 Nov 2021 01:31:12 +0800 Subject: [PATCH] refactor code --- .../nossr50/listeners/EntityListener.java | 7 +-- .../com/gmail/nossr50/util/ItemUtils.java | 49 ------------------- .../gmail/nossr50/util/MaterialMapStore.java | 49 ++++++++++++++++++- 3 files changed, 50 insertions(+), 55 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/listeners/EntityListener.java b/src/main/java/com/gmail/nossr50/listeners/EntityListener.java index 743ad6aa7..2f44f13aa 100644 --- a/src/main/java/com/gmail/nossr50/listeners/EntityListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/EntityListener.java @@ -17,10 +17,7 @@ import com.gmail.nossr50.skills.mining.MiningManager; import com.gmail.nossr50.skills.taming.Taming; import com.gmail.nossr50.skills.taming.TamingManager; import com.gmail.nossr50.skills.unarmed.UnarmedManager; -import com.gmail.nossr50.util.BlockUtils; -import com.gmail.nossr50.util.ItemUtils; -import com.gmail.nossr50.util.Misc; -import com.gmail.nossr50.util.Permissions; +import com.gmail.nossr50.util.*; import com.gmail.nossr50.util.compat.layers.persistentdata.AbstractPersistentDataLayer; import com.gmail.nossr50.util.compat.layers.persistentdata.MobMetaFlagType; import com.gmail.nossr50.util.player.NotificationManager; @@ -931,7 +928,7 @@ public class EntityListener implements Listener { return; } - Float foodSaturation = ItemUtils.getFoodSaturation(foodInHand); + Float foodSaturation = MaterialMapStore.getFoodSaturation(foodInHand); if (foodSaturation != null) { newSaturationLevel = currentSaturationLevel + foodSaturation; diff --git a/src/main/java/com/gmail/nossr50/util/ItemUtils.java b/src/main/java/com/gmail/nossr50/util/ItemUtils.java index e5e5f9828..0e83ad850 100644 --- a/src/main/java/com/gmail/nossr50/util/ItemUtils.java +++ b/src/main/java/com/gmail/nossr50/util/ItemUtils.java @@ -31,51 +31,6 @@ public final class ItemUtils { */ private ItemUtils() {} - private static final HashMap foodSaturation = new HashMap<>(); - - static { - foodSaturation.put(Material.APPLE, 1.2F); - foodSaturation.put(Material.BAKED_POTATO, 3.0F); - foodSaturation.put(Material.BEETROOT, 0.6F); - foodSaturation.put(Material.BEETROOT_SOUP, 3.6F); - foodSaturation.put(Material.BREAD, 3.0F); - foodSaturation.put(Material.CAKE, 0.2F); - foodSaturation.put(Material.CARROT, 1.8F); - foodSaturation.put(Material.CHORUS_FRUIT, 1.2F); - foodSaturation.put(Material.COOKED_BEEF, 6.4F); - foodSaturation.put(Material.COOKED_CHICKEN, 3.6F); - foodSaturation.put(Material.COOKED_COD, 3.0F); - foodSaturation.put(Material.COOKED_MUTTON, 4.8F); - foodSaturation.put(Material.COOKED_PORKCHOP, 6.4F); - foodSaturation.put(Material.COOKED_RABBIT, 3.0F); - foodSaturation.put(Material.COOKED_SALMON, 4.8F); - foodSaturation.put(Material.COOKIE, 0.2F); - foodSaturation.put(Material.DRIED_KELP, 0.3F); - foodSaturation.put(Material.ENCHANTED_GOLDEN_APPLE, 4.8F); - foodSaturation.put(Material.HONEY_BOTTLE, 0.6F); - foodSaturation.put(Material.GOLDEN_APPLE, 4.8F); - foodSaturation.put(Material.GOLDEN_CARROT, 7.2F); - foodSaturation.put(Material.MELON_SLICE, 0.6F); - foodSaturation.put(Material.MUSHROOM_STEW, 3.6F); - foodSaturation.put(Material.POISONOUS_POTATO, 0.6F); - foodSaturation.put(Material.POTATO, 0.3F); - foodSaturation.put(Material.PUFFERFISH, 0.1F); - foodSaturation.put(Material.PUMPKIN_PIE, 2.4F); - foodSaturation.put(Material.RABBIT_STEW, 6.0F); - foodSaturation.put(Material.BEEF, 0.9F); - foodSaturation.put(Material.CHICKEN, 0.6F); - foodSaturation.put(Material.COD, 0.2F); - foodSaturation.put(Material.MUTTON, 0.6F); - foodSaturation.put(Material.PORKCHOP, 0.9F); - foodSaturation.put(Material.RABBIT, 0.9F); - foodSaturation.put(Material.SALMON, 0.2F); - foodSaturation.put(Material.ROTTEN_FLESH, 0.4F); - foodSaturation.put(Material.SPIDER_EYE, 1.6F); - foodSaturation.put(Material.SUSPICIOUS_STEW, 3.6F); - foodSaturation.put(Material.SWEET_BERRIES, 0.2F); - foodSaturation.put(Material.TROPICAL_FISH, 0.1F); - } - /** * Checks if the item is a bow. * @@ -696,8 +651,4 @@ public final class ItemUtils { int randomIndex = Misc.getRandom().nextInt(enchantmentWrappers.size()); return enchantmentWrappers.get(randomIndex); } - - public static @Nullable Float getFoodSaturation(@NotNull Material food) { - return foodSaturation.get(food); - } } diff --git a/src/main/java/com/gmail/nossr50/util/MaterialMapStore.java b/src/main/java/com/gmail/nossr50/util/MaterialMapStore.java index 90104acce..5d7f334e6 100644 --- a/src/main/java/com/gmail/nossr50/util/MaterialMapStore.java +++ b/src/main/java/com/gmail/nossr50/util/MaterialMapStore.java @@ -2,6 +2,7 @@ package com.gmail.nossr50.util; import org.bukkit.Material; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.HashMap; import java.util.HashSet; @@ -59,7 +60,7 @@ public class MaterialMapStore { private final @NotNull HashSet intendedToolShovel; private final @NotNull HashMap tierValue; - + private static final @NotNull HashMap foodSaturation = new HashMap<>(); public MaterialMapStore() { @@ -130,6 +131,7 @@ public class MaterialMapStore { fillIntendedTools(); fillTierMap(); + fillFoodSaturation(); } public boolean isMultiBlockPlant(@NotNull Material material) @@ -198,6 +200,49 @@ public class MaterialMapStore { } } + private void fillFoodSaturation() { + foodSaturation.put(Material.APPLE, 1.2F); + foodSaturation.put(Material.BAKED_POTATO, 3.0F); + foodSaturation.put(Material.BEETROOT, 0.6F); + foodSaturation.put(Material.BEETROOT_SOUP, 3.6F); + foodSaturation.put(Material.BREAD, 3.0F); + foodSaturation.put(Material.CAKE, 0.2F); + foodSaturation.put(Material.CARROT, 1.8F); + foodSaturation.put(Material.CHORUS_FRUIT, 1.2F); + foodSaturation.put(Material.COOKED_BEEF, 6.4F); + foodSaturation.put(Material.COOKED_CHICKEN, 3.6F); + foodSaturation.put(Material.COOKED_COD, 3.0F); + foodSaturation.put(Material.COOKED_MUTTON, 4.8F); + foodSaturation.put(Material.COOKED_PORKCHOP, 6.4F); + foodSaturation.put(Material.COOKED_RABBIT, 3.0F); + foodSaturation.put(Material.COOKED_SALMON, 4.8F); + foodSaturation.put(Material.COOKIE, 0.2F); + foodSaturation.put(Material.DRIED_KELP, 0.3F); + foodSaturation.put(Material.ENCHANTED_GOLDEN_APPLE, 4.8F); + foodSaturation.put(Material.HONEY_BOTTLE, 0.6F); + foodSaturation.put(Material.GOLDEN_APPLE, 4.8F); + foodSaturation.put(Material.GOLDEN_CARROT, 7.2F); + foodSaturation.put(Material.MELON_SLICE, 0.6F); + foodSaturation.put(Material.MUSHROOM_STEW, 3.6F); + foodSaturation.put(Material.POISONOUS_POTATO, 0.6F); + foodSaturation.put(Material.POTATO, 0.3F); + foodSaturation.put(Material.PUFFERFISH, 0.1F); + foodSaturation.put(Material.PUMPKIN_PIE, 2.4F); + foodSaturation.put(Material.RABBIT_STEW, 6.0F); + foodSaturation.put(Material.BEEF, 0.9F); + foodSaturation.put(Material.CHICKEN, 0.6F); + foodSaturation.put(Material.COD, 0.2F); + foodSaturation.put(Material.MUTTON, 0.6F); + foodSaturation.put(Material.PORKCHOP, 0.9F); + foodSaturation.put(Material.RABBIT, 0.9F); + foodSaturation.put(Material.SALMON, 0.2F); + foodSaturation.put(Material.ROTTEN_FLESH, 0.4F); + foodSaturation.put(Material.SPIDER_EYE, 1.6F); + foodSaturation.put(Material.SUSPICIOUS_STEW, 3.6F); + foodSaturation.put(Material.SWEET_BERRIES, 0.2F); + foodSaturation.put(Material.TROPICAL_FISH, 0.1F); + } + private void fillOres() { ores.add("coal_ore"); ores.add("diamond_ore"); @@ -1318,6 +1363,8 @@ public class MaterialMapStore { return tierValue.getOrDefault(id, 1); //1 for unknown items } + public @Nullable static float getFoodSaturation(@NotNull Material food) { return foodSaturation.get(food); } + private void addToHashSet(@NotNull String string, @NotNull HashSet stringHashSet) { stringHashSet.add(string.toLowerCase(Locale.ENGLISH));