refactoring

This commit is contained in:
nossr50 2024-05-24 13:07:45 -07:00
parent 4277384c22
commit 6c1502fc67
3 changed files with 29 additions and 13 deletions

View File

@ -36,6 +36,7 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
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.ItemUtils.spawnItem;
import static com.gmail.nossr50.util.skills.RankUtils.hasUnlockedSubskill; import static com.gmail.nossr50.util.skills.RankUtils.hasUnlockedSubskill;
@ -322,14 +323,14 @@ public class WoodcuttingManager extends SkillManager {
xp += processTreeFellerXPGains(blockState, processedLogCount); xp += processTreeFellerXPGains(blockState, processedLogCount);
//Drop displaced block //Drop displaced block
ItemUtils.spawnItemsFromCollection(player, getBlockCenter(blockState), block.getDrops(itemStack), ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK); spawnItemsFromCollection(player, getBlockCenter(blockState), block.getDrops(itemStack), ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK);
//Bonus Drops / Harvest lumber checks //Bonus Drops / Harvest lumber checks
processBonusDropCheck(blockState); processBonusDropCheck(blockState);
} else if (BlockUtils.isNonWoodPartOfTree(blockState)) { } else if (BlockUtils.isNonWoodPartOfTree(blockState)) {
// 75% of the time do not drop leaf blocks // 75% of the time do not drop leaf blocks
if (ThreadLocalRandom.current().nextInt(100) > 75) { if (ThreadLocalRandom.current().nextInt(100) > 75) {
ItemUtils.spawnItemsFromCollection(player, spawnItemsFromCollection(player,
getBlockCenter(blockState), getBlockCenter(blockState),
block.getDrops(itemStack), block.getDrops(itemStack),
ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK); ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK);
@ -421,8 +422,8 @@ public class WoodcuttingManager extends SkillManager {
* *
* @param blockState Block being broken * @param blockState Block being broken
*/ */
protected void spawnHarvestLumberBonusDrops(@NotNull BlockState blockState) { void spawnHarvestLumberBonusDrops(@NotNull BlockState blockState) {
ItemUtils.spawnItemsFromCollection( spawnItemsFromCollection(
getPlayer(), getPlayer(),
getBlockCenter(blockState), getBlockCenter(blockState),
blockState.getBlock().getDrops(getPlayer().getInventory().getItemInMainHand()), blockState.getBlock().getDrops(getPlayer().getInventory().getItemInMainHand()),

View File

@ -35,12 +35,11 @@ import java.util.function.Predicate;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
public final class ItemUtils { public final class ItemUtils {
/** private ItemUtils() {
* This is a static utility class, therefore we don't want any instances of // private constructor
* this class. Making the constructor private prevents accidents like that. }
*/
private ItemUtils() {}
// Reflection for setItemName only available in newer APIs
private static final Method setItemName; private static final Method setItemName;
static { static {
@ -899,7 +898,12 @@ public final class ItemUtils {
* @param speed the speed that the item should travel * @param speed the speed that the item should travel
* @return Dropped Item entity or null if invalid or cancelled * @return Dropped Item entity or null if invalid or cancelled
*/ */
public static @Nullable Item spawnItemTowardsLocation(@Nullable Player player, @NotNull Location fromLocation, @NotNull Location toLocation, @NotNull ItemStack itemToSpawn, double speed, @NotNull ItemSpawnReason itemSpawnReason) { public static @Nullable Item spawnItemTowardsLocation(@Nullable Player player,
@NotNull Location fromLocation,
@NotNull Location toLocation,
@NotNull ItemStack itemToSpawn,
double speed,
@NotNull ItemSpawnReason itemSpawnReason) {
if (itemToSpawn.getType() == Material.AIR) { if (itemToSpawn.getType() == Material.AIR) {
return null; return null;
} }
@ -935,7 +939,10 @@ public final class ItemUtils {
return spawnedItem; return spawnedItem;
} }
public static void spawnItemsFromCollection(@NotNull Player player, @NotNull Location location, @NotNull Collection<ItemStack> drops, @NotNull ItemSpawnReason itemSpawnReason) { public static void spawnItemsFromCollection(@NotNull Player player,
@NotNull Location location,
@NotNull Collection<ItemStack> drops,
@NotNull ItemSpawnReason itemSpawnReason) {
for (ItemStack drop : drops) { for (ItemStack drop : drops) {
spawnItem(player, location, drop, itemSpawnReason); spawnItem(player, location, drop, itemSpawnReason);
} }
@ -949,7 +956,11 @@ public final class ItemUtils {
* @param drops collection to iterate over * @param drops collection to iterate over
* @param sizeLimit the number of drops to process * @param sizeLimit the number of drops to process
*/ */
public static void spawnItemsFromCollection(@Nullable Player player, @NotNull Location location, @NotNull Collection<ItemStack> drops, @NotNull ItemSpawnReason itemSpawnReason, int sizeLimit) { public static void spawnItemsFromCollection(@Nullable Player player,
@NotNull Location location,
@NotNull Collection<ItemStack> drops,
@NotNull ItemSpawnReason itemSpawnReason,
int sizeLimit) {
ItemStack[] arrayDrops = drops.toArray(new ItemStack[0]); ItemStack[] arrayDrops = drops.toArray(new ItemStack[0]);
for(int i = 0; i < sizeLimit-1; i++) { for(int i = 0; i < sizeLimit-1; i++) {

View File

@ -17,6 +17,9 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import java.util.Collections;
import java.util.List;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
@ -68,7 +71,8 @@ class WoodcuttingTest extends MMOTestEnvironment {
// wire block // wire block
Mockito.when(blockState.getBlock()).thenReturn(block); Mockito.when(blockState.getBlock()).thenReturn(block);
Mockito.when(blockState.getBlock().getDrops(any())).thenReturn(null); // return empty collection if ItemStack
Mockito.when(blockState.getBlock().getDrops(any())).thenReturn(Collections.EMPTY_LIST);
Mockito.when(blockState.getType()).thenReturn(Material.OAK_LOG); Mockito.when(blockState.getType()).thenReturn(Material.OAK_LOG);
woodcuttingManager.processBonusDropCheck(blockState); woodcuttingManager.processBonusDropCheck(blockState);