Add Command Framework by @CovertJaguar, rewrite /buildcraft version to fit into it

This commit is contained in:
Adrian 2015-05-07 19:15:17 +02:00
parent d6f603d635
commit c911f21138
7 changed files with 394 additions and 83 deletions

View file

@ -12,8 +12,10 @@ import java.io.File;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.Random;
import java.util.UUID;
import buildcraft.core.command.SubCommandVersion;
import buildcraft.core.lib.commands.RootCommand;
import com.mojang.authlib.GameProfile;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
@ -69,7 +71,6 @@ import buildcraft.core.BCCreativeTab;
import buildcraft.core.BlockBuildTool;
import buildcraft.core.BlockEngine;
import buildcraft.core.BlockSpring;
import buildcraft.core.CommandBuildCraft;
import buildcraft.core.CompatHooks;
import buildcraft.core.CoreGuiHandler;
import buildcraft.core.CoreIconProvider;
@ -143,6 +144,7 @@ public class BuildCraftCore extends BuildCraftMod {
Full, NoDynamic
}
public static RootCommand commandBuildcraft = new RootCommand("buildcraft");
public static XorShift128Random random = new XorShift128Random();
public static RenderMode render = RenderMode.Full;
public static boolean debugWorldgen = false;
@ -238,6 +240,9 @@ public class BuildCraftCore extends BuildCraftMod {
new BCCreativeTab("main");
commandBuildcraft.addAlias("bc");
commandBuildcraft.addChildCommand(new SubCommandVersion());
BuildcraftRecipeRegistry.assemblyTable = AssemblyRecipeManager.INSTANCE;
BuildcraftRecipeRegistry.integrationTable = IntegrationRecipeManager.INSTANCE;
BuildcraftRecipeRegistry.refinery = RefineryRecipeManager.INSTANCE;
@ -441,7 +446,7 @@ public class BuildCraftCore extends BuildCraftMod {
@Mod.EventHandler
public void serverStarting(FMLServerStartingEvent event) {
event.registerServerCommand(new CommandBuildCraft());
event.registerServerCommand(commandBuildcraft);
}
@Mod.EventHandler

View file

@ -1,80 +0,0 @@
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core;
import java.util.List;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.StatCollector;
import buildcraft.core.proxy.CoreProxy;
public class CommandBuildCraft extends CommandBase {
@Override
public int compareTo(Object arg0) {
return this.getCommandName().compareTo(((ICommand) arg0).getCommandName());
}
@Override
public String getCommandName() {
return "buildcraft";
}
@Override
public String getCommandUsage(ICommandSender sender) {
return "/" + this.getCommandName() + " help";
}
@Override
public boolean canCommandSenderUseCommand(ICommandSender par1ICommandSender) {
return true;
}
@SuppressWarnings("rawtypes")
@Override
public List getCommandAliases() {
return null;
}
@Override
public void processCommand(ICommandSender sender, String[] arguments) {
if (arguments.length <= 0) {
throw new WrongUsageException("Type '" + this.getCommandUsage(sender) + "' for help.");
}
if (arguments[0].matches("version")) {
commandVersion(sender, arguments);
return;
} else if (arguments[0].matches("help")) {
sender.addChatMessage(new ChatComponentText("Format: '" + this.getCommandName() + " <command> <arguments>'"));
sender.addChatMessage(new ChatComponentText(StatCollector.translateToLocal("command.buildcraft.available")));
sender.addChatMessage(new ChatComponentText(" - version :" + StatCollector.translateToLocal("command.buildcraft.versioninfo")));
return;
}
throw new WrongUsageException(this.getCommandUsage(sender));
}
private void commandVersion(ICommandSender sender, String[] arguments) {
String colour = Version.isOutdated() ? "\u00A7c" : "\u00A7a";
sender.addChatMessage(new ChatComponentText(String.format(colour + StatCollector.translateToLocal("command.buildcraft.version"), Version.getVersion(),
CoreProxy.proxy.getMinecraftVersion(), Version.getRecommendedVersion())));
// TODD This takes too much realstate. See how to improve
// if (Version.isOutdated()) {
// Version.displayChangelog(sender);
// }
}
}

View file

@ -0,0 +1,22 @@
package buildcraft.core.command;
import buildcraft.core.Version;
import buildcraft.core.lib.commands.SubCommand;
import buildcraft.core.proxy.CoreProxy;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.StatCollector;
public class SubCommandVersion extends SubCommand {
public SubCommandVersion() {
super("version");
}
@Override
public void processSubCommand(ICommandSender sender, String[] args) {
String colour = Version.isOutdated() ? "\u00A7c" : "\u00A7a";
sender.addChatMessage(new ChatComponentText(String.format(colour + StatCollector.translateToLocal("command.buildcraft.version"), Version.getVersion(),
CoreProxy.proxy.getMinecraftVersion(), Version.getRecommendedVersion())));
}
}

View file

@ -0,0 +1,125 @@
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.lib.commands;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.*;
import net.minecraft.world.World;
public class CommandHelpers {
public static World getWorld(ICommandSender sender, IModCommand command, String[] args, int worldArgIndex) {
// Handle passed in world argument
if (worldArgIndex < args.length)
try {
int dim = Integer.parseInt(args[worldArgIndex]);
World world = MinecraftServer.getServer().worldServerForDimension(dim);
if (world != null)
return world;
} catch (Exception ex) {
throwWrongUsage(sender, command);
}
return getWorld(sender, command);
}
public static World getWorld(ICommandSender sender, IModCommand command) {
if (sender instanceof EntityPlayer) {
EntityPlayer player = (EntityPlayer) sender;
return player.worldObj;
}
return MinecraftServer.getServer().worldServerForDimension(0);
}
public static String[] getPlayers() {
return MinecraftServer.getServer().getAllUsernames();
}
public static void sendLocalizedChatMessage(ICommandSender sender, String locTag, Object... args) {
sender.addChatMessage(new ChatComponentTranslation(locTag, args));
}
public static void sendLocalizedChatMessage(ICommandSender sender, ChatStyle chatStyle, String locTag, Object... args) {
ChatComponentTranslation chat = new ChatComponentTranslation(locTag, args);
chat.setChatStyle(chatStyle);
sender.addChatMessage(chat);
}
/**
Avoid using this function if at all possible. Commands are processed on the server,
which has no localization information.
@param sender
@param message
*/
public static void sendChatMessage(ICommandSender sender, String message) {
sender.addChatMessage(new ChatComponentText(message));
}
public static void throwWrongUsage(ICommandSender sender, IModCommand command) throws WrongUsageException {
throw new WrongUsageException(String.format(StatCollector.translateToLocal("command.buildcraft.help"), command.getCommandUsage(sender)));
}
public static void processChildCommand(ICommandSender sender, SubCommand child, String[] args) {
if (!sender.canCommandSenderUseCommand(child.getRequiredPermissionLevel(), child.getFullCommandString()))
throw new WrongUsageException(StatCollector.translateToLocal("command.buildcraft.noperms"));
String[] newargs = new String[args.length - 1];
System.arraycopy(args, 1, newargs, 0, newargs.length);
child.processCommand(sender, newargs);
}
public static void printHelp(ICommandSender sender, IModCommand command) {
ChatStyle header = new ChatStyle();
header.setColor(EnumChatFormatting.BLUE);
sendLocalizedChatMessage(sender, header, "command.buildcraft." + command.getFullCommandString().replace(" ", ".") + ".format", command.getFullCommandString());
ChatStyle body = new ChatStyle();
body.setColor(EnumChatFormatting.GRAY);
if (command.getCommandAliases().size() > 0) {
sendLocalizedChatMessage(sender, body, "command.buildcraft.aliases", command.getCommandAliases().toString().replace("[", "").replace("]", ""));
}
if (command.getRequiredPermissionLevel() > 0) {
sendLocalizedChatMessage(sender, body, "command.buildcraft.permlevel", command.getRequiredPermissionLevel());
}
sendLocalizedChatMessage(sender, body, "command.buildcraft." + command.getFullCommandString().replace(" ", ".") + ".help");
if (!command.getChildren().isEmpty()) {
sendLocalizedChatMessage(sender, "command.buildcraft.list");
for (SubCommand child : command.getChildren()) {
sendLocalizedChatMessage(sender, "command.buildcraft." + child.getFullCommandString().replace(" ", ".") + ".desc", child.getCommandName());
}
}
}
public static boolean processStandardCommands(ICommandSender sender, IModCommand command, String[] args) {
if (args.length >= 1) {
if (args[0].equals("help")) {
command.printHelp(sender);
return true;
}
for (SubCommand child : command.getChildren()) {
if (matches(args[0], child)) {
processChildCommand(sender, child, args);
return true;
}
}
}
return false;
}
public static boolean matches(String commandName, IModCommand command) {
if (commandName.equals(command.getCommandName()))
return true;
else if (command.getCommandAliases() != null)
for (String alias : command.getCommandAliases()) {
if (commandName.equals(alias))
return true;
}
return false;
}
}

View file

@ -0,0 +1,30 @@
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.lib.commands;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import java.util.List;
import java.util.SortedSet;
public interface IModCommand extends ICommand {
String getFullCommandString();
@Override
List<String> getCommandAliases();
int getRequiredPermissionLevel();
SortedSet<SubCommand> getChildren();
void printHelp(ICommandSender sender);
}

View file

@ -0,0 +1,80 @@
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.lib.commands;
import net.minecraft.command.CommandBase;
import net.minecraft.command.ICommandSender;
import java.util.*;
public class RootCommand extends CommandBase implements IModCommand {
public final String name;
private final List<String> aliases = new ArrayList<String>();
private final SortedSet<SubCommand> children = new TreeSet<SubCommand>(new Comparator<SubCommand>() {
@Override
public int compare(SubCommand o1, SubCommand o2) {
return o1.compareTo(o2);
}
});
public RootCommand(String name) {
this.name = name;
}
public void addChildCommand(SubCommand child) {
child.setParent(this);
children.add(child);
}
public void addAlias(String alias) {
aliases.add(alias);
}
@Override
public SortedSet<SubCommand> getChildren() {
return children;
}
@Override
public String getCommandName() {
return name;
}
@Override
public int getRequiredPermissionLevel() {
return 0;
}
@Override
public List<String> getCommandAliases() {
return aliases;
}
@Override
public String getCommandUsage(ICommandSender sender) {
return "/" + this.getCommandName() + " help";
}
@Override
public void processCommand(ICommandSender sender, String[] args) {
if (!CommandHelpers.processStandardCommands(sender, this, args))
CommandHelpers.throwWrongUsage(sender, this);
}
@Override
public String getFullCommandString() {
return getCommandName();
}
@Override
public void printHelp(ICommandSender sender) {
CommandHelpers.printHelp(sender, this);
}
}

View file

@ -0,0 +1,129 @@
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.lib.commands;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import java.util.*;
public abstract class SubCommand implements IModCommand {
public enum PermLevel {
EVERYONE(0), ADMIN(2);
int permLevel;
PermLevel(int permLevel) {
this.permLevel = permLevel;
}
}
private final String name;
private final List<String> aliases = new ArrayList<String>();
private PermLevel permLevel = PermLevel.EVERYONE;
private IModCommand parent;
private final SortedSet<SubCommand> children = new TreeSet<SubCommand>(new Comparator<SubCommand>() {
@Override
public int compare(SubCommand o1, SubCommand o2) {
return o1.compareTo(o2);
}
});
public SubCommand(String name) {
this.name = name;
}
@Override
public final String getCommandName() {
return name;
}
public SubCommand addChildCommand(SubCommand child) {
child.setParent(this);
children.add(child);
return this;
}
void setParent(IModCommand parent) {
this.parent = parent;
}
@Override
public SortedSet<SubCommand> getChildren() {
return children;
}
public void addAlias(String alias) {
aliases.add(alias);
}
@Override
public List<String> getCommandAliases() {
return aliases;
}
@Override
public List addTabCompletionOptions(ICommandSender p_71516_1_, String[] p_71516_2_) {
return null;
}
@Override
public final void processCommand(ICommandSender sender, String[] args) {
if (!CommandHelpers.processStandardCommands(sender, this, args))
processSubCommand(sender, args);
}
public void processSubCommand(ICommandSender sender, String[] args) {
CommandHelpers.throwWrongUsage(sender, this);
}
public SubCommand setPermLevel(PermLevel permLevel) {
this.permLevel = permLevel;
return this;
}
@Override
public final int getRequiredPermissionLevel() {
return permLevel.permLevel;
}
@Override
public boolean canCommandSenderUseCommand(ICommandSender sender) {
return sender.canCommandSenderUseCommand(getRequiredPermissionLevel(), getCommandName());
}
@Override
public boolean isUsernameIndex(String[] args, int index) {
return false;
}
@Override
public String getCommandUsage(ICommandSender sender) {
return "/" + getFullCommandString() + " help";
}
@Override
public void printHelp(ICommandSender sender) {
CommandHelpers.printHelp(sender, this);
}
@Override
public String getFullCommandString() {
return parent.getFullCommandString() + " " + getCommandName();
}
public int compareTo(ICommand command) {
return this.getCommandName().compareTo(command.getCommandName());
}
@Override
public int compareTo(Object command) {
return this.compareTo((ICommand) command);
}
}