2016-03-14 19:00:03 -07:00
|
|
|
package WayofTime.bloodmagic.command.sub;
|
|
|
|
|
|
|
|
import WayofTime.bloodmagic.api.Constants;
|
|
|
|
import WayofTime.bloodmagic.api.iface.IBindable;
|
|
|
|
import WayofTime.bloodmagic.api.util.helper.BindableHelper;
|
|
|
|
import WayofTime.bloodmagic.api.util.helper.PlayerHelper;
|
|
|
|
import WayofTime.bloodmagic.util.helper.TextHelper;
|
|
|
|
import com.google.common.base.Strings;
|
2017-08-15 21:30:48 -07:00
|
|
|
import net.minecraft.command.CommandBase;
|
|
|
|
import net.minecraft.command.CommandException;
|
|
|
|
import net.minecraft.command.ICommandSender;
|
|
|
|
import net.minecraft.command.PlayerNotFoundException;
|
2016-03-14 19:00:03 -07:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2016-03-17 13:00:44 -07:00
|
|
|
import net.minecraft.server.MinecraftServer;
|
|
|
|
import net.minecraft.util.text.TextComponentTranslation;
|
2016-03-14 19:00:03 -07:00
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
public class SubCommandBind extends CommandBase {
|
2016-03-14 19:00:03 -07:00
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public String getName() {
|
2017-01-01 21:43:34 -08:00
|
|
|
return "bind";
|
2016-03-14 19:00:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public String getUsage(ICommandSender commandSender) {
|
2017-01-01 21:43:34 -08:00
|
|
|
return TextHelper.localizeEffect("commands.bind.usage");
|
2016-03-14 19:00:03 -07:00
|
|
|
}
|
|
|
|
|
2017-05-21 12:56:51 -07:00
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public int getRequiredPermissionLevel() {
|
2017-05-21 12:56:51 -07:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2016-03-14 19:00:03 -07:00
|
|
|
@Override
|
2017-08-15 21:30:48 -07:00
|
|
|
public void execute(MinecraftServer server, ICommandSender commandSender, String[] args) throws CommandException {
|
2016-03-14 19:00:03 -07:00
|
|
|
if (commandSender.getEntityWorld().isRemote)
|
|
|
|
return;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
try {
|
2016-03-17 13:00:44 -07:00
|
|
|
EntityPlayer player = CommandBase.getCommandSenderAsPlayer(commandSender);
|
2016-03-14 19:00:03 -07:00
|
|
|
String playerName = player.getName();
|
|
|
|
String uuid = PlayerHelper.getUUIDFromPlayer(player).toString();
|
2016-03-17 13:00:44 -07:00
|
|
|
ItemStack held = player.getHeldItemMainhand();
|
2016-03-14 19:00:03 -07:00
|
|
|
boolean bind = true;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (held.getItem() instanceof IBindable) {
|
|
|
|
if (args.length > 0) {
|
2016-03-14 19:00:03 -07:00
|
|
|
|
|
|
|
if (args[0].equalsIgnoreCase("help"))
|
|
|
|
return;
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (isBoolean(args[0])) {
|
2016-03-14 19:00:03 -07:00
|
|
|
bind = Boolean.parseBoolean(args[0]);
|
|
|
|
|
|
|
|
if (args.length > 2)
|
|
|
|
playerName = args[1];
|
2017-08-15 21:30:48 -07:00
|
|
|
} else {
|
2016-03-14 19:00:03 -07:00
|
|
|
playerName = args[0];
|
2016-03-17 13:00:44 -07:00
|
|
|
uuid = PlayerHelper.getUUIDFromPlayer(CommandBase.getPlayer(server, commandSender, playerName)).toString();
|
2016-03-14 19:00:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
if (bind) {
|
2016-03-14 19:00:03 -07:00
|
|
|
BindableHelper.setItemOwnerName(held, playerName);
|
|
|
|
BindableHelper.setItemOwnerUUID(held, uuid);
|
2017-01-01 21:43:34 -08:00
|
|
|
commandSender.sendMessage(new TextComponentTranslation("commands.bind.success"));
|
2017-08-15 21:30:48 -07:00
|
|
|
} else {
|
|
|
|
if (!Strings.isNullOrEmpty(((IBindable) held.getItem()).getOwnerUUID(held))) {
|
2016-03-14 19:00:03 -07:00
|
|
|
held.getTagCompound().removeTag(Constants.NBT.OWNER_UUID);
|
|
|
|
held.getTagCompound().removeTag(Constants.NBT.OWNER_NAME);
|
2017-01-01 21:43:34 -08:00
|
|
|
commandSender.sendMessage(new TextComponentTranslation("commands.bind.remove.success"));
|
2016-03-14 19:00:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-15 21:30:48 -07:00
|
|
|
} catch (PlayerNotFoundException e) {
|
2017-01-01 21:43:34 -08:00
|
|
|
commandSender.sendMessage(new TextComponentTranslation(TextHelper.localizeEffect("commands.error.404")));
|
2016-03-14 19:00:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 21:30:48 -07:00
|
|
|
private boolean isBoolean(String string) {
|
2016-03-14 19:00:03 -07:00
|
|
|
return string.equalsIgnoreCase("true") || string.equalsIgnoreCase("false");
|
|
|
|
}
|
|
|
|
}
|