From 590b00aeca1d60bcb39036c70febb49afcb4b1e8 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Thu, 23 May 2024 18:14:07 -0700 Subject: [PATCH] minor refactoring --- .../woodcutting/WoodcuttingManager.java | 32 ++++++++------ .../java/com/gmail/nossr50/util/Misc.java | 42 +++++++++++++++---- 2 files changed, 55 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/skills/woodcutting/WoodcuttingManager.java b/src/main/java/com/gmail/nossr50/skills/woodcutting/WoodcuttingManager.java index 679f15017..58b7403d9 100644 --- a/src/main/java/com/gmail/nossr50/skills/woodcutting/WoodcuttingManager.java +++ b/src/main/java/com/gmail/nossr50/skills/woodcutting/WoodcuttingManager.java @@ -35,10 +35,15 @@ import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.ThreadLocalRandom; -import java.util.function.Predicate; + +import static com.gmail.nossr50.util.Misc.getBlockCenter; +import static com.gmail.nossr50.util.Misc.spawnItem; +import static com.gmail.nossr50.util.skills.RankUtils.hasUnlockedSubskill; //TODO: Seems to not be using the item drop event for bonus drops, may want to change that.. or may not be able to be changed? public class WoodcuttingManager extends SkillManager { + public static final String SAPLING = "sapling"; + public static final String PROPAGULE = "propagule"; private boolean treeFellerReachedThreshold = false; private static int treeFellerThreshold; //TODO: Shared setting, will be removed in 2.2 @@ -61,7 +66,7 @@ public class WoodcuttingManager extends SkillManager { public boolean canUseLeafBlower(ItemStack heldItem) { return Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.WOODCUTTING_LEAF_BLOWER) - && RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.WOODCUTTING_LEAF_BLOWER) + && hasUnlockedSubskill(getPlayer(), SubSkillType.WOODCUTTING_LEAF_BLOWER) && ItemUtils.isAxe(heldItem); } @@ -317,7 +322,7 @@ public class WoodcuttingManager extends SkillManager { xp += processTreeFellerXPGains(blockState, processedLogCount); //Drop displaced block - Misc.spawnItemsFromCollection(player, Misc.getBlockCenter(blockState), block.getDrops(itemStack), ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK); + Misc.spawnItemsFromCollection(player, getBlockCenter(blockState), block.getDrops(itemStack), ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK); //Bonus Drops / Harvest lumber checks processBonusDropCheck(blockState); @@ -325,19 +330,22 @@ public class WoodcuttingManager extends SkillManager { // 75% of the time do not drop leaf blocks if (ThreadLocalRandom.current().nextInt(100) > 75) { Misc.spawnItemsFromCollection(player, - Misc.getBlockCenter(blockState), + getBlockCenter(blockState), block.getDrops(itemStack), ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK); - } - // if KnockOnWood is unlocked, then drop any saplings from the remaining blocks - else if (RankUtils.hasUnlockedSubskill(player, SubSkillType.WOODCUTTING_KNOCK_ON_WOOD)) { - Predicate isSapling = p -> p.contains("sapling") || p.contains("propagule"); - Misc.conditionallySpawn(isSapling, player, Misc.getBlockCenter(blockState), - block.getDrops(itemStack), ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK); + } else if (hasUnlockedSubskill(player, SubSkillType.WOODCUTTING_KNOCK_ON_WOOD)) { + // if KnockOnWood is unlocked, then drop any saplings from the remaining blocks + spawnItem(block.getDrops(itemStack), + ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK, + getBlockCenter(blockState), + // only spawn saplings + p -> p.toLowerCase().contains(SAPLING) || p.toLowerCase().contains(PROPAGULE), + player + ); } //Drop displaced non-woodcutting XP blocks - if (RankUtils.hasUnlockedSubskill(player, SubSkillType.WOODCUTTING_KNOCK_ON_WOOD)) { + if (hasUnlockedSubskill(player, SubSkillType.WOODCUTTING_KNOCK_ON_WOOD)) { if (RankUtils.hasReachedRank(2, player, SubSkillType.WOODCUTTING_KNOCK_ON_WOOD)) { if (mcMMO.p.getAdvancedConfig().isKnockOnWoodXPOrbEnabled()) { if (ProbabilityUtil.isStaticSkillRNGSuccessful(PrimarySkillType.WOODCUTTING, mmoPlayer, 10)) { @@ -416,7 +424,7 @@ public class WoodcuttingManager extends SkillManager { protected void spawnHarvestLumberBonusDrops(@NotNull BlockState blockState) { Misc.spawnItemsFromCollection( getPlayer(), - Misc.getBlockCenter(blockState), + getBlockCenter(blockState), blockState.getBlock().getDrops(getPlayer().getInventory().getItemInMainHand()), ItemSpawnReason.BONUS_DROPS); } diff --git a/src/main/java/com/gmail/nossr50/util/Misc.java b/src/main/java/com/gmail/nossr50/util/Misc.java index 9b719b7aa..167b70596 100644 --- a/src/main/java/com/gmail/nossr50/util/Misc.java +++ b/src/main/java/com/gmail/nossr50/util/Misc.java @@ -130,14 +130,42 @@ public final class Misc { } /** - * Drops the item from the item stack only if it is a sapling (or equivalent) - * Needed for TreeFeller + * Spawn items form a collection if conditions are met. + * Each item is tested against the condition and spawned if it passes. + * + * @param potentialItemDrops The collection of items to iterate over, each one is tested and spawned if the + * predicate is true + * @param itemSpawnReason The reason for the item drop + * @param spawnLocation The location to spawn the item at + * @param predicate The predicate to test the item against + * @param player The player to spawn the item for */ - public static void conditionallySpawn(@NotNull Predicate predicate, @NotNull Player player, @NotNull Location spawnLocation, @NotNull Collection drops, @NotNull ItemSpawnReason itemSpawnReason) { - for (ItemStack drop : drops) { - if (predicate.test(drop.getType().getKey().getKey())) { - spawnItem(player, spawnLocation, drop, itemSpawnReason); - } + public static void spawnItem(@NotNull Collection potentialItemDrops, + @NotNull ItemSpawnReason itemSpawnReason, + @NotNull Location spawnLocation, + @NotNull Predicate predicate, + @NotNull Player player) { + for (ItemStack drop : potentialItemDrops) { + spawnItem(drop, itemSpawnReason, spawnLocation, predicate, player); + } + } + + /** + * Spawn item if conditions are met. + * + * @param potentialItemSpawn The item to spawn if conditions are met + * @param itemSpawnReason The reason for the item drop + * @param spawnLocation The location to spawn the item at + * @param predicate The predicate to test the item against + * @param player The player to spawn the item for + */ + public static void spawnItem(@NotNull ItemStack potentialItemSpawn, + @NotNull ItemSpawnReason itemSpawnReason, + @NotNull Location spawnLocation, + @NotNull Predicate predicate, + @NotNull Player player) { + if (predicate.test(potentialItemSpawn.getType().getKey().getKey())) { + spawnItem(player, spawnLocation, potentialItemSpawn, itemSpawnReason); } }