From 00967a2d3e6d3a5c5e9721e2d626a7615adb2783 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 19 Jan 2020 17:25:11 +0100 Subject: [PATCH 1/4] WIP Adding personal kits Signed-off-by: Lixfel --- .../src/de/steamwar/fightsystem/Config.java | 2 + .../fightsystem/utils/PersonalKitCreator.java | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 FightSystem_Main/src/de/steamwar/fightsystem/utils/PersonalKitCreator.java diff --git a/FightSystem_API/src/de/steamwar/fightsystem/Config.java b/FightSystem_API/src/de/steamwar/fightsystem/Config.java index aa0782d..51f438e 100644 --- a/FightSystem_API/src/de/steamwar/fightsystem/Config.java +++ b/FightSystem_API/src/de/steamwar/fightsystem/Config.java @@ -97,6 +97,7 @@ public class Config { //default kits public static final String MemberDefault; public static final String LeaderDefault; + public static final boolean PersonalKits; //tech hider parameter public static final Set HiddenBlocks; @@ -177,6 +178,7 @@ public class Config { MemberDefault = config.getString("Kits.MemberDefault"); LeaderDefault = config.getString("Kits.LeaderDefault"); + PersonalKits = config.getBoolean("Kits.PersonalKits"); ConfigurationSection techhiderConfig = config.getConfigurationSection("Techhider.HiddenBlocks"); Set blocks = new HashSet<>(); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/utils/PersonalKitCreator.java b/FightSystem_Main/src/de/steamwar/fightsystem/utils/PersonalKitCreator.java new file mode 100644 index 0000000..2a36a20 --- /dev/null +++ b/FightSystem_Main/src/de/steamwar/fightsystem/utils/PersonalKitCreator.java @@ -0,0 +1,46 @@ +package de.steamwar.fightsystem.utils; + +import de.steamwar.fightsystem.FightSystem; +import org.bukkit.Bukkit; +import org.bukkit.GameMode; +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.InventoryCloseEvent; +import org.bukkit.inventory.ItemStack; + +public class PersonalKitCreator implements Listener { + + private final Player player; + private final ItemStack[] contents; + private final ItemStack[] armor; + + public PersonalKitCreator(Player player){ + this.player = player; + contents = player.getInventory().getContents(); + armor = player.getInventory().getArmorContents(); + + //TODO Get Current Kit inventory + //TODO Prevent unwanted items + player.getInventory().setContents(); + player.getInventory().setArmorContents(); + + player.setGameMode(GameMode.CREATIVE); + player.openInventory(this.inventory); + Bukkit.getPluginManager().registerEvents(this, FightSystem.getPlugin()); + } + + @EventHandler + public void onInventoryClose(InventoryCloseEvent e) { + if(!player.equals(e.getPlayer())) + return; + + //TODO Autoclose inventory on PreFightStart + player.closeInventory(); + player.getInventory().setContents(contents); + player.getInventory().setArmorContents(contents); + player.setGameMode(GameMode.SURVIVAL); + HandlerList.unregisterAll(this); + } +} -- 2.39.5 From c5c6e8c32b9df3d6852fdeff97106f04cdb1392a Mon Sep 17 00:00:00 2001 From: Lixfel Date: Thu, 23 Jan 2020 08:31:32 +0100 Subject: [PATCH 2/4] Implement personal kits (untested) Signed-off-by: Lixfel --- .../src/de/steamwar/fightsystem/Config.java | 2 + FightSystem_Main/src/config.yml | 2 + .../de/steamwar/fightsystem/FightSystem.java | 2 + .../de/steamwar/fightsystem/commands/GUI.java | 6 +++ .../steamwar/fightsystem/fight/FightTeam.java | 2 +- .../fightsystem/utils/PersonalKitCreator.java | 49 ++++++++++++++++--- 6 files changed, 55 insertions(+), 8 deletions(-) diff --git a/FightSystem_API/src/de/steamwar/fightsystem/Config.java b/FightSystem_API/src/de/steamwar/fightsystem/Config.java index 51f438e..c362ec4 100644 --- a/FightSystem_API/src/de/steamwar/fightsystem/Config.java +++ b/FightSystem_API/src/de/steamwar/fightsystem/Config.java @@ -98,6 +98,7 @@ public class Config { public static final String MemberDefault; public static final String LeaderDefault; public static final boolean PersonalKits; + public static final List ForbiddenItems; //tech hider parameter public static final Set HiddenBlocks; @@ -179,6 +180,7 @@ public class Config { MemberDefault = config.getString("Kits.MemberDefault"); LeaderDefault = config.getString("Kits.LeaderDefault"); PersonalKits = config.getBoolean("Kits.PersonalKits"); + ForbiddenItems = config.getStringList("Kits.ForbiddenItems"); ConfigurationSection techhiderConfig = config.getConfigurationSection("Techhider.HiddenBlocks"); Set blocks = new HashSet<>(); diff --git a/FightSystem_Main/src/config.yml b/FightSystem_Main/src/config.yml index 3960676..7b64223 100644 --- a/FightSystem_Main/src/config.yml +++ b/FightSystem_Main/src/config.yml @@ -41,6 +41,8 @@ WinConditionParams: Kits: MemberDefault: default LeaderDefault: default + PersonalKits: false + ForbiddenItems: [] Techhider: ObfuscateWith: 121 ObfuscateWithTag: ENDER_STONE diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java index 00766cd..999292d 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java @@ -10,6 +10,7 @@ import de.steamwar.fightsystem.listener.*; import de.steamwar.fightsystem.states.FightState; import de.steamwar.fightsystem.states.StateDependent; import de.steamwar.fightsystem.utils.FightScoreboard; +import de.steamwar.fightsystem.utils.PersonalKitCreator; import de.steamwar.fightsystem.utils.TechHider; import de.steamwar.fightsystem.utils.WaterRemover; import de.steamwar.fightsystem.winconditions.*; @@ -136,6 +137,7 @@ public class FightSystem extends JavaPlugin { throw new SecurityException(fightState.name()); setFightState(FightState.PRE_RUNNING); + PersonalKitCreator.closeAll(); loadKits(Fight.getBlueTeam()); loadKits(Fight.getRedTeam()); 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 dc9fe1d..e58eadf 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/commands/GUI.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/commands/GUI.java @@ -8,6 +8,7 @@ import de.steamwar.fightsystem.kit.Kit; import de.steamwar.fightsystem.kit.KitManager; import de.steamwar.fightsystem.Config; 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; @@ -77,6 +78,11 @@ public class GUI { if(fightPlayer == null) return; + if(Config.PersonalKits){ + new PersonalKitCreator(p); + return; + } + List> iconList = new ArrayList<>(); List kitList = KitManager.getKits(fightPlayer.isLeader()); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java b/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java index 8dc57cf..783a53b 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java @@ -149,7 +149,7 @@ public class FightTeam implements IFightTeam{ Fight.setPlayerGamemode(player, GameMode.SURVIVAL); player.teleport(spawn); - if(KitManager.getKits(false).size() > 1) + if(KitManager.getKits(false).size() > 1 || Config.PersonalKits) player.getInventory().setItem(1, new ItemBuilder(Material.LEATHER_CHESTPLATE).removeAllAttributs().addEnchantment(Enchantment.DURABILITY, 1).setDisplayName("§eKit wählen").build()); player.getInventory().setItem(7, new ItemBuilder(Material.BEACON).removeAllAttributs().setDisplayName("§eRespawn").build()); TechHider.reloadChunks(player, chunksToReload); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/utils/PersonalKitCreator.java b/FightSystem_Main/src/de/steamwar/fightsystem/utils/PersonalKitCreator.java index 2a36a20..8e5e3d9 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/utils/PersonalKitCreator.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/utils/PersonalKitCreator.java @@ -1,18 +1,30 @@ package de.steamwar.fightsystem.utils; +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.sql.PersonalKit; +import de.steamwar.sql.SteamwarUser; import org.bukkit.Bukkit; import org.bukkit.GameMode; 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.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.inventory.ItemStack; +import java.util.HashSet; +import java.util.Set; + public class PersonalKitCreator implements Listener { + private static final Set openKitCreators = new HashSet<>(); + private final Player player; + private final SteamwarUser user; private final ItemStack[] contents; private final ItemStack[] armor; @@ -20,15 +32,33 @@ public class PersonalKitCreator implements Listener { this.player = player; contents = player.getInventory().getContents(); armor = player.getInventory().getArmorContents(); + user = SteamwarUser.get(player.getUniqueId()); - //TODO Get Current Kit inventory - //TODO Prevent unwanted items - player.getInventory().setContents(); - player.getInventory().setArmorContents(); + 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); - player.openInventory(this.inventory); + player.openInventory(player.getInventory()); Bukkit.getPluginManager().registerEvents(this, FightSystem.getPlugin()); + openKitCreators.add(this); + } + + public static void closeAll(){ + for(PersonalKitCreator creator : openKitCreators) + creator.close(); + } + + @EventHandler + public void onInventoryClick(InventoryClickEvent e){ + if(e.getCursor() != null && Config.ForbiddenItems.contains(e.getCursor().getType().name())) + e.setCancelled(true); } @EventHandler @@ -36,11 +66,16 @@ public class PersonalKitCreator implements Listener { if(!player.equals(e.getPlayer())) return; - //TODO Autoclose inventory on PreFightStart + close(); + } + + private void close(){ + PersonalKit.save(user.getId(), Config.SchematicType.toDB(), player.getInventory().getContents(), player.getInventory().getArmorContents()); player.closeInventory(); player.getInventory().setContents(contents); - player.getInventory().setArmorContents(contents); + player.getInventory().setArmorContents(armor); player.setGameMode(GameMode.SURVIVAL); HandlerList.unregisterAll(this); + openKitCreators.remove(this); } } -- 2.39.5 From 0c212517201d92e2345a8b7567e8ff1d24329d24 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Thu, 23 Jan 2020 17:40:48 +0100 Subject: [PATCH 3/4] Loading personalKits to player Signed-off-by: Lixfel --- .../de/steamwar/fightsystem/FightSystem.java | 15 ++----------- .../steamwar/fightsystem/fight/FightTeam.java | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java index 999292d..7f1c098 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java @@ -138,8 +138,8 @@ public class FightSystem extends JavaPlugin { setFightState(FightState.PRE_RUNNING); PersonalKitCreator.closeAll(); - loadKits(Fight.getBlueTeam()); - loadKits(Fight.getRedTeam()); + Fight.getBlueTeam().loadKits(); + Fight.getRedTeam().loadKits(); setAllPlayersGM(GameMode.SURVIVAL); Bukkit.broadcastMessage(PREFIX + "§aDer Kampf beginnt!"); @@ -256,17 +256,6 @@ public class FightSystem extends JavaPlugin { stateDependent.enable(); } - private static void loadKits(FightTeam team){ - for(FightPlayer allFightPlayers : team.getPlayers()) { - if(allFightPlayers.getPlayer() == null) - continue; - allFightPlayers.getPlayer().getInventory().clear(); - if(allFightPlayers.getKit() == null) - continue; - allFightPlayers.getKit().loadToPlayer(allFightPlayers.getPlayer()); - } - } - private static void setAllPlayersGM(GameMode gm) { for(FightPlayer fightPlayer: Fight.getBlueTeam().getPlayers()){ Fight.setPlayerGamemode(fightPlayer.getPlayer(), gm); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java b/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java index 783a53b..a21c46d 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java @@ -13,7 +13,9 @@ import de.steamwar.fightsystem.utils.ItemBuilder; import de.steamwar.fightsystem.utils.TechHider; import de.steamwar.inventory.SWItem; import de.steamwar.sql.NoClipboardException; +import de.steamwar.sql.PersonalKit; import de.steamwar.sql.Schematic; +import de.steamwar.sql.SteamwarUser; import javafx.util.Pair; import org.bukkit.*; import org.bukkit.block.Block; @@ -24,6 +26,7 @@ import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockPhysicsEvent; import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.PlayerInventory; import org.bukkit.scoreboard.NameTagVisibility; import org.bukkit.scoreboard.Team; @@ -353,6 +356,25 @@ public class FightTeam implements IFightTeam{ return currentHearts / maximumHearts; } + public void loadKits(){ + for(FightPlayer fightPlayer : players) { + if(fightPlayer.getPlayer() == null) + continue; + fightPlayer.getPlayer().getInventory().clear(); + + PersonalKit personalKit = null; + if(Config.PersonalKits) + personalKit = PersonalKit.get(SteamwarUser.get(fightPlayer.getPlayer().getUniqueId()).getId(), Config.SchematicType.toDB()); + + if(personalKit != null){ + PlayerInventory inventory = fightPlayer.getPlayer().getInventory(); + inventory.setContents(personalKit.getInventory()); + inventory.setArmorContents(personalKit.getArmor()); + }else if(fightPlayer.getKit() != null) + fightPlayer.getKit().loadToPlayer(fightPlayer.getPlayer()); + } + } + void replaceSync(boolean replace, Material target, Material replacement) { if(!replace) return; -- 2.39.5 From 0ebc7e5616520b3c77c2873343a55ce784737cdb Mon Sep 17 00:00:00 2001 From: Lixfel Date: Thu, 23 Jan 2020 19:57:21 +0100 Subject: [PATCH 4/4] Fixing bugs occured with testing Signed-off-by: Lixfel --- .../steamwar/fightsystem/fight/FightTeam.java | 2 +- .../listener/FreezeWorldStateListener.java | 7 +++-- .../fightsystem/utils/PersonalKitCreator.java | 31 +++++++++++++------ 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java b/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java index a21c46d..8edacca 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java @@ -202,7 +202,7 @@ public class FightTeam implements IFightTeam{ leader.setKit(KitManager.getKitByName(Config.LeaderDefault)); Inventory inventory = leader.getPlayer().getInventory(); - if (KitManager.getKits(true).size() > 1) + if (KitManager.getKits(true).size() > 1 || Config.PersonalKits) inventory.setItem(1, new ItemBuilder(Material.LEATHER_CHESTPLATE).removeAllAttributs().addEnchantment(Enchantment.DURABILITY, 1).setDisplayName("§eKit wählen").build()); else inventory.setItem(1, new ItemBuilder(Material.AIR).build()); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/listener/FreezeWorldStateListener.java b/FightSystem_Main/src/de/steamwar/fightsystem/listener/FreezeWorldStateListener.java index 6f2a400..b7fd1bd 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/listener/FreezeWorldStateListener.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/listener/FreezeWorldStateListener.java @@ -2,6 +2,7 @@ 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; @@ -42,12 +43,14 @@ public class FreezeWorldStateListener extends BasicListener { @EventHandler public void onInventoryClick(InventoryClickEvent event) { - event.setCancelled(true); + if(!PersonalKitCreator.inKitCreator(event.getWhoClicked())) + event.setCancelled(true); } @EventHandler public void onInventoryDrag(InventoryDragEvent event) { - event.setCancelled(true); + if(!PersonalKitCreator.inKitCreator(event.getWhoClicked())) + event.setCancelled(true); } @EventHandler diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/utils/PersonalKitCreator.java b/FightSystem_Main/src/de/steamwar/fightsystem/utils/PersonalKitCreator.java index 8e5e3d9..9811f81 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/utils/PersonalKitCreator.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/utils/PersonalKitCreator.java @@ -8,20 +8,22 @@ 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.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.inventory.ItemStack; -import java.util.HashSet; -import java.util.Set; +import java.util.HashMap; +import java.util.Map; public class PersonalKitCreator implements Listener { - private static final Set openKitCreators = new HashSet<>(); + private static final Map openKitCreators = new HashMap<>(); private final Player player; private final SteamwarUser user; @@ -45,14 +47,18 @@ public class PersonalKitCreator implements Listener { } player.setGameMode(GameMode.CREATIVE); - player.openInventory(player.getInventory()); Bukkit.getPluginManager().registerEvents(this, FightSystem.getPlugin()); - openKitCreators.add(this); + openKitCreators.put(player, this); } public static void closeAll(){ - for(PersonalKitCreator creator : openKitCreators) - creator.close(); + for(PersonalKitCreator creator : openKitCreators.values()){ + creator.player.closeInventory(); + } + } + + public static boolean inKitCreator(HumanEntity player){ + return openKitCreators.containsKey(player); } @EventHandler @@ -61,6 +67,14 @@ public class PersonalKitCreator implements Listener { e.setCancelled(true); } + @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())) @@ -71,11 +85,10 @@ public class PersonalKitCreator implements Listener { private void close(){ PersonalKit.save(user.getId(), Config.SchematicType.toDB(), player.getInventory().getContents(), player.getInventory().getArmorContents()); - player.closeInventory(); player.getInventory().setContents(contents); player.getInventory().setArmorContents(armor); player.setGameMode(GameMode.SURVIVAL); HandlerList.unregisterAll(this); - openKitCreators.remove(this); + openKitCreators.remove(player); } } -- 2.39.5