|
|
|
@ -1,6 +1,6 @@ |
|
|
|
|
package com.benjocraeft.sharehealth; |
|
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.math.NumberUtils; |
|
|
|
|
import io.netty.util.concurrent.ImmediateEventExecutor; |
|
|
|
|
import org.bukkit.Bukkit; |
|
|
|
|
import org.bukkit.ChatColor; |
|
|
|
|
import org.bukkit.command.Command; |
|
|
|
@ -9,31 +9,53 @@ import org.bukkit.command.TabExecutor; |
|
|
|
|
import org.bukkit.entity.Player; |
|
|
|
|
import org.bukkit.util.StringUtil; |
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.Collections; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.*; |
|
|
|
|
import java.util.function.Consumer; |
|
|
|
|
|
|
|
|
|
public class Commands implements TabExecutor { |
|
|
|
|
|
|
|
|
|
final private String[] mainSchema = { |
|
|
|
|
"get", "set", "reset", "log" |
|
|
|
|
"get", "reset", "log", "stats", "help" |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
final private String[] hasSecondSchema = { |
|
|
|
|
"log", |
|
|
|
|
"get" |
|
|
|
|
"log" |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
final private String[][] secondSchema = { |
|
|
|
|
{ |
|
|
|
|
"on", "off" |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
"max" |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
final private Map<List<String>, Pair<Consumer<CommandSender>, String>> commands = new HashMap<>(); |
|
|
|
|
{ |
|
|
|
|
commands.put( |
|
|
|
|
Arrays.asList("get"), |
|
|
|
|
Pair.pair(this::commandGetHealth, "Displays current health value.") |
|
|
|
|
); |
|
|
|
|
commands.put( |
|
|
|
|
Arrays.asList("reset"), |
|
|
|
|
Pair.pair(this::commandReset, "Gives every player full health and resets 'isFailed' to false. GameMode becomes Survival.") |
|
|
|
|
); |
|
|
|
|
commands.put( |
|
|
|
|
Arrays.asList("log", "on"), |
|
|
|
|
Pair.pair(sender -> this.commandSetLogging(sender, true), "Activates permanent player Logging about Damage and Healing.") |
|
|
|
|
); |
|
|
|
|
commands.put( |
|
|
|
|
Arrays.asList("log", "off"), |
|
|
|
|
Pair.pair(sender -> this.commandSetLogging(sender, false), "Deactivates permanent player Logging about Damage and Healing.") |
|
|
|
|
); |
|
|
|
|
commands.put( |
|
|
|
|
Arrays.asList("stats"), |
|
|
|
|
Pair.pair(this::commandSendStats, "Displays statistics about every player.") |
|
|
|
|
); |
|
|
|
|
commands.put( |
|
|
|
|
Arrays.asList("help"), |
|
|
|
|
Pair.pair(this::commandGetHelp, "Displays help message for command usage.") |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) { |
|
|
|
|
List<String> list = new ArrayList<>(); |
|
|
|
@ -50,127 +72,47 @@ public class Commands implements TabExecutor { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return list; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){ |
|
|
|
|
|
|
|
|
|
if (args.length != 0){ |
|
|
|
|
if (args[0].equalsIgnoreCase("reset")){ |
|
|
|
|
commandSetHealth(20, true); |
|
|
|
|
} |
|
|
|
|
else if (args[0].equalsIgnoreCase("set")){ |
|
|
|
|
if (args.length > 1){ |
|
|
|
|
if (NumberUtils.isNumber(args[1])){ |
|
|
|
|
double num = NumberUtils.createDouble(args[1]); |
|
|
|
|
commandSetHealth(num); |
|
|
|
|
} else if (args[1].equalsIgnoreCase("max")){ |
|
|
|
|
if (args.length > 2){ |
|
|
|
|
if (NumberUtils.isNumber(args[2])){ |
|
|
|
|
double num = NumberUtils.createDouble(args[2]); |
|
|
|
|
commandSetMaxHealth(num); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else if (args[0].equalsIgnoreCase("get")){ |
|
|
|
|
if (args.length > 1){ |
|
|
|
|
if (args[1].equalsIgnoreCase("max")){ |
|
|
|
|
if (args.length > 2){ |
|
|
|
|
if (args[2].equalsIgnoreCase("raw")){ |
|
|
|
|
commandGetMaxHealth(sender, true); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
commandGetMaxHealth(sender, false); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
commandGetHealth(sender); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
else if (args[0].equalsIgnoreCase("log")){ |
|
|
|
|
if (args.length > 1){ |
|
|
|
|
if (args[1].equalsIgnoreCase("off")){ |
|
|
|
|
commandSetLogging(false); |
|
|
|
|
} |
|
|
|
|
if (args[1].equalsIgnoreCase("on")){ |
|
|
|
|
commandSetLogging(true); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else if(args[0].equalsIgnoreCase("stats")){ |
|
|
|
|
commandSendStats(); |
|
|
|
|
} |
|
|
|
|
else if (args[0].equalsIgnoreCase("help")){ |
|
|
|
|
commandGetHelp(sender); |
|
|
|
|
} else { |
|
|
|
|
unknownCommand(sender); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
commandGetHelp(sender); |
|
|
|
|
} |
|
|
|
|
List<String> argList = Arrays.asList(args); |
|
|
|
|
Pair<Consumer<CommandSender>, String> command = commands.get(argList); |
|
|
|
|
if (command == null) |
|
|
|
|
command = Pair.pair(this::unknownCommand, ""); |
|
|
|
|
command.first.accept(sender); |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void commandSetMaxHealth(double health){ |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void commandSetHealth(double health, boolean reset){ |
|
|
|
|
Sharehealth.Instance.getHealthManager().setHealth(health); |
|
|
|
|
if (reset){ |
|
|
|
|
private void commandReset(CommandSender sender){ |
|
|
|
|
Sharehealth.Instance.reset(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void commandSetHealth(double health){ |
|
|
|
|
commandSetHealth(health, false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void commandGetHealth(CommandSender sender){ |
|
|
|
|
sender.sendMessage("Current health: " + Sharehealth.Instance.getHealthManager().getHealthString()); |
|
|
|
|
String message = "Current health: " + Sharehealth.Instance.getHealthManager().getHealthString(); |
|
|
|
|
sender.sendMessage(message); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void commandGetMaxHealth(CommandSender sender, boolean raw){ |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void commandSetLogging(boolean log){ |
|
|
|
|
private void commandSetLogging(CommandSender sender, boolean log){ |
|
|
|
|
Sharehealth.Instance.getMessenger().setLogging(log); |
|
|
|
|
//TODO Set by user
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void commandSendStats(){ |
|
|
|
|
StringBuilder stats = new StringBuilder("Statistics:\n"); |
|
|
|
|
Sharehealth.Instance.getStatistics().getStatistics().forEach(((uuid, values) -> { |
|
|
|
|
Player player = Bukkit.getPlayer(uuid); |
|
|
|
|
if (player != null){ |
|
|
|
|
String playerName = player.getDisplayName(); |
|
|
|
|
String stat = ChatColor.AQUA + playerName + ChatColor.WHITE + |
|
|
|
|
": Damage caused: " + ChatColor.RED + String.format("%.2f", values.first) + ChatColor.WHITE + |
|
|
|
|
" Healing done: " + ChatColor.GREEN + String.format("%.2f", values.second) + "\n"; |
|
|
|
|
stats.append(stat); |
|
|
|
|
} |
|
|
|
|
})); |
|
|
|
|
Bukkit.getOnlinePlayers().forEach((Player p) -> p.sendMessage(stats.toString())); |
|
|
|
|
private void commandSendStats(CommandSender sender){ |
|
|
|
|
String message = Sharehealth.Instance.getMessenger().statisticsMessage(); |
|
|
|
|
sender.sendMessage(message); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void commandGetHelp(CommandSender sender){ |
|
|
|
|
String help = "Usage:\n" + |
|
|
|
|
"get -> returns current globally shared health\n" + |
|
|
|
|
"set [number] -> sets new globally shared health\n" + |
|
|
|
|
"reset -> heals every player and resets 'isFailed' to false\n" + |
|
|
|
|
"log [on/off] -> activates/deactivates player log messages about damage and healings" + |
|
|
|
|
"stats -> sends everyone statistics for every player"; |
|
|
|
|
sender.sendMessage(help); |
|
|
|
|
String message = Sharehealth.Instance.getMessenger().helpMessage(commands); |
|
|
|
|
sender.sendMessage(message); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void unknownCommand(CommandSender sender){ |
|
|
|
|
String msg = "Unknown command, use help."; |
|
|
|
|
sender.sendMessage(msg); |
|
|
|
|
String message = "Unknown command. Type \"/sh help\" for help."; |
|
|
|
|
sender.sendMessage(message); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|