From d4dcbfeafabfe10dd0e71dafdb72a034f1056952 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sun, 24 Nov 2019 19:48:27 +0100 Subject: [PATCH] cleanup --- .../src/de/steamwar/core/Core.java | 3 + .../core/events/PlayerJoinedEvent.java | 3 + .../de/steamwar/scoreboard/SWScoreboard.java | 58 ++++++++++--------- .../scoreboard/ScoreboardContainer.java | 57 ------------------ 4 files changed, 36 insertions(+), 85 deletions(-) delete mode 100644 SpigotCore_Main/src/de/steamwar/scoreboard/ScoreboardContainer.java diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index 21894c0..f71f947 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -2,6 +2,7 @@ package de.steamwar.core; import de.steamwar.core.events.ChattingEvent; import de.steamwar.core.events.PlayerJoinedEvent; +import de.steamwar.scoreboard.SWScoreboard; import de.steamwar.sql.SQL; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; @@ -34,6 +35,8 @@ public class Core extends JavaPlugin{ public void onEnable() { Bukkit.getPluginManager().registerEvents(new PlayerJoinedEvent(), this); Bukkit.getPluginManager().registerEvents(new ChattingEvent(), this); + + SWScoreboard.startPlayerBoards(); } @Override diff --git a/SpigotCore_Main/src/de/steamwar/core/events/PlayerJoinedEvent.java b/SpigotCore_Main/src/de/steamwar/core/events/PlayerJoinedEvent.java index edae08e..cfb0667 100644 --- a/SpigotCore_Main/src/de/steamwar/core/events/PlayerJoinedEvent.java +++ b/SpigotCore_Main/src/de/steamwar/core/events/PlayerJoinedEvent.java @@ -1,5 +1,6 @@ package de.steamwar.core.events; +import de.steamwar.scoreboard.SWScoreboard; import de.steamwar.sql.SteamwarUser; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -51,5 +52,7 @@ public class PlayerJoinedEvent implements Listener{ Player player = event.getPlayer(); event.setQuitMessage("§c§l« §r" + player.getDisplayName()); + + SWScoreboard.getPlayerBoards().remove(player); } } diff --git a/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java b/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java index d71e169..bb82126 100644 --- a/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java +++ b/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java @@ -3,6 +3,7 @@ package de.steamwar.scoreboard; import com.comphenix.protocol.PacketType; import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.events.PacketContainer; +import de.steamwar.core.Core; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -13,17 +14,15 @@ import java.util.logging.Level; public class SWScoreboard { - HashMap data; - private Player player; - private String title; + private static HashMap playerBoards = new HashMap<>(); - public SWScoreboard(Player player, HashMap data, String title) { - this.data = data; - this.player = player; - this.title = title; + ScoreboardCallback callback; + + public SWScoreboard(Player player, ScoreboardCallback callback) { + this.callback = callback; } - public void sendScoreboard() { + public void sendScoreboard(Player player) { PacketContainer remove = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.SCOREBOARD_OBJECTIVE); remove.getStrings().write(0, "AAA"); remove.getBytes().write(0, (byte) 1); @@ -31,7 +30,7 @@ public class SWScoreboard { PacketContainer createpacket = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.SCOREBOARD_OBJECTIVE); createpacket.getStrings().write(0, "AAA"); createpacket.getBytes().write(0, (byte) 0); - createpacket.getStrings().write(1, this.title); + createpacket.getStrings().write(1, this.callback.getTitle()); createpacket.getIntegers().write(0, 0); try { @@ -41,7 +40,7 @@ public class SWScoreboard { Bukkit.getLogger().log(Level.SEVERE, "COULD NOT SEND PACKAGE", ex); } - for(Map.Entry entry : data.entrySet()) { + for(Map.Entry entry : this.callback.getData().entrySet()) { PacketContainer update = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.SCOREBOARD_SCORE); update.getStrings().write(0, entry.getKey()); update.getBytes().write(0, (byte) 0); @@ -57,10 +56,10 @@ public class SWScoreboard { } - public void display() { + public void display(Player player) { PacketContainer display = ProtocolLibrary.getProtocolManager().createPacket(PacketType.Play.Server.SCOREBOARD_DISPLAY_OBJECTIVE); display.getBytes().write(0, (byte) 1); - display.getStrings().write(0, this.title); + display.getStrings().write(0, this.callback.getTitle()); try { ProtocolLibrary.getProtocolManager().sendServerPacket(player, display); @@ -69,27 +68,30 @@ public class SWScoreboard { } } - public HashMap getData() { - return data; + public static void startPlayerBoards() { + Bukkit.getScheduler().scheduleSyncRepeatingTask(Core.getInstance(), new Runnable() { + @Override + public void run() { + synchronized (playerBoards) { + for(Map.Entry entry : playerBoards.entrySet()) { + entry.getValue().sendScoreboard(entry.getKey()); + entry.getValue().display(entry.getKey()); + } + } + } + }, 10, 5); } - public void setData(HashMap data) { - this.data = data; + + public ScoreboardCallback getCallback() { + return callback; } - public Player getPlayer() { - return player; + public void setCallback(ScoreboardCallback callback) { + this.callback = callback; } - public void setPlayer(Player player) { - this.player = player; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; + public static HashMap getPlayerBoards() { + return playerBoards; } } diff --git a/SpigotCore_Main/src/de/steamwar/scoreboard/ScoreboardContainer.java b/SpigotCore_Main/src/de/steamwar/scoreboard/ScoreboardContainer.java deleted file mode 100644 index 8389c22..0000000 --- a/SpigotCore_Main/src/de/steamwar/scoreboard/ScoreboardContainer.java +++ /dev/null @@ -1,57 +0,0 @@ -package de.steamwar.scoreboard; - -import de.steamwar.core.Core; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; - -import java.util.HashMap; -import java.util.UUID; - -public class ScoreboardContainer { - - private static HashMap containers = new HashMap<>(); - - Player player; - ScoreboardCallback callback; - - public ScoreboardContainer(Player player, ScoreboardCallback callback) { - this.player = player; - this.callback = callback; - } - - public void update() { - - //Update - SWScoreboard update = new SWScoreboard(player, callback.getData(), callback.getTitle()); - update.sendScoreboard(); - Bukkit.getScheduler().runTaskLater(Core.getInstance(), new Runnable() { - @Override - public void run() { - update.display(); - } - }, 2); - } - - public void startScoreboard() { - Bukkit.getScheduler().scheduleSyncRepeatingTask(Core.getInstance(), new Runnable() { - @Override - public void run() { - for(ScoreboardContainer container : ScoreboardContainer.getContainers().values()) { - container.update(); - } - } - }, 10, 5); - } - - public Player getPlayer() { - return player; - } - - public void setPlayer(Player player) { - this.player = player; - } - - public static HashMap getContainers() { - return containers; - } -}