Convert Mail Commands.

And various other improvements to the command code and localization support.
This commit is contained in:
CovertJaguar committed 2014-11-13 04:25:10 -08:00
1 parent 6a3d92e825
commit 79c7ae435c
11 files changed
+191 -201

No files matched your search

@@ -17,6 +17,8 @@ import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.ChatStyle;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;
/**
@@ -26,7 +28,6 @@ import net.minecraft.world.World;
public class CommandHelpers {
public static World getWorld(ICommandSender sender, IForestryCommand command, String[] args, int worldArgIndex) {
// Handle passed in world argument
if (worldArgIndex < args.length)
try {
@@ -37,13 +38,15 @@ public class CommandHelpers {
} catch (Exception ex) {
throwWrongUsage(sender, command);
}
return getWorld(sender, command);
}
public static World getWorld(ICommandSender sender, IForestryCommand command) {
if (sender instanceof EntityPlayer) {
EntityPlayer player = (EntityPlayer) sender;
return player.worldObj;
} else
return MinecraftServer.getServer().worldServerForDimension(0);
}
return MinecraftServer.getServer().worldServerForDimension(0);
}
public static String[] getPlayers() {
@@ -54,6 +57,22 @@ public class CommandHelpers {
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.
StringUtil.localize() is NOT a valid alternative for sendLocalizedChatMessage().
Messages will not be localized properly if you use StringUtil.localize().
@param sender
@param message
*/
public static void sendChatMessage(ICommandSender sender, String message) {
sender.addChatMessage(new ChatComponentText(message));
}
@@ -71,10 +90,14 @@ public class CommandHelpers {
}
public static void printHelp(ICommandSender sender, IForestryCommand command) {
sendLocalizedChatMessage(sender, "for.chat.command.format", command.getCommandFormat(sender));
sendLocalizedChatMessage(sender, "for.chat.command.aliases", command.getCommandAliases().toString().replace("[", "").replace("]", ""));
sendLocalizedChatMessage(sender, "for.chat.command.permlevel", command.getRequiredPermissionLevel());
sendLocalizedChatMessage(sender, "for.chat.command." + command.getFullCommandString().replace(" ", ".") + ".help");
ChatStyle header = new ChatStyle();
header.setColor(EnumChatFormatting.BLUE);
sendLocalizedChatMessage(sender, header, "for.chat.command." + command.getFullCommandString().replace(" ", ".") + ".format", command.getFullCommandString());
ChatStyle body = new ChatStyle();
body.setColor(EnumChatFormatting.GRAY);
sendLocalizedChatMessage(sender, body, "for.chat.command.aliases", command.getCommandAliases().toString().replace("[", "").replace("]", ""));
sendLocalizedChatMessage(sender, body, "for.chat.command.permlevel", command.getRequiredPermissionLevel());
sendLocalizedChatMessage(sender, body, "for.chat.command." + command.getFullCommandString().replace(" ", ".") + ".help");
if (!command.getChildren().isEmpty()) {
sendLocalizedChatMessage(sender, "for.chat.command.list");
for (SubCommand child : command.getChildren()) {
@@ -25,19 +25,11 @@ import net.minecraft.util.StatCollector;
public class CommandPlugins extends SubCommand {
public CommandPlugins() {
super("plugins");
addAlias("plug");
addChildCommand(new CommandPluginsInfo());
}
@Override
public String getCommandName() {
return "plugins";
}
@Override
public String getCommandFormat(ICommandSender sender) {
return "/" + getFullCommandString() + " [info <plugin-name>]";
}
@Override
public void processSubCommand(ICommandSender sender, String[] args) {
if (args.length == 0)
@@ -74,9 +66,9 @@ public class CommandPlugins extends SubCommand {
public static class CommandPluginsInfo extends SubCommand {
@Override
public String getCommandName() {
return "info";
public CommandPluginsInfo() {
super("info");
addAlias("i");
}
@Override
@@ -13,7 +13,8 @@ package forestry.core.commands;
import net.minecraft.command.ICommandSender;
import forestry.core.config.Version;
import forestry.core.proxy.Proxies;
import forestry.core.utils.StringUtil;
import net.minecraft.util.ChatStyle;
import net.minecraft.util.EnumChatFormatting;
/**
*
@@ -21,21 +22,19 @@ import forestry.core.utils.StringUtil;
*/
public class CommandVersion extends SubCommand {
@Override
public String getCommandName() {
return "version";
}
@Override
public String getCommandFormat(ICommandSender sender) {
return "/" + getFullCommandString();
public CommandVersion() {
super("version");
}
@Override
public void processSubCommand(ICommandSender sender, String[] args) {
String colour = Version.isOutdated() ? "\u00A7c" : "\u00A7a";
ChatStyle style = new ChatStyle();
if (Version.isOutdated())
style.setColor(EnumChatFormatting.RED);
else
style.setColor(EnumChatFormatting.GREEN);
CommandHelpers.sendChatMessage(sender, String.format(colour + StringUtil.localize("chat.version"), Version.getVersion(), Proxies.common.getMinecraftVersion(), Version.getRecommendedVersion()));
CommandHelpers.sendLocalizedChatMessage(sender, style, "for.chat.version", Version.getVersion(), Proxies.common.getMinecraftVersion(), Version.getRecommendedVersion());
if (Version.isOutdated())
for (String updateLine : Version.getChangelog()) {
CommandHelpers.sendChatMessage(sender, "\u00A79" + updateLine);
@@ -30,7 +30,5 @@ public interface IForestryCommand extends ICommand {
SortedSet<SubCommand> getChildren();
String getCommandFormat(ICommandSender sender);
void printHelp(ICommandSender sender);
}
@@ -73,11 +73,6 @@ public class RootCommand extends CommandBase implements IForestryCommand {
CommandHelpers.throwWrongUsage(sender, this);
}
@Override
public String getCommandFormat(ICommandSender sender) {
return "/" + getFullCommandString() + " <args>";
}
@Override
public String getFullCommandString() {
return getCommandName();
@@ -24,7 +24,20 @@ import net.minecraft.command.ICommandSender;
*/
public abstract class SubCommand implements IForestryCommand {
public enum PermLevel {
EVERYONE(0), ADMIN(2);
int permLevel;
private PermLevel(int permLevel) {
this.permLevel = permLevel;
}
}
private final String name;
private final List<String> aliases = new ArrayList<String>();
private PermLevel permLevel = PermLevel.EVERYONE;
private IForestryCommand parent;
private final SortedSet<SubCommand> children = new TreeSet<SubCommand>(new Comparator<SubCommand>() {
@@ -34,9 +47,19 @@ public abstract class SubCommand implements IForestryCommand {
}
});
public void addChildCommand(SubCommand child) {
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(IForestryCommand parent) {
@@ -66,16 +89,20 @@ public abstract class SubCommand implements IForestryCommand {
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 int getRequiredPermissionLevel() {
return 0;
public final int getRequiredPermissionLevel() {
return permLevel.permLevel;
}
@Override
@@ -93,11 +120,6 @@ public abstract class SubCommand implements IForestryCommand {
return "/" + getFullCommandString() + " help";
}
@Override
public String getCommandFormat(ICommandSender sender) {
return "/" + getFullCommandString() + " <args>";
}
@Override
public void printHelp(ICommandSender sender) {
CommandHelpers.printHelp(sender, this);
@@ -22,39 +22,17 @@ import net.minecraft.world.World;
public class CommandButterfly extends SubCommand {
public CommandButterfly() {
super("butterfly");
addAlias("bfly");
addChildCommand(new CommandButterflyKill());
}
@Override
public String getCommandName() {
return "butterfly";
}
@Override
public String getCommandFormat(ICommandSender sender) {
return "/" + getFullCommandString() + " <args>";
}
public static class CommandButterflyKill extends SubCommand {
public CommandButterflyKill() {
super("kill");
addAlias("killall");
}
@Override
public String getCommandName() {
return "kill";
}
@Override
public int getRequiredPermissionLevel() {
return 2;
}
@Override
public String getCommandFormat(ICommandSender sender) {
return "/" + getFullCommandString() + " [<dimension#>]";
setPermLevel(PermLevel.ADMIN);
}
@Override
@@ -1,119 +0,0 @@
/*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
******************************************************************************/
package forestry.mail;
import java.util.List;
import net.minecraft.command.ICommand;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.WrongUsageException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import forestry.api.mail.ITradeStation;
import forestry.api.mail.PostManager;
import forestry.api.mail.TradeStationInfo;
import forestry.core.proxy.Proxies;
import forestry.core.utils.CommandMC;
import forestry.core.utils.StringUtil;
public class CommandMail extends CommandMC {
@Override
public int compareTo(Object arg0) {
return this.getCommandName().compareTo(((ICommand) arg0).getCommandName());
}
@Override
public String getCommandName() {
return "mail";
}
@Override
public String getCommandUsage(ICommandSender sender) {
return "/" + this.getCommandName() + " help";
}
@SuppressWarnings("rawtypes")
@Override
public List getCommandAliases() {
return null;
}
@Override
public void processCommand(ICommandSender sender, String[] arguments) {
if (arguments.length <= 0)
throw new WrongUsageException(StringUtil.localizeAndFormat("chat.help", this.getCommandUsage(sender)));
if (arguments[0].matches("trades")) {
commandTrades(sender, arguments);
return;
} else if (arguments[0].matches("virtualize")) {
commandVirtualize(sender, arguments);
return;
} else if (arguments[0].matches("help")) {
sendChatMessage(sender, StringUtil.localizeAndFormat("chat.mail.command.help.0", this.getCommandName()));
sendChatMessage(sender, StringUtil.localize("chat.mail.command.help.1"));
sendChatMessage(sender, StringUtil.localize("chat.mail.command.help.2"));
sendChatMessage(sender, StringUtil.localize("chat.mail.command.help.3"));
return;
}
throw new WrongUsageException(this.getCommandUsage(sender));
}
private void commandTrades(ICommandSender sender, String[] arguments) {
if (!(sender instanceof EntityPlayer))
return;
for (ITradeStation trade : PostManager.postRegistry.getPostOffice(((EntityPlayer) sender).worldObj).getActiveTradeStations(((EntityPlayer) sender).worldObj).values())
sendChatMessage(sender, makeTradeListEntry(trade.getTradeInfo()));
}
private String makeTradeListEntry(TradeStationInfo info) {
String entry = "\u00A7c";
if (info.state == EnumStationState.OK)
entry = "\u00A7a";
String tradegood = "[ ? ]";
if (info.tradegood != null)
tradegood = info.tradegood.stackSize + "x" + info.tradegood.getDisplayName();
String demand = "[ ? ]";
if (info.required.length > 0) {
demand = "";
for (ItemStack dmd : info.required)
demand = StringUtil.append(", ", demand, dmd.stackSize + "x" + dmd.getDisplayName());
}
return String.format("%s%-12s | %-20s | %s", entry, info.address.getName(), tradegood, demand);
}
private void commandVirtualize(ICommandSender sender, String[] arguments) {
if ((sender instanceof EntityPlayer && !Proxies.common.isOp((EntityPlayer) sender)) || (!sender.canCommandSenderUseCommand(4, getCommandName()))) {
sendChatMessage(sender, "\u00a7c" + StringUtil.localize("chat.command.noperms"));
return;
}
if (arguments.length <= 1)
throw new WrongUsageException("/" + getCommandName() + " virtualize <tradestation-name>");
World world = getWorld(sender, arguments);
MailAddress address = new MailAddress(arguments[1]);
ITradeStation trade = PostManager.postRegistry.getTradeStation(world, address);
if (trade == null) {
sendChatMessage(sender, String.format("\u00a7c" + StringUtil.localize("chat.mail.command.no_tradestation"), arguments[1]));
return;
}
trade.setVirtual(!trade.isVirtual());
sendChatMessage(sender, String.format("\u00A7aSet virtualization for '%s' to %s.", trade.getAddress().getName(), trade.isVirtual()));
}
}
@@ -0,0 +1,106 @@
/*
*******************************************************************************
* Copyright (c) 2011-2014 SirSengir.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v3
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-3.0.txt
*
* Various Contributors including, but not limited to:
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
*******************************************************************************
*/
package forestry.mail.commands;
import forestry.api.mail.ITradeStation;
import forestry.api.mail.PostManager;
import forestry.api.mail.TradeStationInfo;
import forestry.core.commands.CommandHelpers;
import forestry.core.commands.SubCommand;
import forestry.core.utils.StringUtil;
import forestry.mail.EnumStationState;
import forestry.mail.MailAddress;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ChatStyle;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;
/**
*
* @author CovertJaguar <http://www.railcraft.info/>
*/
public class CommandMail extends SubCommand {
public CommandMail() {
super("mail");
addChildCommand(new CommandMailTrades());
addChildCommand(new CommandMailVirtualize());
addAlias("ml");
}
public static class CommandMailTrades extends SubCommand {
public CommandMailTrades() {
super("trades");
addAlias("tr");
}
@Override
public void processSubCommand(ICommandSender sender, String[] args) {
if (!(sender instanceof EntityPlayer))
return;
for (ITradeStation trade : PostManager.postRegistry.getPostOffice(((EntityPlayer) sender).worldObj).getActiveTradeStations(((EntityPlayer) sender).worldObj).values()) {
CommandHelpers.sendChatMessage(sender, makeTradeListEntry(trade.getTradeInfo()));
}
}
private String makeTradeListEntry(TradeStationInfo info) {
String entry = "\u00A7c";
if (info.state == EnumStationState.OK)
entry = "\u00A7a";
String tradegood = "[ ? ]";
if (info.tradegood != null)
tradegood = info.tradegood.stackSize + "x" + info.tradegood.getDisplayName();
String demand = "[ ? ]";
if (info.required.length > 0) {
demand = "";
for (ItemStack dmd : info.required) {
demand = StringUtil.append(", ", demand, dmd.stackSize + "x" + dmd.getDisplayName());
}
}
return String.format("%s%-12s | %-20s | %s", entry, info.address.getName(), tradegood, demand);
}
}
public static class CommandMailVirtualize extends SubCommand {
public CommandMailVirtualize() {
super("virtualize");
addAlias("virt");
setPermLevel(PermLevel.ADMIN);
}
@Override
public void processSubCommand(ICommandSender sender, String[] args) {
if (args.length != 1)
CommandHelpers.throwWrongUsage(sender, this);
World world = CommandHelpers.getWorld(sender, this);
MailAddress address = new MailAddress(args[0]);
ITradeStation trade = PostManager.postRegistry.getTradeStation(world, address);
if (trade == null) {
CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.mail.virtualize.no_tradestation", args[0]);
return;
}
trade.setVirtual(!trade.isVirtual());
ChatStyle green = new ChatStyle();
green.setColor(EnumChatFormatting.GREEN);
CommandHelpers.sendLocalizedChatMessage(sender, green, "for.chat.command.forestry.mail.virtualize.set", trade.getAddress().getName(), trade.isVirtual());
}
}
}
@@ -11,7 +11,6 @@
package forestry.plugins;
import net.minecraft.block.material.Material;
import net.minecraft.command.ICommand;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
@@ -37,7 +36,6 @@ import forestry.core.proxy.Proxies;
import forestry.core.triggers.Trigger;
import forestry.core.utils.LiquidHelper;
import forestry.core.utils.ShapedRecipeCustom;
import forestry.mail.CommandMail;
import forestry.api.mail.EnumAddressee;
import forestry.mail.GuiHandlerMail;
import forestry.mail.PacketHandlerMail;
@@ -45,6 +43,7 @@ import forestry.mail.PostRegistry;
import forestry.mail.PostalCarrier;
import forestry.mail.SaveEventHandlerMail;
import forestry.mail.TickHandlerMailClient;
import forestry.mail.commands.CommandMail;
import forestry.mail.gadgets.MachineMailbox;
import forestry.mail.gadgets.MachinePhilatelist;
import forestry.mail.gadgets.MachineTrader;
@@ -73,7 +72,7 @@ public class PluginMail extends ForestryPlugin {
public static Trigger lowInput10;
public static Trigger highBuffer75;
public static Trigger highBuffer90;
public static MachineDefinition definitionMailbox;
public static MachineDefinition definitionMailbox;
public static MachineDefinition definitionTradestation;
public static MachineDefinition definitionPhilatelist;
public static StampInfo[] stampDefinitions;
@@ -81,6 +80,8 @@ public class PluginMail extends ForestryPlugin {
@Override
public void preInit() {
super.preInit();
PluginCore.rootCommand.addChildCommand(new CommandMail());
new TickHandlerMailClient();
@@ -224,9 +225,4 @@ public class PluginMail extends ForestryPlugin {
public IOreDictionaryHandler getDictionaryHandler() {
return null;
}
@Override
public ICommand[] getConsoleCommands() {
return new ICommand[]{new CommandMail()};
}
}