diff --git a/src/main/java/plugin/Listeners.java b/src/main/java/plugin/Listeners.java new file mode 100644 index 0000000..4a4125e --- /dev/null +++ b/src/main/java/plugin/Listeners.java @@ -0,0 +1,31 @@ +package plugin; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; + +import me.catcoder.sidebar.Sidebar; + +public class Listeners implements Listener { + + @EventHandler(priority = EventPriority.HIGH) + public void onPlayerJoin(PlayerJoinEvent event) { + + final Sidebar sidebar = ScoreboardOG.getInstance().getSidebar(); + + sidebar.addViewer(event.getPlayer()); + + } + + @EventHandler(priority = EventPriority.HIGH) + public void onPlayerQuit(PlayerQuitEvent event) { + + final Sidebar sidebar = ScoreboardOG.getInstance().getSidebar(); + + sidebar.removeViewer(event.getPlayer()); + + } + +} \ No newline at end of file diff --git a/src/main/java/plugin/ScoreboardOG.java b/src/main/java/plugin/ScoreboardOG.java index 68e5aac..11b2a9e 100644 --- a/src/main/java/plugin/ScoreboardOG.java +++ b/src/main/java/plugin/ScoreboardOG.java @@ -8,67 +8,65 @@ import org.bukkit.plugin.java.JavaPlugin; import me.catcoder.sidebar.ProtocolSidebar; import me.catcoder.sidebar.Sidebar; +import net.kyori.adventure.text.minimessage.MiniMessage; public class ScoreboardOG extends JavaPlugin { - private static ScoreboardOG plugin; - private static FileConfiguration config; + private static ScoreboardOG instance; + + private Sidebar sidebar; + private FileConfiguration config; @Override public void onEnable() { - plugin = this; + instance = this; saveDefaultConfig(); - config = getConfig(); + this.config = getConfig(); + + // Build the sidebar after the plugin instance exists. + this.sidebar = ProtocolSidebar.newMiniplaceholdersSidebar("TrueOG Network", + this, MiniMessage.miniMessage()); initializeScoreboards(); - } - - public static ScoreboardOG getPlugin() { - - return plugin; + getServer().getPluginManager().registerEvents(new Listeners(), this); } - static FileConfiguration config() { + public static ScoreboardOG getInstance() { + + return instance; + + } + + public Sidebar getSidebar() { + + return sidebar; + + } + + public FileConfiguration config() { return config; } - private static void initializeScoreboards() { + private void initializeScoreboards() { - // Create sidebar which uses MiniMessage API. - final Sidebar sidebar = ProtocolSidebar - .newMiniMessageSidebar("TrueOG Network", getPlugin()); - - // Add a line with example text. - sidebar.addLine("Just a static line"); - - // Add an empty line + sidebar.addLine("Just a blank line."); sidebar.addBlankLine(); - - // Add a dynamic line for hunger. - sidebar.addUpdatableLine(player -> ("Your Hunger: " + (player.getFoodLevel()))); - - // Add an empty line + sidebar.addUpdatableLine(player -> "Your Hunger: " + player.getFoodLevel()); sidebar.addBlankLine(); - - // Add a dynamic line for health. sidebar.addUpdatableLine(player -> "Your Health: " + player.getHealth()); - - // Add an empty line sidebar.addBlankLine(); - - // Add a line with a URL. sidebar.addLine("https://github.com/CatCoderr/ProtocolSidebar"); - // Update all lines except static ones every 10 ticks + // Update all non-static lines every 10 ticks. sidebar.updateLinesPeriodically(0, 10); - // Show the sidebar to all online players. + // Show to all currently online players. Bukkit.getOnlinePlayers().forEach(sidebar::addViewer); }