summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/Commandcondense.java
blob: fd0408890566ab9704f6ae5a048e5defbe69702e (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.earth2me.essentials.commands;

import static com.earth2me.essentials.I18n.tl;
import java.util.*;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.Trade.OverflowType;
import com.earth2me.essentials.User;
import net.ess3.api.MaxMoneyException;


public class Commandcondense extends EssentialsCommand
{
	public Commandcondense()
	{
		super("condense");
	}
	private Map<ItemStack, SimpleRecipe> condenseList = new HashMap<ItemStack, SimpleRecipe>();

	@Override
	public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
	{
		List<ItemStack> is = new ArrayList<ItemStack>();

		boolean validateReverse = false;
		if (args.length > 0)
		{
			is = ess.getItemDb().getMatching(user, args);
		}
		else
		{
			for (ItemStack stack : user.getBase().getInventory().getContents())
			{
				if (stack == null || stack.getType() == Material.AIR)
				{
					continue;
				}
				is.add(stack);
			}
			validateReverse = true;
		}

		boolean didConvert = false;
		for (final ItemStack itemStack : is)
		{
			if (condenseStack(user, itemStack, validateReverse))
			{
				didConvert = true;
			}
		}
		user.getBase().updateInventory();

		if (didConvert)
		{
			user.sendMessage(tl("itemsConverted"));
		}
		else
		{
			user.sendMessage(tl("itemsNotConverted"));
			throw new NoChargeException();
		}
	}

	private boolean condenseStack(final User user, final ItemStack stack, final boolean validateReverse) throws ChargeException, MaxMoneyException
	{
		final SimpleRecipe condenseType = getCondenseType(stack);
		if (condenseType != null)
		{
			final ItemStack input = condenseType.getInput();
			final ItemStack result = condenseType.getResult();

			if (validateReverse)
			{
				boolean pass = false;
				for (Recipe revRecipe : ess.getServer().getRecipesFor(input))
				{
					if (getStackOnRecipeMatch(revRecipe, result) != null)
					{
						pass = true;
						break;
					}
				}
				if (!pass)
				{
					return false;
				}
			}

			int amount = 0;

			for (final ItemStack contents : user.getBase().getInventory().getContents())
			{
				if (contents != null && contents.isSimilar(stack))
				{
					amount += contents.getAmount();
				}
			}

			int output = ((amount / input.getAmount()) * result.getAmount());
			amount -= amount % input.getAmount();

			if (amount > 0)
			{
				input.setAmount(amount);
				result.setAmount(output);
				final Trade remove = new Trade(input, ess);
				final Trade add = new Trade(result, ess);
				remove.charge(user);
				add.pay(user, OverflowType.DROP);
				return true;
			}
		}
		return false;
	}

	private SimpleRecipe getCondenseType(final ItemStack stack)
	{
		if (condenseList.containsKey(stack))
		{
			return condenseList.get(stack);
		}

		final Iterator<Recipe> intr = ess.getServer().recipeIterator();
		while (intr.hasNext())
		{
			final Recipe recipe = intr.next();
			final Collection<ItemStack> recipeItems = getStackOnRecipeMatch(recipe, stack);

			if (recipeItems != null && (recipeItems.size() == 4 || recipeItems.size() == 9)
				&& (recipeItems.size() > recipe.getResult().getAmount()))
			{
				final ItemStack input = stack.clone();
				input.setAmount(recipeItems.size());
				final SimpleRecipe newRecipe = new SimpleRecipe(recipe.getResult(), input);
				condenseList.put(stack, newRecipe);
				return newRecipe;
			}
		}

		condenseList.put(stack, null);
		return null;
	}

	private Collection<ItemStack> getStackOnRecipeMatch(final Recipe recipe, final ItemStack stack)
	{
		final Collection<ItemStack> inputList;

		if (recipe instanceof ShapedRecipe)
		{
			ShapedRecipe sRecipe = (ShapedRecipe)recipe;
			inputList = sRecipe.getIngredientMap().values();
		}
		else if (recipe instanceof ShapelessRecipe)
		{
			ShapelessRecipe slRecipe = (ShapelessRecipe)recipe;
			inputList = slRecipe.getIngredientList();
		}
		else
		{
			return null;
		}

		boolean match = true;
		Iterator<ItemStack> iter = inputList.iterator();
		while (iter.hasNext())
		{
			ItemStack inputSlot = iter.next();
			if (inputSlot == null)
			{
				iter.remove();
				continue;
			}

			if (inputSlot.getDurability() == Short.MAX_VALUE)
			{
				inputSlot.setDurability((short)0);
			}
			if (!inputSlot.isSimilar(stack))
			{
				match = false;
			}
		}

		if (match)
		{
			return inputList;
		}
		return null;
	}


	private class SimpleRecipe implements Recipe
	{
		private ItemStack result;
		private ItemStack input;

		private SimpleRecipe(ItemStack result, ItemStack input)
		{
			this.result = result;
			this.input = input;
		}

		@Override
		public ItemStack getResult()
		{
			return result.clone();
		}

		public ItemStack getInput()
		{
			return input.clone();
		}
	}
}