Fix combat NPC issues

- Cashed out players were treated as combat-logged players
- Player inventories were not cleared before returning items, resulting
  in duplicated items
- Players would be given the "locked slot" item
- Allow multiple stacks of the same type and amount
This commit is contained in:
cnr 2017-02-24 05:49:10 -06:00
parent 68f61bbb20
commit 3858421194
3 changed files with 24 additions and 17 deletions

View File

@ -3,6 +3,7 @@ package mineplex.gemhunters.death.npc;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameMode; import org.bukkit.GameMode;
@ -24,6 +25,7 @@ import mineplex.core.disguise.DisguiseManager;
import mineplex.core.hologram.HologramManager; import mineplex.core.hologram.HologramManager;
import mineplex.core.updater.UpdateType; import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent; import mineplex.core.updater.event.UpdateEvent;
import mineplex.gemhunters.economy.PlayerCashOutCompleteEvent;
import mineplex.minecraft.game.core.damage.CustomDamageEvent; import mineplex.minecraft.game.core.damage.CustomDamageEvent;
public class NPCManager extends MiniPlugin public class NPCManager extends MiniPlugin
@ -39,7 +41,8 @@ public class NPCManager extends MiniPlugin
private Set<CombatLogNPC> _logoutNpcs; private Set<CombatLogNPC> _logoutNpcs;
private Set<Integer> _toKillIds; private Set<Integer> _toKillIds;
private Set<UUID> _cashedOutPreventNPCs = new HashSet<>();
private HologramManager _hologramManager; private HologramManager _hologramManager;
public NPCManager(HologramManager hologramManager) public NPCManager(HologramManager hologramManager)
@ -51,6 +54,12 @@ public class NPCManager extends MiniPlugin
_toKillIds = new HashSet<>(); _toKillIds = new HashSet<>();
_hologramManager = hologramManager; _hologramManager = hologramManager;
} }
@EventHandler
public void onCashOut(PlayerCashOutCompleteEvent event)
{
_cashedOutPreventNPCs.add(event.getPlayer().getUniqueId());
}
@EventHandler @EventHandler
public void playerQuit(PlayerQuitEvent event) public void playerQuit(PlayerQuitEvent event)
@ -73,7 +82,7 @@ public class NPCManager extends MiniPlugin
public void spawnLogoutNpc(Player player) public void spawnLogoutNpc(Player player)
{ {
if (!hasLogoutNpc(player)) if (!_cashedOutPreventNPCs.remove(player.getUniqueId()) && !hasLogoutNpc(player))
{ {
CombatLogNPC npc = new CombatLogNPC(player, require(DisguiseManager.class), _hologramManager, player.getGameMode().equals(GameMode.CREATIVE)); CombatLogNPC npc = new CombatLogNPC(player, require(DisguiseManager.class), _hologramManager, player.getGameMode().equals(GameMode.CREATIVE));
npc.spawn(); npc.spawn();

View File

@ -1,7 +1,7 @@
package mineplex.gemhunters.death.npc; package mineplex.gemhunters.death.npc;
import java.util.HashSet; import java.util.ArrayList;
import java.util.Set; import java.util.List;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -12,12 +12,14 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.PlayerInventory;
import mineplex.gemhunters.loot.InventoryModule;
public class PlayerInfo public class PlayerInfo
{ {
private String _playerName; private String _playerName;
private UUID _playerUuid; private UUID _playerUuid;
private ItemStack[] _armor; private ItemStack[] _armor;
private Set<ItemStack> _items; private List<ItemStack> _items;
private Location _location; private Location _location;
public PlayerInfo(Player player) public PlayerInfo(Player player)
@ -41,7 +43,8 @@ public class PlayerInfo
public void restore() public void restore()
{ {
Player player = getPlayer(); Player player = getPlayer();
player.getInventory().clear();
player.getInventory().setArmorContents(_armor); player.getInventory().setArmorContents(_armor);
player.getInventory().addItem(_items.toArray(new ItemStack[0])); player.getInventory().addItem(_items.toArray(new ItemStack[0]));
player.teleport(_location); player.teleport(_location);
@ -51,12 +54,7 @@ public class PlayerInfo
{ {
return _playerName; return _playerName;
} }
public Set<ItemStack> getItems()
{
return _items;
}
public UUID getUniqueId() public UUID getUniqueId()
{ {
return _playerUuid; return _playerUuid;
@ -72,20 +70,20 @@ public class PlayerInfo
return Bukkit.getPlayerExact(_playerName); return Bukkit.getPlayerExact(_playerName);
} }
private Set<ItemStack> fetchItems(PlayerInventory inventory) private List<ItemStack> fetchItems(PlayerInventory inventory)
{ {
Set<ItemStack> items = new HashSet<ItemStack>(); List<ItemStack> items = new ArrayList<>();
addItems(items, inventory.getContents()); addItems(items, inventory.getContents());
return items; return items;
} }
private void addItems(Set<ItemStack> items, ItemStack[] itemsToAdd) private void addItems(List<ItemStack> items, ItemStack[] itemsToAdd)
{ {
for (ItemStack item : itemsToAdd) for (ItemStack item : itemsToAdd)
{ {
if (item != null && item.getType() != Material.AIR) if (item != null && item.getType() != Material.AIR && !item.isSimilar(InventoryModule.LOCKED))
{ {
items.add(item); items.add(item);
} }

View File

@ -28,8 +28,8 @@ import mineplex.gemhunters.death.event.PlayerCustomRespawnEvent;
public class InventoryModule extends MiniPlugin public class InventoryModule extends MiniPlugin
{ {
public static final ItemStack LOCKED = new ItemBuilder(Material.STAINED_GLASS_PANE, (byte) 15).setTitle(C.cGray + "Locked").build();
private static final int START_INDEX = 9; private static final int START_INDEX = 9;
private static final ItemStack LOCKED = new ItemBuilder(Material.STAINED_GLASS_PANE, (byte) 15).setTitle(C.cGray + "Locked").build();
private static final String ITEM_METADATA = "UNLOCKER"; private static final String ITEM_METADATA = "UNLOCKER";
private final LootModule _loot; private final LootModule _loot;