Skip to content

Commit

Permalink
Bug Fixes
Browse files Browse the repository at this point in the history
- Proper fix for null appearing on signs(was due to the new sign color settings not being loaded)
- Changed serialized value for ObjectHolder so data storage is more readable
- Fix for bad data on a shop sign(That side of the trade will more than likely be removed)
- Dry'd Line formatting on signs for lines 2/3
- Added serialized name for `itemSettings` to properly read in the object with the old `shopSettings` name
- Added method to add ItemStacks to an inventory while temporary setting the inventories Max Stack Size to prevent overstacking items(May add as a shop setting in the future)
- Added updateFullTradeCount and updateSign to what command
  • Loading branch information
KillerOfPie committed Jun 27, 2022
1 parent 3ea9ed0 commit 9233527
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public void what() {
if (shop == null)
return;

shop.updateFullTradeCount();
shop.updateSign();

if (!Permissions.hasPermission(pSender, Permissions.INFO)) {
command.sendMessage(Message.NO_COMMAND_PERMISSION.getPrefixed());
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ public void save() {

switch (configType) {
case CONFIG:
Arrays.stream(SettingSection.values()).sorted(new CompareSettingSections()).forEach((section) -> outputMap.put(section.getPath(), section.getFileString()));
Arrays.stream(Setting.values()).forEach((setting) -> outputMap.put(setting.getSection().getPath(),
outputMap.getOrDefault(setting.getSection().getPath(), "") + setting.getFileString()));
Arrays.stream(SettingSection.values()).sorted(new CompareSettingSections()).forEach((section) -> outputMap.put(section.getPath().toLowerCase().replace("_", "-"), section.getFileString()));
Arrays.stream(Setting.values()).forEach((setting) -> outputMap.put(setting.getSection().getPath().toLowerCase().replace("_", "-"),
outputMap.getOrDefault(setting.getSection().getPath().toLowerCase().replace("_", "-"), "") + setting.getFileString()));
break;
case MESSAGES:
Arrays.stream(MessageSection.values()).sorted(new CompareMessageSections()).forEach((section) -> outputMap.put(section.getPath(), section.getFileString()));
Arrays.stream(Message.values()).forEach((message) -> outputMap.put(message.getSection().getPath(),
outputMap.getOrDefault(message.getSection().getPath(), "") + message.getFileString()));
Arrays.stream(MessageSection.values()).sorted(new CompareMessageSections()).forEach((section) -> outputMap.put(section.getPath().toLowerCase().replace("_", "-"), section.getFileString()));
Arrays.stream(Message.values()).forEach((message) -> outputMap.put(message.getSection().getPath().toLowerCase().replace("_", "-"),
outputMap.getOrDefault(message.getSection().getPath().toLowerCase().replace("_", "-"), "") + message.getFileString()));
break;
}

Expand All @@ -213,9 +213,10 @@ public void save() {
}

private boolean addKeyValue(String node, Object value) {
node = node.toLowerCase().replace("_", "-");
if (value instanceof Map) {
for (Map.Entry entry : ((Map<?, ?>) value).entrySet()) {
String newNode = node + "." + entry.getKey().toString();
String newNode = node + "." + entry.getKey().toString().toLowerCase().replace("_", "-");
if (config.get(newNode) == null || (config.get(newNode) != null && config.get(newNode).toString().isEmpty())) {
config.set(newNode, entry.getValue().toString());
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/shanerx/tradeshop/data/config/Setting.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,15 @@ public Object getDefaultValue() {
}

public String getMappedString(String subKey) {
return PLUGIN.getSettingManager().getConfig().getConfigurationSection(getPath()).getString(subKey.replace("_", "-"));
return PLUGIN.getSettingManager().getConfig().getConfigurationSection(getPath()).getString(subKey.toLowerCase().replace("_", "-"));
}

public boolean getMappedBoolean(String subKey) {
return PLUGIN.getSettingManager().getConfig().getConfigurationSection(getPath()).getBoolean(subKey.replace("_", "-"));
return PLUGIN.getSettingManager().getConfig().getConfigurationSection(getPath()).getBoolean(subKey.toLowerCase().replace("_", "-"));
}

public Object getMappedObject(String subKey) {
return PLUGIN.getSettingManager().getConfig().getConfigurationSection(getPath()).get(subKey.replace("_", "-"));
return PLUGIN.getSettingManager().getConfig().getConfigurationSection(getPath()).get(subKey.toLowerCase().replace("_", "-"));
}

public String getPostComment() {
Expand Down
32 changes: 15 additions & 17 deletions src/main/java/org/shanerx/tradeshop/item/ShopItemStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import net.md_5.bungee.api.ChatColor;
import org.apache.commons.lang.WordUtils;
import org.bukkit.FireworkEffect;
Expand Down Expand Up @@ -65,32 +66,30 @@ public class ShopItemStack implements Serializable, Cloneable {

private String itemStackB64;

@SerializedName(value = "itemSettings", alternate = "shopSettings")
private Map<ShopItemStackSettingKeys, ObjectHolder<?>> itemSettings;

public ShopItemStack(ItemStack itemStack) {
this.itemStack = itemStack;
itemSettings = new HashMap<>();
buildMap();
toBase64();
this(itemStack, new HashMap<>());
}

public ShopItemStack(ItemStack itemStack, HashMap<ShopItemStackSettingKeys, ObjectHolder<?>> settingMap) {
public ShopItemStack(ItemStack itemStack, HashMap<ShopItemStackSettingKeys, ObjectHolder<?>> itemSettings) {
this.itemStack = itemStack;
this.itemSettings = settingMap;
this.itemSettings = itemSettings;
buildMap();
toBase64();
}

public ShopItemStack(String itemStackB64) {
this.itemStackB64 = itemStackB64;
itemSettings = new HashMap<>();
buildMap();
fromBase64();
this(itemStackB64, new HashMap<>());
}

public ShopItemStack(String itemStackB64, HashMap<ShopItemStackSettingKeys, ObjectHolder<?>> settingMap) {
public ShopItemStack(String itemStackB64, HashMap<String, ObjectHolder<?>> itemSettings) {
this.itemStackB64 = itemStackB64;
this.itemSettings = settingMap;
this.itemSettings = new HashMap<>();

itemSettings.forEach((key, value) -> this.itemSettings.put(ShopItemStackSettingKeys.match(key), value));

buildMap();
fromBase64();
}
Expand Down Expand Up @@ -125,6 +124,10 @@ public Map<ShopItemStackSettingKeys, ObjectHolder<?>> getItemSettings() {
return itemSettings;
}

public String serialize() {
return new Gson().toJson(this);
}

public static ShopItemStack deserialize(String serialized) {
ShopItemStack item = new Gson().fromJson(serialized, ShopItemStack.class);
item.fromBase64();
Expand Down Expand Up @@ -555,10 +558,6 @@ public String getStateString(ObjectHolder<?> stateSetting) {
}
}

public String serialize() {
return new Gson().toJson(this);
}

@Override
public String toString() {
return "ShopItemStack{" +
Expand Down Expand Up @@ -613,5 +612,4 @@ private void fromBase64() {
public String toConsoleText() {
return new GsonBuilder().setPrettyPrinting().create().toJson(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,8 @@ public boolean isUserEditable() {
public String getConfigName() {
return name().toLowerCase().replace("_", "-");
}

public static ShopItemStackSettingKeys match(String name) {
return valueOf(name.toUpperCase().replace("-", "_"));
}
}
123 changes: 71 additions & 52 deletions src/main/java/org/shanerx/tradeshop/shop/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@

import com.google.gson.Gson;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Container;
Expand All @@ -52,6 +54,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -271,7 +274,7 @@ public ShopLocation getInventoryLocationAsSL() {
}

/**
* Fixes values that cannot be serialized after loading
* Fixes values and objects after loading or creating a Shop
*/
public void fixAfterLoad() {
if (utils == null)
Expand All @@ -286,6 +289,9 @@ public void fixAfterLoad() {

setShopSettings();

fixSide(ShopItemSide.COST);
fixSide(ShopItemSide.PRODUCT);

if (getShopSign() != null)
updateSign();
}
Expand Down Expand Up @@ -415,36 +421,17 @@ private String[] updateSignLines(String defaultColour) {
if (product.isEmpty()) {
signLines[1] = "";
} else if (product.size() == 1) {
StringBuilder sb = new StringBuilder();

ShopItemStack item = product.get(0);

sb.append(item.getItemStack().getAmount());
sb.append(" ");

sb.append(item.getCleanItemName());

signLines[1] = utils.colorize(defaultColour + sb.substring(0, Math.min(sb.length(), 15)));

signLines[1] = itemLineFormatter(defaultColour, String.valueOf(product.get(0).getItemStack().getAmount()), " ", product.get(0).getCleanItemName());
} else {
signLines[1] = utils.colorize(defaultColour + Setting.MULTIPLE_ITEMS_ON_SIGN.getString().replace("%amount%", ""));
}
signLines[1] = itemLineFormatter(defaultColour, Setting.MULTIPLE_ITEMS_ON_SIGN.getString().replace("%amount%", ""));
}

if (cost.isEmpty()) {
signLines[2] = "";
} else if (cost.size() == 1) {
StringBuilder sb = new StringBuilder();

ShopItemStack item = cost.get(0);

sb.append(item.getItemStack().getAmount());
sb.append(" ");

sb.append(item.getCleanItemName());

signLines[2] = utils.colorize(defaultColour + sb.substring(0, Math.min(sb.length(), 15)));
signLines[2] = itemLineFormatter(defaultColour, String.valueOf(cost.get(0).getItemStack().getAmount()), " ", cost.get(0).getCleanItemName());
} else {
signLines[2] = utils.colorize(defaultColour + Setting.MULTIPLE_ITEMS_ON_SIGN.getString());
signLines[2] = itemLineFormatter(defaultColour, Setting.MULTIPLE_ITEMS_ON_SIGN.getString().replace("%amount%", ""));
}

updateStatus();
Expand All @@ -459,6 +446,18 @@ private String[] updateSignLines(String defaultColour) {
return signLines;
}

private String itemLineFormatter(String defaultColour, String... strings) {
StringBuilder sb = new StringBuilder();

for (String s : strings) {
sb.append(s);
}

String colorFix = ChatColor.stripColor(utils.colorize(sb.toString()));

return utils.colorize(defaultColour + colorFix.substring(0, Math.min(colorFix.length(), 15)));
}

/**
* Returns the shops inventory as a BlockState
*
Expand Down Expand Up @@ -955,6 +954,34 @@ private void purgeFromUserFiles() {
//region Item Management - Methods for adding/deleting/updating/viewing Cost and Product lists
//------------------------------------------------------------------------------------------------------------------

/**
* Checks if shop has a bade side and fixes if necessary
*/
public void fixSide(ShopItemSide side) {
List<ShopItemStack> ogItems = (side.equals(ShopItemSide.PRODUCT) ? product : cost);

Set<Material> matSet = new HashSet<>();

ogItems.forEach((item) -> matSet.add(item.getItemStack().getType()));


if (ogItems.size() > 1 && ogItems.size() != matSet.size()) {
List<ShopItemStack> scrapList = new ArrayList<>(ogItems);
(side.equals(ShopItemSide.PRODUCT) ? product : cost).clear();

ogItems.forEach((item) -> {
while (scrapList.contains(item)) {
addSideItem(side, item);
scrapList.remove(scrapList.lastIndexOf(item));
}
});

updateFullTradeCount();
updateSign();
saveShop();
}
}

/**
* Checks if shop has items on specified side
*
Expand Down Expand Up @@ -1065,50 +1092,32 @@ public void setSideItems(ShopItemSide side, ItemStack newItem) {
addSideItem(side, newItem);
}

/**
* Attempts to fix the side of the trade in cases where an error was found.
*
* @param side Side of the trade to fix
*/
public void fixSide(ShopItemSide side) {
getSide(side).clear();
try {
getSideItemStacks(side).forEach((item) -> {
if (item != null) addSideItem(side, item);
});
} catch (NullPointerException ignored) {
getSide(side).clear();
}
}

/**
* Adds more items to the specified side
*
* @param side Side to add items to
* @param newItem ItemStack to be added
* @param side Side to add items to
* @param newShopItem ShopItemStack to be added
*/
public void addSideItem(ShopItemSide side, ItemStack newItem) {
if (utils.isIllegal(side, newItem.getType()))
public void addSideItem(ShopItemSide side, ShopItemStack newShopItem) {
if (utils.isIllegal(side, newShopItem.getItemStack().getType()))
return;

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


for (int i = 0; i < getSide(side).size(); i++) {
if (getSideList(side).get(i).getItemStack().getType().equals(newItem.getType()) && getSideList(side).get(i).isSimilar(newItem)) {
if (getSideList(side).get(i).getItemStack().getType().equals(newShopItem.getItemStack().getType()) && getSideList(side).get(i).isSimilar(newShopItem.getItemStack())) {
toRemoveShopItemStack = i;
toAddShopItemStack = getSideList(side).get(i).clone();
toAddShopItemStack.setAmount(getSideList(side).get(i).getAmount() + newItem.getAmount());
newShopItem = getSideList(side).get(i).clone();
newShopItem.setAmount(getSideList(side).get(i).getAmount() + newShopItem.getItemStack().getAmount());
break;
}
}

if (toRemoveShopItemStack > -1)
getSide(side).remove(toRemoveShopItemStack);

getSide(side).add(toAddShopItemStack);
getSide(side).add(newShopItem);
//*/


Expand All @@ -1119,11 +1128,21 @@ public void addSideItem(ShopItemSide side, ItemStack newItem) {
updateSign();
}

/**
* Adds more items to the specified side
*
* @param side Side to add items to
* @param newItem ItemStack to be added
*/
public void addSideItem(ShopItemSide side, ItemStack newItem) {
addSideItem(side, new ShopItemStack(newItem));
}

/**
* Checks if the shop has sufficient stock to make a trade on specified side
*/
public boolean hasSideStock(ShopItemSide side) {
return !shopType.isITrade() && hasSide(side) && getChestAsSC() != null && getChestAsSC().hasStock(side, getSideList(side));
return !shopType.isITrade() && hasSide(side) && getChestAsSC() != null && getChestAsSC().hasStock(getSideList(side));
}

/**
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/org/shanerx/tradeshop/shop/ShopChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataType;
import org.shanerx.tradeshop.TradeShop;
import org.shanerx.tradeshop.item.ShopItemSide;
import org.shanerx.tradeshop.item.ShopItemStack;
import org.shanerx.tradeshop.shoplocation.IllegalWorldException;
import org.shanerx.tradeshop.shoplocation.ShopLocation;
Expand Down Expand Up @@ -209,13 +208,8 @@ public Inventory getInventory() {
return null;
}

public boolean hasStock(ShopItemSide side, List<ShopItemStack> itemToCheck) {
List<ItemStack> result = getItems(getInventory().getStorageContents(), itemToCheck, 1);
if (result.get(0) == null && result.size() == 1) {
getShop().fixSide(side);
}

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

public void loadFromName() {
Expand Down
Loading

0 comments on commit 9233527

Please sign in to comment.