Skip to content

Commit

Permalink
Remove "feature" to sell different items in one transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Brokkonaut committed Mar 12, 2022
1 parent 91a2434 commit c8518ee
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 168 deletions.
8 changes: 3 additions & 5 deletions src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java
Expand Up @@ -72,11 +72,9 @@ public static boolean isEmpty(Inventory inventory) {
* inventory
* @return Does the inventory contain stock of this type?
*/
public static boolean hasItems(ItemStack[] items, Inventory inventory) {
for (ItemStack item : items) {
if (!inventory.containsAtLeast(item, item.getAmount())) {
return false;
}
public static boolean hasItems(ItemStack item, Inventory inventory) {
if (!inventory.containsAtLeast(item, item.getAmount())) {
return false;
}

return true;
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/com/Acrobot/ChestShop/Events/PreTransactionEvent.java
Expand Up @@ -28,12 +28,12 @@ public class PreTransactionEvent extends Event {
private Inventory ownerInventory;
private Inventory clientInventory;

private ItemStack[] items;
private ItemStack items;
private double price;

private TransactionOutcome transactionOutcome = TRANSACTION_SUCCESFUL;

public PreTransactionEvent(Inventory ownerInventory, Inventory clientInventory, ItemStack[] items, double price, Player client, OfflinePlayer owner, Sign sign, TransactionType type) {
public PreTransactionEvent(Inventory ownerInventory, Inventory clientInventory, ItemStack items, double price, Player client, OfflinePlayer owner, Sign sign, TransactionType type) {
this.ownerInventory = ownerInventory;
this.clientInventory = (clientInventory == null ? client.getInventory() : clientInventory);

Expand Down Expand Up @@ -77,14 +77,14 @@ public void setPrice(double price) {
* @param stock
* Stock
*/
public void setStock(ItemStack... stock) {
public void setStock(ItemStack stock) {
items = stock;
}

/**
* @return Stock available
*/
public ItemStack[] getStock() {
public ItemStack getStock() {
return items;
}

Expand Down Expand Up @@ -177,6 +177,7 @@ public void setCancelled(TransactionOutcome reason) {
transactionOutcome = reason;
}

@Override
public HandlerList getHandlers() {
return handlers;
}
Expand All @@ -186,21 +187,28 @@ public static HandlerList getHandlerList() {
}

public enum TransactionOutcome {
SHOP_DOES_NOT_BUY_THIS_ITEM, SHOP_DOES_NOT_SELL_THIS_ITEM,
SHOP_DOES_NOT_BUY_THIS_ITEM,
SHOP_DOES_NOT_SELL_THIS_ITEM,

CLIENT_DOES_NOT_HAVE_PERMISSION,

CLIENT_DOES_NOT_HAVE_ENOUGH_MONEY, SHOP_DOES_NOT_HAVE_ENOUGH_MONEY,
CLIENT_DOES_NOT_HAVE_ENOUGH_MONEY,
SHOP_DOES_NOT_HAVE_ENOUGH_MONEY,

CLIENT_DEPOSIT_FAILED, SHOP_DEPOSIT_FAILED,
CLIENT_DEPOSIT_FAILED,
SHOP_DEPOSIT_FAILED,

NOT_ENOUGH_SPACE_IN_CHEST, NOT_ENOUGH_SPACE_IN_INVENTORY,
NOT_ENOUGH_SPACE_IN_CHEST,
NOT_ENOUGH_SPACE_IN_INVENTORY,

NOT_ENOUGH_STOCK_IN_CHEST, NOT_ENOUGH_STOCK_IN_INVENTORY,
NOT_ENOUGH_STOCK_IN_CHEST,
NOT_ENOUGH_STOCK_IN_INVENTORY,

INVALID_SHOP,

SPAM_CLICKING_PROTECTION, CREATIVE_MODE_PROTECTION, SHOP_IS_RESTRICTED,
SPAM_CLICKING_PROTECTION,
CREATIVE_MODE_PROTECTION,
SHOP_IS_RESTRICTED,

OTHER, // For plugin use!

Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/Acrobot/ChestShop/Events/TransactionEvent.java
Expand Up @@ -23,7 +23,7 @@ public class TransactionEvent extends Event {
private final Player client;
private final OfflinePlayer owner;

private final ItemStack[] stock;
private final ItemStack stock;
private final double price;

private final Sign sign;
Expand All @@ -43,7 +43,7 @@ public TransactionEvent(PreTransactionEvent event, Sign sign) {
this.sign = sign;
}

public TransactionEvent(TransactionType type, Inventory ownerInventory, Inventory clientInventory, Player client, OfflinePlayer owner, ItemStack[] stock, double price, Sign sign) {
public TransactionEvent(TransactionType type, Inventory ownerInventory, Inventory clientInventory, Player client, OfflinePlayer owner, ItemStack stock, double price, Sign sign) {
this.type = type;

this.ownerInventory = ownerInventory;
Expand Down Expand Up @@ -96,7 +96,7 @@ public OfflinePlayer getOwner() {
/**
* @return Stock available
*/
public ItemStack[] getStock() {
public ItemStack getStock() {
return stock;
}

Expand All @@ -114,6 +114,7 @@ public Sign getSign() {
return sign;
}

@Override
public HandlerList getHandlers() {
return handlers;
}
Expand All @@ -126,6 +127,7 @@ public static HandlerList getHandlerList() {
* Possible transaction types
*/
public enum TransactionType {
BUY, SELL
BUY,
SELL
}
}
Expand Up @@ -210,10 +210,8 @@ private static PreTransactionEvent preparePreTransactionEvent(Sign sign, Player

item.setAmount(amount);

ItemStack[] items = { item };

TransactionType transactionType = (action == buy ? BUY : SELL);
return new PreTransactionEvent(ownerInventory, player.getInventory(), items, price, player, owner, sign, transactionType);
return new PreTransactionEvent(ownerInventory, player.getInventory(), item, price, player, owner, sign, transactionType);
}

private static boolean isAllowedForShift(boolean buyTransaction) {
Expand Down
Expand Up @@ -61,7 +61,7 @@ public static void onTransaction(TransactionEvent event) {
}
}

private static boolean shopShouldBeRemoved(Inventory inventory, ItemStack[] stock) {
private static boolean shopShouldBeRemoved(Inventory inventory, ItemStack stock) {
return Properties.REMOVE_EMPTY_SHOPS && !ChestShopSign.isAdminShop(inventory) && !InventoryUtil.hasItems(stock, inventory);
}
}
Expand Up @@ -40,21 +40,15 @@ public static void inventoryItemRemover(TransactionEvent event) {
event.getClient().updateInventory();
}

private static void removeItems(Inventory inventory, ItemStack[] items) {
for (ItemStack item : items) {
InventoryUtil.remove(item, inventory);
}
private static void removeItems(Inventory inventory, ItemStack item) {
InventoryUtil.remove(item, inventory);
}

private static void addItems(Inventory inventory, ItemStack[] items) {
private static void addItems(Inventory inventory, ItemStack item) {
if (Properties.STACK_TO_64) {
for (ItemStack item : items) {
InventoryUtil.add(item, inventory, 64);
}
InventoryUtil.add(item, inventory, 64);
} else {
for (ItemStack item : items) {
InventoryUtil.add(item, inventory);
}
InventoryUtil.add(item, inventory);
}
}
}
Expand Up @@ -26,9 +26,8 @@ public static void onTransaction(final TransactionEvent event) {

StringBuilder items = new StringBuilder(50);

for (ItemStack item : event.getStock()) {
items.append(item.getAmount()).append(' ').append(getSignName(item));
}
ItemStack item = event.getStock();
items.append(item.getAmount()).append(' ').append(getSignName(item));

String message = String.format(template, event.getClient().getUniqueId().toString(), event.getClient().getName(), items.toString(), event.getPrice(), event.getOwner().getUniqueId().toString(), NameManager.getFullNameFor(event.getOwner().getUniqueId()),
LocationUtil.locationToString(event.getSign().getLocation()));
Expand Down
Expand Up @@ -11,7 +11,6 @@
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;

import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.ChestShop.Commands.Toggle;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
Expand Down Expand Up @@ -75,20 +74,13 @@ protected static void sendSellMessage(TransactionEvent event) {
}
}

private static BaseComponent[] parseItemInformation(ItemStack[] items) {
ItemStack[] stock = InventoryUtil.mergeSimilarStacks(items);

private static BaseComponent[] parseItemInformation(ItemStack item) {
ArrayList<BaseComponent> message = new ArrayList<>();
// StringBuilder message = new StringBuilder(15);
// Joiner joiner = Joiner.on(' ');

for (ItemStack item : stock) {
if (!message.isEmpty()) {
message.add(new TextComponent(", "));
}
message.add(new TextComponent(item.getAmount() + " "));
message.add(ComponentUtils.getLocalizedItemName(item.getType()));
}
message.add(new TextComponent(item.getAmount() + " "));
message.add(ComponentUtils.getLocalizedItemName(item.getType()));

return message.toArray(new BaseComponent[message.size()]);
}
Expand Down
Expand Up @@ -26,7 +26,7 @@ public static void onBuyItemCheck(PreTransactionEvent event) {
return;
}

ItemStack[] stock = event.getStock();
ItemStack stock = event.getStock();
Inventory ownerInventory = event.getOwnerInventory();

CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(BigDecimal.valueOf(event.getPrice()), event.getClient());
Expand All @@ -48,7 +48,7 @@ public static void onSellItemCheck(PreTransactionEvent event) {
return;
}

ItemStack[] stock = event.getStock();
ItemStack stock = event.getStock();
Inventory clientInventory = event.getClientInventory();

CurrencyCheckEvent currencyCheckEvent = new CurrencyCheckEvent(BigDecimal.valueOf(event.getPrice()), event.getOwner().getUniqueId(), event.getSign().getWorld());
Expand Down
Expand Up @@ -12,7 +12,6 @@
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;

import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.ChestShop.Commands.Toggle;
import com.Acrobot.ChestShop.Configuration.Messages;
Expand Down Expand Up @@ -94,17 +93,10 @@ public static void onMessage(PreTransactionEvent event) {
}
}

private static String getItemNames(ItemStack[] stock) {
ItemStack[] items = InventoryUtil.mergeSimilarStacks(stock);

private static String getItemNames(ItemStack stock) {
StringBuilder names = new StringBuilder(50);

for (ItemStack item : items) {
if (names.length() > 0) {
names.append(',').append(' ');
}
names.append(MaterialUtil.getName(item.getType()));
}
names.append(MaterialUtil.getName(stock.getType()));

return names.toString();
}
Expand Down

0 comments on commit c8518ee

Please sign in to comment.