package com.nicewuerfel.blockown.command;
import com.nicewuerfel.blockown.InvalidWorldNameException;
import com.nicewuerfel.blockown.Material;
import com.nicewuerfel.blockown.Message;
import com.nicewuerfel.blockown.OwnedBlock;
import com.nicewuerfel.blockown.Setting;
import com.nicewuerfel.blockown.User;
import com.nicewuerfel.blockown.WaitType;
import com.nicewuerfel.blockown.database.Database;
import com.nicewuerfel.blockown.protection.ProtectAction;
import com.nicewuerfel.blockown.protection.Protection;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.Set;
public class CE_Unlock extends CommandExecutor {
private static final String PARAM_ENTITY = "e";
private static final String PARAM_BLOCK = "b";
public CE_Unlock(Setting setting, Database database, Protection protection) {
super(setting, database, protection);
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String cmdName, String[] args) {
if (!isPlayer(sender)) {
return true;
}
Player player = (Player) sender;
User user = User.getInstance(player.getUniqueId());
if (args.length == 1) {
if (args[0].equalsIgnoreCase(PARAM_ENTITY)) {
if (setting.isWaiting(user, WaitType.UNLOCK_ENTITY)) {
setting.removeWaiting(user);
player.sendMessage(Message.COMMAND_NO_MORE_CLICK_WAITING.getMessage());
} else {
setting.addWaiting(user, WaitType.UNLOCK_ENTITY);
player.sendMessage(Message.COMMAND_UNLOCK_CLICK_ENTITY.getMessage());
}
return true;
}
if (args[0].equalsIgnoreCase(PARAM_BLOCK)) {
if (setting.isWaiting(user, WaitType.UNLOCK_BLOCK)) {
setting.removeWaiting(user);
player.sendMessage(Message.COMMAND_NO_MORE_CLICK_WAITING.getMessage());
} else {
setting.addWaiting(user, WaitType.UNLOCK_BLOCK);
player.sendMessage(Message.COMMAND_UNLOCK_CLICK_BLOCK.getMessage());
}
return true;
}
try {
Material material = Material.parseMaterial(args[0]);
ProtectAction protectAction = new ProtectAction.Builder(user).unlock(material).build();
protection.enqueue(protectAction);
player.sendMessage(Message.COMMAND_UNLOCK_SUCCESS.getMessage(material.getName()));
return true;
} catch (IllegalArgumentException e) {
player.sendMessage(Message.COMMAND_INVALID_MATERIAL.getMessage(args[0]));
return false;
}
} else if (args.length == 0) {
Block targetBlock = player.getTargetBlock((Set<org.bukkit.Material>) null, 20);
if (targetBlock != null) {
Material material;
try {
material = (OwnedBlock.newInstance(targetBlock)).getMaterial();
ProtectAction protectAction = new ProtectAction.Builder(user).unlock(material).build();
protection.enqueue(protectAction);
player.sendMessage(Message.COMMAND_UNLOCK_SUCCESS.getMessage(material.getName()));
return true;
} catch (InvalidWorldNameException e) {
// can't happen, since targetBlock is not null and it's world is used
getOutput().printError("This should never happen! CE_UNLOCK", e);
return false;
}
} else {
player.sendMessage(Message.COMMAND_NOTARGET.getMessage());
return false;
}
} else {
return false;
}
}
}