Skip to content

Commit

Permalink
Added stock counter to Statuses
Browse files Browse the repository at this point in the history
* updates during #saveShop so shops must be interacted with before the numbers will show
  • Loading branch information
KillerOfPie committed Oct 31, 2020
1 parent 7853ac6 commit 3386757
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/main/java/org/shanerx/tradeshop/objects/PlayerSetting.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,14 @@ public String serialize() {
public String getInvolvedStatusesString() {
StringBuilder sb = new StringBuilder();
sb.append("&eStatus of your shops: \n");
sb.append("&eShop Role &f| &eType &f| &eLocation &f| &eInventory Status\n&b");
sb.append("&eShop Role &f| &eType &f| &eAvailable Trades &f| &eLocation &f| &eInventory Status\n&b");
if (getOwnedShops().size() > 0) {
getOwnedShops().forEach(s -> {
Shop shop = utils.plugin.getDataStorage().loadShopFromSign(ShopLocation.deserialize(s));
if (shop.checkRole(uuid) != ShopRole.SHOPPER) {
sb.append(shop.checkRole(uuid).toString()).append(" &f|&a ");
sb.append(shop.getShopType().toString()).append(" &f|&d ");
sb.append(shop.getShopType().toString()).append(" &f|&b ");
sb.append(shop.getAvailableTrades()).append(" &f|&d ");
sb.append(s).append(" &f| ");
sb.append(shop.getStatus().getLine()).append("\n&b");
}
Expand All @@ -162,7 +163,8 @@ public String getInvolvedStatusesString() {
Shop shop = utils.plugin.getDataStorage().loadShopFromSign(ShopLocation.deserialize(s));
if (shop.checkRole(uuid) != ShopRole.SHOPPER) {
sb.append(shop.checkRole(uuid).toString()).append(" &f|&a ");
sb.append(shop.getShopType().toString()).append(" &f|&d ");
sb.append(shop.getShopType().toString()).append(" &f|&b ");
sb.append(shop.getAvailableTrades()).append(" &f|&d ");
sb.append(s).append(" &f| ");
sb.append(shop.getStatus().getLine()).append("\n&b");
}
Expand All @@ -184,9 +186,14 @@ public InventoryGui getInvolvedStatusesInventory() {
new ItemStack(shop.getInventoryLocation() != null ?
shop.getInventoryLocation().getBlock().getType() :
Material.getMaterial(shop.getShopLocation().getBlock().getType().toString().replaceAll("WALL_", ""))),
Math.min(shop.getAvailableTrades(), 64),
click -> {
return true; //Prevents clicking the item from doing anything, required parameter when using amount
},
utils.colorize("&d" + s),
utils.colorize("&a" + shop.getShopType().toString()),
utils.colorize("&b" + shop.checkRole(uuid).toString()),
utils.colorize("&bAvailable Trades: " + shop.getAvailableTrades()),
utils.colorize(shop.getStatus().getLine())));
}
});
Expand All @@ -199,9 +206,14 @@ public InventoryGui getInvolvedStatusesInventory() {
new ItemStack(shop.getInventoryLocation() != null ?
shop.getInventoryLocation().getBlock().getType() :
Material.getMaterial(shop.getShopLocation().getBlock().getType().toString().replaceAll("WALL_", ""))),
Math.min(shop.getAvailableTrades(), 64),
click -> {
return true; //Prevents clicking the item from doing anything, required parameter when using amount
},
utils.colorize("&d" + s),
utils.colorize("&a" + shop.getShopType().toString()),
utils.colorize("&b" + shop.checkRole(uuid).toString()),
utils.colorize("&bAvailable Trades: " + shop.getAvailableTrades()),
utils.colorize(shop.getStatus().getLine())));
}
});
Expand All @@ -221,7 +233,7 @@ public InventoryGui getInvolvedStatusesInventory() {
gui.addElement(new GuiPageElement('l', new ItemStack(Material.ARROW), GuiPageElement.PageAction.LAST, "Go to last page (%pages%)"));

//Blank Item
gui.setFiller(new ItemStack(Material.GRAY_STAINED_GLASS, 1));
gui.setFiller(new ItemStack(Material.LIGHT_GRAY_STAINED_GLASS_PANE, 1));

return gui;
}
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/org/shanerx/tradeshop/objects/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package org.shanerx.tradeshop.objects;

import com.google.gson.Gson;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
Expand Down Expand Up @@ -60,6 +61,8 @@ public class Shop implements Serializable {
private transient Utils utils = new Utils();
private ShopStatus status = ShopStatus.INCOMPLETE;

private int availableTrades = 0;

/**
* Creates a Shop object
*
Expand Down Expand Up @@ -510,6 +513,16 @@ public boolean hasCost() {
return cost.size() > 0;
}


/**
* Returns the amount of trades the shop could do when last accessed
*
* @return amount of trades the shop can do
*/
public int getAvailableTrades() {
return availableTrades;
}

/**
* Adds more product items
*
Expand Down Expand Up @@ -630,6 +643,7 @@ public List<UUID> getUsersUUID() {
* Saves the shop too file
*/
public void saveShop() {
updateFullTradeCount();
utils.plugin.getDataStorage().saveShop(this);
updateUserFiles();
}
Expand Down Expand Up @@ -966,4 +980,46 @@ public ShopRole checkRole(UUID uuidToCheck) {
return ShopRole.SHOPPER;
}
}

/**
* Updates the number of trades the shop can make
*/
public void updateFullTradeCount() {
Utils utils = new Utils();
if (!hasStorage() || !hasProduct())
availableTrades = 0;

Inventory shopInventory = hasStorage() ? getChestAsSC().getInventory() : null;

Inventory clone = Bukkit.createInventory(null, shopInventory.getStorageContents().length);
clone.setContents(shopInventory.getStorageContents());
int totalCount = 0, currentCount = 0;

for (ShopItemStack item : getProduct()) {
totalCount += item.getItemStack().getAmount();
if (item.getItemStack().getType().name().endsWith("SHULKER_BOX")) {
for (ItemStack itm : clone.getStorageContents()) {
if (itm != null && itm.getType().name().endsWith("SHULKER_BOX")) {
if (utils.compareShulkers(itm, item.getItemStack())) {
clone.removeItem(itm);
currentCount++;
}
}
}
} else {
int traded;
for (ItemStack storageItem : clone.getStorageContents()) {
if (storageItem != null && item.isSimilar(storageItem)) {
traded = Math.min(storageItem.getAmount(), item.getItemStack().getMaxStackSize());

storageItem.setAmount(traded);
clone.removeItem(storageItem);
currentCount += traded;
}
}
}
}

availableTrades = currentCount < totalCount ? 0 : currentCount / totalCount;
}
}

0 comments on commit 3386757

Please sign in to comment.