From 002b05820e773ca78feabbdd4839f5681f1a8147 Mon Sep 17 00:00:00 2001 From: Trixkz Date: Sun, 21 Nov 2021 10:12:16 -0500 Subject: [PATCH] some more shit --- .idea/workspace.xml | 49 +++++-- src/main/java/assemble/Assemble.java | 108 +++++++++++++++ src/main/java/assemble/AssembleAdapter.java | 25 ++++ src/main/java/assemble/AssembleBoard.java | 127 ++++++++++++++++++ .../java/assemble/AssembleBoardEntry.java | 116 ++++++++++++++++ src/main/java/assemble/AssembleException.java | 14 ++ src/main/java/assemble/AssembleListener.java | 51 +++++++ src/main/java/assemble/AssembleStyle.java | 26 ++++ src/main/java/assemble/AssembleThread.java | 127 ++++++++++++++++++ .../events/AssembleBoardCreateEvent.java | 31 +++++ .../events/AssembleBoardCreatedEvent.java | 30 +++++ .../events/AssembleBoardDestroyEvent.java | 31 +++++ src/main/java/rip/tilly/bedwars/BedWars.java | 7 + .../bedwars/commands/level/LevelCommand.java | 30 ++--- .../bedwars/commands/level/RemoveCommand.java | 5 +- .../bedwars/commands/level/SetCommand.java | 24 ++-- .../commands/setspawn/SpawnCommand.java | 1 + .../tilly/bedwars/commands/xp/AddCommand.java | 24 ++-- .../bedwars/commands/xp/RemoveCommand.java | 24 ++-- .../tilly/bedwars/commands/xp/SetCommand.java | 24 ++-- .../tilly/bedwars/commands/xp/XpCommand.java | 30 ++--- .../bedwars/managers/CommandManager.java | 4 + .../bedwars/managers/ScoreboardManager.java | 62 +++++++++ .../rip/tilly/bedwars/player/PlayerData.java | 2 +- .../bedwars/player/PlayerDataManager.java | 2 +- src/main/resources/plugin.yml | 6 +- 26 files changed, 868 insertions(+), 112 deletions(-) create mode 100644 src/main/java/assemble/Assemble.java create mode 100644 src/main/java/assemble/AssembleAdapter.java create mode 100644 src/main/java/assemble/AssembleBoard.java create mode 100644 src/main/java/assemble/AssembleBoardEntry.java create mode 100644 src/main/java/assemble/AssembleException.java create mode 100644 src/main/java/assemble/AssembleListener.java create mode 100644 src/main/java/assemble/AssembleStyle.java create mode 100644 src/main/java/assemble/AssembleThread.java create mode 100644 src/main/java/assemble/events/AssembleBoardCreateEvent.java create mode 100644 src/main/java/assemble/events/AssembleBoardCreatedEvent.java create mode 100644 src/main/java/assemble/events/AssembleBoardDestroyEvent.java create mode 100644 src/main/java/rip/tilly/bedwars/managers/ScoreboardManager.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 54169a3..2cca83a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -23,21 +23,31 @@ - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/assemble/Assemble.java b/src/main/java/assemble/Assemble.java new file mode 100644 index 0000000..ebce240 --- /dev/null +++ b/src/main/java/assemble/Assemble.java @@ -0,0 +1,108 @@ +package assemble; + +import assemble.events.AssembleBoardCreateEvent; +import lombok.Getter; +import lombok.Setter; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.bukkit.plugin.java.JavaPlugin; + +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.ConcurrentHashMap; + +@Getter @Setter +public class Assemble { + + private JavaPlugin plugin; + + private AssembleAdapter adapter; + private AssembleThread thread; + private AssembleListener listeners; + private AssembleStyle assembleStyle = AssembleStyle.MODERN; + + private Map boards; + + private long ticks = 2; + private boolean hook = false, debugMode = true; + + /** + * Assemble. + * + * @param plugin instance. + * @param adapter + */ + public Assemble(JavaPlugin plugin, AssembleAdapter adapter) { + if (plugin == null) { + throw new RuntimeException("Assemble can not be instantiated without a plugin instance!"); + } + + this.plugin = plugin; + this.adapter = adapter; + this.boards = new ConcurrentHashMap<>(); + + this.setup(); + } + + /** + * Setup Assemble. + */ + public void setup() { + // Register Events. + this.listeners = new AssembleListener(this); + this.plugin.getServer().getPluginManager().registerEvents(listeners, this.plugin); + + // Ensure that the thread has stopped running. + if (this.thread != null) { + this.thread.stop(); + this.thread = null; + } + + // Register new boards for existing online players. + for (Player player : Bukkit.getOnlinePlayers()) { + // Make sure it doesn't double up. + AssembleBoardCreateEvent createEvent = new AssembleBoardCreateEvent(player); + + Bukkit.getPluginManager().callEvent(createEvent); + if (createEvent.isCancelled()) { + return; + } + + getBoards().putIfAbsent(player.getUniqueId(), new AssembleBoard(player, this)); + } + + // Start Thread. + this.thread = new AssembleThread(this); + } + + /** + * + */ + public void cleanup() { + // Stop thread. + if (this.thread != null) { + this.thread.stop(); + this.thread = null; + } + + // Unregister listeners. + if (listeners != null) { + HandlerList.unregisterAll(listeners); + listeners = null; + } + + // Destroy player scoreboards. + for (UUID uuid : getBoards().keySet()) { + Player player = Bukkit.getPlayer(uuid); + + if (player == null || !player.isOnline()) { + continue; + } + + getBoards().remove(uuid); + player.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard()); + } + } + +} diff --git a/src/main/java/assemble/AssembleAdapter.java b/src/main/java/assemble/AssembleAdapter.java new file mode 100644 index 0000000..ee6739c --- /dev/null +++ b/src/main/java/assemble/AssembleAdapter.java @@ -0,0 +1,25 @@ +package assemble; + +import org.bukkit.entity.Player; + +import java.util.List; + +public interface AssembleAdapter { + + /** + * Get's the scoreboard title. + * + * @param player who's title is being displayed. + * @return title. + */ + String getTitle(Player player); + + /** + * Get's the scoreboard lines. + * + * @param player who's lines are being displayed. + * @return lines. + */ + List getLines(Player player); + +} diff --git a/src/main/java/assemble/AssembleBoard.java b/src/main/java/assemble/AssembleBoard.java new file mode 100644 index 0000000..5ccc794 --- /dev/null +++ b/src/main/java/assemble/AssembleBoard.java @@ -0,0 +1,127 @@ +package assemble; + +import assemble.events.AssembleBoardCreatedEvent; +import lombok.Getter; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.bukkit.scoreboard.DisplaySlot; +import org.bukkit.scoreboard.Objective; +import org.bukkit.scoreboard.Scoreboard; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class AssembleBoard { + + @Getter private Assemble assemble; + + @Getter private final List entries = new ArrayList<>(); + @Getter private final List identifiers = new ArrayList<>(); + + @Getter private final UUID uuid; + + /** + * Assemble Board. + * + * @param player that the board belongs to. + * @param assemble instance. + */ + public AssembleBoard(Player player, Assemble assemble) { + this.uuid = player.getUniqueId(); + this.assemble = assemble; + this.setup(player); + } + + /** + * Get's a player's bukkit scoreboard. + * + * @return either existing scoreboard or new scoreboard. + */ + public Scoreboard getScoreboard() { + Player player = Bukkit.getPlayer(getUuid()); + if (getAssemble().isHook() || player.getScoreboard() != Bukkit.getScoreboardManager().getMainScoreboard()) { + return player.getScoreboard(); + } else { + return Bukkit.getScoreboardManager().getNewScoreboard(); + } + } + + /** + * Get's the player's scoreboard objective. + * + * @return either existing objecting or new objective. + */ + public Objective getObjective() { + Scoreboard scoreboard = getScoreboard(); + if (scoreboard.getObjective("Assemble") == null) { + Objective objective = scoreboard.registerNewObjective("Assemble", "dummy"); + objective.setDisplaySlot(DisplaySlot.SIDEBAR); + objective.setDisplayName(getAssemble().getAdapter().getTitle(Bukkit.getPlayer(getUuid()))); + return objective; + } else { + return scoreboard.getObjective("Assemble"); + } + } + + /** + * Setup the board. + * + * @param player who's board to setup. + */ + private void setup(Player player) { + Scoreboard scoreboard = getScoreboard(); + player.setScoreboard(scoreboard); + getObjective(); + + // Send Update. + AssembleBoardCreatedEvent createdEvent = new AssembleBoardCreatedEvent(this); + Bukkit.getPluginManager().callEvent(createdEvent); + } + + /** + * Get the board entry at a specific position. + * + * @param pos to find entry. + * @return entry if it isn't out of range. + */ + public AssembleBoardEntry getEntryAtPosition(int pos) { + return pos >= this.entries.size() ? null : this.entries.get(pos); + } + + /** + * Get the unique identifier for position in scoreboard. + * + * @param position for identifier. + * @return unique identifier. + */ + public String getUniqueIdentifier(int position) { + String identifier = getRandomChatColor(position) + ChatColor.WHITE; + + while (this.identifiers.contains(identifier)) { + identifier = identifier + getRandomChatColor(position) + ChatColor.WHITE; + } + + // This is rare, but just in case, make the method recursive + if (identifier.length() > 16) { + return this.getUniqueIdentifier(position); + } + + // Add our identifier to the list so there are no duplicates + this.identifiers.add(identifier); + + return identifier; + } + + /** + * Gets a ChatColor based off the position in the collection. + * + * @param position of entry. + * @return ChatColor adjacent to position. + */ + private static String getRandomChatColor(int position) { + return ChatColor.values()[position].toString(); + } + +} diff --git a/src/main/java/assemble/AssembleBoardEntry.java b/src/main/java/assemble/AssembleBoardEntry.java new file mode 100644 index 0000000..76abb85 --- /dev/null +++ b/src/main/java/assemble/AssembleBoardEntry.java @@ -0,0 +1,116 @@ +package assemble; + +import lombok.Setter; +import org.bukkit.ChatColor; +import org.bukkit.scoreboard.Score; +import org.bukkit.scoreboard.Scoreboard; +import org.bukkit.scoreboard.Team; + +public class AssembleBoardEntry { + + private final AssembleBoard board; + @Setter private String text, identifier; + private Team team; + private int position; + + /** + * Assemble Board Entry + * + * @param board that entry belongs to. + * @param text of entry. + * @param position of entry. + */ + public AssembleBoardEntry(AssembleBoard board, String text, int position) { + this.board = board; + this.text = text; + this.position = position; + this.identifier = this.board.getUniqueIdentifier(position); + + this.setup(); + } + + /** + * Setup Board Entry. + */ + public void setup() { + final Scoreboard scoreboard = this.board.getScoreboard(); + + if (scoreboard == null) { + return; + } + + String teamName = this.identifier; + + // This shouldn't happen, but just in case. + if (teamName.length() > 16) { + teamName = teamName.substring(0, 16); + } + + Team team = scoreboard.getTeam(teamName); + + // Register the team if it does not exist. + if (team == null) { + team = scoreboard.registerNewTeam(teamName); + } + + // Add the entry to the team. + if (team.getEntries() == null || team.getEntries().isEmpty() || !team.getEntries().contains(this.identifier)) { + team.addEntry(this.identifier); + } + + // Add the entry if it does not exist. + if (!this.board.getEntries().contains(this)) { + this.board.getEntries().add(this); + } + + this.team = team; + } + + /** + * Send Board Entry Update. + * + * @param position of entry. + */ + public void send(int position) { + if (this.text.length() > 16) { + String prefix = this.text.substring(0, 16); + String suffix; + + if (prefix.charAt(15) == ChatColor.COLOR_CHAR) { + prefix = prefix.substring(0, 15); + suffix = this.text.substring(15, this.text.length()); + } else if (prefix.charAt(14) == ChatColor.COLOR_CHAR) { + prefix = prefix.substring(0, 14); + suffix = this.text.substring(14, this.text.length()); + } else { + if (ChatColor.getLastColors(prefix).equalsIgnoreCase(ChatColor.getLastColors(this.identifier))) { + suffix = this.text.substring(16, this.text.length()); + } else { + suffix = ChatColor.getLastColors(prefix) + this.text.substring(16, this.text.length()); + } + } + + if (suffix.length() > 16) { + suffix = suffix.substring(0, 16); + } + + this.team.setPrefix(prefix); + this.team.setSuffix(suffix); + } else { + this.team.setPrefix(this.text); + this.team.setSuffix(""); + } + + Score score = this.board.getObjective().getScore(this.identifier); + score.setScore(position); + } + + /** + * Remove Board Entry from Board. + */ + public void remove() { + this.board.getIdentifiers().remove(this.identifier); + this.board.getScoreboard().resetScores(this.identifier); + } + +} diff --git a/src/main/java/assemble/AssembleException.java b/src/main/java/assemble/AssembleException.java new file mode 100644 index 0000000..0eeb6d6 --- /dev/null +++ b/src/main/java/assemble/AssembleException.java @@ -0,0 +1,14 @@ +package assemble; + +public class AssembleException extends RuntimeException { + + /** + * Assemble Exception. + * + * @param message attributed to exception. + */ + public AssembleException(String message) { + super(message); + } + +} diff --git a/src/main/java/assemble/AssembleListener.java b/src/main/java/assemble/AssembleListener.java new file mode 100644 index 0000000..98a2f3e --- /dev/null +++ b/src/main/java/assemble/AssembleListener.java @@ -0,0 +1,51 @@ +package assemble; + +import assemble.events.AssembleBoardCreateEvent; +import assemble.events.AssembleBoardDestroyEvent; +import lombok.Getter; +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; + +@Getter +public class AssembleListener implements Listener { + + private Assemble assemble; + + /** + * Assemble Listener. + * + * @param assemble instance. + */ + public AssembleListener(Assemble assemble) { + this.assemble = assemble; + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + AssembleBoardCreateEvent createEvent = new AssembleBoardCreateEvent(event.getPlayer()); + + Bukkit.getPluginManager().callEvent(createEvent); + if (createEvent.isCancelled()) { + return; + } + + getAssemble().getBoards().put(event.getPlayer().getUniqueId(), new AssembleBoard(event.getPlayer(), getAssemble())); + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + AssembleBoardDestroyEvent destroyEvent = new AssembleBoardDestroyEvent(event.getPlayer()); + + Bukkit.getPluginManager().callEvent(destroyEvent); + if (destroyEvent.isCancelled()) { + return; + } + + getAssemble().getBoards().remove(event.getPlayer().getUniqueId()); + event.getPlayer().setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard()); + } + +} diff --git a/src/main/java/assemble/AssembleStyle.java b/src/main/java/assemble/AssembleStyle.java new file mode 100644 index 0000000..561df6f --- /dev/null +++ b/src/main/java/assemble/AssembleStyle.java @@ -0,0 +1,26 @@ +package assemble; + +import lombok.Getter; + +@Getter +public enum AssembleStyle { + + KOHI(true, 15), + VIPER(true, -1), + MODERN(false, 1); + + private boolean descending; + private int startNumber; + + /** + * Assemble Style. + * + * @param descending whether the positions are going down or up. + * @param startNumber from where to loop from. + */ + AssembleStyle(boolean descending, int startNumber) { + this.descending = descending; + this.startNumber = startNumber; + } + +} diff --git a/src/main/java/assemble/AssembleThread.java b/src/main/java/assemble/AssembleThread.java new file mode 100644 index 0000000..45d0169 --- /dev/null +++ b/src/main/java/assemble/AssembleThread.java @@ -0,0 +1,127 @@ +package assemble; + +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; +import org.bukkit.scoreboard.Objective; +import org.bukkit.scoreboard.Scoreboard; + +import java.util.Collections; +import java.util.List; + +public class AssembleThread extends Thread { + + private Assemble assemble; + + /** + * Assemble Thread. + * + * @param assemble instance. + */ + AssembleThread(Assemble assemble) { + this.assemble = assemble; + this.start(); + } + + @Override + public void run() { + while(true) { + try { + tick(); + sleep(assemble.getTicks() * 50); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * Tick logic for thread. + */ + private void tick() { + for (Player player : this.assemble.getPlugin().getServer().getOnlinePlayers()) { + try { + AssembleBoard board = this.assemble.getBoards().get(player.getUniqueId()); + + // This shouldn't happen, but just in case. + if (board == null) { + continue; + } + + Scoreboard scoreboard = board.getScoreboard(); + Objective objective = board.getObjective(); + + if (scoreboard == null || objective == null) { + continue; + } + + // Just make a variable so we don't have to + // process the same thing twice. + String title = ChatColor.translateAlternateColorCodes('&', this.assemble.getAdapter().getTitle(player)); + + // Update the title if needed. + if (!objective.getDisplayName().equals(title)) { + objective.setDisplayName(title); + } + + List newLines = this.assemble.getAdapter().getLines(player); + + // Allow adapter to return null/empty list to display nothing. + if (newLines == null || newLines.isEmpty()) { + board.getEntries().forEach(AssembleBoardEntry::remove); + board.getEntries().clear(); + } else { + if (newLines.size() > 15) { + newLines = this.assemble.getAdapter().getLines(player).subList(0, 15); + } + + // Reverse the lines because scoreboard scores are in descending order. + if (!this.assemble.getAssembleStyle().isDescending()) { + Collections.reverse(newLines); + } + + // Remove excessive amount of board entries. + if (board.getEntries().size() > newLines.size()) { + for (int i = newLines.size(); i < board.getEntries().size(); i++) { + AssembleBoardEntry entry = board.getEntryAtPosition(i); + + if (entry != null) { + entry.remove(); + } + } + } + + // Update existing entries / add new entries. + int cache = this.assemble.getAssembleStyle().getStartNumber(); + for (int i = 0; i < newLines.size(); i++) { + AssembleBoardEntry entry = board.getEntryAtPosition(i); + + // Translate any colors. + String line = ChatColor.translateAlternateColorCodes('&', newLines.get(i)); + + // If the entry is null, just create a new one. + // Creating a new AssembleBoardEntry instance will add + // itself to the provided board's entries list. + if (entry == null) { + entry = new AssembleBoardEntry(board, line, i); + } + + // Update text, setup the team, and update the display values. + entry.setText(line); + entry.setup(); + entry.send( + this.assemble.getAssembleStyle().isDescending() ? cache-- : cache++ + ); + } + } + + if (player.getScoreboard() != scoreboard && !assemble.isHook()) { + player.setScoreboard(scoreboard); + } + } catch(Exception e) { + e.printStackTrace(); + throw new AssembleException("There was an error updating " + player.getName() + "'s scoreboard."); + } + } + } + +} diff --git a/src/main/java/assemble/events/AssembleBoardCreateEvent.java b/src/main/java/assemble/events/AssembleBoardCreateEvent.java new file mode 100644 index 0000000..f67852a --- /dev/null +++ b/src/main/java/assemble/events/AssembleBoardCreateEvent.java @@ -0,0 +1,31 @@ +package assemble.events; + +import lombok.Getter; +import lombok.Setter; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +@Getter @Setter + public class AssembleBoardCreateEvent extends Event implements Cancellable { + + @Getter public static HandlerList handlerList = new HandlerList(); + + private Player player; + private boolean cancelled = false; + + /** + * Assemble Board Create Event. + * + * @param player that the board is being created for. + */ + public AssembleBoardCreateEvent(Player player) { + this.player = player; + } + + @Override + public HandlerList getHandlers() { + return handlerList; + } +} diff --git a/src/main/java/assemble/events/AssembleBoardCreatedEvent.java b/src/main/java/assemble/events/AssembleBoardCreatedEvent.java new file mode 100644 index 0000000..d201da8 --- /dev/null +++ b/src/main/java/assemble/events/AssembleBoardCreatedEvent.java @@ -0,0 +1,30 @@ +package assemble.events; + +import assemble.AssembleBoard; +import lombok.Getter; +import lombok.Setter; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +@Getter @Setter +public class AssembleBoardCreatedEvent extends Event { + + @Getter public static HandlerList handlerList = new HandlerList(); + + private boolean cancelled = false; + private final AssembleBoard board; + + /** + * Assemble Board Created Event. + * + * @param board of player. + */ + public AssembleBoardCreatedEvent(AssembleBoard board) { + this.board = board; + } + + @Override + public HandlerList getHandlers() { + return handlerList; + } +} diff --git a/src/main/java/assemble/events/AssembleBoardDestroyEvent.java b/src/main/java/assemble/events/AssembleBoardDestroyEvent.java new file mode 100644 index 0000000..191f92a --- /dev/null +++ b/src/main/java/assemble/events/AssembleBoardDestroyEvent.java @@ -0,0 +1,31 @@ +package assemble.events; + +import lombok.Getter; +import lombok.Setter; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +@Getter @Setter +public class AssembleBoardDestroyEvent extends Event implements Cancellable { + + @Getter public static HandlerList handlerList = new HandlerList(); + + private Player player; + private boolean cancelled = false; + + /** + * Assemble Board Destroy Event. + * + * @param player who's board got destroyed. + */ + public AssembleBoardDestroyEvent(Player player) { + this.player = player; + } + + @Override + public HandlerList getHandlers() { + return handlerList; + } +} diff --git a/src/main/java/rip/tilly/bedwars/BedWars.java b/src/main/java/rip/tilly/bedwars/BedWars.java index 22f8a49..d351093 100644 --- a/src/main/java/rip/tilly/bedwars/BedWars.java +++ b/src/main/java/rip/tilly/bedwars/BedWars.java @@ -1,5 +1,7 @@ package rip.tilly.bedwars; +import assemble.Assemble; +import assemble.AssembleStyle; import lombok.Getter; import org.bukkit.Bukkit; import org.bukkit.Chunk; @@ -8,6 +10,7 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.plugin.java.JavaPlugin; import rip.tilly.bedwars.listeners.RandomListeners; +import rip.tilly.bedwars.managers.ScoreboardManager; import rip.tilly.bedwars.mongo.MongoManager; import rip.tilly.bedwars.player.PlayerDataHandler; import rip.tilly.bedwars.player.PlayerDataManager; @@ -41,6 +44,10 @@ public final class BedWars extends JavaPlugin { this.loadManagers(); this.loadListeners(); + Assemble assemble = new Assemble(this, new ScoreboardManager()); + assemble.setTicks(2); + assemble.setAssembleStyle(AssembleStyle.VIPER); + for (World world : Bukkit.getWorlds()) { for (Entity entity : world.getEntities()) { if (entity.getType() != EntityType.PLAYER && entity.getType() != EntityType.ITEM_FRAME) { diff --git a/src/main/java/rip/tilly/bedwars/commands/level/LevelCommand.java b/src/main/java/rip/tilly/bedwars/commands/level/LevelCommand.java index 8739800..bd07d33 100644 --- a/src/main/java/rip/tilly/bedwars/commands/level/LevelCommand.java +++ b/src/main/java/rip/tilly/bedwars/commands/level/LevelCommand.java @@ -4,44 +4,40 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import xyz.trixkz.bedwarspractice.Main; -import xyz.trixkz.bedwarspractice.utils.Utils; +import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.utils.CC; public class LevelCommand implements CommandExecutor { - private Main main; - - public LevelCommand(Main main) { - this.main = main; - } + private BedWars main = BedWars.getInstance(); public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { Player player = (Player) sender; - if (!player.hasPermission(Utils.ADMIN_PERMISSION_NODE)) { - player.sendMessage(Utils.translate(Utils.NO_PERMISSION_MESSAGE)); + if (!player.hasPermission("bedwars.admin")) { + player.sendMessage(CC.translate("&cNo permission")); return true; } if (args.length == 0) { - player.sendMessage(Utils.translate("&cUsage:")); - player.sendMessage(Utils.translate(" &c/level")); - player.sendMessage(Utils.translate(" &c/level set ")); - player.sendMessage(Utils.translate(" &c/level add ")); - player.sendMessage(Utils.translate(" &c/level remove ")); + player.sendMessage(CC.translate("&cUsage:")); + player.sendMessage(CC.translate(" &c/level")); + player.sendMessage(CC.translate(" &c/level set ")); + player.sendMessage(CC.translate(" &c/level add ")); + player.sendMessage(CC.translate(" &c/level remove ")); } else { switch (args[0]) { case "set": - new SetCommand(this.main).executeAs(sender, cmd, label, args); + new SetCommand().executeAs(sender, cmd, label, args); break; case "add": - new AddCommand(this.main).executeAs(sender, cmd, label, args); + new AddCommand().executeAs(sender, cmd, label, args); break; case "remove": - new RemoveCommand(this.main).executeAs(sender, cmd, label, args); + new RemoveCommand().executeAs(sender, cmd, label, args); break; } diff --git a/src/main/java/rip/tilly/bedwars/commands/level/RemoveCommand.java b/src/main/java/rip/tilly/bedwars/commands/level/RemoveCommand.java index 299100b..12276b4 100644 --- a/src/main/java/rip/tilly/bedwars/commands/level/RemoveCommand.java +++ b/src/main/java/rip/tilly/bedwars/commands/level/RemoveCommand.java @@ -4,12 +4,9 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.commands.BaseCommand; import rip.tilly.bedwars.player.PlayerData; import rip.tilly.bedwars.utils.CC; -import xyz.trixkz.bedwarspractice.Main; -import xyz.trixkz.bedwarspractice.commands.BaseCommand; -import xyz.trixkz.bedwarspractice.managers.user.User; -import xyz.trixkz.bedwarspractice.utils.Utils; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/rip/tilly/bedwars/commands/level/SetCommand.java b/src/main/java/rip/tilly/bedwars/commands/level/SetCommand.java index c0e3b1d..ac5fe2d 100644 --- a/src/main/java/rip/tilly/bedwars/commands/level/SetCommand.java +++ b/src/main/java/rip/tilly/bedwars/commands/level/SetCommand.java @@ -3,21 +3,17 @@ package rip.tilly.bedwars.commands.level; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import xyz.trixkz.bedwarspractice.Main; -import xyz.trixkz.bedwarspractice.commands.BaseCommand; -import xyz.trixkz.bedwarspractice.managers.user.User; -import xyz.trixkz.bedwarspractice.utils.Utils; +import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.commands.BaseCommand; +import rip.tilly.bedwars.player.PlayerData; +import rip.tilly.bedwars.utils.CC; import java.util.ArrayList; import java.util.List; public class SetCommand extends BaseCommand { - private Main main; - - public SetCommand(Main main) { - this.main = main; - } + private BedWars main = BedWars.getInstance(); @Override public void executeAs(CommandSender sender, Command cmd, String label, String[] args) { @@ -25,23 +21,23 @@ public class SetCommand extends BaseCommand { Player target = this.main.getServer().getPlayer(args[1]); - User user = User.getByUUID(target.getUniqueId()); + PlayerData playerData = this.main.getPlayerDataManager().getPlayerData(player.getUniqueId()); if (!target.isOnline()) { - player.sendMessage(Utils.translate("&cError: Player is not currently online")); + player.sendMessage(CC.translate("&cError: Player is not currently online")); return; } - if (!this.main.getUtils().isNumeric(args[2])) { - player.sendMessage(Utils.translate("&cError: Please send a valid number")); + if (!CC.isNumeric(args[2])) { + player.sendMessage(CC.translate("&cError: Please send a valid number")); return; } Integer amount = Integer.parseInt(args[2]); - user.getSettings().setLevel(amount); + playerData.setLevel(amount); } @Override diff --git a/src/main/java/rip/tilly/bedwars/commands/setspawn/SpawnCommand.java b/src/main/java/rip/tilly/bedwars/commands/setspawn/SpawnCommand.java index 8000f2e..2f31ef8 100644 --- a/src/main/java/rip/tilly/bedwars/commands/setspawn/SpawnCommand.java +++ b/src/main/java/rip/tilly/bedwars/commands/setspawn/SpawnCommand.java @@ -5,6 +5,7 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.commands.BaseCommand; import rip.tilly.bedwars.utils.CC; import java.util.ArrayList; diff --git a/src/main/java/rip/tilly/bedwars/commands/xp/AddCommand.java b/src/main/java/rip/tilly/bedwars/commands/xp/AddCommand.java index f4f7834..23a29ac 100644 --- a/src/main/java/rip/tilly/bedwars/commands/xp/AddCommand.java +++ b/src/main/java/rip/tilly/bedwars/commands/xp/AddCommand.java @@ -3,21 +3,17 @@ package rip.tilly.bedwars.commands.xp; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import xyz.trixkz.bedwarspractice.Main; -import xyz.trixkz.bedwarspractice.commands.BaseCommand; -import xyz.trixkz.bedwarspractice.managers.user.User; -import xyz.trixkz.bedwarspractice.utils.Utils; +import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.commands.BaseCommand; +import rip.tilly.bedwars.player.PlayerData; +import rip.tilly.bedwars.utils.CC; import java.util.ArrayList; import java.util.List; public class AddCommand extends BaseCommand { - private Main main; - - public AddCommand(Main main) { - this.main = main; - } + private BedWars main = BedWars.getInstance(); @Override public void executeAs(CommandSender sender, Command cmd, String label, String[] args) { @@ -25,23 +21,23 @@ public class AddCommand extends BaseCommand { Player target = this.main.getServer().getPlayer(args[1]); - User user = User.getByUUID(target.getUniqueId()); + PlayerData playerData = this.main.getPlayerDataManager().getPlayerData(player.getUniqueId()); if (!target.isOnline()) { - player.sendMessage(Utils.translate("&cError: Player is not currently online")); + player.sendMessage(CC.translate("&cError: Player is not currently online")); return; } - if (!this.main.getUtils().isNumeric(args[2])) { - player.sendMessage(Utils.translate("&cError: Please send a valid number")); + if (!CC.isNumeric(args[2])) { + player.sendMessage(CC.translate("&cError: Please send a valid number")); return; } Double amount = Double.parseDouble(args[2]); - user.getSettings().addXp(target, amount); + playerData.setXp(playerData.getXp() + amount); } @Override diff --git a/src/main/java/rip/tilly/bedwars/commands/xp/RemoveCommand.java b/src/main/java/rip/tilly/bedwars/commands/xp/RemoveCommand.java index 443dc96..f7e0461 100644 --- a/src/main/java/rip/tilly/bedwars/commands/xp/RemoveCommand.java +++ b/src/main/java/rip/tilly/bedwars/commands/xp/RemoveCommand.java @@ -3,21 +3,17 @@ package rip.tilly.bedwars.commands.xp; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import xyz.trixkz.bedwarspractice.Main; -import xyz.trixkz.bedwarspractice.commands.BaseCommand; -import xyz.trixkz.bedwarspractice.managers.user.User; -import xyz.trixkz.bedwarspractice.utils.Utils; +import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.commands.BaseCommand; +import rip.tilly.bedwars.player.PlayerData; +import rip.tilly.bedwars.utils.CC; import java.util.ArrayList; import java.util.List; public class RemoveCommand extends BaseCommand { - private Main main; - - public RemoveCommand(Main main) { - this.main = main; - } + private BedWars main = BedWars.getInstance(); @Override public void executeAs(CommandSender sender, Command cmd, String label, String[] args) { @@ -25,23 +21,23 @@ public class RemoveCommand extends BaseCommand { Player target = this.main.getServer().getPlayer(args[1]); - User user = User.getByUUID(target.getUniqueId()); + PlayerData playerData = this.main.getPlayerDataManager().getPlayerData(player.getUniqueId()); if (!target.isOnline()) { - player.sendMessage(Utils.translate("&cError: Player is not currently online")); + player.sendMessage(CC.translate("&cError: Player is not currently online")); return; } - if (!this.main.getUtils().isNumeric(args[2])) { - player.sendMessage(Utils.translate("&cError: Please send a valid number")); + if (!CC.isNumeric(args[2])) { + player.sendMessage(CC.translate("&cError: Please send a valid number")); return; } Double amount = Double.parseDouble(args[2]); - user.getSettings().removeXp(amount); + playerData.setXp(playerData.getXp() - amount); } @Override diff --git a/src/main/java/rip/tilly/bedwars/commands/xp/SetCommand.java b/src/main/java/rip/tilly/bedwars/commands/xp/SetCommand.java index 8098637..8f86b72 100644 --- a/src/main/java/rip/tilly/bedwars/commands/xp/SetCommand.java +++ b/src/main/java/rip/tilly/bedwars/commands/xp/SetCommand.java @@ -3,21 +3,17 @@ package rip.tilly.bedwars.commands.xp; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import xyz.trixkz.bedwarspractice.Main; -import xyz.trixkz.bedwarspractice.commands.BaseCommand; -import xyz.trixkz.bedwarspractice.managers.user.User; -import xyz.trixkz.bedwarspractice.utils.Utils; +import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.commands.BaseCommand; +import rip.tilly.bedwars.player.PlayerData; +import rip.tilly.bedwars.utils.CC; import java.util.ArrayList; import java.util.List; public class SetCommand extends BaseCommand { - private Main main; - - public SetCommand(Main main) { - this.main = main; - } + private BedWars main = BedWars.getInstance(); @Override public void executeAs(CommandSender sender, Command cmd, String label, String[] args) { @@ -25,23 +21,23 @@ public class SetCommand extends BaseCommand { Player target = this.main.getServer().getPlayer(args[1]); - User user = User.getByUUID(target.getUniqueId()); + PlayerData playerData = this.main.getPlayerDataManager().getPlayerData(player.getUniqueId()); if (!target.isOnline()) { - player.sendMessage(Utils.translate("&cError: Player is not currently online")); + player.sendMessage(CC.translate("&cError: Player is not currently online")); return; } - if (!this.main.getUtils().isNumeric(args[2])) { - player.sendMessage(Utils.translate("&cError: Please send a valid number")); + if (!CC.isNumeric(args[2])) { + player.sendMessage(CC.translate("&cError: Please send a valid number")); return; } Double amount = Double.parseDouble(args[2]); - user.getSettings().setXp(amount); + playerData.setXp(amount); } @Override diff --git a/src/main/java/rip/tilly/bedwars/commands/xp/XpCommand.java b/src/main/java/rip/tilly/bedwars/commands/xp/XpCommand.java index d5c81a2..70a55b7 100644 --- a/src/main/java/rip/tilly/bedwars/commands/xp/XpCommand.java +++ b/src/main/java/rip/tilly/bedwars/commands/xp/XpCommand.java @@ -4,44 +4,40 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import xyz.trixkz.bedwarspractice.Main; -import xyz.trixkz.bedwarspractice.utils.Utils; +import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.utils.CC; public class XpCommand implements CommandExecutor { - private Main main; - - public XpCommand(Main main) { - this.main = main; - } + private BedWars main = BedWars.getInstance(); public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { Player player = (Player) sender; - if (!player.hasPermission(Utils.ADMIN_PERMISSION_NODE)) { - player.sendMessage(Utils.translate(Utils.NO_PERMISSION_MESSAGE)); + if (!player.hasPermission("bedwars.admin")) { + player.sendMessage(CC.translate("&cNo permission")); return true; } if (args.length == 0) { - player.sendMessage(Utils.translate("&cUsage:")); - player.sendMessage(Utils.translate(" &c/xp")); - player.sendMessage(Utils.translate(" &c/xp set ")); - player.sendMessage(Utils.translate(" &c/xp add ")); - player.sendMessage(Utils.translate(" &c/xp remove ")); + player.sendMessage(CC.translate("&cUsage:")); + player.sendMessage(CC.translate(" &c/xp")); + player.sendMessage(CC.translate(" &c/xp set ")); + player.sendMessage(CC.translate(" &c/xp add ")); + player.sendMessage(CC.translate(" &c/xp remove ")); } else { switch (args[0]) { case "set": - new SetCommand(this.main).executeAs(sender, cmd, label, args); + new SetCommand().executeAs(sender, cmd, label, args); break; case "add": - new AddCommand(this.main).executeAs(sender, cmd, label, args); + new AddCommand().executeAs(sender, cmd, label, args); break; case "remove": - new RemoveCommand(this.main).executeAs(sender, cmd, label, args); + new RemoveCommand().executeAs(sender, cmd, label, args); break; } diff --git a/src/main/java/rip/tilly/bedwars/managers/CommandManager.java b/src/main/java/rip/tilly/bedwars/managers/CommandManager.java index d09fa4d..ea327f9 100644 --- a/src/main/java/rip/tilly/bedwars/managers/CommandManager.java +++ b/src/main/java/rip/tilly/bedwars/managers/CommandManager.java @@ -1,7 +1,9 @@ package rip.tilly.bedwars.managers; import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.commands.level.LevelCommand; import rip.tilly.bedwars.commands.setspawn.SetSpawnCommand; +import rip.tilly.bedwars.commands.xp.XpCommand; public class CommandManager { @@ -13,5 +15,7 @@ public class CommandManager { private void registerCommands() { this.main.getCommand("setspawn").setExecutor(new SetSpawnCommand()); + this.main.getCommand("level").setExecutor(new LevelCommand()); + this.main.getCommand("xp").setExecutor(new XpCommand()); } } diff --git a/src/main/java/rip/tilly/bedwars/managers/ScoreboardManager.java b/src/main/java/rip/tilly/bedwars/managers/ScoreboardManager.java new file mode 100644 index 0000000..0c3a32d --- /dev/null +++ b/src/main/java/rip/tilly/bedwars/managers/ScoreboardManager.java @@ -0,0 +1,62 @@ +package rip.tilly.bedwars.managers; + +import assemble.AssembleAdapter; +import org.bukkit.entity.Player; +import rip.tilly.bedwars.BedWars; +import rip.tilly.bedwars.player.PlayerData; +import rip.tilly.bedwars.utils.CC; + +import java.util.ArrayList; +import java.util.List; + +public class ScoreboardManager implements AssembleAdapter { + + private BedWars main = BedWars.getInstance(); + + @Override + public String getTitle(Player player) { + return CC.translate("&d&lBedWars"); + } + + @Override + public List getLines(Player player) { + List lines = new ArrayList(); + + PlayerData playerData = this.main.getPlayerDataManager().getPlayerData(player.getUniqueId()); + + lines.add(CC.translate(CC.scoreboardBar)); + lines.add(CC.translate("&fOnline: &d" + this.main.getServer().getOnlinePlayers().size())); + lines.add(CC.translate("&fPlaying: &d")); + lines.add(CC.translate("")); + + if (true) { + lines.add(CC.translate("&fLevel: &d" + playerData.getLevel())); + + String finishedProgress = ""; + + int notFinishedProgress = 10; + + for (int i = 0; i < playerData.getXp() * 100; i++) { + if (i % 10 == 0) { + finishedProgress += "⬛"; + + notFinishedProgress--; + } + } + + String leftOverProgress = ""; + + for (int i = 1; i <= notFinishedProgress; i++) { + leftOverProgress += "⬛"; + } + + lines.add(CC.translate("&8" + finishedProgress + "&7" + leftOverProgress + " &7(" + ((int) (playerData.getXp() * 100)) + "%&7)")); + lines.add(CC.translate("")); + } + + lines.add(CC.translate("&dtilly.rip")); + lines.add(CC.translate(CC.scoreboardBar)); + + return lines; + } +} diff --git a/src/main/java/rip/tilly/bedwars/player/PlayerData.java b/src/main/java/rip/tilly/bedwars/player/PlayerData.java index 660b363..8776085 100644 --- a/src/main/java/rip/tilly/bedwars/player/PlayerData.java +++ b/src/main/java/rip/tilly/bedwars/player/PlayerData.java @@ -24,7 +24,7 @@ public class PlayerData { private int kills; private int deaths; - private int xp; + private double xp; private int level; private int wins; private int losses; diff --git a/src/main/java/rip/tilly/bedwars/player/PlayerDataManager.java b/src/main/java/rip/tilly/bedwars/player/PlayerDataManager.java index 05009a9..f997f29 100644 --- a/src/main/java/rip/tilly/bedwars/player/PlayerDataManager.java +++ b/src/main/java/rip/tilly/bedwars/player/PlayerDataManager.java @@ -40,7 +40,7 @@ public class PlayerDataManager { if (document != null) { playerData.setKills(document.getInteger("kills")); playerData.setDeaths(document.getInteger("deaths")); - playerData.setXp(document.getInteger("xp")); + playerData.setXp(document.getDouble("xp")); playerData.setLevel(document.getInteger("level")); playerData.setWins(document.getInteger("wins")); playerData.setLosses(document.getInteger("losses")); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 1dda7d1..c9b2c73 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,9 +1,11 @@ name: BedWars -version: ${project.version} +version: 1.0 main: rip.tilly.bedwars.BedWars authors: - Lucanius - Trixkz commands: - setspawn: \ No newline at end of file + setspawn: + level: + xp: \ No newline at end of file