more refactoring for spawning items conditionally

This commit is contained in:
nossr50 2024-05-25 12:25:00 -07:00
parent 0708b0a6a2
commit 8633f7b63a
2 changed files with 16 additions and 33 deletions

View File

@ -35,18 +35,20 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Predicate;
import static com.gmail.nossr50.util.ItemUtils.spawnItemsFromCollection; import static com.gmail.nossr50.util.ItemUtils.spawnItemsFromCollection;
import static com.gmail.nossr50.util.Misc.getBlockCenter; import static com.gmail.nossr50.util.Misc.getBlockCenter;
import static com.gmail.nossr50.util.ItemUtils.spawnItem;
import static com.gmail.nossr50.util.skills.RankUtils.hasUnlockedSubskill; 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 class WoodcuttingManager extends SkillManager {
public static final String SAPLING = "sapling"; public static final String SAPLING = "sapling";
public static final String PROPAGULE = "propagule"; public static final String PROPAGULE = "propagule";
private static final Predicate<ItemStack> IS_SAPLING_OR_PROPAGULE =
p -> p.getType().getKey().getKey().toLowerCase().contains(SAPLING)
|| p.getType().getKey().getKey().toLowerCase().contains(PROPAGULE);
private boolean treeFellerReachedThreshold = false; private boolean treeFellerReachedThreshold = false;
private static int treeFellerThreshold; //TODO: Shared setting, will be removed in 2.2 private static int treeFellerThreshold;
/** /**
* The x/y differences to the blocks in a flat cylinder around the center * The x/y differences to the blocks in a flat cylinder around the center
@ -336,11 +338,11 @@ public class WoodcuttingManager extends SkillManager {
ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK); ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK);
} else if (hasUnlockedSubskill(player, SubSkillType.WOODCUTTING_KNOCK_ON_WOOD)) { } else if (hasUnlockedSubskill(player, SubSkillType.WOODCUTTING_KNOCK_ON_WOOD)) {
// if KnockOnWood is unlocked, then drop any saplings from the remaining blocks // if KnockOnWood is unlocked, then drop any saplings from the remaining blocks
spawnItem(block.getDrops(itemStack), ItemUtils.spawnItemsConditionally(block.getDrops(itemStack),
IS_SAPLING_OR_PROPAGULE,
ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK, ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK,
getBlockCenter(blockState), getBlockCenter(blockState),
// only spawn saplings // only spawn saplings
p -> p.toLowerCase().contains(SAPLING) || p.toLowerCase().contains(PROPAGULE),
player player
); );
} }

View File

@ -681,25 +681,6 @@ public final class ItemUtils {
return enchantmentWrappers.get(randomIndex); return enchantmentWrappers.get(randomIndex);
} }
/**
* 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);
}
}
/** /**
* Drop items at a given location. * Drop items at a given location.
* *
@ -879,18 +860,18 @@ public final class ItemUtils {
* *
* @param potentialItemDrops The collection of items to iterate over, each one is tested and spawned if the * @param potentialItemDrops The collection of items to iterate over, each one is tested and spawned if the
* predicate is true * predicate is true
* @param predicate The predicate to test the item against
* @param itemSpawnReason The reason for the item drop * @param itemSpawnReason The reason for the item drop
* @param spawnLocation The location to spawn the item at * @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 * @param player The player to spawn the item for
*/ */
public static void spawnItem(@NotNull Collection<ItemStack> potentialItemDrops, public static void spawnItemsConditionally(@NotNull Collection<ItemStack> potentialItemDrops,
@NotNull ItemSpawnReason itemSpawnReason, @NotNull Predicate<ItemStack> predicate,
@NotNull Location spawnLocation, @NotNull ItemSpawnReason itemSpawnReason,
@NotNull Predicate<String> predicate, @NotNull Location spawnLocation,
@NotNull Player player) { @NotNull Player player) {
for (ItemStack drop : potentialItemDrops) { potentialItemDrops.stream()
spawnItem(drop, itemSpawnReason, spawnLocation, predicate, player); .filter(predicate)
} .forEach(itemStack -> spawnItem(player, spawnLocation, itemStack, itemSpawnReason));
} }
} }