Skip to content

Commit

Permalink
Bug Fix for #134
Browse files Browse the repository at this point in the history
- Moved #createShop to Utils
- Made #createShop more generic so sign creation and commands could both use it
- Added SHOP_CREATION Debug Level at bit 11(1024)
- Added toDebug method to Shop to allow a easily readable output of the shop
- Re-wrote updateSign methods to make them simpler to understand and allow sign/event passing from #createShop
- Removed event storing and related methods from Shop as it was unnecessary
- Deprecated #b64OverstackFixer since it is only used in legacy code
- Fixed some spelling in comments(the green underlines were bothering me)
- Removed unused `failed` methods from Utils
  • Loading branch information
KillerOfPie committed May 28, 2022
1 parent 38f3029 commit 7e72192
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,13 @@

package org.shanerx.tradeshop.commands.commandrunners;

import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.shanerx.tradeshop.TradeShop;
import org.shanerx.tradeshop.commands.CommandPass;
import org.shanerx.tradeshop.data.config.Message;
import org.shanerx.tradeshop.data.config.Setting;
import org.shanerx.tradeshop.framework.events.PlayerShopCreateEvent;
import org.shanerx.tradeshop.item.ShopItemSide;
import org.shanerx.tradeshop.player.ShopRole;
import org.shanerx.tradeshop.player.ShopUser;
import org.shanerx.tradeshop.shop.Shop;
import org.shanerx.tradeshop.shop.ShopChest;
import org.shanerx.tradeshop.shop.ShopType;
import org.shanerx.tradeshop.utils.objects.Tuple;

/**
* Implementation of CommandRunner for plugin commands that create new shops
Expand All @@ -61,7 +53,7 @@ public void createTrade() {
if (sign == null)
return;

createShop(sign, ShopType.TRADE);
createShop(sign, pSender, ShopType.TRADE);
}

/**
Expand All @@ -73,7 +65,7 @@ public void createBiTrade() {
if (sign == null)
return;

createShop(sign, ShopType.BITRADE);
createShop(sign, pSender, ShopType.BITRADE);
}

/**
Expand All @@ -85,81 +77,9 @@ public void createITrade() {
if (sign == null)
return;

createShop(sign, ShopType.ITRADE);
createShop(sign, pSender, ShopType.ITRADE);
}


/**
* Create a shop from a non-shop sign in front of the player
*
* @param shopSign sign to make into a shop
* @param shopType type of shop to make
*/
private void createShop(Sign shopSign, ShopType shopType) {
if (ShopType.isShop(shopSign)) {
Message.EXISTING_SHOP.sendMessage(pSender);
return;
}

ShopUser owner = new ShopUser(pSender, ShopRole.OWNER);

if (!checkShopChest(shopSign.getBlock()) && !shopType.isITrade()) {
Message.NO_CHEST.sendMessage(pSender);
return;
}

if (Setting.MAX_SHOPS_PER_CHUNK.getInt() <= plugin.getDataStorage().getShopCountInChunk(shopSign.getChunk())) {
Message.TOO_MANY_CHESTS.sendMessage(pSender);
return;
}

ShopChest shopChest;
Shop shop;
Block chest = findShopChest(shopSign.getBlock());

if (!shopType.isITrade()) {
if (ShopChest.isShopChest(chest)) {
shopChest = new ShopChest(chest.getLocation());
} else {
shopChest = new ShopChest(chest, pSender.getUniqueId(), shopSign.getLocation());
}

if (shopChest.hasOwner() && !shopChest.getOwner().equals(owner.getUUID())) {
Message.NO_SHOP_PERMISSION.sendMessage(pSender);
return;
}

if (shopChest.hasShopSign() && !shopChest.getShopSign().getLocation().equals(shopSign.getLocation())) {
Message.EXISTING_SHOP.sendMessage(pSender);
return;
}

shop = new Shop(new Tuple<>(shopSign.getLocation(), shopChest.getChest().getLocation()), shopType, owner);
shopChest.setName();


if (shopChest.isEmpty() && shop.hasSide(ShopItemSide.PRODUCT)) {
Message.EMPTY_TS_ON_SETUP.sendMessage(pSender);
}
} else {
shop = new Shop(shopSign.getLocation(), shopType, owner);
}

PlayerShopCreateEvent shopCreateEvent = new PlayerShopCreateEvent(pSender, shop);
Bukkit.getPluginManager().callEvent(shopCreateEvent);
if (shopCreateEvent.isCancelled()) {
return;
}

shopSign.setLine(0, shopType.toHeader());
shopSign.update();

shop.saveShop();

Message.SUCCESSFUL_SETUP.sendMessage(pSender);
}


