Skip to content

Commit

Permalink
Merge pull request #2586 from BentoBoxWorld/paper_fixes
Browse files Browse the repository at this point in the history
Paper fixes
  • Loading branch information
tastybento authored Dec 31, 2024
2 parents b3dfd49 + 50380d3 commit a1c552b
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public void setUp() throws Exception {
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
when(location.toVector()).thenReturn(new Vector(0,0,0));
when(location.clone()).thenReturn(location); // Paper

// Players Manager and meta data
PlayersManager pm = mock(PlayersManager.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
* @author tastybento
*
*/
@Ignore("Needs update to work with PaperAPI")
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class , ServerBuildInfo.class})
public class IslandTeamInviteCommandTest extends RanksManagerBeforeClassTest {
Expand Down Expand Up @@ -227,6 +226,7 @@ public void testCanExecuteNoIsland() {
* Test method for
* {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamInviteCommand#canExecute(User, String, java.util.List)}.
*/
@Ignore("PaperAPI Material issue with Material.get")
@Test
public void testCanExecuteNoTarget() {
assertFalse(itl.canExecute(user, itl.getLabel(), Collections.emptyList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
* @author tastybento
*
*/
@Ignore("Paper API update required")
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, Util.class, ServerBuildInfo.class})
public class BlockInteractionListenerTest extends AbstractCommonSetup {
Expand Down Expand Up @@ -130,8 +129,6 @@ private void setFlags() {
}


/**
*/
@Override
@Before
public void setUp() throws Exception {
Expand All @@ -147,7 +144,9 @@ public void setUp() throws Exception {
when(item.getType()).thenReturn(Material.AIR);
when(mockPlayer.getInventory()).thenReturn(inv);
when(inv.getItemInMainHand()).thenReturn(item);
when(inv.getItemInOffHand()).thenReturn(new ItemStack(Material.BUCKET));
ItemStack mockBucket = mock(ItemStack.class);
when(mockBucket.getType()).thenReturn(Material.BUCKET);
when(inv.getItemInOffHand()).thenReturn(mockBucket);

// FlagsManager
setFlags();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
Expand All @@ -45,7 +44,6 @@
* @author tastybento
*
*/
@Ignore("Needs redo for PaperAPI")
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, Util.class , ServerBuildInfo.class})
public class EntityInteractListenerTest extends AbstractCommonSetup {
Expand All @@ -69,7 +67,9 @@ public void setUp() throws Exception {
// Hand - main hand
hand = EquipmentSlot.HAND;
position = new Vector(10, 10, 10);
when(inv.getItemInMainHand()).thenReturn(new ItemStack(Material.NAME_TAG));
ItemStack mockNameTag = mock(ItemStack.class);
when(mockNameTag.getType()).thenReturn(Material.NAME_TAG);
when(inv.getItemInMainHand()).thenReturn(mockNameTag);

// Initialize the Flags class. This is a workaround to prevent weird errors when mocking
// I think it's because the flag class needs to be initialized before use in argument matchers
Expand Down Expand Up @@ -253,7 +253,9 @@ public void testOnPlayerInteractEntityWanderingTraderNoInteraction() {
clickedEntity = mock(WanderingTrader.class);
when(clickedEntity.getLocation()).thenReturn(location);
when(clickedEntity.getType()).thenReturn(EntityType.WANDERING_TRADER);
when(inv.getItemInMainHand()).thenReturn(new ItemStack(Material.STONE));
ItemStack mockStone = mock(ItemStack.class);
when(mockStone.getType()).thenReturn(Material.STONE);
when(inv.getItemInMainHand()).thenReturn(mockStone);
PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(mockPlayer, clickedEntity, hand);
eil.onPlayerInteractEntity(e);
verify(notifier, never()).notify(any(), eq("protection.protected"));
Expand Down Expand Up @@ -319,7 +321,9 @@ public void testOnPlayerInteractEntitySheepAllowed() {
clickedEntity = mock(Sheep.class);
when(clickedEntity.getLocation()).thenReturn(location);
when(clickedEntity.getType()).thenReturn(EntityType.SHEEP);
when(inv.getItemInMainHand()).thenReturn(new ItemStack(Material.AIR));
ItemStack mockAir = mock(ItemStack.class);
when(mockAir.getType()).thenReturn(Material.AIR);
when(inv.getItemInMainHand()).thenReturn(mockAir);
PlayerInteractEntityEvent e = new PlayerInteractEntityEvent(mockPlayer, clickedEntity, hand);
eil.onPlayerInteractEntity(e);
verify(notifier, never()).notify(any(), eq("protection.protected"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.bukkit.scheduler.BukkitScheduler;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
Expand All @@ -56,7 +55,6 @@
import world.bentobox.bentobox.mocks.ServerMocks;
import world.bentobox.bentobox.util.Util;

@Ignore("Needs PaperAPI update")
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, User.class, Util.class , ServerBuildInfo.class})
public class LockAndBanListenerTest {
Expand Down Expand Up @@ -157,6 +155,7 @@ public void setUp() throws Exception {
when(loc.getBlockY()).thenReturn(Y);
when(loc.getBlockZ()).thenReturn(Z);
when(island.getCenter()).thenReturn(loc);
when(loc.clone()).thenReturn(loc);
when(island.getProtectionRange()).thenReturn(PROTECTION_RANGE);
// Island is not locked by default
when(island.isAllowed(any(), any())).thenReturn(true);
Expand All @@ -171,16 +170,19 @@ public void setUp() throws Exception {
when(outside.getBlockX()).thenReturn(X + PROTECTION_RANGE + 1);
when(outside.getBlockY()).thenReturn(Y);
when(outside.getBlockZ()).thenReturn(Z);
when(outside.clone()).thenReturn(outside);

when(inside.getWorld()).thenReturn(world);
when(inside.getBlockX()).thenReturn(X + PROTECTION_RANGE - 1);
when(inside.getBlockY()).thenReturn(Y);
when(inside.getBlockZ()).thenReturn(Z);
when(inside.clone()).thenReturn(inside);

when(inside.getWorld()).thenReturn(world);
when(inside.getBlockX()).thenReturn(X + PROTECTION_RANGE - 2);
when(inside.getBlockY()).thenReturn(Y);
when(inside.getBlockZ()).thenReturn(Z);
when(inside2.getWorld()).thenReturn(world);
when(inside2.getBlockX()).thenReturn(X + PROTECTION_RANGE - 2);
when(inside2.getBlockY()).thenReturn(Y);
when(inside2.getBlockZ()).thenReturn(Z);
when(inside2.clone()).thenReturn(inside2);

Optional<Island> opIsland = Optional.ofNullable(island);
when(im.getProtectedIslandAt(eq(inside))).thenReturn(opIsland);
Expand Down Expand Up @@ -231,6 +233,7 @@ public void testTeleportToBannedIsland() {
assertTrue(e.isCancelled());
}

@SuppressWarnings("deprecation")
@Test
public void testLoginToBannedIsland() {
// Make player
Expand Down Expand Up @@ -285,11 +288,13 @@ public void testVerticalVehicleMoveOnly() {
when(from.getBlockX()).thenReturn(X);
when(from.getBlockY()).thenReturn(50);
when(from.getBlockZ()).thenReturn(Z);
when(from.clone()).thenReturn(from);
Location to = mock(Location.class);
when(to.getWorld()).thenReturn(world);
when(to.getBlockX()).thenReturn(X);
when(to.getBlockY()).thenReturn(55);
when(to.getBlockZ()).thenReturn(Z);
when(to.clone()).thenReturn(to);
// Create vehicle and put two players in it.
Vehicle vehicle = mock(Vehicle.class);
Player player2 = mock(Player.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.bukkit.Bukkit;
Expand All @@ -21,6 +23,8 @@
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.damage.DamageSource;
import org.bukkit.damage.DamageType;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Cow;
import org.bukkit.entity.Entity;
Expand All @@ -33,10 +37,12 @@
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityDamageEvent.DamageModifier;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
Expand All @@ -46,6 +52,8 @@
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.google.common.base.Function;

import io.papermc.paper.ServerBuildInfo;
import world.bentobox.bentobox.AbstractCommonSetup;
import world.bentobox.bentobox.BentoBox;
Expand All @@ -54,7 +62,6 @@
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.util.Util;

@Ignore("PaperAPI changes required to fix errors and failures")
@RunWith(PowerMockRunner.class)
@PrepareForTest({ BentoBox.class, Util.class, Bukkit.class, ServerBuildInfo.class })
public class TNTListenerTest extends AbstractCommonSetup {
Expand Down Expand Up @@ -112,6 +119,7 @@ public void setUp() throws Exception {
}

@Test
@Ignore("PaperAPI error with Material isn't an item issue")
public void testOnTNTPriming() {
BlockFace clickedFace = BlockFace.DOWN;
Block clickedBlock = mock(Block.class);
Expand Down Expand Up @@ -324,8 +332,29 @@ public void testOnTNTDamageInWorldTNTProjectilePlayerFireArrowNotIslandNotAllowe

@Test
public void testOnEntityExplosion() {
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(entity, mockPlayer, DamageCause.ENTITY_EXPLOSION, null,
20D);
/*
* org.bukkit.event.entity.EntityDamageByEntityEvent.EntityDamageByEntityEvent(
* @NotNull @NotNull Entity damager,
* @NotNull @NotNull Entity damagee,
* @NotNull @NotNull DamageCause cause,
* @NotNull @NotNull DamageSource damageSource,
* @NotNull @NotNull Map<DamageModifier, Double> modifiers,
* @NotNull @NotNull Map<DamageModifier, ?> modifierFunctions,
* boolean critical)
Attempt to use newer event. This works but then other errors appear. Go figure.
@NotNull
Map<DamageModifier, Double> modifiers = new HashMap<>();
modifiers.put(DamageModifier.BASE, 0.0D);
@NotNull
Map<DamageModifier, ? extends Function<? super Double, Double>> modifier = new HashMap<>();
modifier.put(DamageModifier.BASE, null);
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(entity, mockPlayer, DamageCause.ENTITY_EXPLOSION,
DamageSource.builder(DamageType.EXPLOSION).build(), modifiers, modifier, false);
*/
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(entity, mockPlayer, DamageCause.ENTITY_EXPLOSION,
null, 20D);
listener.onExplosion(e);
assertTrue(e.isCancelled());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.bukkit.plugin.PluginManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
Expand Down Expand Up @@ -68,7 +67,6 @@
public class ChestDamageListenerTest extends AbstractCommonSetup
{

private Location location;
private BentoBox plugin;
private World world;

Expand Down Expand Up @@ -104,11 +102,7 @@ public void setUp() throws Exception {
when(itemFactory.getItemMeta(any())).thenReturn(skullMeta);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);

PowerMockito.mockStatic(Flags.class);

FlagsManager flagsManager = new FlagsManager(plugin);
Expand Down Expand Up @@ -180,28 +174,37 @@ public void tearDown() throws Exception {
* Test method for {@link ChestDamageListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}.
*/
@Test
@Ignore("Fixes required for failures PaperAPI")
public void testOnExplosionChestDamageNotAllowed() {
// Srt the flag to not allow chest damage
Flags.CHEST_DAMAGE.setSetting(world, false);
// Set the entity that is causing the damage (TNT)
Entity entity = mock(Entity.class);
when(entity.getType()).thenReturn(EntityType.TNT);

// Create a list of blocks that will potentially be damaged by TNT
List<Block> list = new ArrayList<>();
Block chest = mock(Block.class);
when(chest.getType()).thenReturn(Material.CHEST);
when(chest.getType()).thenReturn(Material.CHEST); // Regular chest
when(chest.getLocation()).thenReturn(location);

Block trappedChest = mock(Block.class);
when(trappedChest.getType()).thenReturn(Material.TRAPPED_CHEST);
when(trappedChest.getType()).thenReturn(Material.TRAPPED_CHEST);// Trapped chest
when(trappedChest.getLocation()).thenReturn(location);

Block stone = mock(Block.class);
when(stone.getType()).thenReturn(Material.STONE);
when(stone.getType()).thenReturn(Material.STONE); // Stone
when(stone.getLocation()).thenReturn(location);
list.add(chest);
list.add(trappedChest);
list.add(stone);
// Create the event
EntityExplodeEvent e = getExplodeEvent(entity, location, list);
// Listener to test
ChestDamageListener listener = new ChestDamageListener();
listener.setPlugin(plugin);
listener.onExplosion(e);

// Verify
assertFalse(e.isCancelled());
assertEquals(1, e.blockList().size());
assertFalse(e.blockList().contains(chest));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
Expand Down Expand Up @@ -52,8 +51,6 @@ public class WitherListenerTest extends AbstractCommonSetup {

private WitherListener wl;
@Mock
private Location location;
@Mock
private Location location2;
@Mock
private World world;
Expand Down Expand Up @@ -82,15 +79,11 @@ public void setUp() throws Exception {
when(ws.getWorldFlags()).thenReturn(map);
when(iwm.getWorldSettings(any())).thenReturn(ws);

when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);

when(location2.getWorld()).thenReturn(world2);
when(location2.getBlockX()).thenReturn(0);
when(location2.getBlockY()).thenReturn(0);
when(location2.getBlockZ()).thenReturn(0);
when(location2.clone()).thenReturn(location2); // Paper

blocks = new ArrayList<>();
for (int i = 0; i < 4; i++) {
Expand Down Expand Up @@ -119,12 +112,12 @@ public void tearDown() {
* Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.WitherListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}.
*/
@Test
@Ignore("Fixes required for failures PaperAPI")
public void testOnExplosionWither() {
Entity entity = mock(Entity.class);
when(entity.getLocation()).thenReturn(location);
when(entity.getWorld()).thenReturn(world);
when(entity.getType()).thenReturn(EntityType.WITHER);
when(location.clone()).thenReturn(location);
EntityExplodeEvent e = getExplodeEvent(entity, location, blocks);
wl.onExplosion(e);
assertTrue(blocks.isEmpty());
Expand Down Expand Up @@ -165,12 +158,12 @@ public void testOnExplosionWitherAllowed() {
* Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.WitherListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}.
*/
@Test
@Ignore("Fixes required for failures PaperAPI")
public void testOnExplosionWitherSkull() {
Entity entity = mock(Entity.class);
when(entity.getLocation()).thenReturn(location);
when(entity.getWorld()).thenReturn(world);
when(entity.getType()).thenReturn(EntityType.WITHER_SKULL);
when(location.clone()).thenReturn(location);
EntityExplodeEvent e = getExplodeEvent(entity, location, blocks);
wl.onExplosion(e);
assertTrue(blocks.isEmpty());
Expand Down

0 comments on commit a1c552b

Please sign in to comment.