Skip to content

Commit

Permalink
Overstack trading is back...
Browse files Browse the repository at this point in the history
- Re-added support for overstack trading(the reasons for removing it were no longer a problem)
- Multistack support is now for items that would not trade the same
- Add(Cost/Product) now add ontop of existing items if they match
- Data will be the same as the first item added, i.e. If you add an item with a custom name then disable custom name checking and add an item without a custom name, the custom name would stay on the edit menu and sign.
  • Loading branch information
KillerOfPie committed Nov 5, 2021
1 parent 33fcf6c commit 6e9aa85
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 24 deletions.
57 changes: 35 additions & 22 deletions src/main/java/org/shanerx/tradeshop/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.FireworkMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.shanerx.tradeshop.TradeShop;
import org.shanerx.tradeshop.enumys.Message;
import org.shanerx.tradeshop.enumys.Permissions;
Expand Down Expand Up @@ -256,9 +257,21 @@ public void edit() {
}

private StaticGuiElement shopItemEditMenu(int index, boolean isCost) {
return new StaticGuiElement('e', (isCost ? costItems : productItems).get(index).getItemStack(), click2 -> {
ShopItemStack item = (isCost ? costItems : productItems).get(index),
tempItem = item.clone();
ShopItemStack item = (isCost ? costItems : productItems).get(index).clone();
ItemStack tempStack = item.getItemStack();
ItemMeta tempMeta = tempStack.getItemMeta();
List<String> newLore = new ArrayList<>();
newLore.add(colorize("&8Amount &7» &f" + item.getAmount()));

if (tempMeta != null && tempMeta.hasLore()) {
newLore.add("");
newLore.addAll(tempMeta.getLore());
}

tempMeta.setLore(newLore);
tempStack.setItemMeta(tempMeta);

return new StaticGuiElement('e', tempStack, click2 -> {
InventoryGui itemEdit = new InventoryGui(plugin, "Edit Cost Item", ITEM_LAYOUT);
GuiElementGroup itemGroup = new GuiElementGroup('g');

Expand All @@ -267,7 +280,7 @@ private StaticGuiElement shopItemEditMenu(int index, boolean isCost) {

// Save and Back
itemEdit.addElement(new StaticGuiElement('s', new ItemStack(Material.ANVIL), click3 -> {
(isCost ? costItems : productItems).set((isCost ? costItems : productItems).indexOf(item), tempItem);
(isCost ? costItems : productItems).set(index, item);
InventoryGui.goBack(pSender);
return true;
}, "Save Changes"));
Expand All @@ -288,36 +301,36 @@ private StaticGuiElement shopItemEditMenu(int index, boolean isCost) {

//Add new item settings below
if (item.getItemStack().getItemMeta() instanceof Damageable) {
itemGroup.addElement(numericalOption(ShopItemStackSettingKeys.COMPARE_DURABILITY, tempItem, new ItemStack[]{
itemGroup.addElement(numericalOption(ShopItemStackSettingKeys.COMPARE_DURABILITY, item, new ItemStack[]{
FALSE_ITEM, new ItemStack(Material.IRON_BLOCK), TRUE_ITEM, new ItemStack(Material.GOLD_BLOCK)
}));
}

if (tempItem.getItemStack().getItemMeta() instanceof BookMeta) {
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_BOOK_AUTHOR, tempItem, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_BOOK_PAGES, tempItem, TRUE_ITEM, FALSE_ITEM));
if (tempMeta instanceof BookMeta) {
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_BOOK_AUTHOR, item, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_BOOK_PAGES, item, TRUE_ITEM, FALSE_ITEM));
}

if (tempItem.getItemStack().getItemMeta() instanceof FireworkMeta) {
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_FIREWORK_DURATION, tempItem, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_FIREWORK_EFFECTS, tempItem, TRUE_ITEM, FALSE_ITEM));
if (tempMeta instanceof FireworkMeta) {
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_FIREWORK_DURATION, item, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_FIREWORK_EFFECTS, item, TRUE_ITEM, FALSE_ITEM));
}

if (tempItem.getItemStack().getType().toString().endsWith("SHULKER_BOX")) {
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_SHULKER_INVENTORY, tempItem, TRUE_ITEM, FALSE_ITEM));
if (tempStack.getType().toString().endsWith("SHULKER_BOX")) {
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_SHULKER_INVENTORY, item, TRUE_ITEM, FALSE_ITEM));
}

if (plugin.getVersion().isAtLeast(1, 17) && tempItem.getItemStack().getType().equals(Material.BUNDLE)) {
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_BUNDLE_INVENTORY, tempItem, TRUE_ITEM, FALSE_ITEM));
if (plugin.getVersion().isAtLeast(1, 17) && tempStack.getType().equals(Material.BUNDLE)) {
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_BUNDLE_INVENTORY, item, TRUE_ITEM, FALSE_ITEM));
}

itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_NAME, tempItem, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_LORE, tempItem, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_UNBREAKABLE, tempItem, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_ENCHANTMENTS, tempItem, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_ITEM_FLAGS, tempItem, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_CUSTOM_MODEL_DATA, tempItem, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_ATTRIBUTE_MODIFIER, tempItem, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_NAME, item, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_LORE, item, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_UNBREAKABLE, item, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_ENCHANTMENTS, item, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_ITEM_FLAGS, item, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_CUSTOM_MODEL_DATA, item, TRUE_ITEM, FALSE_ITEM));
itemGroup.addElement(booleanOption(ShopItemStackSettingKeys.COMPARE_ATTRIBUTE_MODIFIER, item, TRUE_ITEM, FALSE_ITEM));


