Rework commands to be sub commands of /bloodmagic
This commit is contained in:
parent
fa78706fca
commit
731f92a7fd
7 changed files with 461 additions and 3 deletions
|
@ -0,0 +1,71 @@
|
|||
package WayofTime.alchemicalWizardry.common.commands;
|
||||
|
||||
import WayofTime.alchemicalWizardry.api.command.ISubCommand;
|
||||
import WayofTime.alchemicalWizardry.common.commands.sub.SubCommandBind;
|
||||
import WayofTime.alchemicalWizardry.common.commands.sub.SubCommandHelp;
|
||||
import WayofTime.alchemicalWizardry.common.commands.sub.SubCommandNetwork;
|
||||
import net.minecraft.command.CommandBase;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.util.ChatComponentText;
|
||||
import net.minecraft.util.ChatStyle;
|
||||
import net.minecraft.util.EnumChatFormatting;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class CommandBloodMagic extends CommandBase {
|
||||
|
||||
private final List aliases = new ArrayList();
|
||||
private final Map<String, ISubCommand> subCommands = new HashMap<String, ISubCommand>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public CommandBloodMagic() {
|
||||
aliases.add("BloodMagic");
|
||||
aliases.add("bloodmagic");
|
||||
aliases.add("bloodMagic");
|
||||
aliases.add("bm");
|
||||
|
||||
subCommands.put("help", new SubCommandHelp(this));
|
||||
subCommands.put("network", new SubCommandNetwork(this));
|
||||
subCommands.put("bind", new SubCommandBind(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommandName() {
|
||||
return "/bloodmagic";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRequiredPermissionLevel() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCommandUsage(ICommandSender commandSender) {
|
||||
return getCommandName() + " help";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List getCommandAliases() {
|
||||
return aliases;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processCommand(ICommandSender commandSender, String[] args) {
|
||||
if (args.length > 0 && subCommands.containsKey(args[0])) {
|
||||
|
||||
ISubCommand subCommand = subCommands.get(args[0]);
|
||||
String[] subArgs = Arrays.copyOfRange(args, 1, args.length);
|
||||
|
||||
if (subCommand.canSenderUseSubCommand(commandSender))
|
||||
subCommand.processSubCommand(commandSender, subArgs);
|
||||
else
|
||||
commandSender.addChatMessage(new ChatComponentText("You do not have permission to use this command.").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.RED)));
|
||||
} else {
|
||||
commandSender.addChatMessage(new ChatComponentText("Unknown command!").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.RED)));
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, ISubCommand> getSubCommands() {
|
||||
return subCommands;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package WayofTime.alchemicalWizardry.common.commands.sub;
|
||||
|
||||
import WayofTime.alchemicalWizardry.api.command.SubCommandBase;
|
||||
import WayofTime.alchemicalWizardry.api.items.interfaces.IBindable;
|
||||
import WayofTime.alchemicalWizardry.common.items.EnergyItems;
|
||||
import net.minecraft.command.ICommand;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.ChatComponentText;
|
||||
|
||||
public class SubCommandBind extends SubCommandBase {
|
||||
|
||||
public SubCommandBind(ICommand parent) {
|
||||
super(parent, "bind");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getArgUsage(ICommandSender commandSender) {
|
||||
return "/bloodmagic bind [true|false] [player]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpText() {
|
||||
return "Attempts to (un)bind the currently held item.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRequiredPermissionLevel() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processSubCommand(ICommandSender commandSender, String[] args) {
|
||||
super.processSubCommand(commandSender, args);
|
||||
|
||||
EntityPlayer player = getCommandSenderAsPlayer(commandSender);
|
||||
String playerName = player.getCommandSenderName();
|
||||
ItemStack held = player.getHeldItem();
|
||||
boolean bind = true;
|
||||
|
||||
if (held != null && held.getItem() instanceof IBindable) {
|
||||
if (args.length > 0) {
|
||||
|
||||
if (args[0].equalsIgnoreCase("help"))
|
||||
return;
|
||||
|
||||
if (isBoolean(args[0])) {
|
||||
bind = Boolean.parseBoolean(args[0]);
|
||||
|
||||
if (args.length > 2)
|
||||
playerName = args[1];
|
||||
} else {
|
||||
playerName = args[0];
|
||||
}
|
||||
}
|
||||
|
||||
if (bind) {
|
||||
EnergyItems.setItemOwner(held, playerName);
|
||||
commandSender.addChatMessage(new ChatComponentText("Binding successful"));
|
||||
} else {
|
||||
if (!EnergyItems.getOwnerName(held).isEmpty()) {
|
||||
held.stackTagCompound.removeTag("ownerName");
|
||||
commandSender.addChatMessage(new ChatComponentText("Unbinding successful"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isBoolean(String string) {
|
||||
return string.equalsIgnoreCase("true") || string.equalsIgnoreCase("false");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package WayofTime.alchemicalWizardry.common.commands.sub;
|
||||
|
||||
import WayofTime.alchemicalWizardry.api.command.ISubCommand;
|
||||
import WayofTime.alchemicalWizardry.api.command.SubCommandBase;
|
||||
import WayofTime.alchemicalWizardry.common.commands.CommandBloodMagic;
|
||||
import net.minecraft.command.ICommand;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.util.ChatComponentText;
|
||||
import net.minecraft.util.ChatStyle;
|
||||
import net.minecraft.util.EnumChatFormatting;
|
||||
|
||||
public class SubCommandHelp extends SubCommandBase {
|
||||
|
||||
public SubCommandHelp(ICommand parent) {
|
||||
super(parent, "help");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getArgUsage(ICommandSender commandSender) {
|
||||
return "/bloodmagic help";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpText() {
|
||||
return "Displays the help information for the \"/bloodmagic\" command.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRequiredPermissionLevel() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processSubCommand(ICommandSender commandSender, String[] args) {
|
||||
super.processSubCommand(commandSender, args);
|
||||
|
||||
if (args.length > 0)
|
||||
return;
|
||||
|
||||
for (ISubCommand subCommand : ((CommandBloodMagic)getParentCommand()).getSubCommands().values())
|
||||
commandSender.addChatMessage(new ChatComponentText(String.format("%s - %s", capitalizeFirstLetter(subCommand.getSubCommandName()), subCommand.getArgUsage(commandSender))).setChatStyle(new ChatStyle().setColor(EnumChatFormatting.GREEN)));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,161 @@
|
|||
package WayofTime.alchemicalWizardry.common.commands.sub;
|
||||
|
||||
import WayofTime.alchemicalWizardry.api.command.SubCommandBase;
|
||||
import WayofTime.alchemicalWizardry.api.soulNetwork.SoulNetworkHandler;
|
||||
import net.minecraft.command.ICommand;
|
||||
import net.minecraft.command.ICommandSender;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.util.ChatComponentText;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class SubCommandNetwork extends SubCommandBase {
|
||||
|
||||
public SubCommandNetwork(ICommand parent) {
|
||||
super(parent, "network");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getArgUsage(ICommandSender commandSender) {
|
||||
return "/bloodmagic network [syphon|add|get|fill|cap] player [amount]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelpText() {
|
||||
return "LP network utilities";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRequiredPermissionLevel() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processSubCommand(ICommandSender commandSender, String[] args) {
|
||||
super.processSubCommand(commandSender, args);
|
||||
|
||||
if (args.length > 0) {
|
||||
|
||||
if (args[0].equalsIgnoreCase("help"))
|
||||
return;
|
||||
|
||||
String givenName = commandSender.getCommandSenderName();
|
||||
EntityPlayer player = getPlayer(commandSender, givenName);
|
||||
|
||||
if (args.length > 1) {
|
||||
givenName = args[1];
|
||||
player = getPlayer(commandSender, givenName);
|
||||
}
|
||||
|
||||
boolean displayHelp = isBounded(0, 2, args.length);
|
||||
|
||||
try {
|
||||
switch (ValidCommands.valueOf(args[0].toUpperCase(Locale.ENGLISH))) {
|
||||
case SYPHON: {
|
||||
if (displayHelp) {
|
||||
displayHelpString(commandSender, ValidCommands.SYPHON.help);
|
||||
break;
|
||||
}
|
||||
|
||||
if (args.length == 3) {
|
||||
if (isInteger(args[2])) {
|
||||
int amount = Integer.parseInt(args[2]);
|
||||
SoulNetworkHandler.syphonAndDamageFromNetwork(givenName, player, amount);
|
||||
} else {
|
||||
displayErrorString(commandSender, "Invalid arguments");
|
||||
}
|
||||
} else {
|
||||
displayErrorString(commandSender, "Not enough arguments");
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case ADD: {
|
||||
if (displayHelp) {
|
||||
displayHelpString(commandSender, ValidCommands.ADD.help);
|
||||
break;
|
||||
}
|
||||
|
||||
if (args.length == 3) {
|
||||
if (isInteger(args[2])) {
|
||||
int amount = Integer.parseInt(args[2]);
|
||||
int maxOrb = SoulNetworkHandler.getMaximumForOrbTier(SoulNetworkHandler.getCurrentMaxOrb(givenName));
|
||||
SoulNetworkHandler.addCurrentEssenceToMaximum(givenName, amount, maxOrb);
|
||||
} else {
|
||||
displayErrorString(commandSender, "Invalid arguments");
|
||||
}
|
||||
} else {
|
||||
displayErrorString(commandSender, "Not enough arguments");
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case GET: {
|
||||
if (displayHelp) {
|
||||
displayHelpString(commandSender, ValidCommands.GET.help);
|
||||
break;
|
||||
}
|
||||
|
||||
if (args.length > 1)
|
||||
commandSender.addChatMessage(new ChatComponentText("Current Essence: " + SoulNetworkHandler.getCurrentEssence(givenName)));
|
||||
|
||||
break;
|
||||
}
|
||||
case FILL: {
|
||||
if (displayHelp) {
|
||||
displayHelpString(commandSender, ValidCommands.FILL.help);
|
||||
break;
|
||||
}
|
||||
|
||||
if (args.length > 1)
|
||||
SoulNetworkHandler.setCurrentEssence(givenName, Integer.MAX_VALUE);
|
||||
|
||||
break;
|
||||
}
|
||||
case CAP: {
|
||||
if (displayHelp) {
|
||||
displayHelpString(commandSender, ValidCommands.CAP.help);
|
||||
break;
|
||||
}
|
||||
|
||||
if (args.length > 1) {
|
||||
int maxOrb = SoulNetworkHandler.getMaximumForOrbTier(SoulNetworkHandler.getCurrentMaxOrb(givenName));
|
||||
SoulNetworkHandler.setCurrentEssence(givenName, maxOrb);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
displayErrorString(commandSender, "Command not found!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private enum ValidCommands {
|
||||
SYPHON("Removes the given amount of LP from the given player's LP network."),
|
||||
ADD("Adds the given amount of LP to the given player's LP network."),
|
||||
GET("Returns the amount of LP in the given player's LP network."),
|
||||
FILL(String.format("Fills the given player's LP network to %d", Integer.MAX_VALUE)),
|
||||
CAP("Fills the given player's LP network to the max that their highest Blood Orb can store.");
|
||||
|
||||
public String help;
|
||||
|
||||
ValidCommands(String help) {
|
||||
this.help = help;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
private static boolean isInteger(String s) {
|
||||
try {
|
||||
Integer.parseInt(s);
|
||||
} catch(NumberFormatException e) {
|
||||
return false;
|
||||
} catch(NullPointerException e) {
|
||||
return false;
|
||||
}
|
||||
// only got here if we didn't return false
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue