diff --git a/src/me/PauMAVA/UhcPlugin/UhcPluginCore.java b/src/me/PauMAVA/UhcPlugin/UhcPluginCore.java index 2e9819c..d07de68 100644 --- a/src/me/PauMAVA/UhcPlugin/UhcPluginCore.java +++ b/src/me/PauMAVA/UhcPlugin/UhcPluginCore.java @@ -22,9 +22,11 @@ import me.PauMAVA.UhcPlugin.commands.UhcCompleteTab; import me.PauMAVA.UhcPlugin.gameplay.CustomRecipes; import me.PauMAVA.UhcPlugin.match.UhcMatchHandler; +import me.PauMAVA.UhcPlugin.match.UhcScoreboardManager; import me.PauMAVA.UhcPlugin.teams.UhcTeamsManager; import me.PauMAVA.UhcPlugin.util.EventsRegister; import me.PauMAVA.UhcPlugin.util.PacketIntercepter; +import me.PauMAVA.UhcPlugin.util.UhcTabList; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; @@ -58,6 +60,8 @@ public void onEnable() { public void onDisable() { for(Player p: Bukkit.getServer().getOnlinePlayers()) { PacketIntercepter.rmPlayer(p); + UhcScoreboardManager.rmPlayer(p); + UhcTabList.resetTab(p); } UhcLogger.info("Disabled UhcPlugin!"); } diff --git a/src/me/PauMAVA/UhcPlugin/commands/UhcConfigCmd.java b/src/me/PauMAVA/UhcPlugin/commands/UhcConfigCmd.java index e927a84..aecba3b 100644 --- a/src/me/PauMAVA/UhcPlugin/commands/UhcConfigCmd.java +++ b/src/me/PauMAVA/UhcPlugin/commands/UhcConfigCmd.java @@ -62,7 +62,7 @@ public static void config(CommandSender theSender, String[] args) { String value = args[2]; switch(option) { case "season": { - setInt(option, value); + setString(option, value); break; } case "chapter_length": { @@ -171,9 +171,6 @@ public static Difficulty getDifficultyObject() { case "normal": { return Difficulty.NORMAL; } - case "hard": { - return Difficulty.HARD; - } default: { return Difficulty.HARD; } diff --git a/src/me/PauMAVA/UhcPlugin/match/UhcMatchHandler.java b/src/me/PauMAVA/UhcPlugin/match/UhcMatchHandler.java index 559f35e..21cdc1d 100644 --- a/src/me/PauMAVA/UhcPlugin/match/UhcMatchHandler.java +++ b/src/me/PauMAVA/UhcPlugin/match/UhcMatchHandler.java @@ -24,6 +24,7 @@ import me.PauMAVA.UhcPlugin.teams.UhcTeam; import me.PauMAVA.UhcPlugin.teams.UhcTeamsManager; import me.PauMAVA.UhcPlugin.util.PacketIntercepter; +import me.PauMAVA.UhcPlugin.util.UhcTabList; import me.PauMAVA.UhcPlugin.world.UhcWorldConfig; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -39,6 +40,7 @@ public class UhcMatchHandler { private UhcPluginCore plugin; private Boolean isRunning = false; private Integer timerTaskID; + private Integer tabTaskID; private List matchPlayers = new ArrayList<>(); private List teams = new ArrayList(); private UhcMatchTimer timer; @@ -65,11 +67,13 @@ public void start() { } this.timer = new UhcMatchTimer(); this.timerTaskID = timer.runTaskTimer(plugin, 0L, 20L).getTaskId(); + this.tabTaskID = new UhcTabList().runTaskTimer(plugin, 0L, 20L).getTaskId(); } public void end() { this.isRunning = false; Bukkit.getScheduler().cancelTask(this.timerTaskID); + Bukkit.getScheduler().cancelTask(this.tabTaskID); } /* Adds a player to the match diff --git a/src/me/PauMAVA/UhcPlugin/match/UhcScoreboardManager.java b/src/me/PauMAVA/UhcPlugin/match/UhcScoreboardManager.java index 44b5e7c..2636fdd 100644 --- a/src/me/PauMAVA/UhcPlugin/match/UhcScoreboardManager.java +++ b/src/me/PauMAVA/UhcPlugin/match/UhcScoreboardManager.java @@ -43,13 +43,14 @@ public class UhcScoreboardManager { private static int chapterLength = plugin.getConfig().getInt("chapter_length"); public static void setUp() { - infoObjective = uhcScoreboard.registerNewObjective("Info", "dummy", ChatColor.GOLD + "" + ChatColor.BOLD + "UHC S" + plugin.getConfig().get("season")); - lifeObjective = uhcScoreboard.registerNewObjective("Health", "health", "health", RenderType.HEARTS); - lifeObjective.setDisplaySlot(DisplaySlot.PLAYER_LIST); - updateHealth(); - for(Player player: Bukkit.getOnlinePlayers()) { - player.setScoreboard(uhcScoreboard); - } + uhcScoreboard = scManager.getNewScoreboard(); + infoObjective = uhcScoreboard.registerNewObjective("Info", "dummy", ChatColor.GOLD + "" + ChatColor.BOLD + "UHC S" + plugin.getConfig().get("season")); + lifeObjective = uhcScoreboard.registerNewObjective("Health", "health", "health", RenderType.HEARTS); + lifeObjective.setDisplaySlot(DisplaySlot.PLAYER_LIST); + updateHealth(); + for(Player player: Bukkit.getOnlinePlayers()) { + player.setScoreboard(uhcScoreboard); + } } /* Updates the health on the list scoreboard */ @@ -97,4 +98,9 @@ static void refreshSidebar(String minutes, String seconds, String episode, Strin static void refreshSidebarTitle(String newTitle) { infoObjective.setDisplayName(newTitle); } + + public static void rmPlayer(Player player) { + player.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard()); + } + } diff --git a/src/me/PauMAVA/UhcPlugin/teams/UhcTeamsManager.java b/src/me/PauMAVA/UhcPlugin/teams/UhcTeamsManager.java index 80ffab8..ff3287c 100644 --- a/src/me/PauMAVA/UhcPlugin/teams/UhcTeamsManager.java +++ b/src/me/PauMAVA/UhcPlugin/teams/UhcTeamsManager.java @@ -349,6 +349,4 @@ public void run() { } }.runTaskLater(plugin, 40L); } - - -} +} \ No newline at end of file diff --git a/src/me/PauMAVA/UhcPlugin/util/EventsRegister.java b/src/me/PauMAVA/UhcPlugin/util/EventsRegister.java index a038b57..81f4b2a 100644 --- a/src/me/PauMAVA/UhcPlugin/util/EventsRegister.java +++ b/src/me/PauMAVA/UhcPlugin/util/EventsRegister.java @@ -27,6 +27,7 @@ import org.bukkit.GameMode; import org.bukkit.Material; import org.bukkit.advancement.Advancement; +import org.bukkit.event.player.*; import org.bukkit.inventory.meta.Damageable; import org.bukkit.entity.Drowned; import org.bukkit.entity.Player; @@ -38,10 +39,6 @@ import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.event.entity.EntityRegainHealthEvent; import org.bukkit.event.entity.PlayerDeathEvent; -import org.bukkit.event.player.PlayerAdvancementDoneEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -121,7 +118,13 @@ public void onEntityKill(EntityDeathEvent event) { item.setItemMeta((ItemMeta) meta); event.getEntity().getWorld().dropItem(event.getEntity().getLocation(), item); } - Bukkit.getServer().broadcastMessage("KILLED Drowned!: " + randomNum); + } + } + + @EventHandler + public void onPlayerTeleport(PlayerTeleportEvent event) { + if(plugin.getMatchHandler().getMatchStatus() && event.getCause().equals(PlayerTeleportEvent.TeleportCause.SPECTATE)) { + event.setCancelled(true); } } diff --git a/src/me/PauMAVA/UhcPlugin/util/PacketIntercepter.java b/src/me/PauMAVA/UhcPlugin/util/PacketIntercepter.java index 29ed228..d3b999a 100644 --- a/src/me/PauMAVA/UhcPlugin/util/PacketIntercepter.java +++ b/src/me/PauMAVA/UhcPlugin/util/PacketIntercepter.java @@ -94,7 +94,9 @@ public void write(ChannelHandlerContext context, Object packet, ChannelPromise p }; ChannelPipeline pipe = ((CraftPlayer)player).getHandle().playerConnection.networkManager.channel.pipeline(); - pipe.addBefore("packet_handler", player.getName(), channelDuplexHandler); + try{ + pipe.addBefore("packet_handler", player.getName(), channelDuplexHandler); + } catch (IllegalArgumentException ignored) {} } } diff --git a/src/me/PauMAVA/UhcPlugin/util/UhcTabList.java b/src/me/PauMAVA/UhcPlugin/util/UhcTabList.java new file mode 100644 index 0000000..31ebfe1 --- /dev/null +++ b/src/me/PauMAVA/UhcPlugin/util/UhcTabList.java @@ -0,0 +1,109 @@ +/* + * UhcPlugin + * Copyright (c) 2019 Pau Machetti Vallverdu + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.PauMAVA.UhcPlugin.util; + +import me.PauMAVA.UhcPlugin.UhcPluginCore; +import me.PauMAVA.UhcPlugin.teams.UhcTeam; +import net.minecraft.server.v1_15_R1.ChatComponentText; +import net.minecraft.server.v1_15_R1.PacketPlayOutPlayerListHeaderFooter; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitRunnable; + +import java.lang.reflect.Field; + +public class UhcTabList extends BukkitRunnable { + + private ChatComponentText headerComp; + private ChatComponentText footerComp; + private String season = UhcPluginCore.getInstance().getConfig().getString("season"); + private int i = 1; + + @Override + public void run() { + switch (i) { + case 1: { + this.headerComp = new ChatComponentText("\n" + ChatColor.YELLOW + "" + ChatColor.BOLD + "U" + ChatColor.GOLD + "" + ChatColor.BOLD + "HC S" + season + "\n"); + break; + } + case 2: { + this.headerComp = new ChatComponentText("\n" + ChatColor.GOLD + "" + ChatColor.BOLD + "U" + ChatColor.YELLOW + "" + ChatColor.BOLD + "H" + ChatColor.GOLD + "" + ChatColor.BOLD + "C S" + season + "\n"); + break; + } + case 3: { + this.headerComp = new ChatComponentText("\n" + ChatColor.GOLD + "" + ChatColor.BOLD + "UH" + ChatColor.YELLOW + "" + ChatColor.BOLD + "C" + ChatColor.GOLD + "" + ChatColor.BOLD + " S" + season + "\n"); + break; + } + case 4: { + this.headerComp = new ChatComponentText("\n" + ChatColor.GOLD + "" + ChatColor.BOLD + "UHC" + ChatColor.YELLOW + "" + ChatColor.BOLD + " " + ChatColor.GOLD + "" + ChatColor.BOLD + "S" + season + "\n"); + break; + } + case 5: { + this.headerComp = new ChatComponentText("\n" + ChatColor.GOLD + "" + ChatColor.BOLD + "UHC " + ChatColor.YELLOW + "" + ChatColor.BOLD + "S" + ChatColor.GOLD + "" + ChatColor.BOLD + "" + season + "\n"); + break; + } + case 6: { + this.headerComp = new ChatComponentText("\n" + ChatColor.GOLD + "" + ChatColor.BOLD + "UHC S" + ChatColor.YELLOW + "" + ChatColor.BOLD + season + "\n"); + break; + } + case 7: { + this.headerComp = new ChatComponentText("\n" + ChatColor.GOLD + "" + ChatColor.BOLD + "UHC S" + season + "\n"); + i = -20; + break; + } + } + i++; + Integer alivePlayers = 0; + for(UhcTeam team: UhcPluginCore.getInstance().getMatchHandler().getRemainingTeams()) { + alivePlayers += team.alive().size(); + } + this.footerComp = new ChatComponentText("\n" + ChatColor.AQUA + "" + ChatColor.BOLD + "Alive Players: " + ChatColor.RESET + "" + alivePlayers + "\n" + ChatColor.LIGHT_PURPLE + "\n(c) 2019 UhcPlugin by PauMAVA\n"); + PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter(); + try { + Field headerField = packet.getClass().getDeclaredField("header"); + Field footerField = packet.getClass().getDeclaredField("footer"); + headerField.setAccessible(true); + footerField.setAccessible(true); + headerField.set(packet, this.headerComp); + footerField.set(packet, this.footerComp); + for(Player player: Bukkit.getServer().getOnlinePlayers()) { + ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); + } + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } + } + + public static void resetTab(Player player) { + PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter(); + try{ + Field header = packet.getClass().getDeclaredField("header"); + Field footer = packet.getClass().getDeclaredField("footer"); + header.setAccessible(true); + footer.setAccessible(true); + header.set(packet, new ChatComponentText("")); + footer.set(packet, new ChatComponentText("")); + } catch (IllegalAccessException | NoSuchFieldException | IllegalArgumentException e) { + e.printStackTrace(); + } + ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet); + } +}