Fixed items incorrectly appearning in messages

This commit is contained in:
Acrobot 2012-08-10 22:25:08 +02:00
parent 30fa329645
commit 2cd4e7b914
3 changed files with 39 additions and 15 deletions

View File

@ -45,6 +45,8 @@ public class ItemManager implements Listener {
} }
private static void addItems(Inventory inventory, ItemStack[] items) { private static void addItems(Inventory inventory, ItemStack[] items) {
inventory.addItem(items); for (ItemStack item : items) {
inventory.addItem(item.clone());
}
} }
} }

View File

@ -1,5 +1,6 @@
package com.Acrobot.ChestShop.Listeners.PostTransaction; package com.Acrobot.ChestShop.Listeners.PostTransaction;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.ChestShop.Config.Config; import com.Acrobot.ChestShop.Config.Config;
import com.Acrobot.ChestShop.Config.Language; import com.Acrobot.ChestShop.Config.Language;
import com.Acrobot.ChestShop.Economy.Economy; import com.Acrobot.ChestShop.Economy.Economy;
@ -11,8 +12,8 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.HashMap; import java.util.LinkedList;
import java.util.Map; import java.util.List;
import static com.Acrobot.ChestShop.Config.Language.*; import static com.Acrobot.ChestShop.Config.Language.*;
import static com.Acrobot.ChestShop.Config.Property.SHOW_TRANSACTION_INFORMATION_CLIENT; import static com.Acrobot.ChestShop.Config.Property.SHOW_TRANSACTION_INFORMATION_CLIENT;
@ -78,24 +79,28 @@ public class TransactionMessageSender implements Listener {
} }
private static String parseItemInformation(ItemStack[] items) { private static String parseItemInformation(ItemStack[] items) {
Map<String, Integer> itemMap = new HashMap<String, Integer>(); List<ItemStack> stock = new LinkedList<ItemStack>();
for (ItemStack item : items) { for (ItemStack item : items) {
String matName = item.getType().name(); boolean added = false;
if (itemMap.containsKey(matName)) { for (ItemStack iStack : stock) {
int curAmount = itemMap.get(matName); if (MaterialUtil.equals(item, iStack)) {
iStack.setAmount(iStack.getAmount() + item.getAmount());
added = true;
break;
}
}
itemMap.put(matName, curAmount + item.getAmount()); if (!added) {
} else { stock.add(item);
itemMap.put(matName, item.getAmount());
} }
} }
StringBuilder message = new StringBuilder(15); StringBuilder message = new StringBuilder(15);
for (Map.Entry<String, Integer> item : itemMap.entrySet()) { for (ItemStack item : stock) {
message.append(item.getValue()).append(' ').append(item.getKey()); message.append(item.getType().name()).append(' ').append(item.getAmount());
} }
return message.toString(); return message.toString();

View File

@ -139,6 +139,7 @@ public class PartialTransactionModule implements Listener {
if (MaterialUtil.equals(item, needed)) { if (MaterialUtil.equals(item, needed)) {
needed.setAmount(needed.getAmount() + item.getAmount()); needed.setAmount(needed.getAmount() + item.getAmount());
itemAdded = true; itemAdded = true;
break;
} }
} }
@ -152,8 +153,8 @@ public class PartialTransactionModule implements Listener {
for (ItemStack item : neededItems) { for (ItemStack item : neededItems) {
int amount = InventoryUtil.getAmount(item, inventory); int amount = InventoryUtil.getAmount(item, inventory);
ItemStack clone = item.clone();
ItemStack clone = item.clone();
clone.setAmount(amount > item.getAmount() ? item.getAmount() : amount); clone.setAmount(amount > item.getAmount() ? item.getAmount() : amount);
toReturn.add(clone); toReturn.add(clone);
@ -168,17 +169,33 @@ public class PartialTransactionModule implements Listener {
for (ItemStack stack : stock) { for (ItemStack stack : stock) {
int count = stack.getAmount(); int count = stack.getAmount();
ItemStack toAdd;
if (left > count) { if (left > count) {
stacks.add(stack); toAdd = stack;
left -= count; left -= count;
} else { } else {
ItemStack clone = stack.clone(); ItemStack clone = stack.clone();
clone.setAmount(left); clone.setAmount(left);
stacks.add(clone); toAdd = clone;
left = 0; left = 0;
} }
boolean added = false;
for (ItemStack iStack : stacks) {
if (MaterialUtil.equals(toAdd, iStack)) {
iStack.setAmount(iStack.getAmount() + toAdd.getAmount());
added = true;
break;
}
}
if (!added) {
stacks.add(toAdd);
}
if (left <= 0) { if (left <= 0) {
break; break;
} }