Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nossr50 committed May 24, 2024
1 parent 2f1278c commit 590b00a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@
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

Expand All @@ -61,7 +66,7 @@ public WoodcuttingManager(McMMOPlayer mcMMOPlayer) {

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);
}

Expand Down Expand Up @@ -317,27 +322,30 @@ private void dropTreeFellerLootFromBlocks(@NotNull Set<BlockState> treeFellerBlo
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);
} else if (BlockUtils.isNonWoodPartOfTree(blockState)) {
// 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<String> 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)) {
Expand Down Expand Up @@ -416,7 +424,7 @@ protected static int getExperienceFromLog(BlockState blockState) {
protected void spawnHarvestLumberBonusDrops(@NotNull BlockState blockState) {
Misc.spawnItemsFromCollection(
getPlayer(),
Misc.getBlockCenter(blockState),
getBlockCenter(blockState),
blockState.getBlock().getDrops(getPlayer().getInventory().getItemInMainHand()),
ItemSpawnReason.BONUS_DROPS);
}
Expand Down
42 changes: 35 additions & 7 deletions src/main/java/com/gmail/nossr50/util/Misc.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,42 @@ public static void spawnItemsFromCollection(@Nullable Player player, @NotNull Lo
}

/**
* 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<String> predicate, @NotNull Player player, @NotNull Location spawnLocation, @NotNull Collection <ItemStack> 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 <ItemStack> potentialItemDrops,
@NotNull ItemSpawnReason itemSpawnReason,
@NotNull Location spawnLocation,
@NotNull Predicate<String> 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<String> predicate,
@NotNull Player player) {
if (predicate.test(potentialItemSpawn.getType().getKey().getKey())) {
spawnItem(player, spawnLocation, potentialItemSpawn, itemSpawnReason);
}
}

Expand Down

0 comments on commit 590b00a

Please sign in to comment.