From ce67d0e4f10a589d8c447c7cbde29f6647962a69 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 18 Aug 2020 17:31:05 +0200 Subject: [PATCH] Improve PersonalKitCreator Signed-off-by: Lixfel --- .../de/steamwar/fightsystem/FightSystem.java | 2 +- .../de/steamwar/fightsystem/commands/GUI.java | 6 +- .../src/de/steamwar/fightsystem/kit/Kit.java | 32 ++- .../listener/FreezeWorldStateListener.java | 1 - .../listener/HotbarGUIListener.java | 73 +++---- .../listener/PersonalKitCreator.java | 187 ++++++++++++++++++ .../fightsystem/utils/PersonalKitCreator.java | 135 ------------- 7 files changed, 256 insertions(+), 180 deletions(-) create mode 100644 FightSystem_Main/src/de/steamwar/fightsystem/listener/PersonalKitCreator.java delete mode 100644 FightSystem_Main/src/de/steamwar/fightsystem/utils/PersonalKitCreator.java diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java index 98706f7..c472997 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java @@ -74,6 +74,7 @@ public class FightSystem extends JavaPlugin { new TestListener(); new NormalJoinListener(); new RankedJoinListener(); + new PersonalKitCreator(); if(Core.getVersion() > 8) new ExtendedInventoryListener(); @@ -154,7 +155,6 @@ public class FightSystem extends JavaPlugin { throw new SecurityException(fightState.name()); setFightState(FightState.PRE_RUNNING); - PersonalKitCreator.closeAll(); Fight.getBlueTeam().loadKits(); Fight.getRedTeam().loadKits(); setAllPlayersGM(GameMode.SURVIVAL); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/commands/GUI.java b/FightSystem_Main/src/de/steamwar/fightsystem/commands/GUI.java index bb2fc80..6a9382f 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/commands/GUI.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/commands/GUI.java @@ -1,14 +1,14 @@ package de.steamwar.fightsystem.commands; +import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.fight.Fight; import de.steamwar.fightsystem.fight.FightPlayer; import de.steamwar.fightsystem.fight.FightTeam; import de.steamwar.fightsystem.kit.Kit; import de.steamwar.fightsystem.kit.KitManager; -import de.steamwar.fightsystem.Config; +import de.steamwar.fightsystem.listener.PersonalKitCreator; import de.steamwar.fightsystem.states.FightState; -import de.steamwar.fightsystem.utils.PersonalKitCreator; import de.steamwar.inventory.SWInventory; import de.steamwar.inventory.SWItem; import de.steamwar.inventory.SWListInv; @@ -78,7 +78,7 @@ public class GUI { return; if(Config.PersonalKits){ - new PersonalKitCreator(p); + PersonalKitCreator.openKitCreator(p); return; } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/kit/Kit.java b/FightSystem_Main/src/de/steamwar/fightsystem/kit/Kit.java index 9ab9a64..410e522 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/kit/Kit.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/kit/Kit.java @@ -5,13 +5,12 @@ import de.steamwar.fightsystem.commands.GUI; import de.steamwar.inventory.SWInventory; import de.steamwar.inventory.SWItem; import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; -import java.util.Collection; -import java.util.Iterator; -import java.util.Objects; +import java.util.*; public class Kit { @@ -74,20 +73,41 @@ public class Kit { return enterStage; } - public boolean isStackInKit(ItemStack stack){ + public boolean isEnchantmentInKit(ItemStack stack){ for(ItemStack is : inventory){ - if(stack.equals(is)) + if(similar(stack, is)) return true; } if(armor != null){ for(ItemStack is : armor){ - if(stack.equals(is)) + if(similar(stack, is)) return true; } } return false; } + private boolean similar(ItemStack stack, ItemStack stack2){ + if(stack == null || stack2 == null) + return false; + if(stack.getType() != stack2.getType()) + return false; + if(stack.hasItemMeta() != stack2.hasItemMeta()) + return false; + if(stack.getItemMeta() == null || stack2.getItemMeta() == null) + return true; + + //Enchantment Map comparison used for default similarity check does not work + Map en = stack.getItemMeta().getEnchants(); + Map en2 = new HashMap<>(stack.getItemMeta().getEnchants()); + + for(Map.Entry e : en.entrySet()){ + if(!en2.remove(e.getKey(), e.getValue())) + return false; + } + return en2.isEmpty(); + } + public void loadToPlayer(Player player) { player.getInventory().setContents(inventory); if(armor != null) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/listener/FreezeWorldStateListener.java b/FightSystem_Main/src/de/steamwar/fightsystem/listener/FreezeWorldStateListener.java index b8547af..1a73a18 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/listener/FreezeWorldStateListener.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/listener/FreezeWorldStateListener.java @@ -2,7 +2,6 @@ package de.steamwar.fightsystem.listener; import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.states.FightState; -import de.steamwar.fightsystem.utils.PersonalKitCreator; import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/listener/HotbarGUIListener.java b/FightSystem_Main/src/de/steamwar/fightsystem/listener/HotbarGUIListener.java index 244e2f4..80741a1 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/listener/HotbarGUIListener.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/listener/HotbarGUIListener.java @@ -22,44 +22,49 @@ public class HotbarGUIListener extends BasicListener { public void handlePlayerInteract(PlayerInteractEvent event) { Player player = event.getPlayer(); - if(event.getItem() != null) { - FightTeam fightTeam = Fight.getPlayerTeam(player); - if(fightTeam == null) - return; + if(!PersonalKitCreator.notInKitCreator(player)) + return; - event.setCancelled(true); - ItemMeta itemMeta = event.getItem().getItemMeta(); - String displayName = itemMeta.getDisplayName(); + if(event.getItem() == null) + return; - if(displayName == null) - return; + FightTeam fightTeam = Fight.getPlayerTeam(player); + if(fightTeam == null) + return; - if(displayName.equals("§e" + Config.GameName + " wählen")){ - GUI.preSchemDialog(player); - return; - } + event.setCancelled(true); + ItemMeta itemMeta = event.getItem().getItemMeta(); + String displayName = itemMeta.getDisplayName(); + + if(displayName == null) + return; + + if(displayName.equals("§e" + Config.GameName + " wählen")){ + GUI.preSchemDialog(player); + return; + } + + switch (displayName) { + case "§cSpieler rauswerfen": + GUI.chooseRemove(player); + break; + case "§eSpieler einladen": + GUI.chooseInvitation(player); + break; + case "§cNicht bereit": + fightTeam.setReady(true); + break; + case "§aBereit": + fightTeam.setReady(false); + break; + case "§eKit bearbeiten": + case "§eKit wählen": + GUI.kitSelection(player); + break; + case "§eRespawn": + player.teleport(fightTeam.getSpawn()); + break; - switch (displayName) { - case "§cSpieler rauswerfen": - GUI.chooseRemove(player); - break; - case "§eSpieler einladen": - GUI.chooseInvitation(player); - break; - case "§cNicht bereit": - fightTeam.setReady(true); - break; - case "§aBereit": - fightTeam.setReady(false); - break; - case "§eKit bearbeiten": - case "§eKit wählen": - GUI.kitSelection(player); - break; - case "§eRespawn": - player.teleport(fightTeam.getSpawn()); - break; - } } } } \ No newline at end of file diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/listener/PersonalKitCreator.java b/FightSystem_Main/src/de/steamwar/fightsystem/listener/PersonalKitCreator.java new file mode 100644 index 0000000..81523f1 --- /dev/null +++ b/FightSystem_Main/src/de/steamwar/fightsystem/listener/PersonalKitCreator.java @@ -0,0 +1,187 @@ +package de.steamwar.fightsystem.listener; + +import de.steamwar.core.Core; +import de.steamwar.fightsystem.Config; +import de.steamwar.fightsystem.FightSystem; +import de.steamwar.fightsystem.fight.Fight; +import de.steamwar.fightsystem.fight.FightPlayer; +import de.steamwar.fightsystem.kit.Kit; +import de.steamwar.fightsystem.kit.KitManager; +import de.steamwar.fightsystem.states.FightState; +import de.steamwar.sql.PersonalKit; +import de.steamwar.sql.SteamwarUser; +import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.Bukkit; +import org.bukkit.GameMode; +import org.bukkit.entity.HumanEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.inventory.InventoryAction; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; + +import java.util.EnumSet; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.logging.Level; + +public class PersonalKitCreator extends BasicListener { + + private static final Map openKitCreators = new HashMap<>(); + private static final EnumSet enabled = Config.PersonalKits ? EnumSet.of(FightState.PRE_LEADER_SETUP, FightState.PRE_SCHEM_SETUP, FightState.POST_SCHEM_SETUP) : EnumSet.noneOf(FightState.class); + + public PersonalKitCreator(){ + super(enabled); + } + + public static void openKitCreator(Player player){ + if(!enabled.contains(FightSystem.getFightState())) + return; + + new InventoryBackup(player); + + FightPlayer fightPlayer = Fight.getFightPlayer(player); + assert fightPlayer != null; + + PersonalKit kit = PersonalKit.get(SteamwarUser.get(player.getUniqueId()).getId(), Config.SchematicType.toDB()); + if(kit == null){ + fightPlayer.getKit().loadToPlayer(player); + }else{ + player.getInventory().setContents(kit.getInventory()); + player.getInventory().setArmorContents(kit.getArmor()); + } + + player.updateInventory(); + player.setGameMode(GameMode.CREATIVE); + } + + static boolean notInKitCreator(HumanEntity player){ + return !openKitCreators.containsKey(player); + } + + @Override + public void disable(){ + while(!openKitCreators.isEmpty()){ + openKitCreators.values().iterator().next().player.closeInventory(); + } + super.disable(); + } + + @EventHandler + public void onInventoryClick(InventoryClickEvent e){ + if(!openKitCreators.containsKey(e.getWhoClicked())) + return; + + Player player = (Player) e.getWhoClicked(); + //Deny bad items + if(isBadItem(e.getCursor(), player)) + e.setCancelled(true); + + checkForClear(e, player); + } + + @EventHandler + public void onMoveEvent(PlayerMoveEvent e){ + if(!openKitCreators.containsKey(e.getPlayer())) + return; + + e.setCancelled(true); + toActionbar(e.getPlayer(), TextComponent.fromLegacyText("§eInventar zum Anpassen des Kits öffnen§8!")); + } + + @EventHandler + public void onInventoryClose(InventoryCloseEvent e) { + InventoryBackup backup = openKitCreators.get(e.getPlayer()); + if(backup == null) + return; + + backup.close(); + } + + @EventHandler + public void onPlayerExit(PlayerQuitEvent e){ + InventoryBackup backup = openKitCreators.get(e.getPlayer()); + if(backup == null) + return; + + backup.close(); + } + + private void checkForClear(InventoryClickEvent e, Player player){ + if(e.getAction() != InventoryAction.PLACE_ALL) + return; + + ItemStack[] items = e.getWhoClicked().getInventory().getContents(); + for(int i = 0; i < items.length; i++){ + ItemStack stack = items[i]; + if(stack != null && i != e.getSlot()) + return; + } + + FightPlayer fightPlayer = Fight.getFightPlayer(player); + assert fightPlayer != null; + Bukkit.getScheduler().runTaskLater(FightSystem.getPlugin(), () -> fightPlayer.getKit().loadToPlayer(player), 1); + } + + private static boolean isBadItem(ItemStack stack, Player player){ + if(stack == null) + return false; + + //Check for forbidden item + if(Config.ForbiddenItems.contains(stack.getType().name())) + return true; + + //Check for attribute modifiers + if(Core.getVersion() >= 14 && stack.hasItemMeta() && Objects.requireNonNull(stack.getItemMeta()).hasAttributeModifiers()){ + Bukkit.getLogger().log(Level.SEVERE, "Spieler " + player.getName() + " hat versucht ein Item mit einem Attribute-Modifier zu bekommen."); + return true; + } + + Kit normal = KitManager.getKitByName(Config.MemberDefault); + assert normal != null; + return !normal.isEnchantmentInKit(stack) && !stack.getEnchantments().isEmpty(); + } + + private static class InventoryBackup{ + private final Player player; + private final ItemStack[] contents; + private final ItemStack[] armor; + + private InventoryBackup(Player player){ + openKitCreators.put(player, this); + this.player = player; + this.contents = player.getInventory().getContents(); + this.armor = player.getInventory().getArmorContents(); + } + + private void loadBackup(){ + PlayerInventory inventory = player.getInventory(); + inventory.setContents(contents); + inventory.setArmorContents(armor); + player.updateInventory(); + } + + private void close(){ + openKitCreators.remove(player); + PersonalKit.save(SteamwarUser.get(player.getUniqueId()).getId(), Config.SchematicType.toDB(), removeBadItems(player.getInventory().getContents()), removeBadItems(player.getInventory().getArmorContents())); + loadBackup(); + player.setGameMode(GameMode.SURVIVAL); + } + + private ItemStack[] removeBadItems(ItemStack[] inventory){ + Kit normal = KitManager.getKitByName(Config.MemberDefault); + assert normal != null; + + for(int i = 0; i < inventory.length; i++){ + if(isBadItem(inventory[i], player)) + inventory[i] = null; + } + return inventory; + } + } +} diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/utils/PersonalKitCreator.java b/FightSystem_Main/src/de/steamwar/fightsystem/utils/PersonalKitCreator.java deleted file mode 100644 index ba2fc60..0000000 --- a/FightSystem_Main/src/de/steamwar/fightsystem/utils/PersonalKitCreator.java +++ /dev/null @@ -1,135 +0,0 @@ -package de.steamwar.fightsystem.utils; - -import de.steamwar.core.Core; -import de.steamwar.fightsystem.Config; -import de.steamwar.fightsystem.FightSystem; -import de.steamwar.fightsystem.fight.Fight; -import de.steamwar.fightsystem.fight.FightPlayer; -import de.steamwar.fightsystem.kit.Kit; -import de.steamwar.fightsystem.kit.KitManager; -import de.steamwar.sql.PersonalKit; -import de.steamwar.sql.SteamwarUser; -import org.bukkit.Bukkit; -import org.bukkit.GameMode; -import org.bukkit.entity.HumanEntity; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.HandlerList; -import org.bukkit.event.Listener; -import org.bukkit.event.inventory.InventoryAction; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.inventory.InventoryCloseEvent; -import org.bukkit.event.player.PlayerMoveEvent; -import org.bukkit.inventory.ItemStack; - -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import java.util.logging.Level; - -public class PersonalKitCreator implements Listener { - - private static final Map openKitCreators = new HashMap<>(); - - private final Player player; - private final SteamwarUser user; - private final ItemStack[] contents; - private final ItemStack[] armor; - - public PersonalKitCreator(Player player){ - this.player = player; - contents = player.getInventory().getContents(); - armor = player.getInventory().getArmorContents(); - user = SteamwarUser.get(player.getUniqueId()); - - PersonalKit kit = PersonalKit.get(user.getId(), Config.SchematicType.toDB()); - if(kit == null){ - FightPlayer fightPlayer = Fight.getFightPlayer(player); - assert fightPlayer != null; - fightPlayer.getKit().loadToPlayer(player); - }else{ - player.getInventory().setContents(kit.getInventory()); - player.getInventory().setArmorContents(kit.getArmor()); - } - - player.setGameMode(GameMode.CREATIVE); - Bukkit.getPluginManager().registerEvents(this, FightSystem.getPlugin()); - openKitCreators.put(player, this); - } - - public static void closeAll(){ - while(!openKitCreators.isEmpty()){ - openKitCreators.values().iterator().next().player.closeInventory(); - } - } - - public static boolean notInKitCreator(HumanEntity player){ - return !openKitCreators.containsKey(player); - } - - @EventHandler - public void onInventoryClick(InventoryClickEvent e){ - if(!player.equals(e.getWhoClicked())) - return; - - //Deny bad items - if(e.getCursor() != null){ - ItemStack stack = e.getCursor(); - if(Config.ForbiddenItems.contains(stack.getType().name())){ - e.setCancelled(true); - return; - } - - Kit normal = KitManager.getKitByName(Config.MemberDefault); - if(!normal.isStackInKit(stack)){ - if(stack.getEnchantments().size() > 0){ - e.setCancelled(true); - return; - } - if(Core.getVersion() >= 14 && stack.hasItemMeta() && Objects.requireNonNull(stack.getItemMeta()).hasAttributeModifiers()){ - e.setCancelled(true); - Bukkit.getLogger().log(Level.SEVERE, "Spieler " + player.getName() + " hat versucht ein Item mit einem Attribute-Modifier zu bekommen."); - return; - } - } - } - - //Check for clear - if(e.getAction() != InventoryAction.PLACE_ALL) - return; - ItemStack[] items = e.getWhoClicked().getInventory().getContents(); - for(int i = 0; i < items.length; i++){ - ItemStack stack = items[i]; - if(stack != null && i != e.getSlot()) - return; - } - FightPlayer fightPlayer = Fight.getFightPlayer(player); - assert fightPlayer != null; - Bukkit.getScheduler().runTaskLater(FightSystem.getPlugin(), () -> fightPlayer.getKit().loadToPlayer(player), 1); - } - - @EventHandler - public void onMoveEvent(PlayerMoveEvent e){ - if(!player.equals(e.getPlayer())) - return; - - e.setCancelled(true); - } - - @EventHandler - public void onInventoryClose(InventoryCloseEvent e) { - if(!player.equals(e.getPlayer())) - return; - - close(); - } - - private void close(){ - PersonalKit.save(user.getId(), Config.SchematicType.toDB(), player.getInventory().getContents(), player.getInventory().getArmorContents()); - player.getInventory().setContents(contents); - player.getInventory().setArmorContents(armor); - player.setGameMode(GameMode.SURVIVAL); - HandlerList.unregisterAll(this); - openKitCreators.remove(player); - } -} -- 2.39.2