From cb1bcda8dc7bf7f8530e9a5049f3a2276086056c Mon Sep 17 00:00:00 2001 From: PauMAVA Date: Sat, 21 Dec 2019 19:59:19 +0100 Subject: [PATCH] Fixed minor bugs, added packet pipeline reload support and drowneds have 50% chance of dropping tridents. --- src/me/PauMAVA/UhcPlugin/UhcPluginCore.java | 9 +++- .../UhcPlugin/match/UhcMatchHandler.java | 4 ++ .../UhcPlugin/match/UhcMatchTimer.java | 2 + .../UhcPlugin/teams/UhcTeamsManager.java | 7 +-- .../UhcPlugin/util/EventsRegister.java | 48 ++++++++++++++----- .../UhcPlugin/util/PacketIntercepter.java | 6 +-- .../UhcPlugin/world/UhcWorldBorder.java | 9 ++-- 7 files changed, 60 insertions(+), 25 deletions(-) diff --git a/src/me/PauMAVA/UhcPlugin/UhcPluginCore.java b/src/me/PauMAVA/UhcPlugin/UhcPluginCore.java index c303298..2e9819c 100644 --- a/src/me/PauMAVA/UhcPlugin/UhcPluginCore.java +++ b/src/me/PauMAVA/UhcPlugin/UhcPluginCore.java @@ -18,17 +18,19 @@ package me.PauMAVA.UhcPlugin; -import java.util.logging.Logger; - import me.PauMAVA.UhcPlugin.commands.UhcCmdHub; import me.PauMAVA.UhcPlugin.commands.UhcCompleteTab; import me.PauMAVA.UhcPlugin.gameplay.CustomRecipes; import me.PauMAVA.UhcPlugin.match.UhcMatchHandler; import me.PauMAVA.UhcPlugin.teams.UhcTeamsManager; import me.PauMAVA.UhcPlugin.util.EventsRegister; +import me.PauMAVA.UhcPlugin.util.PacketIntercepter; import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; +import java.util.logging.Logger; + public class UhcPluginCore extends JavaPlugin { @@ -54,6 +56,9 @@ public void onEnable() { @Override public void onDisable() { + for(Player p: Bukkit.getServer().getOnlinePlayers()) { + PacketIntercepter.rmPlayer(p); + } UhcLogger.info("Disabled UhcPlugin!"); } diff --git a/src/me/PauMAVA/UhcPlugin/match/UhcMatchHandler.java b/src/me/PauMAVA/UhcPlugin/match/UhcMatchHandler.java index 4f1c4b6..559f35e 100644 --- a/src/me/PauMAVA/UhcPlugin/match/UhcMatchHandler.java +++ b/src/me/PauMAVA/UhcPlugin/match/UhcMatchHandler.java @@ -23,6 +23,7 @@ import me.PauMAVA.UhcPlugin.commands.UhcConfigCmd; import me.PauMAVA.UhcPlugin.teams.UhcTeam; import me.PauMAVA.UhcPlugin.teams.UhcTeamsManager; +import me.PauMAVA.UhcPlugin.util.PacketIntercepter; import me.PauMAVA.UhcPlugin.world.UhcWorldConfig; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -59,6 +60,9 @@ public void start() { UhcWorldConfig.setTime(0L); RandomTeleporter.teleportPlayers(); UhcScoreboardManager.setUp(); + for(Player p: Bukkit.getServer().getOnlinePlayers()) { + PacketIntercepter.injectPlayer(p); + } this.timer = new UhcMatchTimer(); this.timerTaskID = timer.runTaskTimer(plugin, 0L, 20L).getTaskId(); } diff --git a/src/me/PauMAVA/UhcPlugin/match/UhcMatchTimer.java b/src/me/PauMAVA/UhcPlugin/match/UhcMatchTimer.java index e589fec..fe8288d 100644 --- a/src/me/PauMAVA/UhcPlugin/match/UhcMatchTimer.java +++ b/src/me/PauMAVA/UhcPlugin/match/UhcMatchTimer.java @@ -19,6 +19,7 @@ package me.PauMAVA.UhcPlugin.match; import me.PauMAVA.UhcPlugin.UhcPluginCore; +import me.PauMAVA.UhcPlugin.world.UhcWorldBorder; import org.bukkit.ChatColor; import org.bukkit.scheduler.BukkitRunnable; @@ -43,6 +44,7 @@ public void run() { minutes = chapterLength - 1; episode++; UhcPluginCore.getInstance().getMatchHandler().episodeAnnouncement(episode); + UhcWorldBorder.refreshBorder(episode); } } if(seconds < 10) { diff --git a/src/me/PauMAVA/UhcPlugin/teams/UhcTeamsManager.java b/src/me/PauMAVA/UhcPlugin/teams/UhcTeamsManager.java index 0448a5b..80ffab8 100644 --- a/src/me/PauMAVA/UhcPlugin/teams/UhcTeamsManager.java +++ b/src/me/PauMAVA/UhcPlugin/teams/UhcTeamsManager.java @@ -329,14 +329,15 @@ public static void revive(Player invoker, Block block) { new BukkitRunnable(){ @Override public void run() { - invoker.getWorld().strikeLightningEffect(block.getLocation()); - invoker.getWorld().spawn(new Location(invoker.getWorld(), block.getX(), block.getY() + 1, block.getZ()), EnderCrystal.class); - block.setType(Material.AIR); if(team.dead().size() == 0 || plugin.getMatchHandler().getTimer().getEpisode() >= 10 || team.hasBeenRevived(team.dead().get(0))) { invoker.sendMessage(Prefix.INGAME_UHC + "" + "You just wasted a " + ChatColor.BLACK + "" + ChatColor.BOLD + "DARK " + ChatColor.AQUA + "" + ChatColor.BOLD + "Crystal"); } else { + invoker.getWorld().strikeLightningEffect(block.getLocation()); + invoker.getWorld().spawn(new Location(invoker.getWorld(), block.getX(), block.getY() + 1, block.getZ()), EnderCrystal.class); + block.setType(Material.AIR); Player revived = team.dead().get(0); team.markPlayerAsAlive(revived); + team.markPlayerAsRevived(revived); revived.setGameMode(GameMode.SURVIVAL); revived.setExp(0); revived.setFoodLevel(20); diff --git a/src/me/PauMAVA/UhcPlugin/util/EventsRegister.java b/src/me/PauMAVA/UhcPlugin/util/EventsRegister.java index 20c4f57..a038b57 100644 --- a/src/me/PauMAVA/UhcPlugin/util/EventsRegister.java +++ b/src/me/PauMAVA/UhcPlugin/util/EventsRegister.java @@ -27,18 +27,23 @@ import org.bukkit.GameMode; import org.bukkit.Material; import org.bukkit.advancement.Advancement; +import org.bukkit.inventory.meta.Damageable; +import org.bukkit.entity.Drowned; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.entity.EntityDamageEvent; +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; public class EventsRegister implements Listener { @@ -47,8 +52,7 @@ public class EventsRegister implements Listener { /*Listens for players joining the server and: * - Injects them to the pipeline*/ @EventHandler(priority=EventPriority.HIGH) - public void onjoin(PlayerJoinEvent event) { - plugin.getPluginLogger().info("Registered event!"); + public void onPlayerJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); PacketIntercepter.injectPlayer(player); } @@ -56,51 +60,69 @@ public void onjoin(PlayerJoinEvent event) { /*Listens for players quitting the game and: * - Removes them from the pipeline*/ @EventHandler(priority=EventPriority.HIGH) - public void onleave(PlayerQuitEvent event) { - plugin.getPluginLogger().info("Registered event!"); + public void onPlayerLeave(PlayerQuitEvent event) { Player player = event.getPlayer(); PacketIntercepter.rmPlayer(player); } @EventHandler public void onDeath(PlayerDeathEvent event) { + if(plugin.getMatchHandler().getMatchStatus()) { UhcDeathManager death = new UhcDeathManager(event.getEntity(), event.getEntity().getWorld(), event.getDeathMessage()); death.setTotem(Material.BLACK_STAINED_GLASS_PANE); death.setPlayerGamemode(GameMode.SPECTATOR); death.displayDeathMsgAndUpdateTeam(); + } } @EventHandler public void onAdvancement(PlayerAdvancementDoneEvent event) { - Advancement advancement = event.getAdvancement(); - String advancementID = advancement.getKey().toString(); - if(advancementID.contains("story") || advancementID.contains("nether") || advancementID.contains("husbandry") || advancementID.contains("end")|| advancementID.contains("adventure")) { - AdvancementsDatabase db = new AdvancementsDatabase(); - String advancementName = db.getCanonicalName(advancementID); - UhcChatManager.dispatchAdvancementEvent(advancementName); + if(plugin.getMatchHandler().getMatchStatus()) { + Advancement advancement = event.getAdvancement(); + String advancementID = advancement.getKey().toString(); + if(advancementID.contains("story") || advancementID.contains("nether") || advancementID.contains("husbandry") || advancementID.contains("end")|| advancementID.contains("adventure")) { + AdvancementsDatabase db = new AdvancementsDatabase(); + String advancementName = db.getCanonicalName(advancementID); + UhcChatManager.dispatchAdvancementEvent(advancementName); + } } } @EventHandler public void onPlayerDamage(EntityDamageEvent event) { - if(event.getEntity() instanceof Player) { + if(event.getEntity() instanceof Player && plugin.getMatchHandler().getMatchStatus()) { UhcScoreboardManager.updateHealth(); } } @EventHandler public void onPlayerHeal(EntityRegainHealthEvent event) { - if(event.getEntity() instanceof Player) { + if(event.getEntity() instanceof Player && plugin.getMatchHandler().getMatchStatus()) { UhcScoreboardManager.updateHealth(); } } @EventHandler(priority=EventPriority.MONITOR) public void onPlayerInteract(PlayerInteractEvent event) { - if(event.getMaterial() == Material.END_CRYSTAL && event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getType() == Material.BEDROCK) { + if(event.getMaterial() == Material.END_CRYSTAL && event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getType() == Material.BEDROCK && plugin.getMatchHandler().getMatchStatus()) { event.getItem().setType(Material.COAL); UhcTeamsManager.revive(event.getPlayer(), event.getClickedBlock()); } } + + @EventHandler + public void onEntityKill(EntityDeathEvent event) { + if(event.getEntity() instanceof Drowned && event.getEntity().getKiller() != null) { + Integer randomNum = new Range(0 , 100).getRandomInteger(); + if(randomNum > 50) { + ItemStack item = new ItemStack(Material.TRIDENT, 1); + Damageable meta = (Damageable) item.getItemMeta(); + meta.setDamage(new Range(100, 250).getRandomInteger()); + item.setItemMeta((ItemMeta) meta); + event.getEntity().getWorld().dropItem(event.getEntity().getLocation(), item); + } + Bukkit.getServer().broadcastMessage("KILLED Drowned!: " + randomNum); + } + } } diff --git a/src/me/PauMAVA/UhcPlugin/util/PacketIntercepter.java b/src/me/PauMAVA/UhcPlugin/util/PacketIntercepter.java index b66be93..29ed228 100644 --- a/src/me/PauMAVA/UhcPlugin/util/PacketIntercepter.java +++ b/src/me/PauMAVA/UhcPlugin/util/PacketIntercepter.java @@ -36,11 +36,11 @@ import net.minecraft.server.v1_15_R1.PacketPlayOutChat; -class PacketIntercepter { +public class PacketIntercepter { private static final UhcPluginCore plugin = UhcPluginCore.getInstance(); - static void rmPlayer(Player player) { + public static void rmPlayer(Player player) { Channel channel = ((CraftPlayer) player).getHandle().playerConnection.networkManager.channel; channel.eventLoop().submit(() -> { channel.pipeline().remove(player.getName()); @@ -49,7 +49,7 @@ static void rmPlayer(Player player) { } - static void injectPlayer(Player player) { + public static void injectPlayer(Player player) { ChannelDuplexHandler channelDuplexHandler = new ChannelDuplexHandler() { /* Override parent method to print all packets before letting the client/server to read them This enables to block any packet or modify it before sending it to the client or server */ diff --git a/src/me/PauMAVA/UhcPlugin/world/UhcWorldBorder.java b/src/me/PauMAVA/UhcPlugin/world/UhcWorldBorder.java index 9401c0e..4bdb06e 100644 --- a/src/me/PauMAVA/UhcPlugin/world/UhcWorldBorder.java +++ b/src/me/PauMAVA/UhcPlugin/world/UhcWorldBorder.java @@ -21,6 +21,7 @@ import java.util.List; import me.PauMAVA.UhcPlugin.UhcPluginCore; +import me.PauMAVA.UhcPlugin.chat.Prefix; import me.PauMAVA.UhcPlugin.commands.UhcConfigCmd; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -46,11 +47,11 @@ public static void refreshBorder(Integer episode) { for(World dimension: dimensions) { dimension.getWorldBorder().setSize(getFinalRadius(), (10 - getBorderClosingEpisode()) * config.getInt("chapter_length") * 60); } - float velocity = (getOriginalBorderRadius() - getFinalRadius()) / ((10 - getBorderClosingEpisode())* config.getInt("chapter_length") * 60); + float velocity = (getOriginalBorderRadius() - getFinalRadius()) / ((10 - getBorderClosingEpisode())* (float) config.getInt("chapter_length") * 60); for(Player p: Bukkit.getServer().getOnlinePlayers()) { - p.sendMessage(ChatColor.GOLD + "[Game] " + ChatColor.YELLOW + "The border is closing as you have reached episode " + getBorderClosingEpisode() + "!"); - p.sendMessage(ChatColor.GOLD + "[Game] " + ChatColor.YELLOW + "It is time for you to reach 0,0. Get ready for the fight. Good luck!"); - p.sendMessage(ChatColor.BLUE + "[Info] " + ChatColor.AQUA + "The border is closing at a velocity of " + velocity + " blocks/second."); + p.sendMessage(Prefix.INGAME_UHC + "" + ChatColor.YELLOW + "The border is closing as you have reached episode " + getBorderClosingEpisode() + "!"); + p.sendMessage(Prefix.INGAME_UHC + "" + ChatColor.YELLOW + "It is time for you to reach 0,0. Get ready for the fight. Good luck!"); + p.sendMessage(Prefix.INGAME_UHC + "" + ChatColor.AQUA + "The border is closing at a velocity of " + velocity + " blocks/second."); } } return;