From 62f05196c73d7b87efb74520bc33d21dd00259d6 Mon Sep 17 00:00:00 2001 From: Fabrizio La Rosa Date: Wed, 24 Jun 2020 19:42:38 +0200 Subject: [PATCH] Allow toggling scoreboards with /is scoreboard --- .../skyblock/command/CommandManager.java | 1 + .../commands/island/ScoreboardCommand.java | 82 +++++++++++ .../com/songoda/skyblock/listeners/Join.java | 36 +---- .../skyblock/playerdata/PlayerData.java | 11 +- .../scoreboard/ScoreboardManager.java | 132 +++++++++++++----- src/main/resources/language.yml | 11 ++ 6 files changed, 201 insertions(+), 72 deletions(-) create mode 100644 src/main/java/com/songoda/skyblock/command/commands/island/ScoreboardCommand.java diff --git a/src/main/java/com/songoda/skyblock/command/CommandManager.java b/src/main/java/com/songoda/skyblock/command/CommandManager.java index 5b844db1..97778834 100644 --- a/src/main/java/com/songoda/skyblock/command/CommandManager.java +++ b/src/main/java/com/songoda/skyblock/command/CommandManager.java @@ -89,6 +89,7 @@ public class CommandManager implements CommandExecutor, TabCompleter { new VisitCommand(), new VisitorsCommand(), new VoteCommand(), + new ScoreboardCommand(), new WeatherCommand() ); diff --git a/src/main/java/com/songoda/skyblock/command/commands/island/ScoreboardCommand.java b/src/main/java/com/songoda/skyblock/command/commands/island/ScoreboardCommand.java new file mode 100644 index 00000000..cfe4ee8a --- /dev/null +++ b/src/main/java/com/songoda/skyblock/command/commands/island/ScoreboardCommand.java @@ -0,0 +1,82 @@ +package com.songoda.skyblock.command.commands.island; + +import com.songoda.core.compatibility.CompatibleSound; +import com.songoda.skyblock.command.SubCommand; +import com.songoda.skyblock.config.FileManager.Config; +import com.songoda.skyblock.island.Island; +import com.songoda.skyblock.island.IslandManager; +import com.songoda.skyblock.island.IslandRole; +import com.songoda.skyblock.message.MessageManager; +import com.songoda.skyblock.permission.PermissionManager; +import com.songoda.skyblock.playerdata.PlayerData; +import com.songoda.skyblock.playerdata.PlayerDataManager; +import com.songoda.skyblock.scoreboard.ScoreboardManager; +import com.songoda.skyblock.sound.SoundManager; +import org.bukkit.Bukkit; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; + +import java.io.File; + +public class ScoreboardCommand extends SubCommand { + + @Override + public void onCommandByPlayer(Player player, String[] args) { + MessageManager messageManager = skyblock.getMessageManager(); + PlayerDataManager playerDataManager = skyblock.getPlayerDataManager(); + SoundManager soundManager = skyblock.getSoundManager(); + ScoreboardManager scoreboardManager = skyblock.getScoreboardManager(); + + Config config = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml")); + FileConfiguration configLoad = config.getFileConfiguration(); + + PlayerData playerData = playerDataManager.getPlayerData(player); + + if (playerData == null) { + messageManager.sendMessage(player, configLoad.getString("Command.Scoreboard.Error.Message")); + soundManager.playSound(player, CompatibleSound.BLOCK_ANVIL_LAND.getSound(), 1.0F, 1.0F); + } else if(!skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "config.yml")) + .getFileConfiguration().getBoolean("Island.Scoreboard.Enable", false)) { + messageManager.sendMessage(player, configLoad.getString("Command.Scoreboard.GlobalDisable.Message")); + soundManager.playSound(player, CompatibleSound.BLOCK_ANVIL_LAND.getSound(), 1.0F, 1.0F); + }else if (playerData.isScoreboard()) { + playerData.setScoreboard(false); + scoreboardManager.removePlayer(player); + + messageManager.sendMessage(player, configLoad.getString("Command.Scoreboard.Disabled.Message")); + soundManager.playSound(player, CompatibleSound.BLOCK_WOODEN_DOOR_CLOSE.getSound(), 1.0F, 1.0F); + } else { + playerData.setScoreboard(true); + scoreboardManager.addPlayer(player); + + messageManager.sendMessage(player, configLoad.getString("Command.Scoreboard.Enabled.Message")); + soundManager.playSound(player, CompatibleSound.BLOCK_WOODEN_DOOR_OPEN.getSound(), 1.0F, 1.0F); + } + } + + @Override + public void onCommandByConsole(ConsoleCommandSender sender, String[] args) { + sender.sendMessage("SkyBlock | Error: You must be a player to perform that command."); + } + + @Override + public String getName() { + return "scoreboard"; + } + + @Override + public String getInfoMessagePath() { + return "Command.Scoreboard.Info.Message"; + } + + @Override + public String[] getAliases() { + return new String[0]; + } + + @Override + public String[] getArguments() { + return new String[0]; + } +} diff --git a/src/main/java/com/songoda/skyblock/listeners/Join.java b/src/main/java/com/songoda/skyblock/listeners/Join.java index 82a702a3..9733fdc1 100644 --- a/src/main/java/com/songoda/skyblock/listeners/Join.java +++ b/src/main/java/com/songoda/skyblock/listeners/Join.java @@ -105,41 +105,7 @@ public class Join implements Listener { cooldownManager.addCooldownPlayer(CooldownType.Deletion, cooldownManager.loadCooldownPlayer(CooldownType.Deletion, player)); if (scoreboardManager != null) { - Config config = fileManager.getConfig(new File(skyblock.getDataFolder(), "language.yml")); - Scoreboard scoreboard = new Scoreboard(skyblock, player); - Island island = islandManager.getIsland(player); - - if (island != null) { - OfflinePlayer offlinePlayer = Bukkit.getServer().getOfflinePlayer(island.getOwnerUUID()); - - cooldownManager.addCooldownPlayer(CooldownType.Levelling, cooldownManager.loadCooldownPlayer(CooldownType.Levelling, offlinePlayer)); - cooldownManager.addCooldownPlayer(CooldownType.Ownership, cooldownManager.loadCooldownPlayer(CooldownType.Ownership, offlinePlayer)); - - if (island.getRole(IslandRole.Member).size() == 0 && island.getRole(IslandRole.Operator).size() == 0) { - scoreboard.setDisplayName(ChatColor.translateAlternateColorCodes('&', config.getFileConfiguration().getString("Scoreboard.Island.Solo.Displayname"))); - - if (islandManager.getVisitorsAtIsland(island).size() == 0) { - scoreboard.setDisplayList(config.getFileConfiguration().getStringList("Scoreboard.Island.Solo.Empty.Displaylines")); - } else { - scoreboard.setDisplayList(config.getFileConfiguration().getStringList("Scoreboard.Island.Solo.Occupied.Displaylines")); - } - } else { - scoreboard.setDisplayName(ChatColor.translateAlternateColorCodes('&', config.getFileConfiguration().getString("Scoreboard.Island.Team.Displayname"))); - - if (islandManager.getVisitorsAtIsland(island).size() == 0) { - scoreboard.setDisplayList(config.getFileConfiguration().getStringList("Scoreboard.Island.Team.Empty.Displaylines")); - } else { - scoreboard.setDisplayList(config.getFileConfiguration().getStringList("Scoreboard.Island.Team.Occupied.Displaylines")); - } - - } - } else { - scoreboard.setDisplayName(ChatColor.translateAlternateColorCodes('&', config.getFileConfiguration().getString("Scoreboard.Tutorial.Displayname"))); - scoreboard.setDisplayList(config.getFileConfiguration().getStringList("Scoreboard.Tutorial.Displaylines")); - } - - scoreboard.run(); - scoreboardManager.storeScoreboard(player, scoreboard); + scoreboardManager.addPlayer(player); } Island island = islandManager.getIslandPlayerAt(player); diff --git a/src/main/java/com/songoda/skyblock/playerdata/PlayerData.java b/src/main/java/com/songoda/skyblock/playerdata/PlayerData.java index 43868005..e72addcd 100644 --- a/src/main/java/com/songoda/skyblock/playerdata/PlayerData.java +++ b/src/main/java/com/songoda/skyblock/playerdata/PlayerData.java @@ -153,15 +153,22 @@ public class PlayerData { this.playTime = playTime; } + public boolean isScoreboard() { + return getConfig().getFileConfiguration().getBoolean("Scoreboard", true); + } + + public void setScoreboard(boolean scoreboard) { + getConfig().getFileConfiguration().set("Scoreboard", scoreboard); + } + public boolean isPreview() { return preview; } - + public void setPreview(boolean preview) { this.preview = preview; } - public int getVisitTime() { return visitTime; } diff --git a/src/main/java/com/songoda/skyblock/scoreboard/ScoreboardManager.java b/src/main/java/com/songoda/skyblock/scoreboard/ScoreboardManager.java index 8e5f8b40..1f94c4e2 100644 --- a/src/main/java/com/songoda/skyblock/scoreboard/ScoreboardManager.java +++ b/src/main/java/com/songoda/skyblock/scoreboard/ScoreboardManager.java @@ -3,12 +3,17 @@ package com.songoda.skyblock.scoreboard; import com.songoda.skyblock.SkyBlock; import com.songoda.skyblock.config.FileManager; import com.songoda.skyblock.config.FileManager.Config; +import com.songoda.skyblock.cooldown.CooldownManager; +import com.songoda.skyblock.cooldown.CooldownType; import com.songoda.skyblock.island.Island; import com.songoda.skyblock.island.IslandManager; import com.songoda.skyblock.island.IslandRole; +import com.songoda.skyblock.playerdata.PlayerData; +import com.songoda.skyblock.playerdata.PlayerDataManager; import com.songoda.skyblock.utils.version.NMSUtil; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scoreboard.Objective; @@ -23,6 +28,8 @@ public class ScoreboardManager extends BukkitRunnable { private final static int VERSION = NMSUtil.getVersionNumber(); private final SkyBlock skyblock; private final Map scoreboardStorage = new HashMap<>(); + + private final PlayerDataManager playerDataManager; private int runTicks = 0; @@ -31,6 +38,7 @@ public class ScoreboardManager extends BukkitRunnable { public ScoreboardManager(SkyBlock skyblock) { this.skyblock = skyblock; + this.playerDataManager = skyblock.getPlayerDataManager(); this.runTaskTimer(skyblock, 20, 40); } @@ -95,42 +103,45 @@ public class ScoreboardManager extends BukkitRunnable { */ for (Player player : players) { - - final org.bukkit.scoreboard.Scoreboard playerBoard = player.getScoreboard(); - - for (Objective primaryObjective : objectives) { - - Objective obj = playerBoard.getObjective(primaryObjective.getName()); - - if (obj == null) - obj = playerBoard.registerNewObjective(primaryObjective.getName(), primaryObjective.getCriteria()); - - obj.setDisplayName(primaryObjective.getDisplayName()); - obj.setDisplaySlot(primaryObjective.getDisplaySlot()); - if (VERSION > 12) obj.setRenderType(primaryObjective.getRenderType()); - } - - for (Team primaryTeam : teams) { - - Team obj = playerBoard.getTeam(primaryTeam.getName()); - - if (obj == null) obj = playerBoard.registerNewTeam(primaryTeam.getName()); - - obj.setAllowFriendlyFire(primaryTeam.allowFriendlyFire()); - obj.setCanSeeFriendlyInvisibles(primaryTeam.canSeeFriendlyInvisibles()); - if (VERSION > 11) obj.setColor(primaryTeam.getColor()); - obj.setDisplayName(primaryTeam.getDisplayName()); - obj.setNameTagVisibility(primaryTeam.getNameTagVisibility()); - obj.setPrefix(primaryTeam.getPrefix()); - obj.setSuffix(primaryTeam.getSuffix()); - - for (String primaryEntry : primaryTeam.getEntries()) { - obj.addEntry(primaryEntry); + + PlayerData pd = playerDataManager.getPlayerData(player); + if(pd != null && pd.isScoreboard()){ + final org.bukkit.scoreboard.Scoreboard playerBoard = player.getScoreboard(); + + for (Objective primaryObjective : objectives) { + + Objective obj = playerBoard.getObjective(primaryObjective.getName()); + + if (obj == null) + obj = playerBoard.registerNewObjective(primaryObjective.getName(), primaryObjective.getCriteria()); + + obj.setDisplayName(primaryObjective.getDisplayName()); + obj.setDisplaySlot(primaryObjective.getDisplaySlot()); + if (VERSION > 12) obj.setRenderType(primaryObjective.getRenderType()); } - - if (VERSION > 8) { - for (Option option : Option.values()) { - obj.setOption(option, primaryTeam.getOption(option)); + + for (Team primaryTeam : teams) { + + Team obj = playerBoard.getTeam(primaryTeam.getName()); + + if (obj == null) obj = playerBoard.registerNewTeam(primaryTeam.getName()); + + obj.setAllowFriendlyFire(primaryTeam.allowFriendlyFire()); + obj.setCanSeeFriendlyInvisibles(primaryTeam.canSeeFriendlyInvisibles()); + if (VERSION > 11) obj.setColor(primaryTeam.getColor()); + obj.setDisplayName(primaryTeam.getDisplayName()); + obj.setNameTagVisibility(primaryTeam.getNameTagVisibility()); + obj.setPrefix(primaryTeam.getPrefix()); + obj.setSuffix(primaryTeam.getSuffix()); + + for (String primaryEntry : primaryTeam.getEntries()) { + obj.addEntry(primaryEntry); + } + + if (VERSION > 8) { + for (Option option : Option.values()) { + obj.setOption(option, primaryTeam.getOption(option)); + } } } } @@ -211,4 +222,55 @@ public class ScoreboardManager extends BukkitRunnable { public boolean hasScoreboard(Player player) { return scoreboardStorage.containsKey(player.getUniqueId()); } + + public Map getScoreboardStorage() { + return this.scoreboardStorage; + } + + public void addPlayer(Player player){ + CooldownManager cooldownManager = skyblock.getCooldownManager(); + FileManager fileManager = skyblock.getFileManager(); + IslandManager islandManager = skyblock.getIslandManager(); + + Config config = fileManager.getConfig(new File(skyblock.getDataFolder(), "language.yml")); + Scoreboard scoreboard = new Scoreboard(skyblock, player); + Island island = islandManager.getIsland(player); + + if (island != null) { + OfflinePlayer offlinePlayer = Bukkit.getServer().getOfflinePlayer(island.getOwnerUUID()); + + cooldownManager.addCooldownPlayer(CooldownType.Levelling, cooldownManager.loadCooldownPlayer(CooldownType.Levelling, offlinePlayer)); + cooldownManager.addCooldownPlayer(CooldownType.Ownership, cooldownManager.loadCooldownPlayer(CooldownType.Ownership, offlinePlayer)); + + if (island.getRole(IslandRole.Member).size() == 0 && island.getRole(IslandRole.Operator).size() == 0) { + scoreboard.setDisplayName(ChatColor.translateAlternateColorCodes('&', config.getFileConfiguration().getString("Scoreboard.Island.Solo.Displayname"))); + + if (islandManager.getVisitorsAtIsland(island).size() == 0) { + scoreboard.setDisplayList(config.getFileConfiguration().getStringList("Scoreboard.Island.Solo.Empty.Displaylines")); + } else { + scoreboard.setDisplayList(config.getFileConfiguration().getStringList("Scoreboard.Island.Solo.Occupied.Displaylines")); + } + } else { + scoreboard.setDisplayName(ChatColor.translateAlternateColorCodes('&', config.getFileConfiguration().getString("Scoreboard.Island.Team.Displayname"))); + + if (islandManager.getVisitorsAtIsland(island).size() == 0) { + scoreboard.setDisplayList(config.getFileConfiguration().getStringList("Scoreboard.Island.Team.Empty.Displaylines")); + } else { + scoreboard.setDisplayList(config.getFileConfiguration().getStringList("Scoreboard.Island.Team.Occupied.Displaylines")); + } + + } + } else { + scoreboard.setDisplayName(ChatColor.translateAlternateColorCodes('&', config.getFileConfiguration().getString("Scoreboard.Tutorial.Displayname"))); + scoreboard.setDisplayList(config.getFileConfiguration().getStringList("Scoreboard.Tutorial.Displaylines")); + } + + scoreboard.run(); + this.storeScoreboard(player, scoreboard); + } + + public void removePlayer(Player player){ + player.setScoreboard(Bukkit.getScoreboardManager().getNewScoreboard()); + this.scoreboardStorage.remove(player.getUniqueId()); + } } \ No newline at end of file diff --git a/src/main/resources/language.yml b/src/main/resources/language.yml index 098e950e..f039dd40 100644 --- a/src/main/resources/language.yml +++ b/src/main/resources/language.yml @@ -7,6 +7,17 @@ ClassLocalization: Operator: 'Operator' Owner: 'Owner' Command: + Scoreboard: + Disabled: + Message: '&bSkyBlock &8| &aInfo&8: &eScoreboard disabled.' + Enabled: + Message: '&bSkyBlock &8| &aInfo&8: &eScoreboard enabled.' + Error: + Message: '&bSkyBlock &8| &cError&8: &eAn error occurred.' + GlobalDisable: + Message: '&bSkyBlock &8| &cError&8: &eScoreboard are globally disabled.' + Info: + Message: '&bSkyBlock &8| &aInfo&8: &eEnable or disabled the scoreboard.' Island: Bans: Owner: