diff --git a/.gitignore b/.gitignore index 6af25a5..44b35b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.class +.DS_STORE # Mobile Tools for Java (J2ME) .mtj.tmp/ @@ -32,4 +33,4 @@ pom.xml ## gradle ## /build/ -/.gradle/ \ No newline at end of file +/.gradle/ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index f6cab3d..568c50b 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,5 @@ -#Mon Dec 03 15:34:04 GMT 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.5.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.5.1-bin.zip diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/src/main/java/net/moddedminecraft/mmcreboot/Config/Messages.java b/src/main/java/net/moddedminecraft/mmcreboot/Config/Messages.java index a3e0a88..28a133d 100644 --- a/src/main/java/net/moddedminecraft/mmcreboot/Config/Messages.java +++ b/src/main/java/net/moddedminecraft/mmcreboot/Config/Messages.java @@ -77,7 +77,7 @@ public Messages(Main main) throws IOException, ObjectMappingException { //general private static String restartCancel = "&3Restarts have been canceled."; - private static String restartPassed = "Players have voted to restart the server."; + private static String restartPassed = "&aPlayers have voted to restart the server."; private static String restartVoteNotEnoughVoted = "&3The server will not be restarted. Not enough people have voted."; private static String votedYes = "You Voted Yes!"; private static String votedNo = "You Voted No!"; diff --git a/src/main/java/net/moddedminecraft/mmcreboot/Config/Permissions.java b/src/main/java/net/moddedminecraft/mmcreboot/Config/Permissions.java index e779bef..ef7b2b3 100644 --- a/src/main/java/net/moddedminecraft/mmcreboot/Config/Permissions.java +++ b/src/main/java/net/moddedminecraft/mmcreboot/Config/Permissions.java @@ -10,6 +10,7 @@ public class Permissions { public static final String COMMAND_NOW = "mmcreboot.reboot.now"; public static final String COMMAND_START = "mmcreboot.reboot.start"; public static final String COMMAND_CANCEL = "mmcreboot.reboot.cancel"; + public static final String CANCEL_VOTE = "mmcreboot.reboot.cancel.vote"; public static final String TOGGLE_VOTE = "mmcreboot.reboot.toggle.vote"; public static final String BYPASS = "mmcreboot.reboot.bypass"; } diff --git a/src/main/java/net/moddedminecraft/mmcreboot/EventListener.java b/src/main/java/net/moddedminecraft/mmcreboot/EventListener.java index 856a3e9..a5e7221 100644 --- a/src/main/java/net/moddedminecraft/mmcreboot/EventListener.java +++ b/src/main/java/net/moddedminecraft/mmcreboot/EventListener.java @@ -2,16 +2,14 @@ import net.moddedminecraft.mmcreboot.Config.Config; import net.moddedminecraft.mmcreboot.Config.Messages; -import ninja.leaping.configurate.objectmapping.ObjectMappingException; import org.spongepowered.api.Sponge; import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.event.Listener; import org.spongepowered.api.event.filter.cause.Root; import org.spongepowered.api.event.network.ClientConnectionEvent; -import org.spongepowered.api.service.pagination.PaginationService; +import org.spongepowered.api.service.pagination.PaginationList; import org.spongepowered.api.text.Text; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -24,12 +22,9 @@ public EventListener(Main instance) { } @Listener - public void onPlayerLogin(ClientConnectionEvent.Join event, @Root Player player) throws IOException, ObjectMappingException { + public void onPlayerLogin(ClientConnectionEvent.Join event, @Root Player player) { if (plugin.voteStarted) { - Sponge.getScheduler().createTaskBuilder().execute(new Runnable() { - - public void run() { - PaginationService paginationService = Sponge.getServiceManager().provide(PaginationService.class).get(); + Sponge.getScheduler().createTaskBuilder().execute(() -> { List contents = new ArrayList<>(); if (Config.timerUseVoteScoreboard) { plugin.displayVotes(); @@ -40,13 +35,12 @@ public void run() { } if (!contents.isEmpty()) { - paginationService.builder() + PaginationList.builder() .title(plugin.fromLegacy("Restart")) .contents(contents) .padding(Text.of("=")) .sendTo(player); } - } }).delay(3, TimeUnit.SECONDS).name("mmcreboot-s-sendVoteOnLogin").submit(plugin); } } diff --git a/src/main/java/net/moddedminecraft/mmcreboot/Main.java b/src/main/java/net/moddedminecraft/mmcreboot/Main.java index 03a1ac4..856aae3 100644 --- a/src/main/java/net/moddedminecraft/mmcreboot/Main.java +++ b/src/main/java/net/moddedminecraft/mmcreboot/Main.java @@ -75,7 +75,7 @@ public class Main { public boolean voteStarted = false; public int yesVotes = 0; public int noVotes = 0; - public ArrayList hasVoted = new ArrayList(); + public ArrayList hasVoted = new ArrayList(); public static ArrayList realTimeTimes = new ArrayList(); public int voteSeconds; @@ -314,7 +314,7 @@ public void action() { displayRestart(nextRealTimeRestart); } } - if (voteStarted && voteCancel && Config.timerUseVoteScoreboard) { + if (voteStarted && !voteCancel && Config.timerUseVoteScoreboard) { displayVotes(); } } diff --git a/src/main/java/net/moddedminecraft/mmcreboot/commands/RebootVote.java b/src/main/java/net/moddedminecraft/mmcreboot/commands/RebootVote.java index 89e31cb..2665b5c 100644 --- a/src/main/java/net/moddedminecraft/mmcreboot/commands/RebootVote.java +++ b/src/main/java/net/moddedminecraft/mmcreboot/commands/RebootVote.java @@ -11,7 +11,7 @@ import org.spongepowered.api.command.args.CommandContext; import org.spongepowered.api.command.spec.CommandExecutor; import org.spongepowered.api.entity.living.player.Player; -import org.spongepowered.api.service.pagination.PaginationService; +import org.spongepowered.api.service.pagination.PaginationList; import org.spongepowered.api.text.Text; import org.spongepowered.api.text.channel.MessageChannel; @@ -19,6 +19,9 @@ public class RebootVote implements CommandExecutor { + private final String noPermission = "&cYou do not have permission to use this command!"; + private final String commandSuccess = "&fReboot vote has successfully been {toggle}"; + private final Main plugin; public RebootVote(Main instance) { plugin = instance; @@ -26,156 +29,186 @@ public RebootVote(Main instance) { @Override public CommandResult execute(CommandSource src, CommandContext args) throws CommandException { - Optional optional = args.getOne("optional"); - - if (optional.isPresent()) { - String op = optional.get(); - switch (op) { - case "on": - if (src.hasPermission(Permissions.TOGGLE_VOTE)) { - Config.voteEnabled = true; - Config.config.getNode("voting", "enabled").setValue("true"); - return CommandResult.success(); - } else { - return CommandResult.empty(); - } - case "off": - if (src.hasPermission(Permissions.TOGGLE_VOTE)) { - Config.voteEnabled = false; - Config.config.getNode("voting", "enabled").setValue("false"); - return CommandResult.success(); - } else { - return CommandResult.empty(); - } + Optional optional = args.getOne("optional"); + + if (optional.isPresent()) { + String op = optional.get(); + switch (op) { + case "on": + if (src.hasPermission(Permissions.TOGGLE_VOTE)) { + Config.voteEnabled = true; + Config.config.getNode("voting", "enabled").setValue("true"); + src.sendMessage(plugin.fromLegacy(commandSuccess.replace("{toggle}", "&aenabled"))); + return CommandResult.success(); + } else { + throw new CommandException(plugin.fromLegacy(noPermission)); + } - case "yes": - if (plugin.hasVoted.contains(src)) { - throw new CommandException(plugin.fromLegacy(Messages.getErrorAlreadyVoted())); - } - if (plugin.voteStarted) { - plugin.yesVotes += 1; + case "off": + if (src.hasPermission(Permissions.TOGGLE_VOTE)) { + Config.voteEnabled = false; + Config.config.getNode("voting", "enabled").setValue("false"); + src.sendMessage(plugin.fromLegacy(commandSuccess.replace("{toggle}", "&cdisabled"))); + return CommandResult.success(); + } else { + throw new CommandException(plugin.fromLegacy(noPermission)); + } + case "yes": if (src instanceof Player) { - plugin.hasVoted.add((Player) src); + Player player = (Player) src; + if (plugin.hasVoted.contains(player.getUniqueId())) { + throw new CommandException(plugin.fromLegacy(Messages.getErrorAlreadyVoted())); + } + if (plugin.voteStarted) { + plugin.yesVotes += 1; + plugin.hasVoted.add(((Player) src).getUniqueId()); + plugin.displayVotes(); + plugin.sendMessage(src, Messages.getVotedYes()); + return CommandResult.success(); + } else { + throw new CommandException(plugin.fromLegacy(Messages.getErrorNoVoteRunning())); + } + } else { + throw new CommandException(plugin.fromLegacy("&cYou must be a player to vote!")); } - plugin.displayVotes(); - plugin.sendMessage(src, Messages.getVotedYes()); - return CommandResult.success(); - } else { - throw new CommandException(plugin.fromLegacy(Messages.getErrorNoVoteRunning())); - } - - case "no": - if (plugin.hasVoted.contains(src)) { - throw new CommandException(plugin.fromLegacy(Messages.getErrorAlreadyVoted())); - } - if (plugin.voteStarted) { - plugin.noVotes += 1; + case "no": if (src instanceof Player) { - plugin.hasVoted.add((Player) src); + Player player = (Player) src; + if (plugin.hasVoted.contains(player.getUniqueId())) { + throw new CommandException(plugin.fromLegacy(Messages.getErrorAlreadyVoted())); + } + if (plugin.voteStarted) { + plugin.noVotes += 1; + plugin.hasVoted.add(((Player) src).getUniqueId()); + plugin.displayVotes(); + plugin.sendMessage(src, Messages.getVotedNo()); + return CommandResult.success(); + + } else { + throw new CommandException(plugin.fromLegacy(Messages.getErrorNoVoteRunning())); + } + } else { + throw new CommandException(plugin.fromLegacy("&cYou must be a player to vote!")); } - plugin.displayVotes(); - plugin.sendMessage(src, Messages.getVotedNo()); - return CommandResult.success(); + case "cancel": + if (plugin.voteStarted && src.hasPermission(Permissions.CANCEL_VOTE)) { + plugin.removeScoreboard(); + plugin.removeBossBar(); + plugin.yesVotes = 0; + plugin.cdTimer = true; + plugin.voteStarted = false; + plugin.hasVoted.clear(); + src.sendMessage(plugin.fromLegacy("Reboot vote has been successfully &ccancelled")); + return CommandResult.success(); + } + default: + throw new CommandException(plugin.fromLegacy("&cInvalid Argument!")); + } + } else { + double timeLeft = 0; + if (Config.restartInterval > 0) { + timeLeft = (Config.restartInterval * 3600) - ((double) (System.currentTimeMillis() - plugin.startTimestamp) / 1000); + } else if (plugin.nextRealTimeRestart > 0) { + timeLeft = (plugin.nextRealTimeRestart) - ((double) (System.currentTimeMillis() - plugin.startTimestamp) / 1000); + } + int hours = (int) (timeLeft / 3600); + int minutes = (int) ((timeLeft - hours * 3600) / 60); + int onlinePlayers = Sponge.getServer().getOnlinePlayers().size(); - } else { - throw new CommandException(plugin.fromLegacy(Messages.getErrorNoVoteRunning())); + if (!src.hasPermission(Permissions.BYPASS)) { + if (!src.hasPermission(Permissions.COMMAND_VOTE)) { + throw new CommandException(plugin.fromLegacy(Messages.getErrorNoPermission())); + } + if (!Config.voteEnabled) { + throw new CommandException(plugin.fromLegacy(Messages.getErrorVoteToRestartDisabled())); + } + if (plugin.justStarted) { + throw new CommandException(plugin.fromLegacy(Messages.getErrorNotOnlineLongEnough())); } + if (onlinePlayers < Config.timerMinplayers) { + throw new CommandException(plugin.fromLegacy(Messages.getErrorMinPlayers())); + } + } - default: - return CommandResult.empty(); - //break; + if (src instanceof Player && plugin.hasVoted.contains(((Player) src).getUniqueId())) { + throw new CommandException(plugin.fromLegacy(Messages.getErrorAlreadyVoted())); + } + if (plugin.voteStarted) { + throw new CommandException(plugin.fromLegacy(Messages.getErrorVoteAlreadyRunning())); + } + if (plugin.isRestarting && timeLeft != 0 && (hours == 0 && minutes <= 10)) { + throw new CommandException(plugin.fromLegacy(Messages.getErrorAlreadyRestarting())); + } + if (plugin.cdTimer) { + throw new CommandException(plugin.fromLegacy(Messages.getErrorWaitTime())); + } - } - } else { - double timeLeft = 0; - if (Config.restartInterval > 0) { - timeLeft = (Config.restartInterval * 3600) - ((double) (System.currentTimeMillis() - plugin.startTimestamp) / 1000); - } else if (plugin.nextRealTimeRestart > 0){ - timeLeft = (plugin.nextRealTimeRestart) - ((double) (System.currentTimeMillis() - plugin.startTimestamp) / 1000); - } - int hours = (int) (timeLeft / 3600); - int minutes = (int) ((timeLeft - hours * 3600) / 60); - if (!src.hasPermission(Permissions.BYPASS) && !src.hasPermission(Permissions.COMMAND_VOTE)) { - throw new CommandException(plugin.fromLegacy(Messages.getErrorNoPermission())); - } - if (!src.hasPermission(Permissions.BYPASS) && !Config.voteEnabled) { - throw new CommandException(plugin.fromLegacy(Messages.getErrorVoteToRestartDisabled())); - } - if (plugin.hasVoted.contains(src)) { - throw new CommandException(plugin.fromLegacy(Messages.getErrorAlreadyVoted())); - } - if (plugin.voteStarted) { - throw new CommandException(plugin.fromLegacy(Messages.getErrorVoteAlreadyRunning())); - } - if (!src.hasPermission(Permissions.BYPASS) && plugin.justStarted) { - throw new CommandException(plugin.fromLegacy(Messages.getErrorNotOnlineLongEnough())); - } - if (!src.hasPermission(Permissions.BYPASS) && Sponge.getServer().getOnlinePlayers().size() < Config.timerMinplayers) { - throw new CommandException(plugin.fromLegacy(Messages.getErrorMinPlayers())); - } - if (plugin.isRestarting && timeLeft != 0 && (hours == 0 && minutes <= 10)) { - throw new CommandException(plugin.fromLegacy(Messages.getErrorAlreadyRestarting())); - } - if (plugin.cdTimer) { - throw new CommandException(plugin.fromLegacy(Messages.getErrorWaitTime())); - } + if (src instanceof Player) { + Player player = (Player) src; + plugin.voteStarted = true; + plugin.voteCancel = false; + plugin.hasVoted.add(player.getUniqueId()); + plugin.yesVotes += 1; + plugin.noVotes = 0; + plugin.voteSeconds = 90; + plugin.displayVotes(); + } else { + plugin.voteStarted = true; + plugin.displayVotes(); + } + List contents = new ArrayList<>(); - if (src instanceof Player) { - Player player = (Player) src; - plugin.voteStarted = true; - plugin.voteCancel = false; - plugin.hasVoted.add(player); - plugin.yesVotes += 1; - plugin.noVotes = 0; - plugin.voteSeconds = 90; - plugin.displayVotes(); - } else { - plugin.voteStarted = true; - plugin.displayVotes(); - } + List broadcast = Messages.getRestartVoteBroadcast(); + if (broadcast != null) { + for (String line : broadcast) { + String checkLine = line.replace("%playername$", src.getName()).replace("%config.timerminplayers%", String.valueOf(Config.timerMinplayers)); + contents.add(plugin.fromLegacy(checkLine)); + } + } - PaginationService paginationService = Sponge.getServiceManager().provide(PaginationService.class).get(); - List contents = new ArrayList<>(); - List broadcast = Messages.getRestartVoteBroadcast(); - if (broadcast != null) { - for (String line : broadcast) { - String checkLine = line.replace("%playername$", src.getName()).replace("%config.timerminplayers%", String.valueOf(Config.timerMinplayers)); - contents.add(plugin.fromLegacy(checkLine)); + if (!contents.isEmpty()) { + PaginationList.builder() + .title(plugin.fromLegacy("Restart")) + .contents(contents) + .padding(Text.of("=")) + .sendTo(MessageChannel.TO_ALL.getMembers()); } - } - if (!contents.isEmpty()) { - paginationService.builder() - .title(plugin.fromLegacy("Restart")) - .contents(contents) - .padding(Text.of("=")) - .sendTo(MessageChannel.TO_ALL.getMembers()); - } + Timer voteTimer = new Timer(); + voteTimer.schedule(new TimerTask() { + public void run() { + int Online = Sponge.getServer().getOnlinePlayers().size(); + int percentage = plugin.yesVotes / Online * 100; + + final String prefix = "&f[&6Restart&f] "; - Timer voteTimer = new Timer(); - voteTimer.schedule(new TimerTask() { - public void run() { - int Online = Sponge.getServer().getOnlinePlayers().size(); - int percentage = plugin.yesVotes/Online *100; - - boolean yesAboveNo = plugin.yesVotes > plugin.noVotes; - boolean yesAboveMin = plugin.yesVotes >= Config.timerMinplayers; - boolean requiredPercent = percentage >= Config.timerVotepercent; - - if (yesAboveNo && yesAboveMin && !plugin.voteCancel && requiredPercent) { - plugin.isRestarting = true; - Config.restartInterval = (Config.timerVotepassed + 1) / 3600.0; - plugin.logger.info("[MMCReboot] scheduling restart tasks..."); - plugin.reason = Messages.getRestartPassed(); - plugin.scheduleTasks(); - } else { - if (!plugin.voteCancel) { - plugin.broadcastMessage("&f[&6Restart&f] " + Messages.getRestartVoteNotEnoughVoted()); + boolean yesAboveNo = plugin.yesVotes > plugin.noVotes; + boolean yesAboveMin = plugin.yesVotes >= Config.timerMinplayers; + boolean requiredPercent = percentage >= Config.timerVotepercent; + + if (yesAboveNo && yesAboveMin && !plugin.voteCancel && requiredPercent) { + plugin.isRestarting = true; + Config.restartInterval = (Config.timerVotepassed + 1) / 3600.0; + plugin.logger.info("[MMCReboot] scheduling restart tasks..."); + plugin.reason = Messages.getRestartPassed(); + plugin.scheduleTasks(); + + } else { + if (!plugin.voteCancel) { + plugin.broadcastMessage(prefix + Messages.getRestartVoteNotEnoughVoted()); + } + plugin.voteCancel = false; } - plugin.voteCancel = false; + + plugin.removeScoreboard(); + plugin.removeBossBar(); + plugin.yesVotes = 0; + plugin.cdTimer = true; + plugin.voteStarted = false; + plugin.hasVoted.clear(); Timer voteTimer = new Timer(); voteTimer.schedule(new TimerTask() { public void run() { @@ -183,15 +216,8 @@ public void run() { } }, (long) (Config.timerRevote * 60000.0)); } - plugin.removeScoreboard(); - plugin.removeBossBar(); - plugin.yesVotes = 0; - plugin.cdTimer = true; - plugin.voteStarted = false; - plugin.hasVoted.clear(); - } - }, 90000); - return CommandResult.success(); - } + }, 90000); + return CommandResult.success(); + } } }