Java Code Examples for org.bukkit.event.entity.PlayerDeathEvent#setKeepInventory()

The following examples show how to use org.bukkit.event.entity.PlayerDeathEvent#setKeepInventory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlayerDeathListenerTest.java    From PerWorldInventory with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldRemoveInventory() {
    // given
    Player player = mock(Player.class);
    given(player.getInventory()).willReturn(mock(PlayerInventory.class));
    given(player.getActivePotionEffects()).willReturn(new ArrayList<>());
    Location loc = mock(Location.class);
    World world = mock(World.class);

    given(player.getLocation()).willReturn(loc);
    given(loc.getWorld()).willReturn(world);
    given(world.getName()).willReturn("test");

    Group group = mockGroup("test");
    given(groupManager.getGroupFromWorld("test")).willReturn(group);

    List<ItemStack> drops = new ArrayList<>();
    PlayerDeathEvent event = new PlayerDeathEvent(player, drops, 5, 5, "Bob died.");
    event.setKeepInventory(false);

    // when
    listener.onPlayerDeath(event);

    // then
    verify(player).getInventory();
}
 
Example 2
Source File: PlayerDeathListenerTest.java    From PerWorldInventory with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldNotRemoveInventory() {
    // given
    Player player = mock(Player.class);
    given(player.getActivePotionEffects()).willReturn(new ArrayList<>());
    Location loc = mock(Location.class);
    World world = mock(World.class);

    given(player.getLocation()).willReturn(loc);
    given(loc.getWorld()).willReturn(world);
    given(world.getName()).willReturn("test");

    Group group = mockGroup("test");
    given(groupManager.getGroupFromWorld("test")).willReturn(group);

    List<ItemStack> drops = new ArrayList<>();
    PlayerDeathEvent event = new PlayerDeathEvent(player, drops, 5, 5, "Bob died.");
    event.setKeepInventory(true);

    // when
    listener.onPlayerDeath(event);

    // then
    verify(player, never()).getInventory();
}
 
Example 3
Source File: ItemDropEvents.java    From uSkyBlock with GNU General Public License v3.0 6 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
@SuppressWarnings("unused")
public void onDeathEvent(PlayerDeathEvent event) {
    Player player = event.getEntity();
    if (!plugin.getWorldManager().isSkyWorld(player.getWorld())) {
        return;
    }
    if (!visitorsCanDrop && !plugin.playerIsOnIsland(player) && !plugin.playerIsInSpawn(player)) {
        event.setKeepInventory(true);
        return;
    }
    // Take over the drop, since Bukkit don't do this in a Metadatable format.
    for (ItemStack stack : event.getDrops()) {
        addDropInfo(player, stack);
    }
}
 
Example 4
Source File: PlayerDeathListenerTest.java    From PerWorldInventory with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldRemoveInventory() {
    // given
    Player player = mock(Player.class);
    given(player.getInventory()).willReturn(mock(PlayerInventory.class));
    given(player.getActivePotionEffects()).willReturn(new ArrayList<>());
    Location loc = mock(Location.class);
    World world = mock(World.class);

    given(player.getLocation()).willReturn(loc);
    given(loc.getWorld()).willReturn(world);
    given(world.getName()).willReturn("test");

    Group group = mockGroup("test");
    given(groupManager.getGroupFromWorld("test")).willReturn(group);

    List<ItemStack> drops = new ArrayList<>();
    PlayerDeathEvent event = new PlayerDeathEvent(player, drops, 5, 5, "Bob died.");
    event.setKeepInventory(false);

    // when
    listener.onPlayerDeath(event);

    // then
    verify(player).getInventory();
}
 
Example 5
Source File: PlayerDeathListenerTest.java    From PerWorldInventory with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void shouldNotRemoveInventory() {
    // given
    Player player = mock(Player.class);
    given(player.getActivePotionEffects()).willReturn(new ArrayList<>());
    Location loc = mock(Location.class);
    World world = mock(World.class);

    given(player.getLocation()).willReturn(loc);
    given(loc.getWorld()).willReturn(world);
    given(world.getName()).willReturn("test");

    Group group = mockGroup("test");
    given(groupManager.getGroupFromWorld("test")).willReturn(group);

    List<ItemStack> drops = new ArrayList<>();
    PlayerDeathEvent event = new PlayerDeathEvent(player, drops, 5, 5, "Bob died.");
    event.setKeepInventory(true);

    // when
    listener.onPlayerDeath(event);

    // then
    verify(player, never()).getInventory();
}
 
