diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/FightSystem.java b/FightSystem_Core/src/de/steamwar/fightsystem/FightSystem.java index e438033..dc7a867 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/FightSystem.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/FightSystem.java @@ -67,7 +67,6 @@ public class FightSystem extends JavaPlugin { new Permanent(); new PistonListener(); new Chat(); - new HotbarGUI(); new ArenaBorder(); new TeamArea(); new IngameDeath(); diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightSchematic.java b/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightSchematic.java index 6dd1cdc..0d3398c 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightSchematic.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightSchematic.java @@ -103,12 +103,6 @@ public class FightSchematic extends StateDependent { setSchematic(publics.get(new Random().nextInt(publics.size()))); } - if(!ArenaMode.Test.contains(Config.mode)){ - FightPlayer leader = team.getLeader(); - if(leader != null) - leader.getPlayer().getInventory().clear(0); - } - if(ArenaMode.AntiReplay.contains(Config.mode)) { if(team.isBlue()) GlobalRecorder.getInstance().blueSchem(schematic); diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightTeam.java b/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightTeam.java index 8071f7c..f0ea6bd 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightTeam.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/fight/FightTeam.java @@ -23,6 +23,7 @@ import com.sk89q.worldedit.extent.clipboard.Clipboard; import de.steamwar.fightsystem.ArenaMode; import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.FightSystem; +import de.steamwar.fightsystem.commands.GUI; import de.steamwar.fightsystem.countdown.Countdown; import de.steamwar.fightsystem.listener.FightScoreboard; import de.steamwar.fightsystem.listener.PersonalKitCreator; @@ -38,7 +39,6 @@ import net.md_5.bungee.api.ChatMessageType; import org.bukkit.*; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; -import org.bukkit.inventory.Inventory; import org.bukkit.scoreboard.NameTagVisibility; import org.bukkit.scoreboard.Team; @@ -47,6 +47,39 @@ import java.util.*; public class FightTeam { + private static void setKitButton(HotbarKit kit, boolean leader) { + if (Kit.getAvailableKits(leader).size() > 1 || Config.PersonalKits) + kit.setItem(1, "CHOOSE_KIT", new ItemBuilder(Material.LEATHER_CHESTPLATE).removeAllAttributes().addEnchantment(Enchantment.DURABILITY, 1).build(), player -> GUI.kitSelection(player, "")); + else + kit.setItem(1, null, null, null); + } + + private static final HotbarKit memberKit = new HotbarKit(); + static { + setKitButton(memberKit, false); + memberKit.setItem(7, "RESPAWN", new ItemBuilder(Material.BEACON).removeAllAttributes().build(), player -> player.teleport(Objects.requireNonNull(Fight.getPlayerTeam(player)).getSpawn())); + } + private static final HotbarKit notReadyKit = new HotbarKit(memberKit); + static { + setKitButton(notReadyKit, true); + + if(!ArenaMode.RankedEvent.contains(Config.mode)){ + notReadyKit.setItem(2, "INVITE_PLAYERS", new ItemBuilder(Material.PAPER).removeAllAttributes().build(), GUI::chooseInvitation); + notReadyKit.setItem(3, "REMOVE_PLAYERS", new ItemBuilder(SWItem.getMaterial("FIREWORK_CHARGE")).removeAllAttributes().build(), GUI::chooseRemove); + } + + notReadyKit.setItem(4, "TEAM_NOT_READY", new ItemBuilder(SWItem.getDye(10), (short) 10).removeAllAttributes().addEnchantment(Enchantment.DURABILITY, 1).build(), player -> Objects.requireNonNull(Fight.getPlayerTeam(player)).setReady(true)); + } + private static final HotbarKit chooseSchemKit = new HotbarKit(notReadyKit); + static { + chooseSchemKit.setItem(4, "CHOOSE_SCHEMATIC", new ItemBuilder(SWItem.getMaterial("CAULDRON_ITEM")).removeAllAttributes().addEnchantment(Enchantment.DURABILITY, 1).build(), GUI::preSchemDialog); + } + private static final HotbarKit readyKit = new HotbarKit(memberKit); + static { + readyKit.setItem(1, null); + readyKit.setItem(4, "TEAM_READY", new ItemBuilder(SWItem.getDye(8), (short) 8).removeAllAttributes().addEnchantment(Enchantment.DURABILITY,1 ).build(), player -> Objects.requireNonNull(Fight.getPlayerTeam(player)).setReady(false)); + } + private UUID designatedLeader; private FightPlayer leader; private int schemRank; @@ -69,7 +102,7 @@ public class FightTeam { private final Region extendRegion; @SuppressWarnings("deprecation") - public FightTeam(String name, String prefix, Location spawn, Region schemRegion, Region extendRegion, boolean rotate, boolean blue, UUID designatedLeader) { + public FightTeam(String name, String prefix, Location spawn, Region schemRegion, Region extendRegion, boolean rotate, boolean blue, UUID designatedLeader) { //TODO: Static TeamConfig object this.spawn = spawn; this.schemRegion = schemRegion; this.extendRegion = extendRegion; @@ -95,6 +128,10 @@ public class FightTeam { new OneShotStateDependent(ArenaMode.Restartable, FightState.PreLeaderSetup, () -> Bukkit.getScheduler().runTask(FightSystem.getPlugin(), this::reset)); new OneShotStateDependent(Config.replayserver(), FightState.PreLeaderSetup, () -> Bukkit.getScheduler().runTask(FightSystem.getPlugin(), this::reset)); + new OneShotStateDependent(ArenaMode.AntiTest, FightState.PostSchemSetup, () -> { + if(leader != null) + notReadyKit.loadToPlayer(leader.getPlayer()); + }); } public void setPrefixAndName(String prefix, String name){ @@ -216,9 +253,7 @@ public class FightTeam { player.getInventory().clear(); BountifulWrapper.impl.setAttackSpeed(player); player.teleport(spawn); - if(Kit.getAvailableKits(false).size() > 1 || Config.PersonalKits) - player.getInventory().setItem(1, new ItemBuilder(Material.LEATHER_CHESTPLATE).removeAllAttributes().addEnchantment(Enchantment.DURABILITY, 1).setDisplayName(FightSystem.getMessage().parse("CHOOSE_KIT", player)).build()); - player.getInventory().setItem(7, new ItemBuilder(Material.BEACON).removeAllAttributes().setDisplayName(FightSystem.getMessage().parse("RESPAWN", player)).build()); + memberKit.loadToPlayer(player); GlobalRecorder.getInstance().playerJoins(player); TechHider.reloadChunks(player, chunksToReload, false); @@ -293,21 +328,10 @@ public class FightTeam { leader.setKit(Kit.getKitByName(Config.LeaderDefault)); Player player = leader.getPlayer(); - Inventory inventory = leader.getPlayer().getInventory(); - if (Kit.getAvailableKits(true).size() > 1 || Config.PersonalKits) - inventory.setItem(1, new ItemBuilder(Material.LEATHER_CHESTPLATE).removeAllAttributes().addEnchantment(Enchantment.DURABILITY, 1).setDisplayName(FightSystem.getMessage().parse("CHOOSE_KIT", player)).build()); - else - inventory.setItem(1, new ItemBuilder(Material.AIR).build()); - - if(!ArenaMode.RankedEvent.contains(Config.mode)){ - inventory.setItem(2, new ItemBuilder(Material.PAPER).removeAllAttributes().setDisplayName(FightSystem.getMessage().parse("INVITE_PLAYERS", player)).build()); - inventory.setItem(3, new ItemBuilder(SWItem.getMaterial("FIREWORK_CHARGE")).removeAllAttributes().setDisplayName(FightSystem.getMessage().parse("REMOVE_PLAYERS", player)).build()); - } - - inventory.setItem(4, new ItemBuilder(SWItem.getDye(10), (short) 10).removeAllAttributes().addEnchantment(Enchantment.DURABILITY, 1).setDisplayName(FightSystem.getMessage().parse("TEAM_NOT_READY", player)).build()); - if(Config.test() || FightState.getFightState() != FightState.POST_SCHEM_SETUP) - inventory.setItem(0, new ItemBuilder(SWItem.getMaterial("CAULDRON_ITEM")).removeAllAttributes().addEnchantment(Enchantment.DURABILITY, 1).setDisplayName(FightSystem.getMessage().parse("CHOOSE_SCHEMATIC", player, Config.GameName)).build()); + chooseSchemKit.loadToPlayer(player); + else + notReadyKit.loadToPlayer(player); if(FightState.getFightState() == FightState.PRE_LEADER_SETUP && !Fight.getOpposite(this).isLeaderless()){ FightState.setFightState(FightState.PRE_SCHEM_SETUP); @@ -363,13 +387,13 @@ public class FightTeam { this.ready = ready; if(ready) { - l.getInventory().setItem(4, new ItemBuilder(SWItem.getDye(8), (short) 8).removeAllAttributes().addEnchantment(Enchantment.DURABILITY,1 ).setDisplayName(FightSystem.getMessage().parse("TEAM_READY", l)).build()); broadcast("TEAM_READY"); + readyKit.loadToPlayer(l); if(Fight.getOpposite(this).isReady() || ArenaMode.SoloLeader.contains(Config.mode)) FightState.setFightState(FightState.PRE_RUNNING); } else { - l.getInventory().setItem(4, new ItemBuilder(SWItem.getDye(10), (short) 10).removeAllAttributes().addEnchantment(Enchantment.DURABILITY, 1).setDisplayName(FightSystem.getMessage().parse("TEAM_NOT_READY", l)).build()); broadcast("TEAM_NOT_READY"); + notReadyKit.loadToPlayer(l); } } @@ -450,8 +474,6 @@ public class FightTeam { PersonalKitCreator.closeIfInKitCreator(player); player.closeInventory(); - player.getInventory().clear(); - Fight.setPlayerGamemode(player, GameMode.SURVIVAL); fightPlayer.getKit().loadToPlayer(player); } } diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/fight/HotbarKit.java b/FightSystem_Core/src/de/steamwar/fightsystem/fight/HotbarKit.java new file mode 100644 index 0000000..a3ec49d --- /dev/null +++ b/FightSystem_Core/src/de/steamwar/fightsystem/fight/HotbarKit.java @@ -0,0 +1,87 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2022 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ + +package de.steamwar.fightsystem.fight; + +import de.steamwar.fightsystem.ArenaMode; +import de.steamwar.fightsystem.Config; +import de.steamwar.fightsystem.FightSystem; +import de.steamwar.fightsystem.listener.PersonalKitCreator; +import de.steamwar.fightsystem.states.FightState; +import de.steamwar.fightsystem.states.StateDependentListener; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; + +import java.util.Collection; +import java.util.Objects; +import java.util.function.Consumer; + +public class HotbarKit extends Kit implements Listener { + + private static final int HOTBAR_SIZE = 9; + + private final String[] nameTags; + private final Consumer[] onClicks; + + protected HotbarKit(String name, ItemStack[] inventory, ItemStack[] armor, Collection effects, String[] nameTags, Consumer[] onClicks) { + super(name, inventory, armor, effects); + this.nameTags = nameTags; + this.onClicks = onClicks; + new StateDependentListener(ArenaMode.AntiReplay, FightState.Setup, this); + } + + public HotbarKit() { + this(null, new ItemStack[HOTBAR_SIZE], null, null, new String[HOTBAR_SIZE], new Consumer[HOTBAR_SIZE]); + } + + public HotbarKit(HotbarKit kit) { + this(kit.getName(), kit.getInventory().clone(), kit.getArmor().clone(), kit.getEffects(), kit.nameTags.clone(), kit.onClicks.clone()); + } + + public void setItem(int id, String nameTag, ItemStack stack, Consumer onClick) { + super.setItem(id, stack); + nameTags[id] = nameTag; + onClicks[id] = onClick; + } + + @Override + public synchronized void loadToPlayer(Player player) { + for(int i = 0; i < HOTBAR_SIZE; i++) { + if(nameTags[i] != null) + Objects.requireNonNull(getInventory()[i].getItemMeta()).setDisplayName(FightSystem.getMessage().parse(nameTags[i], player, Config.GameName)); + } + super.loadToPlayer(player); + } + + @EventHandler + public void handlePlayerInteract(PlayerInteractEvent event) { + Player player = event.getPlayer(); + int slot = player.getInventory().getHeldItemSlot(); + Kit activeKit = activeKits.get(player); + if(activeKit != this || PersonalKitCreator.inKitCreator(player) || getInventory()[slot] == null) + return; + + event.setCancelled(true); + onClicks[slot].accept(player); + } +} diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/fight/Kit.java b/FightSystem_Core/src/de/steamwar/fightsystem/fight/Kit.java index 89bdcf0..50fcd69 100644 --- a/FightSystem_Core/src/de/steamwar/fightsystem/fight/Kit.java +++ b/FightSystem_Core/src/de/steamwar/fightsystem/fight/Kit.java @@ -50,6 +50,8 @@ public class Kit { private static final File kits = new File(FightSystem.getPlugin().getDataFolder(), Config.KitFile); private static final ArrayList loadedKits = new ArrayList<>(); + protected static final Map activeKits = new HashMap<>(); + static { if(!kits.exists()) { Bukkit.getLogger().log(Level.SEVERE, "Kitconfig fehlend!" + kits.getAbsolutePath()); @@ -72,17 +74,29 @@ public class Kit { private final boolean leaderAllowed; private final boolean memberAllowed; - public Kit(String name, Player player) { + protected Kit(String name, ItemStack[] inventory, ItemStack[] armor, Collection effects) { this.name = name; - this.inventory = player.getInventory().getContents(); - this.armor = player.getInventory().getArmorContents(); - this.effects = player.getActivePotionEffects(); + this.inventory = inventory; + this.armor = armor; + this.effects = effects; this.leaderAllowed = true; this.memberAllowed = true; this.enterStage = 0; this.tnt = true; } + protected Kit(Kit kit) { + this(kit.name, kit.inventory.clone(), kit.armor, kit.effects); + } + + public Kit(String name, Player player) { + this(name, player.getInventory().getContents(), player.getInventory().getArmorContents(), player.getActivePotionEffects()); + } + + public Kit(PersonalKit kit){ + this(kit.getName(), kit.getInventory(), kit.getArmor(), Collections.emptyList()); + } + public Kit(ConfigurationSection kit){ name = kit.getName(); inventory = Objects.requireNonNull(kit.getList("Items")).toArray(new ItemStack[0]); @@ -100,15 +114,8 @@ public class Kit { tnt = kit.getBoolean("TNT", true); } - public Kit(PersonalKit kit){ - this.name = kit.getName(); - this.inventory = kit.getInventory(); - this.armor = kit.getArmor(); - this.effects = Collections.emptyList(); - this.leaderAllowed = true; - this.memberAllowed = true; - this.enterStage = 0; - this.tnt = true; + protected void setItem(int id, ItemStack stack) { + inventory[id] = stack; } public static Kit getKitByName(String kitName) { @@ -153,6 +160,10 @@ public class Kit { return armor; } + public Collection getEffects() { + return effects; + } + /* Is this kit allowed to set/handle tnt? */ public boolean isTnt(){ return tnt; @@ -241,6 +252,9 @@ public class Kit { } public void loadToPlayer(Player player) { + activeKits.put(player, this); + + player.getInventory().clear(); player.getInventory().setContents(inventory); if(armor != null) player.getInventory().setArmorContents(armor); diff --git a/FightSystem_Core/src/de/steamwar/fightsystem/listener/HotbarGUI.java b/FightSystem_Core/src/de/steamwar/fightsystem/listener/HotbarGUI.java deleted file mode 100644 index 125b17f..0000000 --- a/FightSystem_Core/src/de/steamwar/fightsystem/listener/HotbarGUI.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.fightsystem.listener; - -import de.steamwar.fightsystem.ArenaMode; -import de.steamwar.fightsystem.Config; -import de.steamwar.fightsystem.FightSystem; -import de.steamwar.fightsystem.commands.GUI; -import de.steamwar.fightsystem.fight.Fight; -import de.steamwar.fightsystem.fight.FightTeam; -import de.steamwar.fightsystem.states.FightState; -import de.steamwar.fightsystem.states.StateDependentListener; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.meta.ItemMeta; - -public class HotbarGUI implements Listener { - - public HotbarGUI() { - new StateDependentListener(ArenaMode.AntiReplay, FightState.Setup, this); - } - - @EventHandler - public void handlePlayerInteract(PlayerInteractEvent event) { - Player player = event.getPlayer(); - - if(PersonalKitCreator.inKitCreator(player)) - return; - - if(event.getItem() == null) - return; - - FightTeam fightTeam = Fight.getPlayerTeam(player); - if(fightTeam == null) - return; - - ItemMeta itemMeta = event.getItem().getItemMeta(); - String displayName = itemMeta.getDisplayName(); - - if(displayName == null) - return; - - event.setCancelled(true); - onMatch(player, displayName, "CHOOSE_SCHEMATIC", () -> GUI.preSchemDialog(player), Config.GameName); - onMatch(player, displayName, "INVITE_PLAYERS", () -> GUI.chooseInvitation(player)); - onMatch(player, displayName, "REMOVE_PLAYERS", () -> GUI.chooseRemove(player)); - onMatch(player, displayName, "TEAM_NOT_READY", () -> fightTeam.setReady(true)); - onMatch(player, displayName, "TEAM_READY", () -> fightTeam.setReady(false)); - onMatch(player, displayName, "CHOOSE_KIT", () -> GUI.kitSelection(player, "")); - onMatch(player, displayName, "RESPAWN", () -> player.teleport(fightTeam.getSpawn())); - } - - private void onMatch(Player player, String displayName, String message, Runnable run, Object... params) { - if(displayName.equals(FightSystem.getMessage().parse(message, player, params))) - run.run(); - } -}