From 9b69334eea2884c6f42524a0631ba23d7c86aeff Mon Sep 17 00:00:00 2001 From: Chris Ward Date: Wed, 19 Dec 2012 22:06:03 +1100 Subject: Add /recipe command --- Essentials/src/messages.properties | 11 ++ Essentials/src/net/ess3/api/IUser.java | 4 + .../src/net/ess3/commands/Commandrecipe.java | 159 +++++++++++++++++++++ .../ess3/listener/EssentialsPlayerListener.java | 17 +++ Essentials/src/net/ess3/user/User.java | 3 + Essentials/src/plugin.yml | 4 + 6 files changed, 198 insertions(+) mode change 100644 => 100755 Essentials/src/messages.properties mode change 100644 => 100755 Essentials/src/net/ess3/api/IUser.java create mode 100755 Essentials/src/net/ess3/commands/Commandrecipe.java mode change 100644 => 100755 Essentials/src/net/ess3/listener/EssentialsPlayerListener.java mode change 100644 => 100755 Essentials/src/net/ess3/user/User.java mode change 100644 => 100755 Essentials/src/plugin.yml diff --git a/Essentials/src/messages.properties b/Essentials/src/messages.properties old mode 100644 new mode 100755 index 1eb419711..61c30e1d0 --- a/Essentials/src/messages.properties +++ b/Essentials/src/messages.properties @@ -493,3 +493,14 @@ metrics4=This will start 5 minutes after the first admin/op joins. userNotFound=User not found! flyFailed=Can't change fly mode for {0} deniedWorldAccess=You do not have permission to access world {0} +recipeNone=No recipes exist for {0} +invalidNumber=Invalid Number +recipeBadIndex=There is no recipe by that number +recipeNothing=nothing +recipeShapeless=\u00a76Combine \u00a7c{0} +recipe=\u00a76Recipe for \u00a7c{0}\u00a76 ({1} of {2}) +recipeFurnace=\u00a76Smelt \u00a7c{0} +recipeGrid=\u00a7{0}X \u00a76| \u00a7{1}X \u00a76| \u00a7{2}X +recipeGridItem=\ \u00a7{0}X \u00a76is \u00a7c{1} +recipeMore=\u00a76Type /{0} \u00a7c{1}\u00a76 to see other recipes for \u00a7c{2} +recipeWhere=\u00a76Where: {0} \ No newline at end of file diff --git a/Essentials/src/net/ess3/api/IUser.java b/Essentials/src/net/ess3/api/IUser.java old mode 100644 new mode 100755 index bcfb2610f..223d30e3f --- a/Essentials/src/net/ess3/api/IUser.java +++ b/Essentials/src/net/ess3/api/IUser.java @@ -123,6 +123,10 @@ public interface IUser extends OfflinePlayer, CommandSender, IStorageObjectHolde boolean checkSignThrottle(int throttle); + public boolean isRecipeSee(); + + public void setRecipeSee(boolean recipeSee); + /** * Since the Player object should not be stored for a long time, this method should be called again with a null * value. diff --git a/Essentials/src/net/ess3/commands/Commandrecipe.java b/Essentials/src/net/ess3/commands/Commandrecipe.java new file mode 100755 index 000000000..d367da5e7 --- /dev/null +++ b/Essentials/src/net/ess3/commands/Commandrecipe.java @@ -0,0 +1,159 @@ +package net.ess3.commands; + +import static net.ess3.I18n._; + +import net.ess3.api.IUser; +import net.ess3.user.User; +import net.ess3.utils.Util; +import org.bukkit.Material; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.inventory.*; + +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +public class Commandrecipe extends EssentialsCommand +{ + + @Override + public void run(final CommandSender sender, final String commandLabel, final String[] args) throws Exception + { + if (args.length < 1) + { + throw new NotEnoughArgumentsException(); + } + final ItemStack item = ess.getItemDb().get(args[0]); + final List recipes = ess.getServer().getRecipesFor(item); + if (recipes.size() < 1) + { + throw new Exception(_("recipeNone", getMaterialName(item))); + } + int recipeNo = 0; + if (args.length > 1) + { + if (Util.isInt(args[1])) + { + recipeNo = Integer.parseInt(args[1]) - 1; + } + else + { + throw new Exception(_("invalidNumber")); + } + } + if (recipeNo < 0 || recipeNo >= recipes.size()) + { + throw new Exception(_("recipeBadIndex")); + } + final Recipe recipe = recipes.get(recipeNo); + sender.sendMessage(_("recipe", getMaterialName(item), recipeNo + 1, recipes.size())); + if (recipe instanceof FurnaceRecipe) + { + furnaceRecipe(sender, (FurnaceRecipe)recipe); + } + else if (recipe instanceof ShapedRecipe) + { + shapedRecipe(sender, (ShapedRecipe)recipe); + } + else if (recipe instanceof ShapelessRecipe) + { + shapelessRecipe(sender, (ShapelessRecipe)recipe); + } + if (recipes.size() > 1 && args.length == 1) + { + sender.sendMessage(_("recipeMore", commandLabel, args[0], getMaterialName(item))); + } + } + + public void furnaceRecipe(CommandSender sender, FurnaceRecipe recipe) + { + sender.sendMessage(_("recipeFurnace", getMaterialName(recipe.getInput()))); + } + + public void shapedRecipe(CommandSender sender, ShapedRecipe recipe) + { + Map recipeMap = recipe.getIngredientMap(); + if (sender instanceof IUser) + { + IUser user = getUser(sender); + user.setRecipeSee(true); + InventoryView view = user.getPlayer().openWorkbench(null, true); + for (Map.Entry e : recipe.getIngredientMap().entrySet()) + { + view.setItem(" abcdefghi".indexOf(e.getKey()), e.getValue()); + } + } + else + { + HashMap colorMap = new HashMap(); + int i = 1; + for (Character c : "abcdefghi".toCharArray()) + { + if (!colorMap.containsKey(recipeMap.get(c))) + { + colorMap.put(recipeMap.get(c), String.valueOf(i++)); + } + } + sender.sendMessage(_("recipeGrid", colorMap.get(recipeMap.get('a')), colorMap.get(recipeMap.get('b')), colorMap.get(recipeMap.get('c')))); + sender.sendMessage(_("recipeGrid", colorMap.get(recipeMap.get('d')), colorMap.get(recipeMap.get('e')), colorMap.get(recipeMap.get('f')))); + sender.sendMessage(_("recipeGrid", colorMap.get(recipeMap.get('g')), colorMap.get(recipeMap.get('h')), colorMap.get(recipeMap.get('i')))); + + StringBuilder s = new StringBuilder(); + for (ItemStack items : colorMap.keySet().toArray(new ItemStack[colorMap.size()])) + { + s.append(_("recipeGridItem", colorMap.get(items), getMaterialName(items))); + } + sender.sendMessage(_("recipeWhere", s.toString())); + } + } + + public void shapelessRecipe(CommandSender sender, ShapelessRecipe recipe) + { + List ingredients = recipe.getIngredientList(); + if (sender instanceof IUser) + { + IUser user = getUser(sender); + user.setRecipeSee(true); + InventoryView view = user.getPlayer().openWorkbench(null, true); + for (int i = 0; i < ingredients.size(); i++) + { + view.setItem(i + 1, ingredients.get(i)); + } + } + else + { + ess.getLogger().info(sender.getClass().getName()); + StringBuilder s = new StringBuilder(); + for (int i = 0; i < ingredients.size(); i++) + { + s.append(getMaterialName(ingredients.get(i))); + if (i != ingredients.size() - 1) + { + s.append(","); + } + s.append(" "); + } + sender.sendMessage(_("recipeShapeless", s.toString())); + } + } + + public String getMaterialName(ItemStack stack) + { + if (stack == null) + { + return _("recipeNothing"); + } + return getMaterialName(stack.getType()); + } + + public String getMaterialName(Material type) + { + if (type == null) + { + return _("recipeNothing"); + } + return type.toString().replace("_", " ").toLowerCase(Locale.ENGLISH); + } +} \ No newline at end of file diff --git a/Essentials/src/net/ess3/listener/EssentialsPlayerListener.java b/Essentials/src/net/ess3/listener/EssentialsPlayerListener.java old mode 100644 new mode 100755 index f5e5d8371..277820fba --- a/Essentials/src/net/ess3/listener/EssentialsPlayerListener.java +++ b/Essentials/src/net/ess3/listener/EssentialsPlayerListener.java @@ -495,6 +495,14 @@ public class EssentialsPlayerListener implements Listener } } } + if (event.getView().getTopInventory().getType() == InventoryType.WORKBENCH) + { + final IUser user = ess.getUserMap().getUser((Player)event.getWhoClicked()); + if(user.isRecipeSee()) + { + event.setCancelled(true); + } + } } @EventHandler(priority = EventPriority.MONITOR) @@ -505,6 +513,15 @@ public class EssentialsPlayerListener implements Listener final IUser user = ess.getUserMap().getUser((Player)event.getPlayer()); user.setInvSee(false); } + else if (event.getView().getTopInventory().getType() == InventoryType.WORKBENCH) + { + final IUser user = ess.getUserMap().getUser((Player)event.getPlayer()); + if(user.isRecipeSee()) + { + user.setRecipeSee(false); + event.getView().getTopInventory().clear(); + } + } } @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) diff --git a/Essentials/src/net/ess3/user/User.java b/Essentials/src/net/ess3/user/User.java old mode 100644 new mode 100755 index e7d87c555..9aa10d366 --- a/Essentials/src/net/ess3/user/User.java +++ b/Essentials/src/net/ess3/user/User.java @@ -54,6 +54,9 @@ public class User extends UserBase implements IUser private transient Location afkPosition; private AtomicBoolean gotMailInfo = new AtomicBoolean(false); private WeakReference playerCache; + @Getter + @Setter + private boolean recipeSee = false; public User(final OfflinePlayer base, final IEssentials ess) throws InvalidNameException { diff --git a/Essentials/src/plugin.yml b/Essentials/src/plugin.yml old mode 100644 new mode 100755 index b78187493..3c1fb8f93 --- a/Essentials/src/plugin.yml +++ b/Essentials/src/plugin.yml @@ -278,6 +278,10 @@ commands: description: Displays the username of a user based on nick. usage: / aliases: [erealname] + recipe: + description: Displays how to craft items. + usage: / [number] + aliases: [erecipe] remove: description: Removes entities in your world usage: / [radius] -- cgit v1.2.3