Example 6
Source File: PlayerEventHandler.java    From GriefDefender with MIT License 5 votes vote down vote up
@EventHandler(priority = EventPriority.LOWEST)
public void onPlayerDeath(PlayerDeathEvent event) {
    final Player player = event.getEntity();
    GDCauseStackManager.getInstance().pushCause(player);
    final GDPlayerData playerData = GriefDefenderPlugin.getInstance().dataStore.getOrCreatePlayerData(player.getWorld(), player.getUniqueId());
    final GDClaim claim = GriefDefenderPlugin.getInstance().dataStore.getClaimAtPlayer(playerData, player.getLocation());
    Tristate keepInventory = Tristate.UNDEFINED;
    if (GDOptions.isOptionEnabled(Options.PLAYER_KEEP_INVENTORY)) {
        keepInventory = GDPermissionManager.getInstance().getInternalOptionValue(TypeToken.of(Tristate.class), playerData.getSubject(), Options.PLAYER_KEEP_INVENTORY, claim);
    }
    Tristate keepLevel = Tristate.UNDEFINED;
    if (GDOptions.isOptionEnabled(Options.PLAYER_KEEP_LEVEL)) {
        keepLevel = GDPermissionManager.getInstance().getInternalOptionValue(TypeToken.of(Tristate.class), playerData.getSubject(), Options.PLAYER_KEEP_LEVEL, claim);
    }
    if (keepInventory != Tristate.UNDEFINED) {
        event.setKeepInventory(keepInventory.asBoolean());
        if (keepInventory == Tristate.TRUE) {
            event.getDrops().clear();
        }
    }
    if (keepLevel != Tristate.UNDEFINED) {
        event.setKeepLevel(keepLevel.asBoolean());
        if (keepLevel == Tristate.TRUE) {
            event.setDroppedExp(0);
        }
    }
}
 
Example 7
Source File: EffKeepInventory.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void execute(Event e) {
	if (e instanceof PlayerDeathEvent) {
		PlayerDeathEvent event = (PlayerDeathEvent) e;
		if (keepItems)
			event.setKeepInventory(true);
		if (keepExp)
			event.setKeepLevel(true);
	}
}
 
Example 8
Source File: PlayerListener.java    From WorldGuardExtraFlagsPlugin with MIT License 5 votes vote down vote up
@EventHandler
public void onPlayerDeathEvent(PlayerDeathEvent event)
{
	Player player = event.getEntity();
	
	ApplicableRegionSet regions = this.plugin.getWorldGuardCommunicator().getRegionContainer().createQuery().getApplicableRegions(player.getLocation());
	
	Boolean keepInventory = WorldGuardUtils.queryValue(player, player.getWorld(), regions.getRegions(), Flags.KEEP_INVENTORY);
	if (keepInventory != null)
	{
		event.setKeepInventory(keepInventory);
		
		if (keepInventory)
		{
			event.getDrops().clear();
		}
	}
	
	Boolean keepExp = WorldGuardUtils.queryValue(player, player.getWorld(), regions.getRegions(), Flags.KEEP_EXP);
	if (keepExp != null)
	{
		event.setKeepLevel(keepExp);

		if (keepExp)
		{
			event.setDroppedExp(0);
		}
	}
}
 
Example 9
Source File: PlayerListener.java    From BedwarsRel with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerDie(PlayerDeathEvent pde) {
  final Player player = pde.getEntity();
  Game game = BedwarsRel.getInstance().getGameManager().getGameOfPlayer(player);

  if (game == null) {
    return;
  }

  if (game.getState() == GameState.RUNNING) {
    pde.setDroppedExp(0);
    pde.setDeathMessage(null);

    if (!BedwarsRel.getInstance().getBooleanConfig("player-drops", false)) {
      pde.getDrops().clear();
    }

    try {
      if (!BedwarsRel.getInstance().isSpigot()) {
        Class<?> clazz = null;
        try {
          clazz = Class.forName("io.github.bedwarsrel.com."
              + BedwarsRel.getInstance().getCurrentVersion().toLowerCase()
              + ".PerformRespawnRunnable");
        } catch (ClassNotFoundException ex) {
          BedwarsRel.getInstance().getBugsnag().notify(ex);
          clazz = Class
              .forName("io.github.bedwarsrel.com.fallback.PerformRespawnRunnable");
        }

        BukkitRunnable respawnRunnable =
            (BukkitRunnable) clazz.getDeclaredConstructor(Player.class).newInstance(player);
        respawnRunnable.runTaskLater(BedwarsRel.getInstance(), 20L);
      } else {
        new BukkitRunnable() {

          @Override
          public void run() {
            player.spigot().respawn();
          }
        }.runTaskLater(BedwarsRel.getInstance(), 20L);
      }

    } catch (Exception e) {
      BedwarsRel.getInstance().getBugsnag().notify(e);
      e.printStackTrace();
    }

    pde.setKeepInventory(
        BedwarsRel.getInstance().getBooleanConfig("keep-inventory-on-death", false));

    Player killer = player.getKiller();
    if (killer == null) {
      killer = game.getPlayerDamager(player);
    }

    game.getCycle().onPlayerDies(player, killer);
  }
}