summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKHobbits <rob@khobbits.co.uk>2014-05-16 20:30:06 +0100
committerKHobbits <rob@khobbits.co.uk>2014-05-16 20:30:06 +0100
commite4a0e8c11c4d4c2dcccee16616534560d3f991ca (patch)
treea8a8d15d48d3ba5c4f526d371e78dcc84e48bbb3
parente2f5d0a220d141cff61ec3022d178f82398eb63e (diff)
downloadEssentials-e4a0e8c11c4d4c2dcccee16616534560d3f991ca.tar
Essentials-e4a0e8c11c4d4c2dcccee16616534560d3f991ca.tar.gz
Essentials-e4a0e8c11c4d4c2dcccee16616534560d3f991ca.tar.lz
Essentials-e4a0e8c11c4d4c2dcccee16616534560d3f991ca.tar.xz
Essentials-e4a0e8c11c4d4c2dcccee16616534560d3f991ca.zip
First draft of /condense
-rw-r--r--Essentials/src/com/earth2me/essentials/commands/Commandcondense.java211
-rw-r--r--Essentials/src/plugin.yml4
2 files changed, 215 insertions, 0 deletions
diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandcondense.java b/Essentials/src/com/earth2me/essentials/commands/Commandcondense.java
new file mode 100644
index 000000000..46c9e5e33
--- /dev/null
+++ b/Essentials/src/com/earth2me/essentials/commands/Commandcondense.java
@@ -0,0 +1,211 @@
+package com.earth2me.essentials.commands;
+
+import com.earth2me.essentials.ChargeException;
+import com.earth2me.essentials.Trade;
+import com.earth2me.essentials.Trade.OverflowType;
+import com.earth2me.essentials.User;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import net.ess3.api.MaxMoneyException;
+import org.bukkit.Bukkit;
+import org.bukkit.Material;
+import org.bukkit.Server;
+import org.bukkit.inventory.ItemStack;
+import org.bukkit.inventory.Recipe;
+import org.bukkit.inventory.ShapedRecipe;
+import org.bukkit.inventory.ShapelessRecipe;
+
+
+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;
+
+ if (args.length > 0)
+ {
+ is = ess.getItemDb().getMatching(user, args);
+ validateReverse = false;
+ }
+ else
+ {
+ for (ItemStack stack : user.getBase().getInventory().getContents())
+ {
+ if (stack == null || stack.getType() == Material.AIR)
+ {
+ continue;
+ }
+ is.add(stack);
+ }
+ validateReverse = true;
+ }
+
+ for (final ItemStack itemStack : is)
+ {
+ condenseStack(user, itemStack, validateReverse);
+ }
+ user.getBase().updateInventory();
+ }
+
+ private void 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;
+ }
+ }
+
+ 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);
+ }
+ }
+ }
+
+ 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;
+ }
+
+
+ class SimpleRecipe implements Recipe
+ {
+ private ItemStack result;
+ private ItemStack input;
+
+ SimpleRecipe(ItemStack result, ItemStack input)
+ {
+ this.result = result;
+ this.input = input;
+ }
+
+ @Override
+ public ItemStack getResult()
+ {
+ return result.clone();
+ }
+
+ public ItemStack getInput()
+ {
+ return input.clone();
+ }
+ }
+} \ No newline at end of file
diff --git a/Essentials/src/plugin.yml b/Essentials/src/plugin.yml
index 76865d93c..881ce0063 100644
--- a/Essentials/src/plugin.yml
+++ b/Essentials/src/plugin.yml
@@ -63,6 +63,10 @@ commands:
description: Clear all items in your inventory.
usage: /<command> [player|*] [item[:<data>]|*|**] [amount]
aliases: [ci,eci,clean,eclean,clear,eclear,clearinvent,eclearinvent,eclearinventory]
+ condense:
+ description: Condenses items into a more compact form.
+ usage: /<command>
+ aliases: [econdense,compact,ecompact,blocks,eblocks,toblocks,etoblocks]
compass:
description: Describes your current bearing.
usage: /<command>