itemEdit.addElement(itemGroup);
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/org/shanerx/tradeshop/commands/WhatCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.shanerx.tradeshop.TradeShop;
import org.shanerx.tradeshop.enumys.*;
import org.shanerx.tradeshop.objects.Shop;
import org.shanerx.tradeshop.objects.ShopItemStack;

import java.util.ArrayList;
import java.util.List;

public class WhatCommand extends CommandRunner {

public WhatCommand(TradeShop instance, CommandPass command) {
Expand Down Expand Up @@ -126,15 +130,27 @@ private String stateText(int state) {
}

private StaticGuiElement shopitemViewMenu(ShopItemStack item) {
return new StaticGuiElement('e', item.getItemStack(), click2 -> {
ItemStack tempStack = item.getItemStack().clone();
ItemMeta tempMeta = tempStack.getItemMeta();
List<String> newLore = new ArrayList<>();
newLore.add("Amount: " + item.getAmount());

if (tempMeta != null && tempMeta.hasLore()) {
newLore.add("");
newLore.addAll(tempMeta.getLore());
}

tempMeta.setLore(newLore);
tempStack.setItemMeta(tempMeta);
return new StaticGuiElement('e', tempStack, click2 -> {
InventoryGui itemView = new InventoryGui(plugin, "Edit Cost Item", ITEM_LAYOUT);
GuiElementGroup itemGroup = new GuiElementGroup('g');

itemView.addElement(BACK_BUTTON);

itemView.addElement(new StaticGuiElement('a', new ItemStack(Material.BLACK_STAINED_GLASS_PANE), " "));

itemGroup.addElement(new StaticGuiElement('e', item.getItemStack()));
itemGroup.addElement(new StaticGuiElement('e', tempStack));

itemGroup.addElement(new StaticGuiElement('e', settingItem(item.getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_NAME)), "Compare Name", "State: " + item.getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_NAME)));
itemGroup.addElement(new StaticGuiElement('e', settingItem(item.getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_LORE)), "Compare Lore", "State: " + item.getShopSettingAsBoolean(ShopItemStackSettingKeys.COMPARE_LORE)));
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/shanerx/tradeshop/objects/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ public void addCost(ItemStack newItem) {
if (!utils.isValidType(newItem.getType()))
return;

/* Added stacks are separated by stack size
int amount = newItem.getAmount();
List<ItemStack> items = new ArrayList<>();
while (amount > 0) {
Expand All @@ -476,6 +477,30 @@ public void addCost(ItemStack newItem) {
}
items.forEach((ItemStack iS) -> cost.add(new ShopItemStack(iS)));
*/

///* Added stacks are not separated and are added ontop of existing similar stacks
ShopItemStack toAddShopItemStack = new ShopItemStack(newItem),
toRemoveShopItemStack = null;


for (ShopItemStack shopItemStack : getCost()) {
if (shopItemStack.getItemStack().getType().equals(newItem.getType()) && shopItemStack.isSimilar(newItem)) {
toRemoveShopItemStack = shopItemStack;
toAddShopItemStack = shopItemStack.clone();
toAddShopItemStack.setAmount(shopItemStack.getAmount() + newItem.getAmount());
break;
}
}

if (toRemoveShopItemStack != null)
cost.remove(toRemoveShopItemStack);

cost.add(toAddShopItemStack);
//*/


if (!getShopType().isITrade() && chestLoc != null)
cost.removeIf(item -> item.getItemStack().getType().toString().endsWith("SHULKER_BOX") && getInventoryLocation().getBlock().getType().toString().endsWith("SHULKER_BOX"));

Expand Down Expand Up @@ -539,6 +564,7 @@ public void addProduct(ItemStack newItem) {
if (!utils.isValidType(newItem.getType()))
return;

/* Added stacks are separated by stack size
int amount = newItem.getAmount();
List<ItemStack> items = new ArrayList<>();
while (amount > 0) {
Expand All @@ -556,6 +582,28 @@ public void addProduct(ItemStack newItem) {
}
items.forEach((ItemStack iS) -> product.add(new ShopItemStack(iS)));
*/


///* Added stacks are not separated and are added ontop of existing similar stacks
ShopItemStack toAddShopItemStack = new ShopItemStack(newItem),
toRemoveShopItemStack = null;


for (ShopItemStack shopItemStack : getProduct()) {
if (shopItemStack.getItemStack().getType().equals(newItem.getType()) && shopItemStack.isSimilar(newItem)) {
toRemoveShopItemStack = shopItemStack;
toAddShopItemStack = shopItemStack.clone();
toAddShopItemStack.setAmount(shopItemStack.getAmount() + newItem.getAmount());
break;
}
}

if (toRemoveShopItemStack != null)
product.remove(toRemoveShopItemStack);

product.add(toAddShopItemStack);
//*/

saveShop();
updateSign();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ public boolean setShopSettings(ShopItemStackSettingKeys key, ObjectHolder<?> val
return false;
}

public int getAmount() {
return itemStack.getAmount();
}

public void setAmount(int amount) {
itemStack.setAmount(amount);
toBase64();
}

public String getItemStackB64() {
return itemStackB64;
}
Expand Down

0 comments on commit 6e9aa85

Please sign in to comment.