Added death listener.

Counts player deaths in any game world.

Fixes https://github.com/BentoBoxWorld/addon-level/issues/18
This commit is contained in:
tastybento 2018-08-22 23:46:24 -04:00
parent 9b1c7f10ab
commit 474de77ce2
3 changed files with 104 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.commands.BentoBoxCommand;
import world.bentobox.bentobox.listeners.BannedVisitorCommands;
import world.bentobox.bentobox.listeners.BlockEndDragon;
import world.bentobox.bentobox.listeners.DeathListener;
import world.bentobox.bentobox.listeners.JoinLeaveListener;
import world.bentobox.bentobox.listeners.NetherPortals;
import world.bentobox.bentobox.listeners.ObsidianToLava;
@ -156,6 +157,8 @@ public class BentoBox extends JavaPlugin {
manager.registerEvents(new BlockEndDragon(this), this);
// Banned visitor commands
manager.registerEvents(new BannedVisitorCommands(this), this);
// Death counter
manager.registerEvents(new DeathListener(this), this);
}
@Override

View File

@ -0,0 +1,33 @@
/**
*
*/
package world.bentobox.bentobox.listeners;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import world.bentobox.bentobox.BentoBox;
/**
* Counts deaths in game worlds
* @author tastybento
*
*/
public class DeathListener implements Listener {
private BentoBox plugin;
public DeathListener(BentoBox plugin) {
super();
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPlayerDeathEvent(PlayerDeathEvent e) {
if (plugin.getIWM().inWorld(e.getEntity().getLocation())) {
plugin.getPlayers().addDeath(e.getEntity().getWorld(), e.getEntity().getUniqueId());
}
}
}

View File

@ -0,0 +1,68 @@
package world.bentobox.bentobox.listeners;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.util.Util;
@RunWith(PowerMockRunner.class)
@PrepareForTest({BentoBox.class, Util.class, Bukkit.class })
public class DeathListenerTest {
@Test
public void testDeathListener() {
assertNotNull(new DeathListener(mock(BentoBox.class)));
}
@Test
public void testOnPlayerDeathEvent() {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.inWorld(Mockito.any())).thenReturn(true);
when(iwm.getPermissionPrefix(Mockito.any())).thenReturn("bskyblock");
when(iwm.getVisitorBannedCommands(Mockito.any())).thenReturn(new ArrayList<>());
when(plugin.getIWM()).thenReturn(iwm);
// Player
Player player = mock(Player.class);
World world = mock(World.class);
when(player.getWorld()).thenReturn(world);
when(player.getLocation()).thenReturn(mock(Location.class));
UUID uuid = UUID.randomUUID();
when(player.getUniqueId()).thenReturn(uuid);
PlayersManager pm = mock(PlayersManager.class);
when(plugin.getPlayers()).thenReturn(pm);
// Test
DeathListener dl = new DeathListener(plugin);
PlayerDeathEvent e = new PlayerDeathEvent(player, new ArrayList<>(), 0, 0, 0, 0, "died");
dl.onPlayerDeathEvent(e);
Mockito.verify(pm).addDeath(world, uuid);
}
}