From 8b73b89a17f71e7f8124edcbf71bc32b75e4c6a1 Mon Sep 17 00:00:00 2001 From: Shaun Bennett Date: Thu, 13 Nov 2014 16:03:14 -0600 Subject: [PATCH] Merging spectator gui with main branch --- .../mineplex/core/common/util/UtilColor.java | 69 +++++++++++++++ .../game/arcade/addons/CompassAddon.java | 58 ++++++++++-- .../src/nautilus/game/arcade/game/Game.java | 2 + .../game/arcade/gui/SpectatorShop.java | 41 +++++++++ .../arcade/gui/button/SpectatorButton.java | 39 ++++++++ .../game/arcade/gui/page/SpectatorPage.java | 88 +++++++++++++++++++ .../arcade/managers/GamePlayerManager.java | 2 +- .../game/arcade/managers/MiscManager.java | 15 +++- 8 files changed, 304 insertions(+), 10 deletions(-) create mode 100644 Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/UtilColor.java create mode 100644 Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/gui/SpectatorShop.java create mode 100644 Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/gui/button/SpectatorButton.java create mode 100644 Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/gui/page/SpectatorPage.java diff --git a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/UtilColor.java b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/UtilColor.java new file mode 100644 index 000000000..e04ddf689 --- /dev/null +++ b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/UtilColor.java @@ -0,0 +1,69 @@ +package mineplex.core.common.util; + +import org.bukkit.ChatColor; + +/** + * Created by Shaun on 11/12/2014. + */ +public class UtilColor +{ + + public static byte chatColorToClayData(ChatColor chatColor) + { + //TODO + return 1; + } + + public static byte chatColorToWoolData(ChatColor chatColor) + { + switch (chatColor) + { + // 0: white + // 1: orange + // 2: magenta + // 3: light blue + // 4: yellow + // 5: lime + // 6: pink + // 7: gray + // 8: light gray + // 9: cyan + // 10: purple + // 11: blue + // 12: brown + // 13: green + // 14: red + // 15: black + case BLACK: + return 1; + case DARK_BLUE: + return 11; + case DARK_GREEN: + return 13; + case DARK_AQUA: + return 9; + case DARK_PURPLE: + return 10; + case GOLD: + return 1; + case GRAY: + return 8; + case DARK_GRAY: + return 7; + case BLUE: + return 11; + case GREEN: + return 5; + case AQUA: + return 3; + case RED: + return 14; + case LIGHT_PURPLE: + return 2; + case YELLOW: + return 4; + default: + return 0; + } + } +} diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/addons/CompassAddon.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/addons/CompassAddon.java index aee607ab3..2d04344ed 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/addons/CompassAddon.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/addons/CompassAddon.java @@ -6,6 +6,8 @@ import org.bukkit.GameMode; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.block.Action; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.player.PlayerDropItemEvent; @@ -28,16 +30,21 @@ import mineplex.core.updater.UpdateType; import mineplex.core.updater.event.UpdateEvent; import nautilus.game.arcade.ArcadeManager; import nautilus.game.arcade.game.GameTeam; +import nautilus.game.arcade.gui.SpectatorShop; public class CompassAddon extends MiniPlugin { public ArcadeManager Manager; + private SpectatorShop _spectatorShop; + public CompassAddon(JavaPlugin plugin, ArcadeManager manager) { super("Compass Addon", plugin); Manager = manager; + + _spectatorShop = new SpectatorShop(this, manager, manager.GetClients(), manager.GetDonation()); } @EventHandler @@ -179,6 +186,7 @@ public class CompassAddon extends MiniPlugin @EventHandler public void SpectatorTeleport(PlayerInteractEvent event) { + System.out.println("interact event"); if (Manager.GetGame() == null) return; @@ -192,12 +200,29 @@ public class CompassAddon extends MiniPlugin event.setCancelled(true); - if (!Recharge.Instance.use(player, "Spectate", 5000, true, false)) + if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK || !Manager.GetGame().CompassSpectatorMenu) { - return; - } + // Teleport to nearest player when you left click compass - GameTeam team = Manager.GetGame().GetTeam(player); + System.out.println("a"); + if (!Recharge.Instance.use(player, "Spectate", 5000, true, false)) + { + return; + } + + spectateNearestPlayer(player); + } + else + { + // Right click - open spectator menu + + _spectatorShop.attemptShopOpen(player); + } + } + + private void spectateNearestPlayer(Player spectator) + { + GameTeam team = Manager.GetGame().GetTeam(spectator); Player target = null; double bestDist = 0; @@ -207,10 +232,10 @@ public class CompassAddon extends MiniPlugin GameTeam otherTeam = Manager.GetGame().GetTeam(other); //Same Team (Not Solo Game) && Alive - if (Manager.GetGame().GetTeamList().size() > 1 && (team != null && team.equals(otherTeam)) && Manager.GetGame().IsAlive(player)) + if (Manager.GetGame().GetTeamList().size() > 1 && (team != null && team.equals(otherTeam)) && Manager.GetGame().IsAlive(spectator)) continue; - double dist = UtilMath.offset(player, other); + double dist = UtilMath.offset(spectator, other); if (target == null || dist < bestDist) { @@ -221,7 +246,26 @@ public class CompassAddon extends MiniPlugin if (target != null) { - player.teleport(target.getLocation().add(0, 1, 0)); + spectator.teleport(target.getLocation().add(0, 1, 0)); + } + } + + @EventHandler + public void updateShop(UpdateEvent event) + { + if (event.getType() != UpdateType.SEC) + return; + + _spectatorShop.update(); + } + + // This prevents other modules from cancelling clicks in + @EventHandler(priority = EventPriority.HIGH) + public void onInventoryClick(InventoryClickEvent event) + { + if (event.getWhoClicked().getGameMode() == GameMode.CREATIVE && event.getClickedInventory().getTitle().equals("Spectate Menu")) + { + event.setCancelled(false); } } } diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/Game.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/Game.java index c9dcd7481..a10ad842c 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/Game.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/game/Game.java @@ -56,6 +56,7 @@ import nautilus.game.arcade.world.WorldData; public abstract class Game implements Listener { + public long getGameLiveTime() { return _gameLiveTime; @@ -209,6 +210,7 @@ public abstract class Game implements Listener //Addons public boolean CompassEnabled = false; public boolean CompassGiveItem = true; + public boolean CompassSpectatorMenu = true; public boolean SoupEnabled = true; public boolean TeamArmor = false; diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/gui/SpectatorShop.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/gui/SpectatorShop.java new file mode 100644 index 000000000..c161ea981 --- /dev/null +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/gui/SpectatorShop.java @@ -0,0 +1,41 @@ +package nautilus.game.arcade.gui; + +import org.bukkit.entity.Player; +import mineplex.core.account.CoreClientManager; +import mineplex.core.common.CurrencyType; +import mineplex.core.donation.DonationManager; +import mineplex.core.shop.ShopBase; +import mineplex.core.shop.page.ShopPageBase; +import nautilus.game.arcade.ArcadeManager; +import nautilus.game.arcade.addons.CompassAddon; +import nautilus.game.arcade.gui.page.SpectatorPage; + +/** + * Created by shaun on 14-09-24. + */ +public class SpectatorShop extends ShopBase +{ + + private ArcadeManager _arcadeManager; + + public SpectatorShop(CompassAddon plugin, ArcadeManager arcadeManager, CoreClientManager clientManager, DonationManager donationManager, CurrencyType... currencyTypes) + { + super(plugin, clientManager, donationManager, "Spectate Menu", currencyTypes); + _arcadeManager = arcadeManager; + } + + @Override + protected ShopPageBase> BuildPagesFor(Player player) + { + return new SpectatorPage(Plugin, _arcadeManager, this, ClientManager, DonationManager, player); + } + + public void update() + { + for (ShopPageBase> shopPage : PlayerPageMap.values()) + { + shopPage.Refresh(); + } + } + +} \ No newline at end of file diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/gui/button/SpectatorButton.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/gui/button/SpectatorButton.java new file mode 100644 index 000000000..37ec95791 --- /dev/null +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/gui/button/SpectatorButton.java @@ -0,0 +1,39 @@ +package nautilus.game.arcade.gui.button; + +import org.bukkit.entity.Player; +import mineplex.core.common.util.F; +import mineplex.core.shop.item.SingleButton; +import nautilus.game.arcade.ArcadeManager; + +/** + * Created by shaun on 14-09-26. + */ +public class SpectatorButton extends SingleButton +{ + private ArcadeManager _arcadeManager; + private Player _player; + private Player _target; + + public SpectatorButton(ArcadeManager arcadeManager, Player player, Player target) + { + _arcadeManager = arcadeManager; + _player = player; + _target = target; + } + + @Override + public void Clicked(Player player) + { + if (_arcadeManager.IsAlive(_target)) + { + _player.teleport(_target.getLocation()); + _player.sendMessage("Teleported to " + _target); + } + else + { + _player.sendMessage(F.main("Spectate", F.name(_target.getName()) + " is no longer alive.")); + } + System.out.println("click"); +// player.closeInventory(); + } +} \ No newline at end of file diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/gui/page/SpectatorPage.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/gui/page/SpectatorPage.java new file mode 100644 index 000000000..a2e915460 --- /dev/null +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/gui/page/SpectatorPage.java @@ -0,0 +1,88 @@ +package nautilus.game.arcade.gui.page; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.SkullMeta; +import mineplex.core.account.CoreClientManager; +import mineplex.core.common.util.C; +import mineplex.core.common.util.UtilColor; +import mineplex.core.common.util.UtilMath; +import mineplex.core.donation.DonationManager; +import mineplex.core.shop.item.ShopItem; +import mineplex.core.shop.page.ShopPageBase; +import nautilus.game.arcade.ArcadeManager; +import nautilus.game.arcade.addons.CompassAddon; +import nautilus.game.arcade.game.GameTeam; +import nautilus.game.arcade.gui.SpectatorShop; +import nautilus.game.arcade.gui.button.SpectatorButton; + +/** + * Created by shaun on 14-09-24. + */ + +public class SpectatorPage extends ShopPageBase +{ + private ArcadeManager _arcadeManager; + public SpectatorPage(CompassAddon plugin, ArcadeManager arcadeManager, SpectatorShop shop, CoreClientManager clientManager, DonationManager donationManager, Player player) + { + super(plugin, shop, clientManager, donationManager, "Spectator Menu", player); + _arcadeManager = arcadeManager; + BuildPage(); + } + @Override + protected void BuildPage() + { + HashMap> teamPlayerMap = new HashMap>(); + for (GameTeam team : _arcadeManager.GetGame().GetTeamList()) + { + teamPlayerMap.put(team, new ArrayList()); + } + for (Player other : _arcadeManager.GetGame().GetPlayers(true)) + { + GameTeam otherTeam = _arcadeManager.GetGame().GetTeam(other); + teamPlayerMap.get(otherTeam).add(other); + } + int currentRow = 0; + for (Map.Entry> entry : teamPlayerMap.entrySet()) + { + GameTeam team = entry.getKey(); + ArrayList teamPlayers = entry.getValue(); + int rowsNeeded = (teamPlayers.size() / 8) + 1; + for (int row = 0; row < rowsNeeded; row++) + { + int woolSlot = (row * 9) + (currentRow * 9); + ItemStack woolItem = new ItemStack(Material.WOOL, 1, (short) 0, UtilColor.chatColorToWoolData(team.GetColor())); + setItem(woolSlot, woolItem); + for (int playerIndex = 0; playerIndex < teamPlayers.size() && playerIndex < 9; playerIndex++) + { + int slot = (row * 9) + woolSlot + 1 + playerIndex; + Player other = teamPlayers.get(playerIndex + (row * 9)); + double distance = UtilMath.offset(Player, other); + double heightDifference = other.getLocation().getY() - Player.getLocation().getY(); + ItemStack item = new ItemStack(Material.SKULL_ITEM, 1, (byte) 3); + ArrayList lore = new ArrayList(); + lore.add(" "); + lore.add(ChatColor.RESET + "Kit: " + C.cYellow + _arcadeManager.GetGame().GetKit(other).GetName()); + lore.add(ChatColor.RESET + "Distance: " + C.cYellow + UtilMath.trim(1, distance)); + lore.add(ChatColor.RESET + "Height Difference: " + C.cYellow + UtilMath.trim(1, heightDifference)); + lore.add(" "); + lore.add(ChatColor.RESET + "Click to Spectate"); + SkullMeta skullMeta = ((SkullMeta) item.getItemMeta()); + skullMeta.setOwner(other.getName()); + skullMeta.setDisplayName(team.GetColor() + other.getName()); + skullMeta.setLore(lore); + item.setItemMeta(skullMeta); + ShopItem shopItem = new ShopItem(item, other.getName(), other.getName(), 1, false, false); + shopItem.SetLore(lore.toArray(new String[0])); + AddButton(slot, shopItem, new SpectatorButton(_arcadeManager, Player, other)); + } + } + currentRow += rowsNeeded; + } + } +} \ No newline at end of file diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/managers/GamePlayerManager.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/managers/GamePlayerManager.java index 4a3c9e96a..c726956fd 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/managers/GamePlayerManager.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/managers/GamePlayerManager.java @@ -163,7 +163,7 @@ public class GamePlayerManager implements Listener if (event.getWhoClicked().getGameMode() == GameMode.CREATIVE) { event.setCancelled(true); - event.getWhoClicked().closeInventory(); +// event.getWhoClicked().closeInventory(); System.out.println(this.getClass().getName() + " 153"); } } diff --git a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/managers/MiscManager.java b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/managers/MiscManager.java index 75a0928f2..a57c24281 100644 --- a/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/managers/MiscManager.java +++ b/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/managers/MiscManager.java @@ -81,17 +81,28 @@ public class MiscManager implements Listener if (player.getGameMode() != GameMode.SURVIVAL && !player.isOp()) { event.setCancelled(true); - player.closeInventory(); +// player.closeInventory(); System.out.println(this.getClass().getName() + " 84"); } else if (Manager.GetGame().IsLive() && !Manager.GetGame().IsAlive(player) && !((CraftPlayer)player).getHandle().spectating) { event.setCancelled(true); - player.closeInventory(); +// player.closeInventory(); System.out.println(this.getClass().getName() + " 91"); } } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void InventoryClickClose(InventoryClickEvent event) + { + Player player = UtilPlayer.searchExact(event.getWhoClicked().getName()); + if (player == null) + return; + + if (event.getClickedInventory().getViewers().contains(player)) + player.closeInventory(); + } @EventHandler public void addClockPrevent(InventoryOpenEvent event)