Skip to content

Commit

Permalink
Messaging and Text Display Updates + Bug Fixes
Browse files Browse the repository at this point in the history
- Added ITRADE_NO_COST_AMOUNT setting and FAILED_TRADE message
- Changed INSUFFICIENT_ITEMS, SHOP_INSUFFICIENT_ITEMS, and ON_TRADE messages to show full trade details
- Message file incremented to v1.3
- Added ItemMultiLin Message method
- Changed ObjectHolder#toString to just pass Object#toString to reduce output size and readability
- DRY'd Shop#areCostsValid and Shop#areProducts Valid
- Fixed bug caused by Shop#hasProductStock and Shop#hasCostStock pulling directly from product/cost objects instead of Shop#getCost/Shop#getProduct(was causing inaccurate output from getItems the wasn't apparent until other changes were made)
- Changed Utils#getItems to take Inventory#getStorageContents rather than Inventory
- Added Static implementation of ShopItemStack#getCleanItemName to be used without creating new objects when not needed and redirected non-static method to use new static method(DRY)
- Added support to ShopItemStack#getCleanItemName for book Titles and Proper formatting of ItemType returns
- Added MISSING_ITEMS, RECEIVED_LINES, and GIVEN_ITEMS to Variables Enum
- Added Utils#createBadList to clean up creation of `null as first index lists
- Cleaned up some javaDocs and missed significantly more
- Changed return of Utils#canExchangeAll to Tuple<ExchangeStatus, List<ItemStack>> to return bad lists in cases where ExchangeStatus indicates failure do to missing items. returns badList in all other cases
- Cleaned up message calls in ShopTradeListener using new detailed messages.
- Changed ShopTradeListener#tradeAllItems to return Tuple<List<ItemStack>, List<ItemStack>> as Good/Bad CostList, ProductList for detailed messages
- Changed output for cost-free iTrade shops
- Other Cleanup and Optimizations
  • Loading branch information
KillerOfPie committed Mar 30, 2022
1 parent ac4f0b8 commit bd3c245
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 173 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/shanerx/tradeshop/enumys/DebugLevels.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public enum DebugLevels {
ITEM_COMPARE(6, Level.WARNING), // 32
NAME_COMPARE(7, Level.WARNING), // 64
SHULKERS_SUCK(8, Level.WARNING), // 128
ENCHANT_CHECKS(9, Level.WARNING) // 256

ENCHANT_CHECKS(9, Level.WARNING), // 256
OUTPUT(10, Level.WARNING) //512
;

//position is what value to check for this level in the binary string -1.
Expand Down
151 changes: 63 additions & 88 deletions src/main/java/org/shanerx/tradeshop/listeners/ShopTradeListener.java

Large diffs are not rendered by default.

28 changes: 16 additions & 12 deletions src/main/java/org/shanerx/tradeshop/objects/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ public boolean removeUser(UUID oldUser) {
}

saveShop();
updateSign();

return ret;
}
Expand Down Expand Up @@ -710,7 +709,6 @@ public void saveShop() {
updateFullTradeCount();
utils.PLUGIN.getDataStorage().saveShop(this);
updateUserFiles();
updateSign();
}

/**
Expand Down Expand Up @@ -882,14 +880,14 @@ public ShopStatus setOpen() {
* Checks if the shop has sufficient product stock to make a trade
*/
public boolean hasProductStock() {
return !shopType.isITrade() && hasProduct() && getChestAsSC() != null && getChestAsSC().hasStock(product);
return !shopType.isITrade() && hasProduct() && getChestAsSC() != null && getChestAsSC().hasStock(getProduct());
}

/**
* Checks if the shop has sufficient cost stock to make a trade(Use for BiTrade)
*/
public boolean hasCostStock() {
return !shopType.isITrade() && hasCost() && getChestAsSC() != null && getChestAsSC().hasStock(cost);
return !shopType.isITrade() && hasCost() && getChestAsSC() != null && getChestAsSC().hasStock(getCost());
}

/**
Expand Down Expand Up @@ -1003,12 +1001,7 @@ public List<String> getUserNames() {
* @return true if all costs are valid
*/
public boolean areCostsValid() {
for (ShopItemStack iS : cost) {
if (utils.isIllegal(IllegalItemList.TradeItemType.COST, iS.getItemStack().getType()))
return false;
}

return true;
return areItemsValid(IllegalItemList.TradeItemType.COST, cost);
}