//region Util Methods
//------------------------------------------------------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ public int size() {
return jsonObj.size();
}


/**
* Turns old overstacked itemstacks into individual stacks in a list
*
* @param oldB64 old B64 string to check/fix
* @return new list of ItemStacks
* @deprecated
*/
private List<ShopItemStack> b64OverstackFixer(String oldB64) {
ShopItemStack oldStack = new ShopItemStack(oldB64);

Expand Down
112 changes: 70 additions & 42 deletions src/main/java/org/shanerx/tradeshop/shop/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.shanerx.tradeshop.player.PlayerSetting;
import org.shanerx.tradeshop.player.ShopRole;
import org.shanerx.tradeshop.player.ShopUser;
import org.shanerx.tradeshop.shoplocation.ShopChunk;
import org.shanerx.tradeshop.shoplocation.ShopLocation;
import org.shanerx.tradeshop.utils.Utils;
import org.shanerx.tradeshop.utils.objects.Tuple;
Expand All @@ -63,7 +64,6 @@ public class Shop implements Serializable {
private final ShopLocation shopLoc;
private List<ShopItemStack> product, cost;
private ShopLocation chestLoc;
private transient SignChangeEvent signChangeEvent;
private transient Inventory storageInv;
private transient Utils utils = new Utils();
private ShopStatus status = ShopStatus.INCOMPLETE;
Expand Down Expand Up @@ -168,22 +168,6 @@ public void setStorageInventory() {
storageInv = null;
}

/**
* Adds the sign change event to the shop to be used during sign update
*
* @param event The SignChangeEvent to hold
*/
public void setEvent(SignChangeEvent event) {
this.signChangeEvent = event;
}

/**
* Removes the SignChangeEvent from the shop(Should be done before leaving the event)
*/
public void removeEvent() {
this.signChangeEvent = null;
}

/**
* Returns the storage block as a ShopChest
*
Expand Down Expand Up @@ -296,6 +280,37 @@ public void fixAfterLoad() {
updateSign();
}

/**
* Generates a semi readable output of the shop object for debug output
*
* @return Return String for debug output
*/
public String toDebug() {
StringBuilder sb = new StringBuilder();
sb.append("Shop Debug: \n");
sb.append("Shop Chunk: ").append(new ShopChunk(shopLoc.getChunk()).serialize()).append("\n");
sb.append("Sign Location: ").append(shopLoc.serialize()).append("\n");
sb.append("Shop Type: ").append((isMissingItems() ? Setting.SHOP_INCOMPLETE_COLOUR : Setting.SHOP_GOOD_COLOUR).getString() + shopType.toHeader()).append("\n");
sb.append("Shop Status: ").append(status.getLine()).append("\n");
sb.append("Storage Location: ").append(hasStorage() ? getInventoryLocationAsSL().serialize() : "N/A").append("\n");
sb.append("Storage Type: ").append(hasStorage() ? getStorage().getType().toString() : "N/A").append("\n");
sb.append("Owner: ").append(owner.getName()).append(" | ").append(owner.getUUID()).append("\n");
sb.append("Managers: ").append(managers.isEmpty() ? "N/A" : managers.size()).append("\n");
if (!managers.isEmpty())
getUsers(ShopRole.MANAGER).forEach(manager -> sb.append(" ").append(manager.getName()).append(" | ").append(manager.getUUID()).append("\n"));
sb.append("Members: ").append(members.isEmpty() ? "N/A" : members.size()).append("\n");
if (!members.isEmpty())
getUsers(ShopRole.MEMBER).forEach(member -> sb.append(" ").append(member.getName()).append(" | ").append(member.getUUID()).append("\n"));
sb.append("Products: ").append(product.isEmpty() ? "N/A" : product.size()).append("\n");
if (!product.isEmpty())
product.forEach(productItem -> sb.append(" ").append(productItem.toConsoleText()).append("\n"));
sb.append("Costs: ").append(cost.isEmpty() ? "N/A" : cost.size()).append("\n");
if (!cost.isEmpty())
cost.forEach(costItem -> sb.append(" ").append(costItem.toConsoleText()).append("\n"));

return utils.colorize(sb.toString());
}

/**
* Saves the shop to file
*/
Expand Down Expand Up @@ -324,38 +339,50 @@ public Sign getShopSign() {
* Updates the text on the shops sign
*/
public void updateSign() {
if (signChangeEvent != null) {
updateSign(signChangeEvent);
removeEvent();
} else {
Sign s = getShopSign();
Sign s = getShopSign();

if (s != null) {
String[] signLines = updateSignLines();
if (s == null)
return;

for (int i = 0; i < 4; i++) {
if (signLines[i] != null)
s.setLine(i, signLines[i]);
}
String[] signLines = updateSignLines();

s.update();
}
for (int i = 0; i < 4; i++) {
s.setLine(i, signLines[i]);
}

s.update();
}

/**
* Updates the text on the shops sign during SignChangeEvent
*
* @param signEvent SignEvent to update the sign for
* Updates the text on the shops sign using the events sign if it matches location
*/
public void updateSign(SignChangeEvent signEvent) {
String[] signLines = updateSignLines();
public void updateSign(SignChangeEvent event) {
if (event == null || !event.getBlock().getLocation().equals(getShopLocation()))
return;

for (int i = 0; i < 4; i++) {
signEvent.setLine(i, signLines[i]);
String[] signLines = updateSignLines();

for (int i = 0; i < 4; i++) {
event.setLine(i, signLines[i]);
}
}

/**
* Updates the text on the shops sign using the passed sign if it matches location
*/
public void updateSign(Sign sign) {
if (sign == null || !sign.getLocation().equals(getShopLocation()))
return;

String[] signLines = updateSignLines();

for (int i = 0; i < 4; i++) {
sign.setLine(i, signLines[i]);
}

sign.update();
}

/**
* Updates the text for the shop signs
*
Expand All @@ -364,11 +391,7 @@ public void updateSign(SignChangeEvent signEvent) {
private String[] updateSignLines() {
String[] signLines = new String[4];

if (isMissingItems()) {
signLines[0] = utils.colorize(Setting.SHOP_INCOMPLETE_COLOUR.getString() + shopType.toHeader());
} else {
signLines[0] = utils.colorize(Setting.SHOP_GOOD_COLOUR.getString() + shopType.toHeader());
}
signLines[0] = utils.colorize((isMissingItems() ? Setting.SHOP_INCOMPLETE_COLOUR : Setting.SHOP_GOOD_COLOUR).getString() + shopType.toHeader());

if (product.isEmpty()) {
signLines[1] = "";
Expand Down Expand Up @@ -412,6 +435,11 @@ private String[] updateSignLines() {

signLines[3] = status.getLine();

for (int i = 0; i < 4; i++) {
if (signLines[i] == null)
signLines[i] = " ";
}

return signLines;
}

Expand Down
Loading

0 comments on commit 7e72192

Please sign in to comment.