TotalFreedomMod/src/main/java/me/totalfreedom/totalfreedommod/command/Command_notes.java

132 lines
4.0 KiB
Java
Raw Normal View History

2020-01-25 06:27:16 +00:00
package me.totalfreedom.totalfreedommod.command;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import me.totalfreedom.totalfreedommod.player.PlayerData;
import me.totalfreedom.totalfreedommod.rank.Rank;
import me.totalfreedom.totalfreedommod.util.FUtil;
import org.apache.commons.lang.ArrayUtils;
2020-07-01 03:38:29 +00:00
import org.apache.commons.lang.StringUtils;
2020-01-25 06:27:16 +00:00
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@CommandPermissions(level = Rank.NEW_ADMIN, source = SourceType.BOTH)
2020-01-25 06:27:16 +00:00
@CommandParameters(description = "Manage notes for a player", usage = "/<command> <name> <list | add <note> | remove <id> | clear>")
public class Command_notes extends FreedomCommand
{
@Override
public boolean run(CommandSender sender, Player playerSender, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
if (args.length < 2)
{
return false;
}
2020-06-30 07:25:38 +00:00
PlayerData playerData;
2020-01-25 06:27:16 +00:00
final Player player = getPlayer(args[0]);
if (player == null)
{
final PlayerData entry = plugin.pl.getData(args[0]);
if (entry == null)
{
msg("Can't find that user. If target is not logged in, make sure that you spelled the name exactly.");
return true;
}
2020-06-30 07:25:38 +00:00
playerData = plugin.pl.getData(entry.getName());
2020-01-25 06:27:16 +00:00
}
else
{
2020-06-30 07:25:38 +00:00
playerData = plugin.pl.getData(player);
2020-01-25 06:27:16 +00:00
}
if (args[1].equals("list"))
{
final StringBuilder noteList = new StringBuilder();
2020-06-30 07:25:38 +00:00
noteList.append(ChatColor.GREEN + "Player notes for " + playerData.getName() + ":");
2020-01-25 06:27:16 +00:00
int id = 1;
2020-06-30 07:25:38 +00:00
for (String note : playerData.getNotes())
2020-01-25 06:27:16 +00:00
{
2020-06-30 07:25:38 +00:00
String noteLine = id + ". " + note;
2020-01-25 06:27:16 +00:00
noteList.append("\n" + ChatColor.GOLD + noteLine);
id++;
}
msg(noteList.toString());
return true;
}
else if (args[1].equals("add"))
{
if (args.length < 3)
{
return false;
}
2020-06-30 07:25:38 +00:00
String note = sender.getName() + ": " + StringUtils.join(ArrayUtils.subarray(args, 2, args.length), " ");
playerData.addNote(note);
plugin.pl.save(playerData);
2020-01-25 06:27:16 +00:00
msg("Note added.", ChatColor.GREEN);
return true;
}
else if (args[1].equals("remove"))
{
if (args.length < 3)
{
return false;
}
int id;
try
{
id = Integer.valueOf(args[2]);
}
catch (NumberFormatException e)
{
msg("Invalid number: " + args[2], ChatColor.RED);
return true;
}
id--;
2020-06-30 07:25:38 +00:00
if (playerData.removeNote(id))
2020-01-25 06:27:16 +00:00
{
2020-06-30 07:25:38 +00:00
plugin.pl.save(playerData);
2020-01-25 06:27:16 +00:00
msg("Note removed.");
}
else
{
2020-04-23 11:18:03 +00:00
msg("No note with the ID of " + args[2] + " exists.", ChatColor.RED);
2020-01-25 06:27:16 +00:00
}
return true;
}
else if (args[1].equals("clear"))
{
2020-06-30 07:25:38 +00:00
int count = playerData.getNotes().size();
playerData.clearNotes();
plugin.pl.save(playerData);
2020-01-25 06:27:16 +00:00
msg("Cleared " + count + " notes.", ChatColor.GREEN);
return true;
}
return false;
}
@Override
public List<String> getTabCompleteOptions(CommandSender sender, Command command, String alias, String[] args)
{
if (args.length == 1)
{
return FUtil.getPlayerList();
}
else if (args.length == 2)
{
return Arrays.asList("list", "add", "remove", "clear");
}
else if (args.length > 2 && (args[1].equals("add")))
{
return FUtil.getPlayerList();
}
return Collections.emptyList();
}
}