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

76 lines
2.7 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.util.HashMap;
import java.util.Map;
public class EditPosCommand extends GeoffreyCommand {
public EditPosCommand(GeoffreyMC plugin) {
super(plugin);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if ((sender instanceof Player)) {
Map<String, String> params = new HashMap<String, String>();
if (args.length == 0) {
sender.sendMessage(ChatColor.RED + "Location name required!");
return false;
}
Player player = ((Player) sender).getPlayer();
String dimension = GeoffreyUtil.getGeoffreyDimensionString(player.getWorld().getEnvironment());
String locationName = StringUtils.join(args, " ");
params.put("loc_name", locationName);
params.put("mc_uuid", ((Player) sender).getUniqueId().toString().replace("-", ""));
params.put("x", Integer.toString(player.getLocation().getBlockX()));
params.put("z", Integer.toString(player.getLocation().getBlockZ()));
params.put("dimension", dimension);
RunCommand("edit_pos", params, Method.POST , new CommandCallback(sender, locationName));
}
else {
sender.sendMessage(ChatColor.RED + "Console can not add locations!");
}
return true;
}
private class CommandCallback extends GeoffreyAPICallback {
CommandCallback(CommandSender sender, String locationName) {
super(sender);
errors.put("LocationLookUpError", "You do not have a location by the name " + locationName + ".");
}
@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 moved!";
commandSender.sendMessage(msg);
}
catch (GeoffreyCommandError e) {
return;
}
}
}
}