Fix Admin Shops by adding more implementation to AdminInventory

This is required as we now move items between inventories instead of individually removing and adding. This basically adds the stock represented by the admin shop sign to the AdminInventory so all transactions should always be possible.
This commit is contained in:
Phoenix616 2019-04-05 15:17:15 +01:00
parent 1952e5cb46
commit 5a99180aab
2 changed files with 63 additions and 19 deletions

View File

@ -1,11 +1,12 @@
package com.Acrobot.ChestShop.Containers; package com.Acrobot.ChestShop.Containers;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.HumanEntity; import org.bukkit.entity.HumanEntity;
@ -18,6 +19,14 @@ import org.bukkit.inventory.ItemStack;
* @author Acrobot * @author Acrobot
*/ */
public class AdminInventory implements Inventory { public class AdminInventory implements Inventory {
private ItemStack[] content;
private int maxStackSize = 64;
public AdminInventory(ItemStack[] content) {
this.content = content;
}
@Override @Override
public int getSize() { public int getSize() {
return Integer.MAX_VALUE; return Integer.MAX_VALUE;
@ -25,11 +34,12 @@ public class AdminInventory implements Inventory {
@Override @Override
public int getMaxStackSize() { public int getMaxStackSize() {
return Integer.MAX_VALUE; return maxStackSize;
} }
@Override @Override
public void setMaxStackSize(int i) { public void setMaxStackSize(int i) {
maxStackSize = i;
} }
@Override @Override
@ -39,11 +49,21 @@ public class AdminInventory implements Inventory {
@Override @Override
public ItemStack getItem(int i) { public ItemStack getItem(int i) {
if (content.length < i) {
return content[i];
}
return null; return null;
} }
@Override @Override
public void setItem(int i, ItemStack itemStack) { public void setItem(int i, ItemStack itemStack) {
if (i > getSize()) {
throw new IllegalArgumentException("Slot is outside inventory. Max size is " + getSize());
}
if (i >= content.length) {
content = Arrays.copyOfRange(content, 0, i);
}
content[i] = itemStack;
} }
@Override @Override
@ -58,49 +78,59 @@ public class AdminInventory implements Inventory {
@Override @Override
public ItemStack[] getContents() { public ItemStack[] getContents() {
return new ItemStack[]{ return content;
new ItemStack(Material.CHEST, 1),
new ItemStack(Material.AIR, Integer.MAX_VALUE)
};
} }
@Override @Override
public void setContents(ItemStack[] itemStacks) { public void setContents(ItemStack[] itemStacks) {
content = itemStacks;
} }
@Override @Override
public ItemStack[] getStorageContents() { public ItemStack[] getStorageContents() {
return new ItemStack[0]; return content;
} }
@Override @Override
public void setStorageContents(ItemStack[] itemStacks) throws IllegalArgumentException { public void setStorageContents(ItemStack[] itemStacks) throws IllegalArgumentException {
content = itemStacks;
} }
@Override @Override
public boolean contains(Material material) { public boolean contains(Material material) {
return true; return first(material) > -1;
} }
@Override @Override
public boolean contains(ItemStack itemStack) { public boolean contains(ItemStack itemStack) {
return true; return first(itemStack) > -1;
} }
@Override @Override
public boolean contains(Material material, int i) { public boolean contains(Material material, int i) {
return true; int amount = 0;
for (ItemStack item : content) {
if (item != null && item.getType() == material) {
amount += item.getAmount();
}
}
return amount >= i;
} }
@Override @Override
public boolean contains(ItemStack itemStack, int i) { public boolean contains(ItemStack itemStack, int i) {
return true; int amount = 0;
for (ItemStack item : content) {
if (MaterialUtil.equals(item, itemStack)) {
amount += itemStack.getAmount();
}
}
return amount >= i;
} }
@Override @Override
public boolean containsAtLeast(ItemStack itemStack, int i) { public boolean containsAtLeast(ItemStack itemStack, int i) {
return true; return contains(itemStack, i);
} }
@Override @Override
@ -133,12 +163,22 @@ public class AdminInventory implements Inventory {
@Override @Override
public int first(Material material) { public int first(Material material) {
return 0; for (int i = 0; i < content.length; i++) {
if (content[i] != null && content[i].getType() == material) {
return i;
}
}
return -1;
} }
@Override @Override
public int first(ItemStack itemStack) { public int first(ItemStack itemStack) {
return 0; for (int i = 0; i < content.length; i++) {
if (MaterialUtil.equals(content[i], itemStack)) {
return i;
}
}
return -1;
} }
@Override @Override
@ -184,12 +224,12 @@ public class AdminInventory implements Inventory {
@Override @Override
public ListIterator<ItemStack> iterator() { public ListIterator<ItemStack> iterator() {
return Collections.emptyListIterator(); return Arrays.asList(content).listIterator();
} }
@Override @Override
public ListIterator<ItemStack> iterator(int i) { public ListIterator<ItemStack> iterator(int i) {
return null; return Arrays.asList(content).listIterator(i);
} }
@Override @Override

View File

@ -174,7 +174,7 @@ public class PlayerInteract implements Listener {
double price = (action == buy ? PriceUtil.getBuyPrice(prices) : PriceUtil.getSellPrice(prices)); double price = (action == buy ? PriceUtil.getBuyPrice(prices) : PriceUtil.getSellPrice(prices));
Container shopBlock = uBlock.findConnectedContainer(sign); Container shopBlock = uBlock.findConnectedContainer(sign);
Inventory ownerInventory = (adminShop ? new AdminInventory() : shopBlock != null ? shopBlock.getInventory() : null); Inventory ownerInventory = shopBlock != null ? shopBlock.getInventory() : null;
ItemParseEvent parseEvent = new ItemParseEvent(material); ItemParseEvent parseEvent = new ItemParseEvent(material);
Bukkit.getPluginManager().callEvent(parseEvent); Bukkit.getPluginManager().callEvent(parseEvent);
@ -191,7 +191,7 @@ public class PlayerInteract implements Listener {
} }
if (Properties.SHIFT_SELLS_IN_STACKS && player.isSneaking() && price != PriceUtil.NO_PRICE && isAllowedForShift(action == buy)) { if (Properties.SHIFT_SELLS_IN_STACKS && player.isSneaking() && price != PriceUtil.NO_PRICE && isAllowedForShift(action == buy)) {
int newAmount = getStackAmount(item, ownerInventory, player, action); int newAmount = adminShop ? InventoryUtil.getMaxStackSize(item) : getStackAmount(item, ownerInventory, player, action);
if (newAmount > 0) { if (newAmount > 0) {
price = (price / amount) * newAmount; price = (price / amount) * newAmount;
amount = newAmount; amount = newAmount;
@ -202,6 +202,10 @@ public class PlayerInteract implements Listener {
ItemStack[] items = InventoryUtil.getItemsStacked(item); ItemStack[] items = InventoryUtil.getItemsStacked(item);
if (adminShop) {
ownerInventory = new AdminInventory(items);
}
TransactionType transactionType = (action == buy ? BUY : SELL); TransactionType transactionType = (action == buy ? BUY : SELL);
return new PreTransactionEvent(ownerInventory, player.getInventory(), items, price, player, account, sign, transactionType); return new PreTransactionEvent(ownerInventory, player.getInventory(), items, price, player, account, sign, transactionType);
} }