1
0
mirror of https://github.com/BentoBoxWorld/Warps.git synced 2024-11-25 03:55:25 +01:00

Call events correctly. #131

This commit is contained in:
tastybento 2024-06-05 14:09:02 -07:00
parent 519a2d05fc
commit efc6b795d1
3 changed files with 45 additions and 3 deletions

View File

@ -15,7 +15,7 @@ import world.bentobox.warps.Warp;
* @author Poslovitch
*
*/
public class WarpCreateEvent extends Event{
public class WarpCreateEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private final Location warpLoc;

View File

@ -38,6 +38,7 @@ import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util;
import world.bentobox.warps.Warp;
import world.bentobox.warps.event.WarpCreateEvent;
import world.bentobox.warps.event.WarpInitiateEvent;
import world.bentobox.warps.objects.WarpsData;
import world.bentobox.warps.panels.Utils;
@ -105,7 +106,7 @@ public class WarpSignsManager {
}
getWarpMap(loc.getWorld()).put(playerUUID, loc);
saveWarpList();
Bukkit.getPluginManager().callEvent(new WarpInitiateEvent(addon, loc, playerUUID));
Bukkit.getPluginManager().callEvent(new WarpCreateEvent(addon, loc, playerUUID));
return true;
}
@ -350,6 +351,11 @@ public class WarpSignsManager {
float yaw = Util.blockFaceToFloat(directionFacing);
final Location actualWarp = new Location(inFront.getWorld(), inFront.getBlockX() + 0.5D, inFront.getBlockY(),
inFront.getBlockZ() + 0.5D, yaw, 30F);
WarpInitiateEvent e = new WarpInitiateEvent(addon, actualWarp, user.getUniqueId());
Bukkit.getPluginManager().callEvent(e);
if (e.isCancelled()) {
return;
}
//BentoBox prevents people from teleporting to an island when
//the user is banned from the island for example.
//By checking if the teleport succeeded before sending the messages,

View File

@ -8,7 +8,9 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ -40,6 +42,7 @@ import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
@ -60,6 +63,7 @@ import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.util.Util;
import world.bentobox.warps.config.Settings;
import world.bentobox.warps.event.WarpCreateEvent;
import world.bentobox.warps.event.WarpInitiateEvent;
import world.bentobox.warps.managers.SignCacheManager;
import world.bentobox.warps.managers.WarpSignsManager;
@ -349,7 +353,7 @@ public class WarpSignsManagerTest {
public void testAddWarp() {
Location loc = mock(Location.class);
assertTrue(wsm.addWarp(uuid, loc));
verify(pim).callEvent(any(WarpInitiateEvent.class));
verify(pim).callEvent(any(WarpCreateEvent.class));
}
/**
@ -441,6 +445,38 @@ public class WarpSignsManagerTest {
PowerMockito.verifyStatic(Util.class);
Util.teleportAsync(eq(p), any(), eq(TeleportCause.COMMAND));
verify(player).sendMessage(anyString());
verify(pim).callEvent(any(WarpInitiateEvent.class));
}
/**
* Test method for {@link WarpSignsManager#warpPlayer(org.bukkit.World, world.bentobox.bentobox.api.user.User, java.util.UUID)}.
*/
@Test
public void testWarpPlayerEventCancelled() {
// Capture the event passed to callEvent
ArgumentCaptor<WarpInitiateEvent> eventCaptor = ArgumentCaptor.forClass(WarpInitiateEvent.class);
// Simulate the event being called and cancelled
doAnswer(invocation -> {
WarpInitiateEvent event = (WarpInitiateEvent) invocation.getArgument(0);
event.setCancelled(true);
return null;
}).when(pim).callEvent(eventCaptor.capture());
Player p = mock(Player.class);
when(p.getUniqueId()).thenReturn(UUID.randomUUID());
when(p.getWorld()).thenReturn(world);
when(p.getName()).thenReturn("tastybento");
when(p.getLocation()).thenReturn(location);
when(p.isOnline()).thenReturn(true);
when(p.canSee(any(Player.class))).thenReturn(true);
@Nullable
User u = User.getInstance(p);
PowerMockito.when(Util.teleportAsync(any(), any(), any())).thenReturn(CompletableFuture.completedFuture(true));
wsm.warpPlayer(world, u, uuid);
PowerMockito.verifyStatic(Util.class, never());
Util.teleportAsync(eq(p), any(), eq(TeleportCause.COMMAND));
verify(player, never()).sendMessage(anyString());
}
/**