Geoffrey-MC-Plugin/src/main/java/com/zerohighdef/geoffrey/Commands/EditNameCommand.java

85 lines
3.0 KiB
Java

package com.zerohighdef.geoffrey.Commands;
import com.google.gson.JsonElement;
import com.zerohighdef.geoffrey.GeoffreyMC;
import com.zerohighdef.geoffrey.Models.GeoffreyLocation;
import com.zerohighdef.geoffrey.Objects.GeoffreyAPICallback;
import com.zerohighdef.geoffrey.Objects.GeoffreyCommand;
import com.zerohighdef.geoffrey.Objects.GeoffreyCommandError;
import com.zerohighdef.geoffrey.Objects.GeoffreyUtil;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class EditNameCommand extends GeoffreyCommand {
public EditNameCommand(GeoffreyMC plugin) {
super(plugin);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if ((sender instanceof Player)) {
List<String> parsedArgs;
try {
parsedArgs = GeoffreyUtil.parseArgs(args);
}
catch (ParseException e) {
sender.sendMessage(ChatColor.RED + e.getMessage());
return false;
}
Map<String, String> params = new HashMap<String, String>();
if (parsedArgs.size() < 2) {
sender.sendMessage(ChatColor.RED + "Not enough parameters!");
return false;
}
Player player = ((Player) sender).getPlayer();
String oldLocationName = parsedArgs.get(0);
String newLocationName = parsedArgs.get(1);
params.put("mc_uuid", player.getUniqueId().toString().replace("-", ""));
params.put("new_name", oldLocationName);
params.put("loc_name", newLocationName);
RunCommand("edit_name", params, Method.POST , new CommandCallback(sender, oldLocationName, newLocationName));
}
else {
sender.sendMessage(ChatColor.RED + "Console can not add locations!");
}
return true;
}
private class CommandCallback extends GeoffreyAPICallback {
CommandCallback(CommandSender sender, String oldLocationName, String newLocationName) {
super(sender);
errors.put("EntryNameNotUniqueError", "A location is already called " + newLocationName + " you ding dong goober");
errors.put("LocationLookUpError", "You do not have a location by the name " + oldLocationName + " you ding dong goober.");
}
@Override
public void invoke(String s) {
try {
JsonElement json = parseJSON(s);
GeoffreyLocation location = new GeoffreyLocation(json.getAsJsonObject());
String msg = ChatColor.GREEN + location.getLocationName() + " has been updated.";
commandSender.sendMessage(msg);
}
catch (GeoffreyCommandError e) {
return;
}
}
}
}