summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/commands/Commandrecipe.java
blob: d7c42564ecce80de2e6c6a9da4c94ee2682adaa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package net.ess3.commands;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import static net.ess3.I18n._;
import net.ess3.api.IUser;
import net.ess3.utils.Util;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.inventory.*;


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 itemType = ess.getItemDb().get(args[0]);
		int recipeNo = 0;

		if (args.length > 1)
		{
			if (Util.isInt(args[1]))
			{
				recipeNo = Integer.parseInt(args[1]) - 1;
			}
			else
			{
				throw new Exception(_("Invalid Number."));
			}
		}

		final List<Recipe> recipesOfType = ess.getServer().getRecipesFor(itemType);
        if (recipesOfType.size() < 1)
		{
			throw new Exception(_("No recipes exist for {0}.", getMaterialName(itemType)));
		}

		if (recipeNo < 0 || recipeNo >= recipesOfType.size())
		{
			throw new Exception(_("There is no recipe by that number."));
		}

		final Recipe selectedRecipe = recipesOfType.get(recipeNo);
		sender.sendMessage(_("Recipe for {0} ({1} of {2})", getMaterialName(itemType), recipeNo + 1, recipesOfType.size()));

		if (selectedRecipe instanceof FurnaceRecipe)
		{
			furnaceRecipe(sender, (FurnaceRecipe)selectedRecipe);
		}
		else if (selectedRecipe instanceof ShapedRecipe)
		{
			shapedRecipe(sender, (ShapedRecipe)selectedRecipe);
		}
		else if (selectedRecipe instanceof ShapelessRecipe)
		{
			shapelessRecipe(sender, (ShapelessRecipe)selectedRecipe);
		}

		if (recipesOfType.size() > 1 && args.length == 1)
		{
			sender.sendMessage(_("Type /{0} {1} <number> to see other recipes for {2}.", commandLabel, args[0], getMaterialName(itemType)));
		}
	}

	public void furnaceRecipe(final CommandSender sender, final FurnaceRecipe recipe)
	{
		sender.sendMessage(_("Smelt {0}", getMaterialName(recipe.getInput())));
	}

	public void shapedRecipe(final CommandSender sender, final ShapedRecipe recipe)
	{
		final Map<Character, ItemStack> recipeMap = recipe.getIngredientMap();
		if (sender instanceof IUser)
		{
			final IUser user = (IUser)sender;
			user.setRecipeSee(true);
			final InventoryView view = user.getPlayer().openWorkbench(null, true);
			for (int j = 0; j < recipe.getShape().length; j++)
			{
				for (int k = 0; k < recipe.getShape()[j].length(); k++)
				{
					final ItemStack item = recipe.getIngredientMap().get(recipe.getShape()[j].toCharArray()[k]);
					if(item == null)
					{
						continue;
					}
					item.setAmount(0);
					view.getTopInventory().setItem(j * 3 + k + 1, item);
				}
			}
		}
		else
		{
			final HashMap<Material, String> colorMap = new HashMap<Material, String>(); //TODO: Might be better as an Enum
			int i = 1;
			for (Character c : "abcdefghi".toCharArray()) // TODO: Faster to use new char[] { 'a','b','c','d','e','f','g','h','i' } ?
			{
				final ItemStack item = recipeMap.get(c);
				if (!colorMap.containsKey(item == null ? null : item.getType()))
				{
					colorMap.put(item == null ? null : item.getType(), String.valueOf(i++));
				}
			}
			final Material[][] materials = new Material[3][3];
			for (int j = 0; j < recipe.getShape().length; j++)
			{
				for (int k = 0; k < recipe.getShape()[j].length(); k++)
				{
					final ItemStack item = recipe.getIngredientMap().get(recipe.getShape()[j].toCharArray()[k]);
					materials[j][k] = item == null ? null : item.getType();
				}
			}
			sender.sendMessage(_("\u00a7{0}X | \u00a7{1}X | \u00a7{2}X", colorMap.get(materials[0][0]), colorMap.get(materials[0][1]), colorMap.get(materials[0][2])));
			sender.sendMessage(_("\u00a7{0}X | \u00a7{1}X | \u00a7{2}X", colorMap.get(materials[1][0]), colorMap.get(materials[1][1]), colorMap.get(materials[1][2])));
			sender.sendMessage(_("\u00a7{0}X | \u00a7{1}X | \u00a7{2}X", colorMap.get(materials[2][0]), colorMap.get(materials[2][1]), colorMap.get(materials[2][2])));

			final StringBuilder s = new StringBuilder();
			for (Material items : colorMap.keySet().toArray(new Material[colorMap.size()]))
			{
				s.append(_("\u00a7{0}X is {1}", colorMap.get(items), getMaterialName(items)));
			}
			sender.sendMessage(_("Where: {0}", s.toString()));
		}
	}

	public void shapelessRecipe(final CommandSender sender, final ShapelessRecipe recipe)
	{
		final List<ItemStack> ingredients = recipe.getIngredientList();
		if (sender instanceof IUser)
		{
			final IUser user = getUser(sender);
			user.setRecipeSee(true);
			final InventoryView view = user.getPlayer().openWorkbench(null, true);
			for (int i = 0; i < ingredients.size(); i++)
			{
				view.setItem(i + 1, ingredients.get(i));
			}
		}
		else
		{
			final 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(_("Combine {0}", s.toString()));
		}
	}

	public String getMaterialName(final ItemStack stack)
	{
		if (stack == null)
		{
			return _("nothing");
		}
		return getMaterialName(stack.getType());
	}

	public String getMaterialName(final Material type)
	{
		if (type == null)
		{
			return _("nothing");
		}
		return type.toString().replace("_", " ").toLowerCase(Locale.ENGLISH);
	}
}