better commands

master
Default 4 years ago
parent 63ab4e3a82
commit 609039978e
  1. 1
      plugin.yml
  2. 158
      src/main/java/com/benjocraeft/sharehealth/Commands.java
  3. 64
      src/main/java/com/benjocraeft/sharehealth/Messenger.java

@ -8,6 +8,5 @@ website: https://mc.benjocraeft.com
commands:
sharehealth:
usage: /<command>
aliases: [sh]
description: Commands to control the Sharehealth System added by the Plugin

@ -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);
}
}

@ -2,17 +2,18 @@ package com.benjocraeft.sharehealth;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.logging.Logger;
public class Messenger {
@ -36,10 +37,8 @@ public class Messenger {
}
void sendFailedMessage(Player cause){
Bukkit.getOnlinePlayers().forEach(p -> {
String message = "Mission failed, go next! CAUSE: " + ChatColor.RED + cause.getDisplayName();
p.sendMessage(message);
});
Bukkit.getOnlinePlayers().forEach(p -> p.sendMessage(message));
}
void onPlayerGotDamageMessage(Player player, double damage, DamageCause cause){
@ -63,15 +62,6 @@ public class Messenger {
Bukkit.getOnlinePlayers().forEach(p -> p.sendMessage(message));
}
private String damageMessage(Player player, double damage){
String playerS = player.getDisplayName();
String damageS = String.format("%.2f", damage / 2);
return ChatColor.AQUA + playerS
+ ChatColor.WHITE + " shared "
+ ChatColor.RED + damageS
+ ChatColor.WHITE + " hearts damage!";
}
private String damageMessage(Player player, double damage, DamageCause cause){
return damageMessage(player, damage) + ChatColor.YELLOW + " Cause: " + cause;
}
@ -96,15 +86,55 @@ public class Messenger {
return damageMessage(player, damage) + ChatColor.YELLOW + " Block: " + name;
}
private String damageMessage(Player player, double damage){
String playerS = player.getDisplayName();
String damageS = String.format("%.2f", damage / 2);
return ChatColor.BLUE + playerS
+ ChatColor.WHITE + " shared "
+ ChatColor.RED + damageS
+ ChatColor.WHITE + " hearts damage!";
}
private String healMessage(Player player, double regainedHealth, RegainReason reason){
String playerS = player.getDisplayName();
String healingS = Double.toString(regainedHealth / 2);
String healingS = String.format("%.2f", regainedHealth / 2);
String causeS = reason.toString();
return ChatColor.AQUA + playerS
return ChatColor.BLUE + playerS
+ ChatColor.WHITE + " shared "
+ ChatColor.GREEN + healingS
+ ChatColor.WHITE + " hearts healing!"
+ ChatColor.YELLOW + " Cause: " + causeS;
}
String statisticsMessage(){
StringBuilder stats = new StringBuilder("Statistics:");
Sharehealth.Instance.getStatistics().getStatistics().forEach(((uuid, values) -> {
Player currentPlayer = Bukkit.getPlayer(uuid);
if (currentPlayer != null){
String playerName = currentPlayer.getDisplayName();
String stat = "\n" + ChatColor.BLUE + playerName +
ChatColor.WHITE + ": Damage caused: " +
ChatColor.RED + String.format("%.2f", values.first / 2) +
ChatColor.WHITE + " || Healing done: " +
ChatColor.GREEN + String.format("%.2f", values.second / 2);
stats.append(stat);
}
}));
return stats.toString();
}
String helpMessage(Map<List<String>, Pair<Consumer<CommandSender>, String>> commands){
StringBuilder helpMessage = new StringBuilder("Usage:");
commands.forEach((nameList, pair) -> {
StringBuilder name = new StringBuilder();
nameList.forEach(str -> name.append(str).append(" "));
String description = pair.second;
helpMessage.append("\n").
append(ChatColor.AQUA).append(name).
append(ChatColor.WHITE).append("-> ").append(description);
});
return helpMessage.toString();
}
}

Loading…
Cancel
Save