mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-09-27 14:59:09 +02:00
Roll rework / refactor Fixes #5023
This commit is contained in:
@@ -15,11 +15,16 @@ import com.gmail.nossr50.util.*;
|
||||
import com.gmail.nossr50.util.blockmeta.ChunkManager;
|
||||
import com.gmail.nossr50.util.compat.CompatibilityManager;
|
||||
import com.gmail.nossr50.util.platform.MinecraftGameVersion;
|
||||
import com.gmail.nossr50.util.player.NotificationManager;
|
||||
import com.gmail.nossr50.util.player.UserManager;
|
||||
import com.gmail.nossr50.util.skills.RankUtils;
|
||||
import com.gmail.nossr50.util.skills.SkillTools;
|
||||
import com.gmail.nossr50.util.sounds.SoundManager;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.attribute.Attribute;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.inventory.ItemFactory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
@@ -44,6 +49,8 @@ public abstract class MMOTestEnvironment {
|
||||
protected MockedStatic<Misc> mockedMisc;
|
||||
protected MockedStatic<SkillTools> mockedSkillTools;
|
||||
protected MockedStatic<EventUtils> mockedEventUtils;
|
||||
protected MockedStatic<NotificationManager> notificationManager;
|
||||
protected MockedStatic<SoundManager> mockedSoundManager;
|
||||
protected TransientEntityTracker transientEntityTracker;
|
||||
protected AdvancedConfig advancedConfig;
|
||||
protected PartyConfig partyConfig;
|
||||
@@ -63,7 +70,6 @@ public abstract class MMOTestEnvironment {
|
||||
protected PlayerInventory playerInventory;
|
||||
protected PlayerProfile playerProfile;
|
||||
protected McMMOPlayer mmoPlayer;
|
||||
protected String playerName = "testPlayer";
|
||||
protected ItemFactory itemFactory;
|
||||
|
||||
protected ChunkManager chunkManager;
|
||||
@@ -124,14 +130,25 @@ public abstract class MMOTestEnvironment {
|
||||
this.server = mock(Server.class);
|
||||
when(mcMMO.p.getServer()).thenReturn(server);
|
||||
|
||||
// wire Bukkit
|
||||
mockedBukkit = mockStatic(Bukkit.class);
|
||||
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
|
||||
itemFactory = mock(ItemFactory.class);
|
||||
// when(itemFactory.getItemMeta(any())).thenReturn(mock(ItemMeta.class));
|
||||
|
||||
// wire Bukkit call to get server
|
||||
when(Bukkit.getServer()).thenReturn(server);
|
||||
|
||||
// wire plugin manager
|
||||
this.pluginManager = mock(PluginManager.class);
|
||||
// wire server -> plugin manager
|
||||
when(server.getPluginManager()).thenReturn(pluginManager);
|
||||
// wire Bukkit -> plugin manager
|
||||
when(Bukkit.getPluginManager()).thenReturn(pluginManager);
|
||||
// return the argument provided when call event is invoked on plugin manager mock
|
||||
doAnswer(invocation -> {
|
||||
Object[] args = invocation.getArguments();
|
||||
return args[0];
|
||||
}).when(pluginManager).callEvent(any(Event.class));
|
||||
|
||||
// wire world
|
||||
this.world = mock(World.class);
|
||||
@@ -143,10 +160,19 @@ public abstract class MMOTestEnvironment {
|
||||
// setup player and player related mocks after everything else
|
||||
this.player = mock(Player.class);
|
||||
when(player.getUniqueId()).thenReturn(playerUUID);
|
||||
|
||||
when(player.isValid()).thenReturn(true);
|
||||
when(player.isOnline()).thenReturn(true);
|
||||
// health
|
||||
when(player.getHealth()).thenReturn(20D);
|
||||
// wire inventory
|
||||
this.playerInventory = mock(PlayerInventory.class);
|
||||
when(player.getInventory()).thenReturn(playerInventory);
|
||||
// player location
|
||||
Location playerLocation = mock(Location.class);
|
||||
Block playerLocationBlock = mock(Block.class);
|
||||
when(player.getLocation()).thenReturn(playerLocation);
|
||||
when(playerLocation.getBlock()).thenReturn(playerLocationBlock);
|
||||
// when(playerLocationBlock.getType()).thenReturn(Material.AIR);
|
||||
|
||||
// PlayerProfile and McMMOPlayer are partially mocked
|
||||
playerProfile = new PlayerProfile("testPlayer", player.getUniqueId(), 0);
|
||||
@@ -158,6 +184,12 @@ public abstract class MMOTestEnvironment {
|
||||
|
||||
this.materialMapStore = new MaterialMapStore();
|
||||
when(mcMMO.getMaterialMapStore()).thenReturn(materialMapStore);
|
||||
|
||||
// wire notification manager
|
||||
notificationManager = mockStatic(NotificationManager.class);
|
||||
|
||||
// wire sound manager
|
||||
mockedSoundManager = mockStatic(SoundManager.class);
|
||||
}
|
||||
|
||||
private void mockPermissions() {
|
||||
@@ -201,7 +233,7 @@ public abstract class MMOTestEnvironment {
|
||||
when(ExperienceConfig.getInstance().getCombatXP("Cow")).thenReturn(1D);
|
||||
}
|
||||
|
||||
protected void cleanupBaseEnvironment() {
|
||||
protected void cleanUpStaticMocks() {
|
||||
// Clean up resources here if needed.
|
||||
if (mockedMcMMO != null) {
|
||||
mockedMcMMO.close();
|
||||
@@ -230,5 +262,11 @@ public abstract class MMOTestEnvironment {
|
||||
if (mockedBukkit != null) {
|
||||
mockedBukkit.close();
|
||||
}
|
||||
if (notificationManager != null) {
|
||||
notificationManager.close();
|
||||
}
|
||||
if (mockedSoundManager != null) {
|
||||
mockedSoundManager.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -12,12 +12,13 @@ import org.mockito.Mockito;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class PartyManagerTest extends MMOTestEnvironment {
|
||||
private static final Logger logger = Logger.getLogger(PartyManagerTest.class.getName());
|
||||
private static final Logger logger = getLogger(PartyManagerTest.class.getName());
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
@@ -29,7 +30,7 @@ class PartyManagerTest extends MMOTestEnvironment {
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
cleanupBaseEnvironment();
|
||||
cleanUpStaticMocks();
|
||||
|
||||
// disable parties in config for other tests
|
||||
Mockito.when(partyConfig.isPartyEnabled()).thenReturn(false);
|
||||
|
@@ -0,0 +1,101 @@
|
||||
package com.gmail.nossr50.skills.acrobatics;
|
||||
|
||||
import com.gmail.nossr50.MMOTestEnvironment;
|
||||
import com.gmail.nossr50.api.exceptions.InvalidSkillException;
|
||||
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
|
||||
import com.gmail.nossr50.datatypes.skills.SubSkillType;
|
||||
import com.gmail.nossr50.datatypes.skills.subskills.AbstractSubSkill;
|
||||
import com.gmail.nossr50.datatypes.skills.subskills.acrobatics.Roll;
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.util.skills.RankUtils;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class AcrobaticsTest extends MMOTestEnvironment {
|
||||
private static final Logger logger = getLogger(AcrobaticsTest.class.getName());
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws InvalidSkillException {
|
||||
mockBaseEnvironment(logger);
|
||||
when(rankConfig.getSubSkillUnlockLevel(SubSkillType.ACROBATICS_ROLL, 1)).thenReturn(1);
|
||||
when(rankConfig.getSubSkillUnlockLevel(SubSkillType.ACROBATICS_DODGE, 1)).thenReturn(1);
|
||||
|
||||
// wire advanced config
|
||||
when(advancedConfig.getMaximumProbability(SubSkillType.ACROBATICS_ROLL)).thenReturn(100D);
|
||||
when(advancedConfig.getMaxBonusLevel(SubSkillType.ACROBATICS_ROLL)).thenReturn(1000);
|
||||
when(advancedConfig.getRollDamageThreshold()).thenReturn(7D);
|
||||
|
||||
Mockito.when(RankUtils.getRankUnlockLevel(SubSkillType.ACROBATICS_ROLL, 1)).thenReturn(1); // needed?
|
||||
Mockito.when(RankUtils.getRankUnlockLevel(SubSkillType.ACROBATICS_DODGE, 1)).thenReturn(1000); // needed?
|
||||
|
||||
when(RankUtils.getRankUnlockLevel(SubSkillType.ACROBATICS_ROLL, 1)).thenReturn(1); // needed?
|
||||
when(RankUtils.hasReachedRank(eq(1), any(Player.class), eq(SubSkillType.ACROBATICS_ROLL))).thenReturn(true);
|
||||
when(RankUtils.hasReachedRank(eq(1), any(Player.class), any(AbstractSubSkill.class))).thenReturn(true);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
cleanUpStaticMocks();
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public void rollShouldLowerDamage() {
|
||||
// Given
|
||||
final Roll roll = new Roll();
|
||||
final double damage = 2D;
|
||||
final EntityDamageEvent mockEvent = mockEntityDamageEvent(damage);
|
||||
mmoPlayer.modifySkill(PrimarySkillType.ACROBATICS, 1000);
|
||||
when(roll.canRoll(mmoPlayer)).thenReturn(true);
|
||||
assertThat(roll.canRoll(mmoPlayer)).isTrue();
|
||||
|
||||
// When
|
||||
roll.doInteraction(mockEvent, mcMMO.p);
|
||||
|
||||
// Then
|
||||
verify(mockEvent, atLeastOnce()).setDamage(0);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Test
|
||||
public void rollShouldNotLowerDamage() {
|
||||
// Given
|
||||
final Roll roll = new Roll();
|
||||
final double damage = 100D;
|
||||
final EntityDamageEvent mockEvent = mockEntityDamageEvent(damage);
|
||||
mmoPlayer.modifySkill(PrimarySkillType.ACROBATICS, 0);
|
||||
when(roll.canRoll(mmoPlayer)).thenReturn(true);
|
||||
assertThat(roll.canRoll(mmoPlayer)).isTrue();
|
||||
|
||||
// When
|
||||
roll.doInteraction(mockEvent, mcMMO.p);
|
||||
|
||||
// Then
|
||||
assertThat(roll.canRoll(mmoPlayer)).isTrue();
|
||||
verify(mockEvent, Mockito.never()).setDamage(any(Double.class));
|
||||
}
|
||||
|
||||
private @NotNull EntityDamageEvent mockEntityDamageEvent(double damage) {
|
||||
final EntityDamageEvent mockEvent = mock(EntityDamageEvent.class);
|
||||
when(mockEvent.getCause()).thenReturn(EntityDamageEvent.DamageCause.FALL);
|
||||
when(mockEvent.getFinalDamage()).thenReturn(damage);
|
||||
when(mockEvent.getDamage(any(EntityDamageEvent.DamageModifier.class))).thenReturn(damage);
|
||||
when(mockEvent.getDamage()).thenReturn(damage);
|
||||
when(mockEvent.isCancelled()).thenReturn(false);
|
||||
when(mockEvent.getEntity()).thenReturn(player);
|
||||
return mockEvent;
|
||||
}
|
||||
}
|
@@ -13,7 +13,6 @@ import org.bukkit.block.BlockState;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -29,7 +28,6 @@ import static org.mockito.Mockito.*;
|
||||
class ExcavationTest extends MMOTestEnvironment {
|
||||
private static final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(ExcavationTest.class.getName());
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws InvalidSkillException {
|
||||
mockBaseEnvironment(logger);
|
||||
@@ -48,18 +46,13 @@ class ExcavationTest extends MMOTestEnvironment {
|
||||
when(player.getUniqueId()).thenReturn(playerUUID);
|
||||
|
||||
// wire inventory
|
||||
this.playerInventory = Mockito.mock(PlayerInventory.class);
|
||||
this.itemInMainHand = new ItemStack(Material.DIAMOND_SHOVEL);
|
||||
when(player.getInventory()).thenReturn(playerInventory);
|
||||
when(playerInventory.getItemInMainHand()).thenReturn(itemInMainHand);
|
||||
|
||||
// Set up spy for Excavation Manager
|
||||
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
cleanupBaseEnvironment();
|
||||
cleanUpStaticMocks();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@@ -10,8 +10,12 @@ import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
|
||||
class TridentsTest extends MMOTestEnvironment {
|
||||
private static final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(TridentsTest.class.getName());
|
||||
private static final Logger logger = getLogger(TridentsTest.class.getName());
|
||||
|
||||
TridentsManager tridentsManager;
|
||||
ItemStack trident;
|
||||
@@ -34,6 +38,6 @@ class TridentsTest extends MMOTestEnvironment {
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
cleanupBaseEnvironment();
|
||||
cleanUpStaticMocks();
|
||||
}
|
||||
}
|
||||
|
@@ -11,21 +11,23 @@ import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
|
||||
class WoodcuttingTest extends MMOTestEnvironment {
|
||||
private static final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(WoodcuttingTest.class.getName());
|
||||
private static final Logger logger = getLogger(WoodcuttingTest.class.getName());
|
||||
|
||||
private WoodcuttingManager woodcuttingManager;
|
||||
|
||||
WoodcuttingManager woodcuttingManager;
|
||||
@BeforeEach
|
||||
void setUp() throws InvalidSkillException {
|
||||
mockBaseEnvironment(logger);
|
||||
@@ -42,12 +44,7 @@ class WoodcuttingTest extends MMOTestEnvironment {
|
||||
Mockito.when(RankUtils.hasReachedRank(eq(1), any(Player.class), eq(SubSkillType.WOODCUTTING_HARVEST_LUMBER))).thenReturn(true);
|
||||
Mockito.when(RankUtils.hasReachedRank(eq(1), any(Player.class), eq(SubSkillType.WOODCUTTING_CLEAN_CUTS))).thenReturn(true);
|
||||
|
||||
// setup player and player related mocks after everything else
|
||||
this.player = Mockito.mock(Player.class);
|
||||
Mockito.when(player.getUniqueId()).thenReturn(playerUUID);
|
||||
|
||||
// wire inventory
|
||||
this.playerInventory = Mockito.mock(PlayerInventory.class);
|
||||
this.itemInMainHand = new ItemStack(Material.DIAMOND_AXE);
|
||||
Mockito.when(player.getInventory()).thenReturn(playerInventory);
|
||||
Mockito.when(playerInventory.getItemInMainHand()).thenReturn(itemInMainHand);
|
||||
@@ -58,7 +55,7 @@ class WoodcuttingTest extends MMOTestEnvironment {
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
cleanupBaseEnvironment();
|
||||
cleanUpStaticMocks();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@@ -38,7 +38,7 @@ class ProbabilityUtilTest extends MMOTestEnvironment {
|
||||
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
cleanupBaseEnvironment();
|
||||
cleanUpStaticMocks();
|
||||
}
|
||||
|
||||
private static Stream<Arguments> staticChanceSkills() {
|
||||
|
Reference in New Issue
Block a user