From a68cbc54e79368dfbd1288aeabf29cf3f5803947 Mon Sep 17 00:00:00 2001 From: mezz Date: Sat, 13 Dec 2014 23:38:36 -0800 Subject: [PATCH] Fix and migrate Tree commands to new format. Remove CommandMC --- lang | 2 +- .../apiculture/commands/BeeModeHelper.java | 59 +++++ .../commands/BeeStatsSaveHelper.java | 78 ++++++ .../apiculture/commands/CommandBee.java | 32 +++ ...eepingGiveBee.java => CommandBeeGive.java} | 6 +- .../arboriculture/CommandTreekeepingMode.java | 247 ------------------ .../arboriculture/commands/CommandTree.java | 33 +++ ...dTreesSpawn.java => CommandTreeSpawn.java} | 73 +----- .../commands/CommandTreesSpawnTree.java | 31 --- ...eesSpawnForest.java => ForestSpawner.java} | 16 +- .../{CommandTrees.java => ITreeSpawner.java} | 11 +- .../arboriculture/commands/TreeGenHelper.java | 80 ++++++ .../commands/TreeModeHelper.java | 59 +++++ .../arboriculture/commands/TreeSpawner.java | 37 +++ .../commands/TreeStatsSaveHelper.java | 63 +++++ .../commands/CommandMode.java} | 14 +- .../commands/CommandModeInfo.java} | 62 ++--- .../commands/CommandModeSet.java} | 34 +-- .../commands/CommandSaveStats.java} | 113 ++++---- .../commands/ICommandModeHelper.java} | 22 +- .../core/commands/IStatsSaveHelper.java | 35 +++ .../java/forestry/core/utils/CommandMC.java | 53 ---- .../java/forestry/core/utils/StringUtil.java | 8 + .../forestry/plugins/PluginApiculture.java | 18 +- .../forestry/plugins/PluginArboriculture.java | 24 +- 25 files changed, 621 insertions(+), 589 deletions(-) create mode 100644 src/main/java/forestry/apiculture/commands/BeeModeHelper.java create mode 100644 src/main/java/forestry/apiculture/commands/BeeStatsSaveHelper.java create mode 100644 src/main/java/forestry/apiculture/commands/CommandBee.java rename src/main/java/forestry/apiculture/commands/{CommandBeekeepingGiveBee.java => CommandBeeGive.java} (95%) delete mode 100755 src/main/java/forestry/arboriculture/CommandTreekeepingMode.java create mode 100644 src/main/java/forestry/arboriculture/commands/CommandTree.java rename src/main/java/forestry/arboriculture/commands/{CommandTreesSpawn.java => CommandTreeSpawn.java} (53%) delete mode 100644 src/main/java/forestry/arboriculture/commands/CommandTreesSpawnTree.java rename src/main/java/forestry/arboriculture/commands/{CommandTreesSpawnForest.java => ForestSpawner.java} (75%) rename src/main/java/forestry/arboriculture/commands/{CommandTrees.java => ITreeSpawner.java} (72%) create mode 100644 src/main/java/forestry/arboriculture/commands/TreeGenHelper.java create mode 100644 src/main/java/forestry/arboriculture/commands/TreeModeHelper.java create mode 100644 src/main/java/forestry/arboriculture/commands/TreeSpawner.java create mode 100644 src/main/java/forestry/arboriculture/commands/TreeStatsSaveHelper.java rename src/main/java/forestry/{apiculture/commands/CommandBeekeepingMode.java => core/commands/CommandMode.java} (68%) rename src/main/java/forestry/{apiculture/commands/CommandBeekeepingModeInfo.java => core/commands/CommandModeInfo.java} (53%) rename src/main/java/forestry/{apiculture/commands/CommandBeekeepingModeSet.java => core/commands/CommandModeSet.java} (60%) rename src/main/java/forestry/{apiculture/commands/CommandBeekeepingSave.java => core/commands/CommandSaveStats.java} (63%) rename src/main/java/forestry/{apiculture/commands/CommandBeekeeping.java => core/commands/ICommandModeHelper.java} (62%) create mode 100644 src/main/java/forestry/core/commands/IStatsSaveHelper.java delete mode 100755 src/main/java/forestry/core/utils/CommandMC.java diff --git a/lang b/lang index 76846d9c7..71f517c5a 160000 --- a/lang +++ b/lang @@ -1 +1 @@ -Subproject commit 76846d9c7646a02c8ac28e673579f065d499b3b9 +Subproject commit 71f517c5a6d4bb564b3996822b84da91bff791f3 diff --git a/src/main/java/forestry/apiculture/commands/BeeModeHelper.java b/src/main/java/forestry/apiculture/commands/BeeModeHelper.java new file mode 100644 index 000000000..396ed7229 --- /dev/null +++ b/src/main/java/forestry/apiculture/commands/BeeModeHelper.java @@ -0,0 +1,59 @@ +/******************************************************************************* + * 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 java.util.ArrayList; +import java.util.List; + +import net.minecraft.world.World; + +import forestry.api.apiculture.IBeekeepingMode; +import forestry.core.commands.ICommandModeHelper; +import forestry.plugins.PluginApiculture; + +public class BeeModeHelper implements ICommandModeHelper { + + @Override + public String[] getModeNames() { + ArrayList beekeepingModes = PluginApiculture.beeInterface.getBeekeepingModes(); + int modeStringCount = beekeepingModes.size(); + List modeStrings = new ArrayList(modeStringCount); + for (IBeekeepingMode mode : beekeepingModes) + modeStrings.add(mode.getName()); + + return modeStrings.toArray(new String[modeStringCount]); + } + + @Override + public String getModeNameMatching(String desired) { + IBeekeepingMode mode = PluginApiculture.beeInterface.getBeekeepingMode(desired); + if (mode == null) + return null; + return mode.getName(); + } + + @Override + public String getModeName(World world) { + return PluginApiculture.beeInterface.getBeekeepingMode(world).getName(); + } + + @Override + public void setMode(World world, String modeName) { + PluginApiculture.beeInterface.setBeekeepingMode(world, modeName); + } + + @Override + public ArrayList getDescription(String modeName) { + IBeekeepingMode mode = PluginApiculture.beeInterface.getBeekeepingMode(modeName); + return mode.getDescription(); + } + +} diff --git a/src/main/java/forestry/apiculture/commands/BeeStatsSaveHelper.java b/src/main/java/forestry/apiculture/commands/BeeStatsSaveHelper.java new file mode 100644 index 000000000..6e69bd27f --- /dev/null +++ b/src/main/java/forestry/apiculture/commands/BeeStatsSaveHelper.java @@ -0,0 +1,78 @@ +/******************************************************************************* + * 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 java.util.ArrayList; +import java.util.Collection; + +import net.minecraft.util.StatCollector; +import net.minecraft.world.World; + +import forestry.api.apiculture.IAlleleBeeSpecies; +import forestry.api.apiculture.IApiaristTracker; +import forestry.api.genetics.AlleleManager; +import forestry.api.genetics.IAllele; +import forestry.api.genetics.IAlleleSpecies; +import forestry.api.genetics.IBreedingTracker; +import forestry.core.commands.IStatsSaveHelper; +import forestry.core.utils.StringUtil; +import forestry.plugins.PluginApiculture; + +import com.mojang.authlib.GameProfile; + +public class BeeStatsSaveHelper implements IStatsSaveHelper { + + @Override + public String getUnlocalizedSaveStatsString() { + return "for.chat.command.forestry.bee.save.stats"; + } + + @Override + public void addExtraInfo(Collection statistics, IBreedingTracker breedingTracker) { + IApiaristTracker tracker = (IApiaristTracker)breedingTracker; + String discoveredLine = StatCollector.translateToLocal("for.chat.command.forestry.stats.save.key.discovered") + ":"; + statistics.add(discoveredLine); + statistics.add(StringUtil.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(""); + } + + @Override + public String getModeName(World world) { + return PluginApiculture.beeInterface.getBeekeepingMode(world).getName(); + } + + @Override + public Collection getSpecies() { + Collection species = new ArrayList(); + for (IAllele allele : AlleleManager.alleleRegistry.getRegisteredAlleles().values()) + if (allele instanceof IAlleleBeeSpecies) + species.add((IAlleleBeeSpecies) allele); + return species; + } + + @Override + public String getFileSuffix() { + return "bees"; + } + + @Override + public IBreedingTracker getBreedingTracker(World world, GameProfile gameProfile) { + return PluginApiculture.beeInterface.getBreedingTracker(world, gameProfile); + } + +} diff --git a/src/main/java/forestry/apiculture/commands/CommandBee.java b/src/main/java/forestry/apiculture/commands/CommandBee.java new file mode 100644 index 000000000..baf36df83 --- /dev/null +++ b/src/main/java/forestry/apiculture/commands/CommandBee.java @@ -0,0 +1,32 @@ +/******************************************************************************* + * 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.CommandMode; +import forestry.core.commands.CommandSaveStats; +import forestry.core.commands.ICommandModeHelper; +import forestry.core.commands.IStatsSaveHelper; +import forestry.core.commands.SubCommand; + +public class CommandBee extends SubCommand { + + public CommandBee() { + super("bee"); + addAlias("beekeeping"); + + IStatsSaveHelper saveHelper = new BeeStatsSaveHelper(); + ICommandModeHelper modeHelper = new BeeModeHelper(); + + addChildCommand(new CommandMode(modeHelper)); + addChildCommand(new CommandSaveStats(saveHelper)); + addChildCommand(new CommandBeeGive()); + } +} diff --git a/src/main/java/forestry/apiculture/commands/CommandBeekeepingGiveBee.java b/src/main/java/forestry/apiculture/commands/CommandBeeGive.java similarity index 95% rename from src/main/java/forestry/apiculture/commands/CommandBeekeepingGiveBee.java rename to src/main/java/forestry/apiculture/commands/CommandBeeGive.java index 4134d27ca..b8a72b595 100644 --- a/src/main/java/forestry/apiculture/commands/CommandBeekeepingGiveBee.java +++ b/src/main/java/forestry/apiculture/commands/CommandBeeGive.java @@ -30,12 +30,12 @@ import forestry.core.commands.SubCommand; import forestry.core.commands.TemplateNotFoundException; import forestry.plugins.PluginApiculture; -public class CommandBeekeepingGiveBee extends SubCommand { +public class CommandBeeGive extends SubCommand { private final String beeTypeHelpString; private final String[] beeTypeArr; - public CommandBeekeepingGiveBee() { + public CommandBeeGive() { super("give"); setPermLevel(PermLevel.ADMIN); @@ -93,7 +93,7 @@ public class CommandBeekeepingGiveBee extends SubCommand { ItemStack beeStack = PluginApiculture.beeInterface.getMemberStack(bee, beeType.ordinal()); player.dropPlayerItemWithRandomChoice(beeStack, true); - CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.give.given", player.getCommandSenderName(), bee.getGenome().getPrimary().getName(), beeType.getName()); + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.bee.give.given", player.getCommandSenderName(), bee.getGenome().getPrimary().getName(), beeType.getName()); } private static IBeeGenome getBeeGenome(String speciesName) { diff --git a/src/main/java/forestry/arboriculture/CommandTreekeepingMode.java b/src/main/java/forestry/arboriculture/CommandTreekeepingMode.java deleted file mode 100755 index d21ca6daa..000000000 --- a/src/main/java/forestry/arboriculture/CommandTreekeepingMode.java +++ /dev/null @@ -1,247 +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.arboriculture; - -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.arboriculture.IAlleleTreeSpecies; -import forestry.api.arboriculture.IArboristTracker; -import forestry.api.arboriculture.ITreekeepingMode; -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.PluginArboriculture; - -public class CommandTreekeepingMode extends CommandMC { - - private final String[] modeStrings; - - public CommandTreekeepingMode() { - modeStrings = new String[PluginArboriculture.treeInterface.getTreekeepingModes().size()]; - for (int i = 0; i < PluginArboriculture.treeInterface.getTreekeepingModes().size(); i++) - modeStrings[i] = PluginArboriculture.treeInterface.getTreekeepingModes().get(i).getName(); - } - - @Override - public int compareTo(Object arg0) { - return this.getCommandName().compareTo(((ICommand) arg0).getCommandName()); - } - - @Override - public String getCommandName() { - return "treekeeping"; - } - - @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]; - - ITreekeepingMode mode = PluginArboriculture.treeInterface.getTreekeepingMode(desired); - if (mode == null) - throw new CommandException(StringUtil.localize("chat.trees.command.treekeeping.error"), desired); - - PluginArboriculture.treeInterface.setTreekeepingMode(world, mode.getName()); - func_152373_a(sender, this, StringUtil.localize("chat.trees.command.treekeeping.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.trees.command.help.0", this.getCommandName())); - sendChatMessage(sender, StringUtil.localize("chat.trees.command.help.1")); - sendChatMessage(sender, StringUtil.localize("chat.trees.command.help.2")); - sendChatMessage(sender, StringUtil.localize("chat.trees.command.help.3")); - sendChatMessage(sender, StringUtil.localize("chat.trees.command.help.4")); - sendChatMessage(sender, StringUtil.localize("chat.trees.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("Treekeeping statistics for %s on %s:", player, DateFormat.getInstance().format(new Date()))); - statistics.add(""); - statistics.add("MODE: " + PluginArboriculture.treeInterface.getTreekeepingMode(world).getName()); - statistics.add(""); - - GameProfile profile = MinecraftServer.getServer().func_152358_ax().func_152655_a(player); - IArboristTracker tracker = PluginArboriculture.treeInterface.getBreedingTracker(world, profile); - if (tracker == null) - statistics.add("No statistics found."); - else { - statistics.add("BRED:"); - statistics.add("-----"); - statistics.add(""); - - Collection species = new ArrayList(); - for (IAllele allele : AlleleManager.alleleRegistry.getRegisteredAlleles().values()) - if (allele instanceof IAlleleTreeSpecies) - species.add((IAlleleTreeSpecies) allele); - - statistics.add(String.format("SPECIES (%s):", species.size())); - statistics.add("-------------"); - statistics.add(""); - - for (IAlleleTreeSpecies 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, "Log file could not be created. Failed to save statistics."); - return; - } - - if (!file.canWrite()) { - sendChatMessage(sender, "Cannot write to log file. Failed to save statistics."); - 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, "Write operation threw an exception. Failed to save statistics."); - ex.printStackTrace(); - } - - sendChatMessage(sender, "Saved statistics for player " + player); - } - - private String generateSpeciesListEntry(IAlleleTreeSpecies species, IArboristTracker 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); - - sendChatMessage(sender, "Current: " + PluginArboriculture.treeInterface.getTreekeepingMode(world).getName() + " (#" + world.getWorldInfo().getSaveVersion() - + ")"); - - String help = ""; - for (ITreekeepingMode mode : PluginArboriculture.treeInterface.getTreekeepingModes()) { - if (!help.isEmpty()) - help += ", "; - help += mode.getName(); - } - sendChatMessage(sender, "Available modes: " + help); - } - - private void listModeInfo(ICommandSender sender, String[] arguments) { - if (arguments.length <= 1) - throw new WrongUsageException("/" + this.getCommandName() + " info "); - - ITreekeepingMode found = null; - for (ITreekeepingMode mode : PluginArboriculture.treeInterface.getTreekeepingModes()) - if (mode.getName().equalsIgnoreCase(arguments[1])) { - found = mode; - break; - } - - if (found == null) - throw new CommandException("No treekeeping 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/arboriculture/commands/CommandTree.java b/src/main/java/forestry/arboriculture/commands/CommandTree.java new file mode 100644 index 000000000..88c86d8b3 --- /dev/null +++ b/src/main/java/forestry/arboriculture/commands/CommandTree.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * 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.arboriculture.commands; + +import forestry.core.commands.CommandMode; +import forestry.core.commands.CommandSaveStats; +import forestry.core.commands.ICommandModeHelper; +import forestry.core.commands.IStatsSaveHelper; +import forestry.core.commands.SubCommand; + +public class CommandTree extends SubCommand { + + public CommandTree() { + super("tree"); + + IStatsSaveHelper saveHelper = new TreeStatsSaveHelper(); + ICommandModeHelper modeHelper = new TreeModeHelper(); + + addChildCommand(new CommandTreeSpawn("spawnTree", new TreeSpawner())); + addChildCommand(new CommandTreeSpawn("spawnForest", new ForestSpawner())); + addChildCommand(new CommandMode(modeHelper)); + addChildCommand(new CommandSaveStats(saveHelper)); + } + +} diff --git a/src/main/java/forestry/arboriculture/commands/CommandTreesSpawn.java b/src/main/java/forestry/arboriculture/commands/CommandTreeSpawn.java similarity index 53% rename from src/main/java/forestry/arboriculture/commands/CommandTreesSpawn.java rename to src/main/java/forestry/arboriculture/commands/CommandTreeSpawn.java index 7a05b7964..39341c665 100644 --- a/src/main/java/forestry/arboriculture/commands/CommandTreesSpawn.java +++ b/src/main/java/forestry/arboriculture/commands/CommandTreeSpawn.java @@ -16,27 +16,23 @@ import java.util.List; import net.minecraft.command.ICommandSender; import net.minecraft.command.PlayerNotFoundException; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.world.gen.feature.WorldGenerator; import forestry.api.arboriculture.IAlleleTreeSpecies; -import forestry.api.arboriculture.ITree; -import forestry.api.arboriculture.ITreeGenome; import forestry.api.genetics.AlleleManager; import forestry.api.genetics.IAllele; import forestry.core.commands.CommandHelpers; -import forestry.core.commands.SpeciesNotFoundException; import forestry.core.commands.SubCommand; -import forestry.core.commands.TemplateNotFoundException; -import forestry.core.worldgen.WorldGenBase; -import forestry.plugins.PluginArboriculture; import org.apache.commons.lang3.StringUtils; -public abstract class CommandTreesSpawn extends SubCommand { +public final class CommandTreeSpawn extends SubCommand { - public CommandTreesSpawn(String name) { + private final ITreeSpawner treeSpawner; + + public CommandTreeSpawn(String name, ITreeSpawner treeSpawner) { super(name); setPermLevel(PermLevel.ADMIN); + this.treeSpawner = treeSpawner; } @Override @@ -58,27 +54,9 @@ public abstract class CommandTreesSpawn extends SubCommand { treeName = StringUtils.join(arguments, " "); } - processSubCommand(sender, treeName, player); - } - - protected abstract void processSubCommand(ICommandSender sender, String treeName, EntityPlayer player); - - protected final WorldGenerator getWorldGen(String treeName, EntityPlayer player, int x, int y, int z) { - ITreeGenome treeGenome = getTreeGenome(treeName); - if (treeGenome == null) { - return null; - } - - ITree tree = PluginArboriculture.treeInterface.getTree(player.worldObj, treeGenome); - return tree.getTreeGenerator(player.worldObj, x, y, z, true); - } - - protected final void generateTree(WorldGenerator gen, EntityPlayer player, int x, int y, int z) { - if (gen instanceof WorldGenBase) { - ((WorldGenBase) gen).generate(player.worldObj, player.worldObj.rand, x, y, z, true); - } else { - gen.generate(player.worldObj, player.worldObj.rand, x, y, z); - } + boolean success = treeSpawner.spawn(sender, treeName, player); + if (!success) + printHelp(sender); } @Override @@ -103,39 +81,4 @@ public abstract class CommandTreesSpawn extends SubCommand { return species.toArray(new String[species.size()]); } - private static ITreeGenome getTreeGenome(String speciesName) { - IAlleleTreeSpecies species = null; - - for (String uid : AlleleManager.alleleRegistry.getRegisteredAlleles().keySet()) { - - if (!uid.equals(speciesName)) - continue; - - IAllele allele = AlleleManager.alleleRegistry.getAllele(uid); - if (allele instanceof IAlleleTreeSpecies) { - species = (IAlleleTreeSpecies) allele; - break; - } - } - - if (species == null) { - for (IAllele allele : AlleleManager.alleleRegistry.getRegisteredAlleles().values()) { - if (allele instanceof IAlleleTreeSpecies && allele.getName().replaceAll("\\s", "").equals(speciesName)) { - species = (IAlleleTreeSpecies) allele; - break; - } - } - } - - if (species == null) - throw new SpeciesNotFoundException(speciesName); - - IAllele[] template = PluginArboriculture.treeInterface.getTemplate(species.getUID()); - - if (template == null) - throw new TemplateNotFoundException(species); - - return PluginArboriculture.treeInterface.templateAsGenome(template); - } - } diff --git a/src/main/java/forestry/arboriculture/commands/CommandTreesSpawnTree.java b/src/main/java/forestry/arboriculture/commands/CommandTreesSpawnTree.java deleted file mode 100644 index 54daa8061..000000000 --- a/src/main/java/forestry/arboriculture/commands/CommandTreesSpawnTree.java +++ /dev/null @@ -1,31 +0,0 @@ -package forestry.arboriculture.commands; - -import net.minecraft.command.ICommandSender; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.util.Vec3; -import net.minecraft.world.gen.feature.WorldGenerator; - -public class CommandTreesSpawnTree extends CommandTreesSpawn { - - public CommandTreesSpawnTree() { - super("spawnTree"); - } - - @Override - protected void processSubCommand(ICommandSender sender, String treeName, EntityPlayer player) { - Vec3 look = player.getLookVec(); - - int x = (int)Math.round(player.posX + (3 * look.xCoord)); - int y = (int)Math.round(player.posY); - int z = (int)Math.round(player.posZ + (3 * look.zCoord)); - - WorldGenerator gen = getWorldGen(treeName, player, x, y, z); - if (gen == null) { - printHelp(sender); - return; - } - - generateTree(gen, player, x, y, z); - } - -} diff --git a/src/main/java/forestry/arboriculture/commands/CommandTreesSpawnForest.java b/src/main/java/forestry/arboriculture/commands/ForestSpawner.java similarity index 75% rename from src/main/java/forestry/arboriculture/commands/CommandTreesSpawnForest.java rename to src/main/java/forestry/arboriculture/commands/ForestSpawner.java index 450b3956a..002f9b962 100644 --- a/src/main/java/forestry/arboriculture/commands/CommandTreesSpawnForest.java +++ b/src/main/java/forestry/arboriculture/commands/ForestSpawner.java @@ -15,13 +15,9 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.Vec3; import net.minecraft.world.gen.feature.WorldGenerator; -public class CommandTreesSpawnForest extends CommandTreesSpawn { +public class ForestSpawner implements ITreeSpawner { - public CommandTreesSpawnForest() { - super("spawnForest"); - } - - protected void processSubCommand(ICommandSender sender, String treeName, EntityPlayer player) { + public boolean spawn(ICommandSender sender, String treeName, EntityPlayer player) { Vec3 look = player.getLookVec(); int x = (int)Math.round(player.posX + (16 * look.xCoord)); @@ -32,13 +28,13 @@ public class CommandTreesSpawnForest extends CommandTreesSpawn { int spawnX = x + player.worldObj.rand.nextInt(32) - 16; int spawnZ = z + player.worldObj.rand.nextInt(32) - 16; - WorldGenerator gen = getWorldGen(treeName, player, spawnX, y, spawnZ); + WorldGenerator gen = TreeGenHelper.getWorldGen(treeName, player, spawnX, y, spawnZ); if (gen == null) { - printHelp(sender); - return; + return false; } - generateTree(gen, player, spawnX, y, spawnZ); + TreeGenHelper.generateTree(gen, player, spawnX, y, spawnZ); } + return true; } } diff --git a/src/main/java/forestry/arboriculture/commands/CommandTrees.java b/src/main/java/forestry/arboriculture/commands/ITreeSpawner.java similarity index 72% rename from src/main/java/forestry/arboriculture/commands/CommandTrees.java rename to src/main/java/forestry/arboriculture/commands/ITreeSpawner.java index dc6da946e..ddeec9b99 100644 --- a/src/main/java/forestry/arboriculture/commands/CommandTrees.java +++ b/src/main/java/forestry/arboriculture/commands/ITreeSpawner.java @@ -10,14 +10,11 @@ ******************************************************************************/ package forestry.arboriculture.commands; -import forestry.core.commands.SubCommand; +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.player.EntityPlayer; -public class CommandTrees extends SubCommand { +public interface ITreeSpawner { - public CommandTrees() { - super("tree"); - addChildCommand(new CommandTreesSpawnTree()); - addChildCommand(new CommandTreesSpawnForest()); - } + boolean spawn(ICommandSender sender, String treeName, EntityPlayer player); } diff --git a/src/main/java/forestry/arboriculture/commands/TreeGenHelper.java b/src/main/java/forestry/arboriculture/commands/TreeGenHelper.java new file mode 100644 index 000000000..870b7d1b6 --- /dev/null +++ b/src/main/java/forestry/arboriculture/commands/TreeGenHelper.java @@ -0,0 +1,80 @@ +/******************************************************************************* + * 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.arboriculture.commands; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.world.gen.feature.WorldGenerator; + +import forestry.api.arboriculture.IAlleleTreeSpecies; +import forestry.api.arboriculture.ITree; +import forestry.api.arboriculture.ITreeGenome; +import forestry.api.genetics.AlleleManager; +import forestry.api.genetics.IAllele; +import forestry.core.commands.SpeciesNotFoundException; +import forestry.core.commands.TemplateNotFoundException; +import forestry.core.worldgen.WorldGenBase; +import forestry.plugins.PluginArboriculture; + +public final class TreeGenHelper { + + public static WorldGenerator getWorldGen(String treeName, EntityPlayer player, int x, int y, int z) { + ITreeGenome treeGenome = getTreeGenome(treeName); + if (treeGenome == null) { + return null; + } + + ITree tree = PluginArboriculture.treeInterface.getTree(player.worldObj, treeGenome); + return tree.getTreeGenerator(player.worldObj, x, y, z, true); + } + + public static void generateTree(WorldGenerator gen, EntityPlayer player, int x, int y, int z) { + if (gen instanceof WorldGenBase) { + ((WorldGenBase) gen).generate(player.worldObj, player.worldObj.rand, x, y, z, true); + } else { + gen.generate(player.worldObj, player.worldObj.rand, x, y, z); + } + } + + private static ITreeGenome getTreeGenome(String speciesName) { + IAlleleTreeSpecies species = null; + + for (String uid : AlleleManager.alleleRegistry.getRegisteredAlleles().keySet()) { + + if (!uid.equals(speciesName)) + continue; + + IAllele allele = AlleleManager.alleleRegistry.getAllele(uid); + if (allele instanceof IAlleleTreeSpecies) { + species = (IAlleleTreeSpecies) allele; + break; + } + } + + if (species == null) { + for (IAllele allele : AlleleManager.alleleRegistry.getRegisteredAlleles().values()) { + if (allele instanceof IAlleleTreeSpecies && allele.getName().replaceAll("\\s", "").equals(speciesName)) { + species = (IAlleleTreeSpecies) allele; + break; + } + } + } + + if (species == null) + throw new SpeciesNotFoundException(speciesName); + + IAllele[] template = PluginArboriculture.treeInterface.getTemplate(species.getUID()); + + if (template == null) + throw new TemplateNotFoundException(species); + + return PluginArboriculture.treeInterface.templateAsGenome(template); + } +} diff --git a/src/main/java/forestry/arboriculture/commands/TreeModeHelper.java b/src/main/java/forestry/arboriculture/commands/TreeModeHelper.java new file mode 100644 index 000000000..c0a13ee1f --- /dev/null +++ b/src/main/java/forestry/arboriculture/commands/TreeModeHelper.java @@ -0,0 +1,59 @@ +/******************************************************************************* + * 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.arboriculture.commands; + +import java.util.ArrayList; +import java.util.List; + +import net.minecraft.world.World; + +import forestry.api.arboriculture.ITreekeepingMode; +import forestry.core.commands.ICommandModeHelper; +import forestry.plugins.PluginArboriculture; + +public class TreeModeHelper implements ICommandModeHelper { + + @Override + public String[] getModeNames() { + ArrayList treekeepingModes = PluginArboriculture.treeInterface.getTreekeepingModes(); + int modeStringCount = treekeepingModes.size(); + List modeStrings = new ArrayList(modeStringCount); + for (ITreekeepingMode mode : treekeepingModes) + modeStrings.add(mode.getName()); + + return modeStrings.toArray(new String[modeStringCount]); + } + + @Override + public String getModeNameMatching(String desired) { + ITreekeepingMode mode = PluginArboriculture.treeInterface.getTreekeepingMode(desired); + if (mode == null) + return null; + return mode.getName(); + } + + @Override + public String getModeName(World world) { + return PluginArboriculture.treeInterface.getTreekeepingMode(world).getName(); + } + + @Override + public void setMode(World world, String modeName) { + PluginArboriculture.treeInterface.setTreekeepingMode(world, modeName); + } + + @Override + public ArrayList getDescription(String modeName) { + ITreekeepingMode mode = PluginArboriculture.treeInterface.getTreekeepingMode(modeName); + return mode.getDescription(); + } + +} diff --git a/src/main/java/forestry/arboriculture/commands/TreeSpawner.java b/src/main/java/forestry/arboriculture/commands/TreeSpawner.java new file mode 100644 index 000000000..6e47c368a --- /dev/null +++ b/src/main/java/forestry/arboriculture/commands/TreeSpawner.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * 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.arboriculture.commands; + +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.util.Vec3; +import net.minecraft.world.gen.feature.WorldGenerator; + +public class TreeSpawner implements ITreeSpawner { + + @Override + public boolean spawn(ICommandSender sender, String treeName, EntityPlayer player) { + Vec3 look = player.getLookVec(); + + int x = (int)Math.round(player.posX + (3 * look.xCoord)); + int y = (int)Math.round(player.posY); + int z = (int)Math.round(player.posZ + (3 * look.zCoord)); + + WorldGenerator gen = TreeGenHelper.getWorldGen(treeName, player, x, y, z); + if (gen == null) { + return false; + } + + TreeGenHelper.generateTree(gen, player, x, y, z); + return true; + } + +} diff --git a/src/main/java/forestry/arboriculture/commands/TreeStatsSaveHelper.java b/src/main/java/forestry/arboriculture/commands/TreeStatsSaveHelper.java new file mode 100644 index 000000000..6a1b2e568 --- /dev/null +++ b/src/main/java/forestry/arboriculture/commands/TreeStatsSaveHelper.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * 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.arboriculture.commands; + +import java.util.ArrayList; +import java.util.Collection; + +import net.minecraft.world.World; + +import forestry.api.arboriculture.IAlleleTreeSpecies; +import forestry.api.genetics.AlleleManager; +import forestry.api.genetics.IAllele; +import forestry.api.genetics.IAlleleSpecies; +import forestry.api.genetics.IBreedingTracker; +import forestry.core.commands.IStatsSaveHelper; +import forestry.plugins.PluginArboriculture; + +import com.mojang.authlib.GameProfile; + +public class TreeStatsSaveHelper implements IStatsSaveHelper { + + @Override + public String getUnlocalizedSaveStatsString() { + return "for.chat.command.forestry.tree.save.stats"; + } + + @Override + public void addExtraInfo(Collection statistics, IBreedingTracker breedingTracker) { + } + + @Override + public String getModeName(World world) { + return PluginArboriculture.treeInterface.getTreekeepingMode(world).getName(); + } + + @Override + public Collection getSpecies() { + Collection species = new ArrayList(); + for (IAllele allele : AlleleManager.alleleRegistry.getRegisteredAlleles().values()) + if (allele instanceof IAlleleTreeSpecies) + species.add((IAlleleTreeSpecies) allele); + return species; + } + + @Override + public String getFileSuffix() { + return "trees"; + } + + @Override + public IBreedingTracker getBreedingTracker(World world, GameProfile gameProfile) { + return PluginArboriculture.treeInterface.getBreedingTracker(world, gameProfile); + } + +} diff --git a/src/main/java/forestry/apiculture/commands/CommandBeekeepingMode.java b/src/main/java/forestry/core/commands/CommandMode.java similarity index 68% rename from src/main/java/forestry/apiculture/commands/CommandBeekeepingMode.java rename to src/main/java/forestry/core/commands/CommandMode.java index 0502deb12..ee3c2cc16 100644 --- a/src/main/java/forestry/apiculture/commands/CommandBeekeepingMode.java +++ b/src/main/java/forestry/core/commands/CommandMode.java @@ -4,19 +4,17 @@ * 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; +package forestry.core.commands; -import forestry.core.commands.SubCommand; +public class CommandMode extends SubCommand { -public class CommandBeekeepingMode extends SubCommand { - - public CommandBeekeepingMode() { + public CommandMode(ICommandModeHelper modeHelper) { super("mode"); - addChildCommand(new CommandBeekeepingModeInfo()); - addChildCommand(new CommandBeekeepingModeSet()); + addChildCommand(new CommandModeInfo(modeHelper)); + addChildCommand(new CommandModeSet(modeHelper)); } } diff --git a/src/main/java/forestry/apiculture/commands/CommandBeekeepingModeInfo.java b/src/main/java/forestry/core/commands/CommandModeInfo.java similarity index 53% rename from src/main/java/forestry/apiculture/commands/CommandBeekeepingModeInfo.java rename to src/main/java/forestry/core/commands/CommandModeInfo.java index dce83b61b..419e5cd09 100644 --- a/src/main/java/forestry/apiculture/commands/CommandBeekeepingModeInfo.java +++ b/src/main/java/forestry/core/commands/CommandModeInfo.java @@ -8,45 +8,28 @@ * Various Contributors including, but not limited to: * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges ******************************************************************************/ -package forestry.apiculture.commands; +package forestry.core.commands; + +import java.util.List; -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 { +import org.apache.commons.lang3.StringUtils; +public class CommandModeInfo extends SubCommand { private final String[] modeStringArr; private final String helpString; + private final ICommandModeHelper modeHelper; - public CommandBeekeepingModeInfo() { + public CommandModeInfo(ICommandModeHelper modeHelper) { 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(); + this.modeHelper = modeHelper; + modeStringArr = modeHelper.getModeNames(); + helpString = StringUtils.join(modeStringArr, ", "); } @Override @@ -56,42 +39,33 @@ public class CommandBeekeepingModeInfo extends SubCommand { return; } - String modeName = args[0]; + String modeName = modeHelper.getModeNameMatching(args[0]); - IBeekeepingMode found = findModeWithName(modeName); - if (found == null) { - CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.mode.info.error", modeName); + if (modeName == null) { + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.mode.info.error", args[0]); printHelp(sender); return; } ChatStyle green = new ChatStyle(); green.setColor(EnumChatFormatting.GREEN); - CommandHelpers.sendLocalizedChatMessage(sender, green, found.getName()); + CommandHelpers.sendLocalizedChatMessage(sender, green, modeName); - for (String desc : found.getDescription()) + for (String desc : modeHelper.getDescription(modeName)) 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 modeName = modeHelper.getModeName(world); 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); + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.mode.info.current", modeName, worldName); + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.mode.info.available", helpString); } @Override diff --git a/src/main/java/forestry/apiculture/commands/CommandBeekeepingModeSet.java b/src/main/java/forestry/core/commands/CommandModeSet.java similarity index 60% rename from src/main/java/forestry/apiculture/commands/CommandBeekeepingModeSet.java rename to src/main/java/forestry/core/commands/CommandModeSet.java index 13aa6bb34..1fd50ba9d 100644 --- a/src/main/java/forestry/apiculture/commands/CommandBeekeepingModeSet.java +++ b/src/main/java/forestry/core/commands/CommandModeSet.java @@ -8,30 +8,24 @@ * Various Contributors including, but not limited to: * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges ******************************************************************************/ -package forestry.apiculture.commands; +package forestry.core.commands; + +import java.util.List; -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 { +public final class CommandModeSet extends SubCommand { private final String[] modeStringArr; + private final ICommandModeHelper modeSetter; - public CommandBeekeepingModeSet() { + public CommandModeSet(ICommandModeHelper modeSetter) { 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]); + this.modeSetter = modeSetter; + modeStringArr = modeSetter.getModeNames(); + } @Override @@ -49,15 +43,15 @@ public class CommandBeekeepingModeSet extends SubCommand { 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); + String modeName = modeSetter.getModeNameMatching(desired); + if (modeName == null) { + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.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()); + modeSetter.setMode(world, modeName); + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.mode.set.success", modeName); } @Override diff --git a/src/main/java/forestry/apiculture/commands/CommandBeekeepingSave.java b/src/main/java/forestry/core/commands/CommandSaveStats.java similarity index 63% rename from src/main/java/forestry/apiculture/commands/CommandBeekeepingSave.java rename to src/main/java/forestry/core/commands/CommandSaveStats.java index 60423d3c8..18f3ea606 100644 --- a/src/main/java/forestry/apiculture/commands/CommandBeekeepingSave.java +++ b/src/main/java/forestry/core/commands/CommandSaveStats.java @@ -8,23 +8,7 @@ * 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; +package forestry.core.commands; import java.io.BufferedWriter; import java.io.File; @@ -38,17 +22,32 @@ import java.util.Date; import java.util.List; import java.util.Locale; -public class CommandBeekeepingSave extends SubCommand { +import net.minecraft.command.ICommandSender; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.util.StatCollector; +import net.minecraft.world.World; + +import forestry.api.genetics.AlleleManager; +import forestry.api.genetics.IAlleleSpecies; +import forestry.api.genetics.IBreedingTracker; +import forestry.core.config.Defaults; +import forestry.core.config.Version; +import forestry.core.proxy.Proxies; +import forestry.core.utils.StringUtil; + +public final class CommandSaveStats extends SubCommand { private final String discoveredSymbol; private final String blacklistedSymbol; private final String notCountedSymbol; + private final IStatsSaveHelper saveHelper; - public CommandBeekeepingSave() { + public CommandSaveStats(IStatsSaveHelper saveHelper) { 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"); + discoveredSymbol = StatCollector.translateToLocal("for.chat.command.forestry.stats.save.key.discovered.symbol"); + blacklistedSymbol = StatCollector.translateToLocal("for.chat.command.forestry.stats.save.key.blacklisted.symbol"); + notCountedSymbol = StatCollector.translateToLocal("for.chat.command.forestry.stats.save.key.notCounted.symbol"); + this.saveHelper = saveHelper; } @Override @@ -58,7 +57,7 @@ public class CommandBeekeepingSave extends SubCommand { @Override public void processSubCommand(ICommandSender sender, String[] args) { - if (args.length != 1) { + if (args.length > 1) { printHelp(sender); return; } @@ -66,76 +65,66 @@ public class CommandBeekeepingSave extends SubCommand { String newLine = System.getProperty("line.separator"); World world = CommandHelpers.getWorld(sender, this); - String player = args[0]; + EntityPlayerMP player; + if (args.length > 0) + player = CommandHelpers.getPlayer(sender, args[0]); + else + player = CommandHelpers.getPlayer(sender, sender.getCommandSenderName()); + 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(StatCollector.translateToLocalFormatted(saveHelper.getUnlocalizedSaveStatsString(), player.getDisplayName(), date)); statistics.add(""); - String modeName = PluginApiculture.beeInterface.getBeekeepingMode(world).getName(); - statistics.add(StatCollector.translateToLocalFormatted("for.chat.command.forestry.beekeeping.save.mode", modeName)); + statistics.add(StatCollector.translateToLocalFormatted("for.chat.command.forestry.stats.save.mode", saveHelper.getModeName(world))); statistics.add(""); - GameProfile profile = MinecraftServer.getServer().func_152358_ax().func_152655_a(player); - IApiaristTracker tracker = PluginApiculture.beeInterface.getBreedingTracker(world, profile); + IBreedingTracker tracker = saveHelper.getBreedingTracker(world, player.getGameProfile()); if (tracker == null) - statistics.add(StatCollector.translateToLocal("for.chat.command.forestry.beekeeping.save.error4")); + statistics.add(StatCollector.translateToLocal("for.chat.command.forestry.stats.save.error4")); else { - String discoveredLine = StatCollector.translateToLocal("for.chat.command.forestry.beekeeping.save.key.discovered") + ":"; - statistics.add(discoveredLine); - statistics.add(line(discoveredLine.length())); + saveHelper.addExtraInfo(statistics, tracker); - 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); + Collection species = saveHelper.getSpecies(); 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(StringUtil.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(discoveredSymbol + ": " + StatCollector.translateToLocal("for.chat.command.forestry.stats.save.key.discovered")); + statistics.add(blacklistedSymbol + ": " + StatCollector.translateToLocal("for.chat.command.forestry.stats.save.key.blacklisted")); + statistics.add(notCountedSymbol + ": " + StatCollector.translateToLocal("for.chat.command.forestry.stats.save.key.notCounted")); statistics.add(""); String header = generateSpeciesListHeader(); statistics.add(header); - statistics.add(line(header.length())); + statistics.add(StringUtil.line(header.length())); statistics.add(""); - for (IAlleleBeeSpecies allele : species) + for (IAlleleSpecies allele : species) statistics.add(generateSpeciesListEntry(allele, tracker)); } - File file = new File(Proxies.common.getForestryRoot(), "config/" + Defaults.MOD.toLowerCase(Locale.ENGLISH) + "/stats/" + player + ".log"); + File file = new File(Proxies.common.getForestryRoot(), "config/" + Defaults.MOD.toLowerCase(Locale.ENGLISH) + "/stats/" + player.getDisplayName() + "-" + saveHelper.getFileSuffix() + ".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"); + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.stats.save.error1"); return; } } if (!file.exists() && !file.createNewFile()) { - CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.save.error1"); + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.stats.save.error1"); return; } if (!file.canWrite()) { - CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.save.error2"); + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.stats.save.error2"); return; } @@ -151,20 +140,12 @@ public class CommandBeekeepingSave extends SubCommand { } catch (IOException ex) { - CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.beekeeping.save.error3"); + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.stats.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(); + CommandHelpers.sendLocalizedChatMessage(sender, "for.chat.command.forestry.stats.save.saved", player.getDisplayName()); } private String generateSpeciesListHeader() { @@ -173,7 +154,7 @@ public class CommandBeekeepingSave extends SubCommand { return speciesListEntry(discoveredSymbol, blacklistedSymbol, notCountedSymbol, "UID", species, authority); } - private String generateSpeciesListEntry(IAlleleBeeSpecies species, IApiaristTracker tracker) { + private String generateSpeciesListEntry(IAlleleSpecies species, IBreedingTracker tracker) { String discovered = ""; if (tracker.isDiscovered(species)) discovered = discoveredSymbol; diff --git a/src/main/java/forestry/apiculture/commands/CommandBeekeeping.java b/src/main/java/forestry/core/commands/ICommandModeHelper.java similarity index 62% rename from src/main/java/forestry/apiculture/commands/CommandBeekeeping.java rename to src/main/java/forestry/core/commands/ICommandModeHelper.java index ceeb66d33..3e57fa387 100644 --- a/src/main/java/forestry/apiculture/commands/CommandBeekeeping.java +++ b/src/main/java/forestry/core/commands/ICommandModeHelper.java @@ -8,17 +8,19 @@ * Various Contributors including, but not limited to: * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges ******************************************************************************/ -package forestry.apiculture.commands; +package forestry.core.commands; -import forestry.core.commands.SubCommand; +import java.util.ArrayList; -public class CommandBeekeeping extends SubCommand { +import net.minecraft.world.World; - public CommandBeekeeping() { - super("bee"); - addAlias("beekeeping"); - addChildCommand(new CommandBeekeepingMode()); - addChildCommand(new CommandBeekeepingSave()); - addChildCommand(new CommandBeekeepingGiveBee()); - } +public interface ICommandModeHelper { + String[] getModeNames(); + + String getModeNameMatching(String desired); + + String getModeName(World world); + void setMode(World world, String modeName); + + ArrayList getDescription(String modeName); } diff --git a/src/main/java/forestry/core/commands/IStatsSaveHelper.java b/src/main/java/forestry/core/commands/IStatsSaveHelper.java new file mode 100644 index 000000000..3cc6092fc --- /dev/null +++ b/src/main/java/forestry/core/commands/IStatsSaveHelper.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * 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.core.commands; + +import java.util.Collection; + +import net.minecraft.world.World; + +import forestry.api.genetics.IAlleleSpecies; +import forestry.api.genetics.IBreedingTracker; + +import com.mojang.authlib.GameProfile; + +public interface IStatsSaveHelper { + + String getUnlocalizedSaveStatsString(); + + void addExtraInfo(Collection statistics, IBreedingTracker breedingTracker); + + String getModeName(World world); + + Collection getSpecies(); + + String getFileSuffix(); + + IBreedingTracker getBreedingTracker(World world, GameProfile gameProfile); +} diff --git a/src/main/java/forestry/core/utils/CommandMC.java b/src/main/java/forestry/core/utils/CommandMC.java deleted file mode 100755 index 138fb83c0..000000000 --- a/src/main/java/forestry/core/utils/CommandMC.java +++ /dev/null @@ -1,53 +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.core.utils; - -import net.minecraft.command.CommandBase; -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.ChatComponentText; -import net.minecraft.world.World; - -@Deprecated -public abstract class CommandMC extends CommandBase { - - protected World getWorld(ICommandSender sender, String[] arguments) { - - // Handle passed in world argument - if (arguments.length > 2) { - int dim = 0; - try { - dim = Integer.parseInt(arguments[1]); - } catch (Exception ex) { - throw new WrongUsageException("/" + getCommandName() + " set [] "); - } - - return MinecraftServer.getServer().worldServerForDimension(dim); - } - - if (sender instanceof EntityPlayer) { - EntityPlayer player = (EntityPlayer) sender; - return player.worldObj; - } else - return MinecraftServer.getServer().worldServerForDimension(0); - - } - - protected String[] getPlayers() { - return MinecraftServer.getServer().getAllUsernames(); - } - - protected void sendChatMessage(ICommandSender sender, String message) { - sender.addChatMessage(new ChatComponentText(message)); - } -} diff --git a/src/main/java/forestry/core/utils/StringUtil.java b/src/main/java/forestry/core/utils/StringUtil.java index cfbd90c70..75698486a 100755 --- a/src/main/java/forestry/core/utils/StringUtil.java +++ b/src/main/java/forestry/core/utils/StringUtil.java @@ -89,4 +89,12 @@ public class StringUtil { public static String floatAsPercent(float val) { return (int) (val * 100) + " %"; } + + public static String line(int length) { + StringBuilder line = new StringBuilder(); + for (int i = 0; i < length; i++) + line.append("-"); + + return line.toString(); + } } diff --git a/src/main/java/forestry/plugins/PluginApiculture.java b/src/main/java/forestry/plugins/PluginApiculture.java index 3b66f571c..4a6d187a8 100644 --- a/src/main/java/forestry/plugins/PluginApiculture.java +++ b/src/main/java/forestry/plugins/PluginApiculture.java @@ -30,7 +30,6 @@ import net.minecraftforge.client.event.TextureStitchEvent; import net.minecraftforge.common.BiomeDictionary; import net.minecraftforge.common.ChestGenHooks; import net.minecraftforge.common.MinecraftForge; -import net.minecraftforge.fluids.FluidRegistry; import net.minecraftforge.oredict.OreDictionary; import cpw.mods.fml.common.FMLCommonHandler; @@ -73,7 +72,7 @@ import forestry.apiculture.GuiHandlerApiculture; import forestry.apiculture.PacketHandlerApiculture; import forestry.apiculture.SaveEventHandlerApiculture; import forestry.apiculture.VillageHandlerApiculture; -import forestry.apiculture.commands.CommandBeekeeping; +import forestry.apiculture.commands.CommandBee; import forestry.apiculture.gadgets.BlockAlveary; import forestry.apiculture.gadgets.BlockBeehives; import forestry.apiculture.gadgets.BlockCandle; @@ -269,8 +268,15 @@ public class PluginApiculture extends ForestryPlugin { // Register village components with the Structure registry. VillageHandlerApiculture.registerVillageComponents(); + // Modes + PluginApiculture.beeInterface.registerBeekeepingMode(BeekeepingMode.easy); + PluginApiculture.beeInterface.registerBeekeepingMode(BeekeepingMode.normal); + PluginApiculture.beeInterface.registerBeekeepingMode(BeekeepingMode.hard); + PluginApiculture.beeInterface.registerBeekeepingMode(BeekeepingMode.hardcore); + PluginApiculture.beeInterface.registerBeekeepingMode(BeekeepingMode.insane); + // Commands - PluginCore.rootCommand.addChildCommand(new CommandBeekeeping()); + PluginCore.rootCommand.addChildCommand(new CommandBee()); } @Override @@ -306,12 +312,6 @@ public class PluginApiculture extends ForestryPlugin { createMutations(); registerBeehiveDrops(); - PluginApiculture.beeInterface.registerBeekeepingMode(BeekeepingMode.easy); - PluginApiculture.beeInterface.registerBeekeepingMode(BeekeepingMode.normal); - PluginApiculture.beeInterface.registerBeekeepingMode(BeekeepingMode.hard); - PluginApiculture.beeInterface.registerBeekeepingMode(BeekeepingMode.hardcore); - PluginApiculture.beeInterface.registerBeekeepingMode(BeekeepingMode.insane); - // Inducers for swarmer BeeManager.inducers.put(ForestryItem.royalJelly.getItemStack(), 10); diff --git a/src/main/java/forestry/plugins/PluginArboriculture.java b/src/main/java/forestry/plugins/PluginArboriculture.java index 304e55b37..1a3ad311b 100644 --- a/src/main/java/forestry/plugins/PluginArboriculture.java +++ b/src/main/java/forestry/plugins/PluginArboriculture.java @@ -16,7 +16,6 @@ import java.util.List; import net.minecraft.block.Block; 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.ItemStack; @@ -46,7 +45,6 @@ import forestry.api.genetics.IClassification.EnumClassLevel; import forestry.api.genetics.IFruitFamily; import forestry.api.recipes.RecipeManagers; import forestry.api.storage.BackpackManager; -import forestry.arboriculture.CommandTreekeepingMode; import forestry.arboriculture.EventHandlerArboriculture; import forestry.arboriculture.FruitProviderNone; import forestry.arboriculture.FruitProviderPod; @@ -57,7 +55,7 @@ import forestry.arboriculture.GuiHandlerArboriculture; import forestry.arboriculture.PacketHandlerArboriculture; import forestry.arboriculture.VillageHandlerArboriculture; import forestry.arboriculture.WoodType; -import forestry.arboriculture.commands.CommandTrees; +import forestry.arboriculture.commands.CommandTree; import forestry.arboriculture.gadgets.BlockArbFence; import forestry.arboriculture.gadgets.BlockArbFence.FenceCat; import forestry.arboriculture.gadgets.BlockArbStairs; @@ -295,8 +293,15 @@ public class PluginArboriculture extends ForestryPlugin { validFences.add(Blocks.fence_gate); validFences.add(Blocks.nether_brick_fence); + // Modes + PluginArboriculture.treeInterface.registerTreekeepingMode(TreekeepingMode.easy); + PluginArboriculture.treeInterface.registerTreekeepingMode(TreekeepingMode.normal); + PluginArboriculture.treeInterface.registerTreekeepingMode(TreekeepingMode.hard); + PluginArboriculture.treeInterface.registerTreekeepingMode(TreekeepingMode.hardcore); + PluginArboriculture.treeInterface.registerTreekeepingMode(TreekeepingMode.insane); + // Commands - PluginCore.rootCommand.addChildCommand(new CommandTrees()); + PluginCore.rootCommand.addChildCommand(new CommandTree()); } @Override @@ -313,12 +318,6 @@ public class PluginArboriculture extends ForestryPlugin { registerTemplates(); registerErsatzGenomes(); - PluginArboriculture.treeInterface.registerTreekeepingMode(TreekeepingMode.easy); - PluginArboriculture.treeInterface.registerTreekeepingMode(TreekeepingMode.normal); - PluginArboriculture.treeInterface.registerTreekeepingMode(TreekeepingMode.hard); - PluginArboriculture.treeInterface.registerTreekeepingMode(TreekeepingMode.hardcore); - PluginArboriculture.treeInterface.registerTreekeepingMode(TreekeepingMode.insane); - MinecraftForge.EVENT_BUS.register(new EventHandlerArboriculture()); VillagerRegistry.instance().registerVillagerId(Defaults.ID_VILLAGER_LUMBERJACK); @@ -1102,11 +1101,6 @@ public class PluginArboriculture extends ForestryPlugin { return new GuiHandlerArboriculture(); } - @Override - public ICommand[] getConsoleCommands() { - return new ICommand[]{new CommandTreekeepingMode()}; - } - @Override public IFuelHandler getFuelHandler() { return new IFuelHandler() {