Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

Commit

Permalink
Scoreboard saving should be complete
Browse files Browse the repository at this point in the history
  • Loading branch information
CactusPuppy committed Aug 29, 2018
1 parent 49140fc commit 923be64
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 80 deletions.
140 changes: 69 additions & 71 deletions src/main/java/usa/cactuspuppy/uhc_automation/Commands/CommandTeam.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import usa.cactuspuppy.uhc_automation.Main;
import usa.cactuspuppy.uhc_automation.ScoreboardUtils.ScoreboardIO;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -49,87 +50,84 @@ public static void onCommand(CommandSender commandSender, String[] args) {
mainSB.registerNewTeam(teamName);
commandSender.sendMessage(ChatColor.GREEN + "Successfully created team " + ChatColor.WHITE + teamName);
Main.getInstance().getLogger().info("Created team " + teamName + " in " + Main.getInstance().getConfig().getString("event-name"));
return;
}
Team team = mainSB.getTeam(args[1]);
if (team == null) {
commandSender.sendMessage(ChatColor.RED + "Could not find team " + ChatColor.WHITE + args[1]);
return;
}
/*Remove Team*/
if (subcommand.equals("remove")) {
team.unregister();
commandSender.sendMessage(ChatColor.GREEN + "Successfully removed team " + ChatColor.WHITE + args[1]);
Main.getInstance().getLogger().info("Removed team " + args[1] + " in " + Main.getInstance().getConfig().getString("event-name"));
return;
}
/*Join Team*/
if (subcommand.equals("join")) {
if (args.length < 3) {
commandSender.sendMessage(ChatColor.RED + "Usage: /uhc team join <team name> <player name>");
return;
}
if (Bukkit.getPlayerExact(args[2]) == null) {
commandSender.sendMessage(ChatColor.RED + "Could not find player " + ChatColor.WHITE + args[2]);
} else {
Team team = mainSB.getTeam(args[1]);
if (team == null) {
commandSender.sendMessage(ChatColor.RED + "Could not find team " + ChatColor.WHITE + args[1]);
return;
}
team.addEntry(args[2]);
commandSender.sendMessage(ChatColor.GREEN + "Successfully added " + ChatColor.WHITE + args[2] + ChatColor.GREEN + " to team " + ChatColor.WHITE + team.getName());
Main.getInstance().getLogger().info("Added " + args[2] + "to team " + team.getName() + " in " + Main.getInstance().getConfig().getString("event-name"));
return;
}
/*Leave Team*/
if (subcommand.equals("leave")) {
if (args.length < 3) {
commandSender.sendMessage(ChatColor.RED + "Usage: /uhc team leave <team name> <player name>");
return;
}
if (Bukkit.getPlayerExact(args[2]) == null) {
commandSender.sendMessage(ChatColor.RED + "Could not find player " + ChatColor.WHITE + args[2]);
return;
}
team.removeEntry(args[2]);
commandSender.sendMessage(ChatColor.GREEN + "Successfully removed " + ChatColor.WHITE + args[2] + ChatColor.GREEN + " from team " + ChatColor.WHITE + team.getName());
}
/*Options*/
if (subcommand.equals("option")) {
if (args.length < 3) {
commandSender.sendMessage(ChatColor.RED + "Usage: /uhc team option <team name> <option> [value]");
return;
}
if (Arrays.stream(Team.Option.values()).noneMatch((v) -> v.name().equals(args[2].toUpperCase())) && !args[2].equals("color")) {
commandSender.sendMessage(ChatColor.RED + "Unknown option " + ChatColor.WHITE + args[2] + ChatColor.RED + ".\n"
+ ChatColor.YELLOW + "Acceptable options: " + ChatColor.WHITE + "color, name_tag_visibility, death_message_visibility, collision_rule");
return;
}
// color first
if (args[2].equals("color")) {
if (args.length == 3) {
commandSender.sendMessage(ChatColor.GOLD + "The color of team " + ChatColor.WHITE + team.getName() + ChatColor.GOLD + " is " + ChatColor.WHITE + team.getColor().name());
/*Remove Team*/
if (subcommand.equals("remove")) {
team.unregister();
commandSender.sendMessage(ChatColor.GREEN + "Successfully removed team " + ChatColor.WHITE + args[1]);
Main.getInstance().getLogger().info("Removed team " + args[1] + " in " + Main.getInstance().getConfig().getString("event-name"));
/*Join Team*/
} else if (subcommand.equals("join")) {
if (args.length < 3) {
commandSender.sendMessage(ChatColor.RED + "Usage: /uhc team join <team name> <player name>");
return;
}
if (Arrays.stream(ChatColor.values()).noneMatch((v) -> v.name().equalsIgnoreCase(args[3]))) {
commandSender.sendMessage(ChatColor.RED + "Unknown color " + ChatColor.WHITE + args[3]);
if (Bukkit.getPlayerExact(args[2]) == null) {
commandSender.sendMessage(ChatColor.RED + "Could not find player " + ChatColor.WHITE + args[2]);
return;
}
ChatColor color = ChatColor.valueOf(args[3].toUpperCase());
team.setColor(color);
commandSender.sendMessage(ChatColor.GREEN + "Successfully set team " + ChatColor.WHITE + team.getName() + ChatColor.GREEN + "'s color to " + ChatColor.WHITE + color.name());
return;
}
// other options
Team.Option option = Team.Option.valueOf(args[2].toUpperCase());
if (args.length == 3) {
commandSender.sendMessage(ChatColor.GOLD + "Option " + ChatColor.WHITE + option.name() + ChatColor.GOLD + " for team " + ChatColor.WHITE + team.getName() + ChatColor.GREEN + " is " + ChatColor.WHITE + team.getOption(option).name());
} else {
if (Arrays.stream(Team.OptionStatus.values()).noneMatch((v) -> v.name().equalsIgnoreCase(args[3].toUpperCase()))) {
commandSender.sendMessage(ChatColor.RED + "Value must be" + ChatColor.WHITE + " always, never, for_other_teams, or for_own_team");
team.addEntry(args[2]);
commandSender.sendMessage(ChatColor.GREEN + "Successfully added " + ChatColor.WHITE + args[2] + ChatColor.GREEN + " to team " + ChatColor.WHITE + team.getName());
Main.getInstance().getLogger().info("Added " + args[2] + "to team " + team.getName() + " in " + Main.getInstance().getConfig().getString("event-name"));
/*Leave Team*/
} else if (subcommand.equals("leave")) {
if (args.length < 3) {
commandSender.sendMessage(ChatColor.RED + "Usage: /uhc team leave <team name> <player name>");
return;
}
if (Bukkit.getPlayerExact(args[2]) == null) {
commandSender.sendMessage(ChatColor.RED + "Could not find player " + ChatColor.WHITE + args[2]);
return;
}
team.removeEntry(args[2]);
commandSender.sendMessage(ChatColor.GREEN + "Successfully removed " + ChatColor.WHITE + args[2] + ChatColor.GREEN + " from team " + ChatColor.WHITE + team.getName());
/*Options*/
} else if (subcommand.equals("option")) {
if (args.length < 3) {
commandSender.sendMessage(ChatColor.RED + "Usage: /uhc team option <team name> <option> [value]");
return;
}
team.setOption(option, Team.OptionStatus.valueOf(args[3].toUpperCase()));
commandSender.sendMessage(ChatColor.GREEN + "Set option " + ChatColor.WHITE + option.name() + ChatColor.GREEN + " for team " + ChatColor.WHITE + team.getName() + ChatColor.GREEN + " to " + ChatColor.WHITE + args[3].toUpperCase());
if (Arrays.stream(Team.Option.values()).noneMatch((v) -> v.name().equals(args[2].toUpperCase())) && !args[2].equals("color")) {
commandSender.sendMessage(ChatColor.RED + "Unknown option " + ChatColor.WHITE + args[2] + ChatColor.RED + ".\n"
+ ChatColor.YELLOW + "Acceptable options: " + ChatColor.WHITE + "color, name_tag_visibility, death_message_visibility, collision_rule");
return;
}
// color first
if (args[2].equals("color")) {
if (args.length == 3) {
commandSender.sendMessage(ChatColor.GOLD + "The color of team " + ChatColor.WHITE + team.getName() + ChatColor.GOLD + " is " + ChatColor.WHITE + team.getColor().name());
return;
}
if (Arrays.stream(ChatColor.values()).noneMatch((v) -> v.name().equalsIgnoreCase(args[3]))) {
commandSender.sendMessage(ChatColor.RED + "Unknown color " + ChatColor.WHITE + args[3]);
return;
}
ChatColor color = ChatColor.valueOf(args[3].toUpperCase());
team.setColor(color);
commandSender.sendMessage(ChatColor.GREEN + "Successfully set team " + ChatColor.WHITE + team.getName() + ChatColor.GREEN + "'s color to " + ChatColor.WHITE + color.name());
// other options
} else {
Team.Option option = Team.Option.valueOf(args[2].toUpperCase());
if (args.length == 3) {
commandSender.sendMessage(ChatColor.GOLD + "Option " + ChatColor.WHITE + option.name() + ChatColor.GOLD + " for team " + ChatColor.WHITE + team.getName() + ChatColor.GREEN + " is " + ChatColor.WHITE + team.getOption(option).name());
return;
} else {
if (Arrays.stream(Team.OptionStatus.values()).noneMatch((v) -> v.name().equalsIgnoreCase(args[3].toUpperCase()))) {
commandSender.sendMessage(ChatColor.RED + "Value must be" + ChatColor.WHITE + " always, never, for_other_teams, or for_own_team");
return;
}
team.setOption(option, Team.OptionStatus.valueOf(args[3].toUpperCase()));
commandSender.sendMessage(ChatColor.GREEN + "Set option " + ChatColor.WHITE + option.name() + ChatColor.GREEN + " for team " + ChatColor.WHITE + team.getName() + ChatColor.GREEN + " to " + ChatColor.WHITE + args[3].toUpperCase());
}
}
}
}
(new ScoreboardIO()).saveScoreboardToFile();
}

public static List<String> onTabComplete(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,10 @@ public void setInitSize(int s) {
world.getWorldBorder().setSize(initSize);
}

void setScoreboard(Scoreboard scoreboard) {
this.scoreboard = scoreboard;
}

public void setUHCMode(boolean um) {
uhcMode = um;
world.setGameRuleValue("naturalRegeneration", String.valueOf(!um));
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/usa/cactuspuppy/uhc_automation/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import org.bukkit.plugin.java.annotation.plugin.LogPrefix;
import org.bukkit.plugin.java.annotation.plugin.Plugin;
import org.bukkit.plugin.java.annotation.plugin.author.Author;
import org.bukkit.scoreboard.Scoreboard;
import usa.cactuspuppy.uhc_automation.Commands.CommandHandler;
import usa.cactuspuppy.uhc_automation.Commands.TabCompleteHelper;
import usa.cactuspuppy.uhc_automation.Database.ConnectionInfo;
import usa.cactuspuppy.uhc_automation.Database.SQLAPI;
import usa.cactuspuppy.uhc_automation.Database.SQLRepeating;
import usa.cactuspuppy.uhc_automation.ScoreboardUtils.ScoreboardIO;
import usa.cactuspuppy.uhc_automation.Tasks.DelayedPrep;
import usa.cactuspuppy.uhc_automation.Tasks.RestartTasks;

Expand Down Expand Up @@ -56,17 +58,24 @@ public void onEnable() {
getLogger().severe("Database integration failed, not attempting reconnect.");
connectionInfo = null;
}
ScoreboardIO sbIO = new ScoreboardIO();
if (sbIO.scoreboardDataExists()) {
Optional<Scoreboard> optSB = sbIO.readScoreboardFromFile();
optSB.ifPresent(scoreboard -> getGameInstance().setScoreboard(scoreboard));
}
if (gameInstance == null) {
gameInstance = new GameInstance(this);
}
registerCommands();
(new RestartTasks()).schedule();
(new DelayedPrep()).schedule();
long timeElapsed = System.currentTimeMillis() - start;
getLogger().info("UHC Automation loaded in " + timeElapsed + " ms");
}

@Override
public void onDisable() {
(new ScoreboardIO()).saveScoreboardToFile();
if (gameInstance.isActive()) {
System.out.println("[UHC_Automation] Game is active, saving game data...");
UHCUtils.saveWorldPlayers(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package usa.cactuspuppy.uhc_automation.ScoreboardUtils;

import org.bukkit.scoreboard.Scoreboard;
import usa.cactuspuppy.uhc_automation.Main;

import java.io.*;
import java.util.Optional;

public class ScoreboardIO {
public boolean saveScoreboardToFile() {
Scoreboard scoreboard = Main.getInstance().getGameInstance().getScoreboard();
String outLoc = Main.getInstance().getDataFolder() + "/scoreboard.dat";
File outFile = new File(outLoc);
if (outFile.isFile() && !outFile.delete()) {
Main.getInstance().getLogger().severe("Could not override existing scoreboard data at " + outFile.getPath());
return false;
}
try {
FileOutputStream fileOut = new FileOutputStream(outLoc);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(new ScoreboardWrapper(scoreboard));
out.close();
fileOut.close();
Main.getInstance().getLogger().info("Scoreboard data saved to " + outLoc);
} catch (IOException e) {
Main.getInstance().getLogger().severe("Failed to save scoreboard data!");
return false;
}
return true;
}

public Optional<Scoreboard> readScoreboardFromFile() {
String inLoc = Main.getInstance().getDataFolder() + "/scoreboard.dat";
Scoreboard scoreboard = null;
try {
FileInputStream fileIn = new FileInputStream(inLoc);
ObjectInputStream in = new ObjectInputStream(fileIn);
scoreboard = (Scoreboard) in.readObject();
in.close();
fileIn.close();
} catch (IOException e) {
Main.getInstance().getLogger().severe("Could not load scoreboard data from " + inLoc);
return Optional.empty();
} catch (ClassNotFoundException e) {
Main.getInstance().getLogger().severe("Did not find expected scoreboard class at " + inLoc);
}
return Optional.ofNullable(scoreboard);
}

public boolean scoreboardDataExists() {
String sbLoc = Main.getInstance().getDataFolder() + "/scoreboard.dat";
return (new File(sbLoc)).isFile();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package usa.cactuspuppy.uhc_automation.ScoreboardUtils;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.ChatColor;
import org.bukkit.scoreboard.Scoreboard;

import java.io.Serializable;
import java.util.Set;

@Getter @AllArgsConstructor
public class ScoreboardWrapper implements Serializable {
private Scoreboard scoreboard;
}

This file was deleted.

0 comments on commit 923be64

Please sign in to comment.