diff --git a/lang b/lang index ec8588ed8..1a5c7121c 160000 --- a/lang +++ b/lang @@ -1 +1 @@ -Subproject commit ec8588ed882959e7ec7d50fe6b1154d0333d9b0c +Subproject commit 1a5c7121ca0cacd7ba91991e86d525d8c8fbb6e7 diff --git a/src/main/java/forestry/apiculture/CommandBeekeepingMode.java b/src/main/java/forestry/apiculture/CommandBeekeepingMode.java deleted file mode 100755 index 2125b5756..000000000 --- a/src/main/java/forestry/apiculture/CommandBeekeepingMode.java +++ /dev/null @@ -1,255 +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.apiculture; - -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileOutputStream; -import java.io.OutputStreamWriter; -import java.text.DateFormat; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Date; -import java.util.List; -import java.util.Locale; - -import net.minecraft.command.CommandException; -import net.minecraft.command.ICommand; -import net.minecraft.command.ICommandSender; -import net.minecraft.command.WrongUsageException; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.server.MinecraftServer; -import net.minecraft.world.World; - -import com.mojang.authlib.GameProfile; - -import forestry.api.apiculture.IAlleleBeeSpecies; -import forestry.api.apiculture.IApiaristTracker; -import forestry.api.apiculture.IBeekeepingMode; -import forestry.api.genetics.AlleleManager; -import forestry.api.genetics.IAllele; -import forestry.core.config.Defaults; -import forestry.core.config.Version; -import forestry.core.proxy.Proxies; -import forestry.core.utils.CommandMC; -import forestry.core.utils.StringUtil; -import forestry.plugins.PluginApiculture; - -public class CommandBeekeepingMode extends CommandMC { - - String[] modeStrings; - - public CommandBeekeepingMode() { - modeStrings = new String[PluginApiculture.beeInterface.getBeekeepingModes().size()]; - for (int i = 0; i < PluginApiculture.beeInterface.getBeekeepingModes().size(); i++) - modeStrings[i] = PluginApiculture.beeInterface.getBeekeepingModes().get(i).getName(); - } - - @Override - public int compareTo(Object arg0) { - return this.getCommandName().compareTo(((ICommand) arg0).getCommandName()); - } - - @Override - public String getCommandName() { - return "beekeeping"; - } - - @Override - public String getCommandUsage(ICommandSender var1) { - 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("list")) - listModes(sender, arguments); - else if (arguments[0].matches("info")) - listModeInfo(sender, arguments); - else if (arguments[0].matches("set")) { - if (arguments.length <= 1) - throw new WrongUsageException("/" + this.getCommandName() + " set [] "); - - World world = getWorld(sender, arguments); - String desired = arguments[arguments.length - 1]; - - IBeekeepingMode mode = PluginApiculture.beeInterface.getBeekeepingMode(desired); - if (mode == null) - throw new CommandException(StringUtil.localize("chat.bees.command.beekeeping.error"), desired); - - PluginApiculture.beeInterface.setBeekeepingMode(world, mode.getName()); - func_152373_a(sender, this, StringUtil.localize("chat.bees.command.beekeeping.set"), mode.getName()); - - } else if (arguments[0].matches("save")) { - if (arguments.length <= 1) - throw new WrongUsageException("/" + this.getCommandName() + " save "); - - saveStatistics(sender, arguments); - } else if (arguments[0].matches("help")) { - sendChatMessage(sender, StringUtil.localizeAndFormat("chat.bees.command.help.0", this.getCommandName())); - sendChatMessage(sender, StringUtil.localize("chat.bees.command.help.1")); - sendChatMessage(sender, StringUtil.localize("chat.bees.command.help.2")); - sendChatMessage(sender, StringUtil.localize("chat.bees.command.help.3")); - sendChatMessage(sender, StringUtil.localize("chat.bees.command.help.4")); - sendChatMessage(sender, StringUtil.localize("chat.bees.command.help.5")); - } - } - - private void saveStatistics(ICommandSender sender, String[] arguments) { - - String newLine = System.getProperty("line.separator"); - World world = getWorld(sender, arguments); - - String player = arguments[1]; - Collection statistics = new ArrayList(); - - statistics.add(String.format("Beekeeping statistics for %s on %s:", player, DateFormat.getInstance().format(new Date()))); - statistics.add(""); - statistics.add("MODE: " + PluginApiculture.beeInterface.getBeekeepingMode(world).getName()); - statistics.add(""); - - GameProfile profile = MinecraftServer.getServer().func_152358_ax().func_152655_a(player); - IApiaristTracker tracker = PluginApiculture.beeInterface.getBreedingTracker(world, profile); - if (tracker == null) - statistics.add("No statistics found."); - else { - statistics.add("BRED:"); - statistics.add("-----"); - statistics.add(""); - - statistics.add("Queens:\t\t" + tracker.getQueenCount()); - statistics.add("Princesses:\t" + tracker.getPrincessCount()); - statistics.add("Drones:\t\t" + tracker.getDroneCount()); - statistics.add(""); - - Collection species = new ArrayList(); - for (IAllele allele : AlleleManager.alleleRegistry.getRegisteredAlleles().values()) - if (allele instanceof IAlleleBeeSpecies) - species.add((IAlleleBeeSpecies) allele); - - statistics.add(String.format("SPECIES (%s):", species.size())); - statistics.add("-------------"); - statistics.add(""); - - for (IAlleleBeeSpecies allele : species) - statistics.add(generateSpeciesListEntry(allele, tracker)); - } - - File file = new File(Proxies.common.getForestryRoot(), "config/" + Defaults.MOD.toLowerCase(Locale.ENGLISH) + "/stats/" + player + ".log"); - try { - - if (file.getParentFile() != null) - file.getParentFile().mkdirs(); - - if (!file.exists() && !file.createNewFile()) { - sendChatMessage(sender, StringUtil.localize("chat.bees.command.stats.error1")); - return; - } - - if (!file.canWrite()) { - sendChatMessage(sender, StringUtil.localize("chat.bees.command.stats.error2")); - return; - } - - FileOutputStream fileout = new FileOutputStream(file); - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fileout, "UTF-8")); - - writer.write("# " + Defaults.MOD + newLine + "# " + Version.getVersion() + newLine); - - for (String line : statistics) - writer.write(line + newLine); - - writer.close(); - - } catch (Exception ex) { - sendChatMessage(sender, StringUtil.localize("chat.bees.command.stats.error3")); - ex.printStackTrace(); - } - - sendChatMessage(sender, StringUtil.localizeAndFormat("chat.bees.command.stats.saved", player)); - } - - private String generateSpeciesListEntry(IAlleleBeeSpecies species, IApiaristTracker tracker) { - String discovered = "[ ]"; - if (tracker.isDiscovered(species)) - discovered = "[ X ]"; - String blacklisted = "[ ]"; - if (AlleleManager.alleleRegistry.isBlacklisted(species.getUID())) - blacklisted = "[ BL ]"; - String notcounted = "[ ]"; - if (!species.isCounted()) - notcounted = "[ NC ]"; - - return String.format("%s %s %s\t%-40s %-20s %-20s", discovered, blacklisted, notcounted, species.getUID(), species.getName(), species.getAuthority()); - } - - private void listModes(ICommandSender sender, String[] arguments) { - World world = getWorld(sender, arguments); - - String message = StringUtil.localize("chat.bees.command.mode.current"); - message = message.replaceAll("%MODE", PluginApiculture.beeInterface.getBeekeepingMode(world).getName()); - message = message.replaceAll("%WORLD", String.valueOf(world.getWorldInfo().getSaveVersion())); - sendChatMessage(sender, message); - - String help = ""; - for (IBeekeepingMode mode : PluginApiculture.beeInterface.getBeekeepingModes()) { - if (!help.isEmpty()) - help += ", "; - help += mode.getName(); - } - sendChatMessage(sender, StringUtil.localizeAndFormat("chat.bees.command.mode.help", help)); - return; - } - - private void listModeInfo(ICommandSender sender, String[] arguments) { - if (arguments.length <= 1) - throw new WrongUsageException("/" + this.getCommandName() + " info "); - - IBeekeepingMode found = null; - for (IBeekeepingMode mode : PluginApiculture.beeInterface.getBeekeepingModes()) - if (mode.getName().equalsIgnoreCase(arguments[1])) { - found = mode; - break; - } - - if (found == null) - throw new CommandException("No beekeeping mode called '%s' is available.", arguments[1]); - - sendChatMessage(sender, "\u00A7aMode: " + found.getName()); - for (String desc : found.getDescription()) - sendChatMessage(sender, StringUtil.localize(desc)); - } - - @Override - public boolean canCommandSenderUseCommand(ICommandSender sender) { - if (sender instanceof EntityPlayer) - return Proxies.common.isOp((EntityPlayer) sender); - else - return sender.canCommandSenderUseCommand(4, getCommandName()); - } - - @SuppressWarnings("rawtypes") - @Override - public List addTabCompletionOptions(ICommandSender sender, String[] incomplete) { - return getListOfStringsMatchingLastWord(incomplete, modeStrings); - } - -} diff --git a/src/main/java/forestry/apiculture/CommandGiveBee.java b/src/main/java/forestry/apiculture/CommandGiveBee.java deleted file mode 100755 index 60b14ad0b..000000000 --- a/src/main/java/forestry/apiculture/CommandGiveBee.java +++ /dev/null @@ -1,122 +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.apiculture; - -import java.util.ArrayList; -import java.util.List; - -import net.minecraft.command.ICommandSender; -import net.minecraft.command.WrongUsageException; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; - -import forestry.api.apiculture.EnumBeeType; -import forestry.api.apiculture.IAlleleBeeSpecies; -import forestry.api.apiculture.IBee; -import forestry.api.apiculture.IBeeGenome; -import forestry.api.genetics.AlleleManager; -import forestry.api.genetics.IAllele; -import forestry.api.genetics.IAlleleSpecies; -import forestry.core.utils.CommandMC; -import forestry.plugins.PluginApiculture; - -public class CommandGiveBee extends CommandMC { - - EnumBeeType type; - - public CommandGiveBee(EnumBeeType type) { - this.type = type; - } - - @Override - public String getCommandName() { - return "give" + type.toString().toLowerCase(); - } - - @Override - public String getCommandUsage(ICommandSender par1ICommandSender) { - return "/" + getCommandName() + " "; - } - - @Override - public void processCommand(ICommandSender sender, String[] arguments) { - if (arguments.length >= 2) { - EntityPlayer player = getPlayer(sender, arguments[0]); - - String parameter = arguments[1]; - - IAlleleBeeSpecies species = null; - - search: for (String uid : AlleleManager.alleleRegistry.getRegisteredAlleles().keySet()) { - - if (!uid.equals(parameter)) - continue; - - if (AlleleManager.alleleRegistry.getAllele(uid) instanceof IAlleleBeeSpecies) { - species = (IAlleleBeeSpecies) AlleleManager.alleleRegistry.getAllele(uid); - break search; - } - } - - search: if (species == null) - for (IAllele allele : AlleleManager.alleleRegistry.getRegisteredAlleles().values()) - if (allele instanceof IAlleleBeeSpecies) - if (((IAlleleBeeSpecies) allele).getName().equals(parameter)) { - species = (IAlleleBeeSpecies) allele; - break search; - } - - if (species == null) - throw new SpeciesNotFoundException(parameter); - - IAllele[] template = PluginApiculture.beeInterface.getTemplate(species.getUID()); - - if (template == null) - throw new TemplateNotFoundException(species); - - IBeeGenome genome = PluginApiculture.beeInterface.templateAsGenome(template); - - IBee bee = PluginApiculture.beeInterface.getBee(player.worldObj, genome); - - if(this.type == EnumBeeType.QUEEN) - bee.mate(bee); - - ItemStack beestack = PluginApiculture.beeInterface.getMemberStack(bee, type.ordinal()); - player.dropPlayerItemWithRandomChoice(beestack, true); - func_152373_a(sender, this, "Player %s was given a %s bee.", player.getCommandSenderName(), ((IAlleleSpecies) template[0]).getName()); - } else - throw new WrongUsageException("/" + getCommandName() + " "); - } - - /** - * Adds the strings available in this command to the given list of tab completion options. - */ - @SuppressWarnings("rawtypes") - @Override - public List addTabCompletionOptions(ICommandSender sender, String[] parameters) { - if (parameters.length == 1) - return getListOfStringsMatchingLastWord(parameters, this.getPlayers()); - else if (parameters.length == 2) - return getListOfStringsMatchingLastWord(parameters, this.getSpecies()); - else - return null; - } - - protected String[] getSpecies() { - List species = new ArrayList(); - - for (IAllele allele : AlleleManager.alleleRegistry.getRegisteredAlleles().values()) - if (allele instanceof IAlleleBeeSpecies) - species.add(((IAlleleSpecies) allele).getName()); - - return species.toArray(new String[] {}); - } -} diff --git a/src/main/java/forestry/apiculture/commands/CommandBeekeeping.java b/src/main/java/forestry/apiculture/commands/CommandBeekeeping.java new file mode 100644 index 000000000..f39dccc23 --- /dev/null +++ b/src/main/java/forestry/apiculture/commands/CommandBeekeeping.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * 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.apiculture.commands; + +import forestry.core.commands.SubCommand; + +public class CommandBeekeeping extends SubCommand { + + public CommandBeekeeping() { + super("beekeeping"); + addAlias("bee"); + addChildCommand(new CommandBeekeepingMode()); + addChildCommand(new CommandBeekeepingSave()); + addChildCommand(new CommandBeekeepingGiveBee()); + } +} diff --git a/src/main/java/forestry/apiculture/commands/CommandBeekeepingGiveBee.java b/src/main/java/forestry/apiculture/commands/CommandBeekeepingGiveBee.java new file mode 100644 index 000000000..90774095d --- /dev/null +++ b/src/main/java/forestry/apiculture/commands/CommandBeekeepingGiveBee.java @@ -0,0 +1,159 @@ +/******************************************************************************* + * 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.apiculture.commands; + +import forestry.api.apiculture.EnumBeeType; +import forestry.api.apiculture.IAlleleBeeSpecies; +import forestry.api.apiculture.IBee; +import forestry.api.apiculture.IBeeGenome; +import forestry.api.genetics.AlleleManager; +import forestry.api.genetics.IAllele; +import forestry.apiculture.SpeciesNotFoundException; +import forestry.apiculture.TemplateNotFoundException; +import forestry.core.commands.CommandHelpers; +import forestry.core.commands.SubCommand; +import forestry.plugins.PluginApiculture; +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class CommandBeekeepingGiveBee extends SubCommand { + + private String beeTypeHelpString; + private String[] beeTypeArr; + + public CommandBeekeepingGiveBee() { + super("give"); + setPermLevel(PermLevel.ADMIN); + + List beeTypeStrings = new ArrayList(); + for (EnumBeeType type : EnumBeeType.values()) { + if (type == EnumBeeType.NONE) + continue; + beeTypeStrings.add(type.getName()); + } + + beeTypeArr = beeTypeStrings.toArray(new String[beeTypeStrings.size()]); + + StringBuilder beeTypeHelp = new StringBuilder(); + String separator = ", "; + + Iterator iter = beeTypeStrings.iterator(); + while (iter.hasNext()) { + beeTypeHelp.append(iter.next()); + if (iter.hasNext()) + beeTypeHelp.append(separator); + } + beeTypeHelpString = beeTypeHelp.toString(); + } + + @Override + public void processSubCommand(ICommandSender sender, String[] arguments) { + if (arguments.length < 3) { + printHelp(sender); + return; + } + + EnumBeeType beeType = getBeeType(arguments[0]); + if (beeType == EnumBeeType.NONE) { + printHelp(sender); + return; + } + + EntityPlayer player = CommandHelpers.getPlayer(sender, arguments[1]); + + String parameter = arguments[2]; + + IAlleleBeeSpecies species = null; + + for (String uid : AlleleManager.alleleRegistry.getRegisteredAlleles().keySet()) { + + if (!uid.equals(parameter)) + continue; + + if (AlleleManager.alleleRegistry.getAllele(uid) instanceof IAlleleBeeSpecies) { + species = (IAlleleBeeSpecies) AlleleManager.alleleRegistry.getAllele(uid); + break; + } + } + + if (species == null) { + for (IAllele allele : AlleleManager.alleleRegistry.getRegisteredAlleles().values()) { + if (allele instanceof IAlleleBeeSpecies && allele.getName().equals(parameter)) { + species = (IAlleleBeeSpecies) allele; + break; + } + } + } + + if (species == null) + throw new SpeciesNotFoundException(parameter); + + IAllele[] template = PluginApiculture.beeInterface.getTemplate(species.getUID()); + + if (template == null) + throw new TemplateNotFoundException(species); + + IBeeGenome genome = PluginApiculture.beeInterface.templateAsGenome(template); + + IBee bee = PluginApiculture.beeInterface.getBee(player.worldObj, genome); + + if(beeType == EnumBeeType.QUEEN) + bee.mate(bee); + + ItemStack beeStack = PluginApiculture.beeInterface.getMemberStack(bee, beeType.ordinal()); + player.dropPlayerItemWithRandomChoice(beeStack, true); + + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.give.given", player.getCommandSenderName(), template[0].getName(), beeType.getName()); + } + + @Override + public List addTabCompletionOptions(ICommandSender sender, String[] parameters) { + if (parameters.length == 1) { + List tabCompletion = CommandHelpers.getListOfStringsMatchingLastWord(parameters, beeTypeArr); + tabCompletion.add("help"); + return tabCompletion; + } else if (parameters.length == 2) { + return CommandHelpers.getListOfStringsMatchingLastWord(parameters, CommandHelpers.getPlayers()); + } else if (parameters.length == 3) { + return CommandHelpers.getListOfStringsMatchingLastWord(parameters, getSpecies()); + } + return null; + } + + String[] getSpecies() { + List species = new ArrayList(); + + for (IAllele allele : AlleleManager.alleleRegistry.getRegisteredAlleles().values()) + if (allele instanceof IAlleleBeeSpecies) + species.add(allele.getName()); + + return species.toArray(new String[species.size()]); + } + + EnumBeeType getBeeType(String beeTypeName) { + for (EnumBeeType beeType : EnumBeeType.values()) { + if (beeType.getName().equalsIgnoreCase(beeTypeName)) + return beeType; + } + return EnumBeeType.NONE; + } + + @Override + public void printHelp(ICommandSender sender) { + super.printHelp(sender); + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.give.available", beeTypeHelpString); + } +} diff --git a/src/main/java/forestry/apiculture/commands/CommandBeekeepingMode.java b/src/main/java/forestry/apiculture/commands/CommandBeekeepingMode.java new file mode 100644 index 000000000..0502deb12 --- /dev/null +++ b/src/main/java/forestry/apiculture/commands/CommandBeekeepingMode.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * 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.apiculture.commands; + +import forestry.core.commands.SubCommand; + +public class CommandBeekeepingMode extends SubCommand { + + public CommandBeekeepingMode() { + super("mode"); + addChildCommand(new CommandBeekeepingModeInfo()); + addChildCommand(new CommandBeekeepingModeSet()); + } +} diff --git a/src/main/java/forestry/apiculture/commands/CommandBeekeepingModeInfo.java b/src/main/java/forestry/apiculture/commands/CommandBeekeepingModeInfo.java new file mode 100644 index 000000000..92e9efcfe --- /dev/null +++ b/src/main/java/forestry/apiculture/commands/CommandBeekeepingModeInfo.java @@ -0,0 +1,101 @@ +/******************************************************************************* + * 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.apiculture.commands; + +import forestry.api.apiculture.IBeekeepingMode; +import forestry.core.commands.CommandHelpers; +import forestry.core.commands.SubCommand; +import forestry.plugins.PluginApiculture; +import net.minecraft.command.ICommandSender; +import net.minecraft.util.ChatStyle; +import net.minecraft.util.EnumChatFormatting; +import net.minecraft.world.World; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class CommandBeekeepingModeInfo extends SubCommand { + + private String[] modeStringArr; + private String helpString; + + public CommandBeekeepingModeInfo() { + super("info"); + + List modeStrings = new ArrayList(); + for (IBeekeepingMode mode : PluginApiculture.beeInterface.getBeekeepingModes()) + modeStrings.add(mode.getName()); + + modeStringArr = modeStrings.toArray(new String[modeStrings.size()]); + + StringBuilder help = new StringBuilder(); + String separator = ", "; + + Iterator iter = modeStrings.iterator(); + while (iter.hasNext()) { + help.append(iter.next()); + if (iter.hasNext()) + help.append(separator); + } + helpString = help.toString(); + } + + @Override + public void processSubCommand(ICommandSender sender, String[] args) { + if (args.length <= 0) { + printHelp(sender); + return; + } + + String modeName = args[0]; + + IBeekeepingMode found = findModeWithName(modeName); + if (found == null) { + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.mode.info.error", modeName); + printHelp(sender); + return; + } + + ChatStyle green = new ChatStyle(); + green.setColor(EnumChatFormatting.GREEN); + CommandHelpers.sendLocalizedChatMessage(sender, green, found.getName()); + + for (String desc : found.getDescription()) + CommandHelpers.sendLocalizedChatMessage(sender, "for." + desc); + } + + private IBeekeepingMode findModeWithName(String name) { + for (IBeekeepingMode mode : PluginApiculture.beeInterface.getBeekeepingModes()) { + if (mode.getName().equalsIgnoreCase(name)) + return mode; + } + return null; + } + + @Override + public void printHelp(ICommandSender sender) { + super.printHelp(sender); + + World world = CommandHelpers.getWorld(sender, this); + + String modeName = PluginApiculture.beeInterface.getBeekeepingMode(world).getName(); + String worldName = String.valueOf(world.getWorldInfo().getSaveVersion()); + + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.mode.info.current", modeName, worldName); + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.mode.info.available", helpString); + } + + @Override + public List addTabCompletionOptions(ICommandSender sender, String[] incomplete) { + return CommandHelpers.getListOfStringsMatchingLastWord(incomplete, modeStringArr); + } +} diff --git a/src/main/java/forestry/apiculture/commands/CommandBeekeepingModeSet.java b/src/main/java/forestry/apiculture/commands/CommandBeekeepingModeSet.java new file mode 100644 index 000000000..450e571d2 --- /dev/null +++ b/src/main/java/forestry/apiculture/commands/CommandBeekeepingModeSet.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * 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.apiculture.commands; + +import forestry.api.apiculture.IBeekeepingMode; +import forestry.core.commands.CommandHelpers; +import forestry.core.commands.SubCommand; +import forestry.plugins.PluginApiculture; +import net.minecraft.command.ICommandSender; +import net.minecraft.world.World; + +import java.util.ArrayList; +import java.util.List; + +public class CommandBeekeepingModeSet extends SubCommand { + private String[] modeStringArr; + + public CommandBeekeepingModeSet() { + super("set"); + setPermLevel(PermLevel.ADMIN); + int modeStringCount = PluginApiculture.beeInterface.getBeekeepingModes().size(); + List modeStrings = new ArrayList(modeStringCount); + for (IBeekeepingMode mode : PluginApiculture.beeInterface.getBeekeepingModes()) + modeStrings.add(mode.getName()); + + modeStringArr = modeStrings.toArray(new String[modeStringCount]); + } + + @Override + public void processSubCommand(ICommandSender sender, String[] args) { + if (args.length == 0 || args.length > 2) { + printHelp(sender); + return; + } + + World world; + if (args.length == 2) + world = CommandHelpers.getWorld(sender, this, args, 0); + else + world = CommandHelpers.getWorld(sender, this); + + String desired = args[args.length - 1]; + + IBeekeepingMode mode = PluginApiculture.beeInterface.getBeekeepingMode(desired); + if (mode == null) { + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.mode.set.error", desired); + printHelp(sender); + return; + } + + PluginApiculture.beeInterface.setBeekeepingMode(world, mode.getName()); + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.mode.set.success", mode.getName()); + } + + @Override + public List addTabCompletionOptions(ICommandSender sender, String[] incomplete) { + return CommandHelpers.getListOfStringsMatchingLastWord(incomplete, modeStringArr); + } +} diff --git a/src/main/java/forestry/apiculture/commands/CommandBeekeepingSave.java b/src/main/java/forestry/apiculture/commands/CommandBeekeepingSave.java new file mode 100644 index 000000000..60423d3c8 --- /dev/null +++ b/src/main/java/forestry/apiculture/commands/CommandBeekeepingSave.java @@ -0,0 +1,195 @@ +/******************************************************************************* + * 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.apiculture.commands; + +import com.mojang.authlib.GameProfile; +import forestry.api.apiculture.IAlleleBeeSpecies; +import forestry.api.apiculture.IApiaristTracker; +import forestry.api.genetics.AlleleManager; +import forestry.api.genetics.IAllele; +import forestry.core.commands.CommandHelpers; +import forestry.core.commands.SubCommand; +import forestry.core.config.Defaults; +import forestry.core.config.Version; +import forestry.core.proxy.Proxies; +import forestry.plugins.PluginApiculture; +import net.minecraft.command.ICommandSender; +import net.minecraft.server.MinecraftServer; +import net.minecraft.util.StatCollector; +import net.minecraft.world.World; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.text.DateFormat; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.List; +import java.util.Locale; + +public class CommandBeekeepingSave extends SubCommand { + + private final String discoveredSymbol; + private final String blacklistedSymbol; + private final String notCountedSymbol; + + public CommandBeekeepingSave() { + super("save"); + discoveredSymbol = StatCollector.translateToLocal("for.chat.command.forestry.beekeeping.save.key.discovered.symbol"); + blacklistedSymbol = StatCollector.translateToLocal("for.chat.command.forestry.beekeeping.save.key.blacklisted.symbol"); + notCountedSymbol = StatCollector.translateToLocal("for.chat.command.forestry.beekeeping.save.key.notCounted.symbol"); + } + + @Override + public List addTabCompletionOptions(ICommandSender sender, String[] parameters) { + return CommandHelpers.getListOfStringsMatchingLastWord(parameters, CommandHelpers.getPlayers()); + } + + @Override + public void processSubCommand(ICommandSender sender, String[] args) { + if (args.length != 1) { + printHelp(sender); + return; + } + + String newLine = System.getProperty("line.separator"); + World world = CommandHelpers.getWorld(sender, this); + + String player = args[0]; + Collection statistics = new ArrayList(); + + String date = DateFormat.getInstance().format(new Date()); + statistics.add(StatCollector.translateToLocalFormatted("for.chat.command.forestry.beekeeping.save.stats", player, date)); + statistics.add(""); + String modeName = PluginApiculture.beeInterface.getBeekeepingMode(world).getName(); + statistics.add(StatCollector.translateToLocalFormatted("for.chat.command.forestry.beekeeping.save.mode", modeName)); + statistics.add(""); + + GameProfile profile = MinecraftServer.getServer().func_152358_ax().func_152655_a(player); + IApiaristTracker tracker = PluginApiculture.beeInterface.getBreedingTracker(world, profile); + if (tracker == null) + statistics.add(StatCollector.translateToLocal("for.chat.command.forestry.beekeeping.save.error4")); + else { + String discoveredLine = StatCollector.translateToLocal("for.chat.command.forestry.beekeeping.save.key.discovered") + ":"; + statistics.add(discoveredLine); + statistics.add(line(discoveredLine.length())); + + String queen = StatCollector.translateToLocal("for.bees.grammar.queen.type"); + String princess = StatCollector.translateToLocal("for.bees.grammar.princess.type"); + String drone = StatCollector.translateToLocal("for.bees.grammar.drone.type"); + statistics.add(queen + ":\t\t" + tracker.getQueenCount()); + statistics.add(princess + ":\t" + tracker.getPrincessCount()); + statistics.add(drone + ":\t\t" + tracker.getDroneCount()); + statistics.add(""); + + Collection species = new ArrayList(); + for (IAllele allele : AlleleManager.alleleRegistry.getRegisteredAlleles().values()) + if (allele instanceof IAlleleBeeSpecies) + species.add((IAlleleBeeSpecies) allele); + + String speciesCount = StatCollector.translateToLocal("for.gui.speciescount"); + String speciesCountLine = String.format("%s (%s):", speciesCount, species.size()); + statistics.add(speciesCountLine); + statistics.add(line(speciesCountLine.length())); + + statistics.add(discoveredSymbol + ": " + StatCollector.translateToLocal("for.chat.command.forestry.beekeeping.save.key.discovered")); + statistics.add(blacklistedSymbol + ": " + StatCollector.translateToLocal("for.chat.command.forestry.beekeeping.save.key.blacklisted")); + statistics.add(notCountedSymbol + ": " + StatCollector.translateToLocal("for.chat.command.forestry.beekeeping.save.key.notCounted")); + statistics.add(""); + + String header = generateSpeciesListHeader(); + statistics.add(header); + + statistics.add(line(header.length())); + statistics.add(""); + + for (IAlleleBeeSpecies allele : species) + statistics.add(generateSpeciesListEntry(allele, tracker)); + } + + File file = new File(Proxies.common.getForestryRoot(), "config/" + Defaults.MOD.toLowerCase(Locale.ENGLISH) + "/stats/" + player + ".log"); + try { + File folder = file.getParentFile(); + if (folder != null && !folder.exists()) { + boolean success = file.getParentFile().mkdirs(); + if (!success) { + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.save.error1"); + return; + } + } + + if (!file.exists() && !file.createNewFile()) { + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.save.error1"); + return; + } + + if (!file.canWrite()) { + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.save.error2"); + return; + } + + FileOutputStream fileout = new FileOutputStream(file); + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fileout, "UTF-8")); + + writer.write("# " + Defaults.MOD + newLine + "# " + Version.getVersion() + newLine); + + for (String line : statistics) + writer.write(line + newLine); + + writer.close(); + + } + catch (IOException ex) { + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.save.error3"); + ex.printStackTrace(); + return; + } + + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.save.saved", player); + } + + private String line(int length) { + StringBuilder line = new StringBuilder(); + for (int i = 0; i < length; i++) + line.append("-"); + + return line.toString(); + } + + private String generateSpeciesListHeader() { + String authority = StatCollector.translateToLocal("for.gui.alyzer.authority"); + String species = StatCollector.translateToLocal("for.gui.species"); + return speciesListEntry(discoveredSymbol, blacklistedSymbol, notCountedSymbol, "UID", species, authority); + } + + private String generateSpeciesListEntry(IAlleleBeeSpecies species, IApiaristTracker tracker) { + String discovered = ""; + if (tracker.isDiscovered(species)) + discovered = discoveredSymbol; + + String blacklisted = ""; + if (AlleleManager.alleleRegistry.isBlacklisted(species.getUID())) + blacklisted = blacklistedSymbol; + + String notCounted = ""; + if (!species.isCounted()) + notCounted = notCountedSymbol; + + return speciesListEntry(discovered, blacklisted, notCounted, species.getUID(), species.getName(), species.getAuthority()); + } + + private String speciesListEntry(String discovered, String blacklisted, String notCounted, String UID, String speciesName, String authority) { + return String.format("[ %-2s ] [ %-2s ] [ %-2s ]\t%-40s %-20s %-20s", discovered, blacklisted, notCounted, UID, speciesName, authority); + } +} diff --git a/src/main/java/forestry/core/commands/CommandHelpers.java b/src/main/java/forestry/core/commands/CommandHelpers.java index 7ad6236ae..1895241fd 100644 --- a/src/main/java/forestry/core/commands/CommandHelpers.java +++ b/src/main/java/forestry/core/commands/CommandHelpers.java @@ -11,9 +11,10 @@ package forestry.core.commands; import forestry.core.utils.StringUtil; +import net.minecraft.command.CommandBase; import net.minecraft.command.ICommandSender; import net.minecraft.command.WrongUsageException; -import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.server.MinecraftServer; import net.minecraft.util.ChatComponentText; import net.minecraft.util.ChatComponentTranslation; @@ -21,6 +22,10 @@ import net.minecraft.util.ChatStyle; import net.minecraft.util.EnumChatFormatting; import net.minecraft.world.World; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + /** * * @author CovertJaguar @@ -29,7 +34,7 @@ public class CommandHelpers { public static World getWorld(ICommandSender sender, IForestryCommand command, String[] args, int worldArgIndex) { // Handle passed in world argument - if (worldArgIndex < args.length) + if (worldArgIndex < args.length) { try { int dim = Integer.parseInt(args[worldArgIndex]); World world = MinecraftServer.getServer().worldServerForDimension(dim); @@ -38,15 +43,16 @@ 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; - } - return MinecraftServer.getServer().worldServerForDimension(0); + return sender.getEntityWorld(); + } + + public static EntityPlayerMP getPlayer(ICommandSender sender, String playerName) { + return CommandBase.getPlayer(sender, playerName); } public static String[] getPlayers() { @@ -90,14 +96,26 @@ public class CommandHelpers { } public static void printHelp(ICommandSender sender, IForestryCommand command) { + + String commandString = command.getFullCommandString().replace(" ", "."); + ChatStyle header = new ChatStyle(); header.setColor(EnumChatFormatting.BLUE); - sendLocalizedChatMessage(sender, header, "for.chat.command." + command.getFullCommandString().replace(" ", ".") + ".format", command.getFullCommandString()); + sendLocalizedChatMessage(sender, header, "for.chat.command." + commandString + ".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"); + + List commandAliases = command.getCommandAliases(); + if (commandAliases.size() > 0) + sendLocalizedChatMessage(sender, body, "for.chat.command.aliases", commandAliases.toString().replace("[", "").replace("]", "")); + + int permLevel = command.getRequiredPermissionLevel(); + if (permLevel > 0) + sendLocalizedChatMessage(sender, body, "for.chat.command.permlevel", permLevel); + + sendLocalizedChatMessage(sender, body, "for.chat.command." + commandString + ".help"); + if (!command.getChildren().isEmpty()) { sendLocalizedChatMessage(sender, "for.chat.command.list"); for (SubCommand child : command.getChildren()) { @@ -132,4 +150,31 @@ public class CommandHelpers { } return false; } + + @SuppressWarnings("unchecked") + public static List getListOfStringsMatchingLastWord(String[] strings, String ... lastWords) { + return CommandBase.getListOfStringsMatchingLastWord(strings, lastWords); + } + + public static List addStandardTabCompletionOptions(IForestryCommand command, ICommandSender sender, String[] incomplete) { + if (incomplete.length > 1) { + String commandName = incomplete[0]; + for (SubCommand child : command.getChildren()) { + if (CommandHelpers.matches(commandName, child)) { + String[] incompleteRemaining = Arrays.copyOfRange(incomplete, 1, incomplete.length); + return child.addTabCompletionOptions(sender, incompleteRemaining); + } + } + } + + List commandNames = new ArrayList(); + for (SubCommand child : command.getChildren()) { + commandNames.add(child.getCommandName()); + } + commandNames.add("help"); + + String[] commandNamesArr = commandNames.toArray(new String[commandNames.size()]); + return CommandHelpers.getListOfStringsMatchingLastWord(incomplete, commandNamesArr); + } + } diff --git a/src/main/java/forestry/core/commands/RootCommand.java b/src/main/java/forestry/core/commands/RootCommand.java index 47df86fac..ce7e060cc 100644 --- a/src/main/java/forestry/core/commands/RootCommand.java +++ b/src/main/java/forestry/core/commands/RootCommand.java @@ -73,6 +73,11 @@ public class RootCommand extends CommandBase implements IForestryCommand { CommandHelpers.throwWrongUsage(sender, this); } + @Override + public List addTabCompletionOptions(ICommandSender sender, String[] incomplete) { + return CommandHelpers.addStandardTabCompletionOptions(this, sender, incomplete); + } + @Override public String getFullCommandString() { return getCommandName(); diff --git a/src/main/java/forestry/core/commands/SubCommand.java b/src/main/java/forestry/core/commands/SubCommand.java index 4c91cf8d3..c2509327d 100644 --- a/src/main/java/forestry/core/commands/SubCommand.java +++ b/src/main/java/forestry/core/commands/SubCommand.java @@ -81,8 +81,8 @@ public abstract class SubCommand implements IForestryCommand { } @Override - public List addTabCompletionOptions(ICommandSender p_71516_1_, String[] p_71516_2_) { - return null; + public List addTabCompletionOptions(ICommandSender sender, String[] incomplete) { + return CommandHelpers.addStandardTabCompletionOptions(this, sender, incomplete); } @Override @@ -92,7 +92,7 @@ public abstract class SubCommand implements IForestryCommand { } public void processSubCommand(ICommandSender sender, String[] args) { - CommandHelpers.throwWrongUsage(sender, this); + printHelp(sender); } public SubCommand setPermLevel(PermLevel permLevel) { diff --git a/src/main/java/forestry/plugins/PluginApiculture.java b/src/main/java/forestry/plugins/PluginApiculture.java index f064ba617..101cafe47 100644 --- a/src/main/java/forestry/plugins/PluginApiculture.java +++ b/src/main/java/forestry/plugins/PluginApiculture.java @@ -37,8 +37,7 @@ import forestry.api.genetics.IAllele; import forestry.api.genetics.IClassification; import forestry.api.genetics.IClassification.EnumClassLevel; import forestry.api.recipes.RecipeManagers; -import forestry.apiculture.CommandBeekeepingMode; -import forestry.apiculture.CommandGiveBee; +import forestry.apiculture.commands.CommandBeekeeping; import forestry.apiculture.FlowerProviderCacti; import forestry.apiculture.FlowerProviderEnd; import forestry.apiculture.FlowerProviderGourd; @@ -264,6 +263,9 @@ public class PluginApiculture extends ForestryPlugin { // Register village components with the Structure registry. VillageHandlerApiculture.registerVillageComponents(); + + // Commands + PluginCore.rootCommand.addChildCommand(new CommandBeekeeping()); } @Override @@ -1275,12 +1277,6 @@ public class PluginApiculture extends ForestryPlugin { return null; } - @Override - public ICommand[] getConsoleCommands() { - return new ICommand[]{new CommandBeekeepingMode(), new CommandGiveBee(EnumBeeType.DRONE), new CommandGiveBee(EnumBeeType.PRINCESS), - new CommandGiveBee(EnumBeeType.QUEEN),}; - } - @Override public boolean processIMCMessage(IMCMessage message) { if (message.key.equals("add-candle-lighting-id")) {