/**
Expand All @@ -1017,8 +1010,19 @@ public boolean areCostsValid() {
* @return true if all products are valid
*/
public boolean areProductsValid() {
for (ShopItemStack iS : product) {
if (utils.isIllegal(IllegalItemList.TradeItemType.PRODUCT, iS.getItemStack().getType()))
return areItemsValid(IllegalItemList.TradeItemType.PRODUCT, product);
}

/**
* Checks if all Items in the list are valid for trade
*
* @param tradeItemType Should Illegal Items be checked as Costs or Products
* @param items List<ShopItemStack> to check
* @return true if all products are valid
*/
private boolean areItemsValid(IllegalItemList.TradeItemType tradeItemType, List<ShopItemStack> items) {
for (ShopItemStack iS : items) {
if (utils.isIllegal(tradeItemType, iS.getItemStack().getType()))
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/shanerx/tradeshop/objects/ShopChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public Inventory getInventory() {
}

public boolean hasStock(List<ShopItemStack> itemToCheck) {
return itemToCheck.size() > 0 && getItems(getInventory(), itemToCheck, 1).get(0) != null;
return itemToCheck.size() > 0 && getItems(getInventory().getStorageContents(), itemToCheck, 1).get(0) != null;
}

public void loadFromName() {
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/shanerx/tradeshop/objects/ShopItemStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.md_5.bungee.api.ChatColor;
import org.apache.commons.lang.WordUtils;
import org.bukkit.FireworkEffect;
import org.bukkit.Material;
import org.bukkit.block.ShulkerBox;
Expand Down Expand Up @@ -494,10 +495,19 @@ public String getItemName() {
itemStack.getType().toString();
}

public String getCleanItemName() {
static public String getCleanItemName(ItemStack itemStack) {
BookMeta bookmeta = itemStack.hasItemMeta() && itemStack.getItemMeta() instanceof BookMeta ? ((BookMeta) itemStack.getItemMeta()) : null;

if (bookmeta != null)
return bookmeta.getTitle();

return ChatColor.stripColor(itemStack.hasItemMeta() && itemStack.getItemMeta().hasDisplayName() ?
itemStack.getItemMeta().getDisplayName() :
itemStack.getType().toString());
WordUtils.capitalizeFully(itemStack.getType().toString().replace("_", " ")));
}

public String getCleanItemName() {
return ShopItemStack.getCleanItemName(itemStack);
}

public String serialize() {
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/shanerx/tradeshop/utils/ObjectHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public Type getObject() {

@Override
public String toString() {
return "ObjectHolder{" +
"obj=" + obj +
'}';
return obj.toString();
}
}
128 changes: 71 additions & 57 deletions src/main/java/org/shanerx/tradeshop/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,47 +355,56 @@ public boolean checkShopChest(Block sign) {
* @return true if shop has enough cost to make trade
*/
public Boolean checkInventory(Inventory inv, List<ShopItemStack> itemList, int multiplier) {
return getItems(inv, itemList, multiplier).get(0) != null;
}


return getItems(inv.getStorageContents(), itemList, multiplier).get(0) != null;
}

/**
* Returns a 'bad' list with index 0 being `null`
*
* @return list with index 0 being `null`
*/
public List<ItemStack> createBadList() {
List<ItemStack> badList = new ArrayList<>();
badList.add(null);
return badList;
}


/**
* Checks whether a trade can take place.
*
* @param shop the Shop object the player is trading with
* @param playerInv the Inventory object representing the inventory that is subject to the transaction.
* @param shop the Shop object the player is trading with
* @param playerInv the storageContents of the player inventory that is subject to the transaction.
* @param multiplier the multiplier for the trade
* @param action the action from the event
* @return Exchange status with appropriate response
*/
public ExchangeStatus canExchangeAll(Shop shop, Inventory playerInv, int multiplier, Action action) {
public Tuple<ExchangeStatus, List<ItemStack>> canExchangeAll(Shop shop, Inventory playerInv, int multiplier, Action action) {
if (shop.getShopType() != ShopType.BITRADE && action == Action.LEFT_CLICK_BLOCK) {
return new Tuple<>(ExchangeStatus.NOT_TRADE, createBadList());
}

Inventory playerInventory = Bukkit.createInventory(null, playerInv.getStorageContents().length);
playerInventory.setContents(playerInv.getStorageContents().clone());

if (shop.getShopType() != ShopType.BITRADE && action == Action.LEFT_CLICK_BLOCK) {
return ExchangeStatus.NOT_TRADE;
}

Inventory shopInv = null;
Inventory shopInventory = null;
Inventory shopInventory = null;

if (shop.getShopType() != ShopType.ITRADE) {
shopInv = shop.getChestAsSC().getInventory();
shopInventory = Bukkit.createInventory(null, shopInv.getStorageContents().length);
shopInventory.setContents(shopInv.getStorageContents().clone());
}
if (shop.getShopType() != ShopType.ITRADE) {
Inventory shopInv = shop.getChestAsSC().getInventory();
shopInventory = Bukkit.createInventory(null, shopInv.getStorageContents().length);
shopInventory.setContents(shopInv.getStorageContents().clone());
}

List<ItemStack> costItems, productItems;

if (shop.getShopType() == ShopType.ITRADE) { //ITrade trade
if (shop.getShopType() == ShopType.ITRADE) { //ITrade trade

//Method to find Cost items in player inventory and add to cost array
costItems = getItems(playerInventory, shop.getCost(), multiplier);
costItems = getItems(playerInventory.getStorageContents(), shop.getCost(), multiplier);

if (!costItems.isEmpty()) {
if (costItems.get(0) == null) {
return ExchangeStatus.PLAYER_NO_COST;
return new Tuple<>(ExchangeStatus.PLAYER_NO_COST, costItems);
}

for (ItemStack item : costItems) {
Expand All @@ -411,42 +420,42 @@ public ExchangeStatus canExchangeAll(Shop shop, Inventory playerInv, int multipl
}
}

productItems = getItems(iTradeVirtualInventory, shop.getProduct(), multiplier);
productItems = getItems(iTradeVirtualInventory.getStorageContents(), shop.getProduct(), multiplier);

for (ItemStack item : productItems) {
if (!playerInventory.addItem(item).isEmpty()) {
return ExchangeStatus.PLAYER_NO_SPACE;
return new Tuple<>(ExchangeStatus.PLAYER_NO_SPACE, createBadList());
}
}

return ExchangeStatus.SUCCESS; //Successfully completed trade
return new Tuple<>(ExchangeStatus.SUCCESS, createBadList()); //Successfully completed trade
} else if (shop.getShopType() == ShopType.BITRADE && action == Action.LEFT_CLICK_BLOCK) { //BiTrade Reversed Trade

//Method to find Cost items in player inventory and add to cost array
costItems = getItems(playerInventory, shop.getProduct(), multiplier); //Reverse BiTrade, Product is Cost
if (costItems.get(0) == null) {
return ExchangeStatus.PLAYER_NO_COST;
costItems = getItems(playerInventory.getStorageContents(), shop.getProduct(), multiplier); //Reverse BiTrade, Product is Cost
if (costItems.get(0) == null) {
return new Tuple<>(ExchangeStatus.PLAYER_NO_COST, costItems);
}

//Method to find Product items in shop inventory and add to product array
productItems = getItems(shopInventory, shop.getCost(), multiplier); //Reverse BiTrade, Cost is Product
productItems = getItems(shopInventory.getStorageContents(), shop.getCost(), multiplier); //Reverse BiTrade, Cost is Product
if (productItems.get(0) == null) {
shop.updateStatus();
return ExchangeStatus.SHOP_NO_PRODUCT;
shop.updateStatus();
return new Tuple<>(ExchangeStatus.SHOP_NO_PRODUCT, productItems);
}
} else { // Normal Trade

//Method to find Cost items in player inventory and add to cost array
costItems = getItems(playerInventory, shop.getCost(), multiplier);
if (costItems.get(0) == null) {
return ExchangeStatus.PLAYER_NO_COST;
costItems = getItems(playerInventory.getStorageContents(), shop.getCost(), multiplier);
if (costItems.get(0) == null) {
return new Tuple<>(ExchangeStatus.PLAYER_NO_COST, costItems);
}

//Method to find Product items in shop inventory and add to product array
productItems = getItems(shopInventory, shop.getProduct(), multiplier);
productItems = getItems(shopInventory.getStorageContents(), shop.getProduct(), multiplier);
if (productItems.get(0) == null) {
shop.updateStatus();
return ExchangeStatus.SHOP_NO_PRODUCT;
shop.updateStatus();
return new Tuple<>(ExchangeStatus.SHOP_NO_PRODUCT, productItems);
}

}
Expand All @@ -464,30 +473,35 @@ public ExchangeStatus canExchangeAll(Shop shop, Inventory playerInv, int multipl
//For loop to put cost items in shop inventory
for (ItemStack item : costItems) {
if (!shopInventory.addItem(item).isEmpty()) {
return ExchangeStatus.SHOP_NO_SPACE;
}
}
return new Tuple<>(ExchangeStatus.SHOP_NO_SPACE, createBadList());
}
}

//For loop to put product items in player inventory
for (ItemStack item : productItems) {
if (!playerInventory.addItem(item).isEmpty()) {
return ExchangeStatus.PLAYER_NO_SPACE;
}
}
//For loop to put product items in player inventory
for (ItemStack item : productItems) {
if (!playerInventory.addItem(item).isEmpty()) {
return new Tuple<>(ExchangeStatus.PLAYER_NO_SPACE, createBadList());
}
}

return ExchangeStatus.SUCCESS; //Successfully completed trade
return new Tuple<>(ExchangeStatus.SUCCESS, createBadList()); //Successfully completed trade
}

//Returns an arraylist of the itemstacks to be removed/added, if it could not get enough of an item, will return index 0 as null and index 1 as item it could not get enough of
public List<ItemStack> getItems(Inventory inventory, List<ShopItemStack> search, int multiplier) {
/**
* Returns an arraylist of the ItemStack Objects to be removed/added, if it could not get enough of any items, it will return index 0 as null, followed by ItemStack Objects that could not be retrieved
*
* @param storageContents the storage contents of the inventory being checked
* @param search List of ShopItemStack Objects that need to be retrieved
* @param multiplier the multiplier for the trade
* @return List of ItemStack Objects to pull from inventory or if not enough; index 0 as null, followed by ItemStack Objects that could not be retrieved
*/
public List<ItemStack> getItems(ItemStack[] storageContents, List<ShopItemStack> search, int multiplier) {
Map<ItemStack, Integer> storage = new HashMap<>(), found = new HashMap<>();
ArrayList<ItemStack> good = new ArrayList<ItemStack>(), bad = new ArrayList<ItemStack>();

debugger.log("ShopTradeListener > Search List: " + search, DebugLevels.TRADE);
List<ItemStack> good = new ArrayList<ItemStack>(), bad = createBadList();

bad.add(null);
debugger.log("Utils > getItems > Search List: " + search, DebugLevels.TRADE);

for (ItemStack itemStack : inventory.getStorageContents()) {
for (ItemStack itemStack : storageContents.clone()) {
if (itemStack != null) {
ItemStack tempItemStack = itemStack.clone();
int amount = tempItemStack.getAmount();
Expand All @@ -498,7 +512,7 @@ public List<ItemStack> getItems(Inventory inventory, List<ShopItemStack> search,
}
}

debugger.log("ShopTradeListener > Storage List: " + storage, DebugLevels.TRADE);
debugger.log("Utils > getItems > Storage List: " + storage, DebugLevels.TRADE);

int totalCount = 0, currentCount = 0;

Expand Down Expand Up @@ -534,9 +548,9 @@ public List<ItemStack> getItems(Inventory inventory, List<ShopItemStack> search,
}
}

debugger.log("ShopTradeListener > Good List: " + good, DebugLevels.TRADE);
debugger.log("ShopTradeListener > Bad List: " + bad, DebugLevels.TRADE);
debugger.log("ShopTradeListener > Return Status: " + (currentCount != totalCount ? "bad" : "good"), DebugLevels.TRADE);
debugger.log("Utils > getItems > Good List: " + good, DebugLevels.TRADE);
debugger.log("Utils > getItems > Bad List: " + bad, DebugLevels.TRADE);
debugger.log("Utils > getItems > Return Status: " + (currentCount != totalCount ? "bad" : "good"), DebugLevels.TRADE);

return currentCount != totalCount ? bad : good;
}
Expand Down
Loading

0 comments on commit bd3c245

Please sign in to comment.