Skip to content

Commit

Permalink
Feature Add and Bug Fixes
Browse files Browse the repository at this point in the history
- Added configurable colours for the middle lines on trade sides per material type #135
- Removed Message call in CommandRunner that should never be used and would never actually send if it was.
- Added SET_MANAGER and SET_MEMBER commands to the autofill for player names
- Added Config support for map values so individual setting enums are not required for settings that may change(like sign types), This should automatically add new items to existing maps as well
- Changed SettingSection weights to start at 50 increments based on nest depth to make them more easily readable
- Changed Member and Manager Lists into Sets to prevent Duplicates(hopefully)
- Added Shop#saveShop with a boolean parameter so that conditional saves don't need a separate if statement
- Removed Wall sign types since each sign type is handled the same whether it is on a wall or not
- Adjusted sign code to properly handle above
- Fixed error in ShoptradeListener where shops in the OOS state but without a product would try to check on stock.
  • Loading branch information
KillerOfPie committed Jun 13, 2022
1 parent 7e72192 commit 37f8891
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public List<String> onTabComplete(CommandSender sender, Command cmd, String labe
return tabCompleter.fillShopPlayer();
case ADD_MANAGER:
case ADD_MEMBER:
case SET_MEMBER:
case SET_MANAGER:
case PLAYER_LEVEL:
return tabCompleter.fillServerPlayer();
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public CommandRunner(TradeShop instance, CommandPass command) {
*/
protected Shop findShop() {
if (pSender == null) {
Message.PLAYER_ONLY_COMMAND.sendMessage(pSender);
return null;
}

Expand Down
18 changes: 13 additions & 5 deletions src/main/java/org/shanerx/tradeshop/data/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;

public class ConfigManager {
Expand Down Expand Up @@ -154,13 +155,13 @@ 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((section) -> outputMap.put(section.getSection().getPath(),
outputMap.getOrDefault(section.getSection().getPath(), "") + section.getFileString()));
Arrays.stream(Setting.values()).forEach((setting) -> outputMap.put(setting.getSection().getPath(),
outputMap.getOrDefault(setting.getSection().getPath(), "") + 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((section) -> outputMap.put(section.getSection().getPath(),
outputMap.getOrDefault(section.getSection().getPath(), "") + section.getFileString()));
Arrays.stream(Message.values()).forEach((message) -> outputMap.put(message.getSection().getPath(),
outputMap.getOrDefault(message.getSection().getPath(), "") + message.getFileString()));
break;
}

Expand All @@ -181,7 +182,14 @@ public void save() {
}

private void addKeyValue(String node, Object value) {
if (config.get(node) == null || (config.get(node) != null && config.get(node).toString().isEmpty())) {
if (value instanceof Map) {
for (Map.Entry entry : ((Map<?, ?>) value).entrySet()) {
String newNode = node + "." + entry.getKey().toString();
if (config.get(newNode) == null || (config.get(newNode) != null && config.get(newNode).toString().isEmpty())) {
config.set(newNode, entry.getValue().toString());
}
}
} else if (config.get(node) == null || (config.get(node) != null && config.get(node).toString().isEmpty())) {
config.set(node, value);
}
}
Expand Down
31 changes: 27 additions & 4 deletions src/main/java/org/shanerx/tradeshop/data/config/Setting.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@
import org.bukkit.Bukkit;
import org.shanerx.tradeshop.TradeShop;
import org.shanerx.tradeshop.item.IllegalItemList;
import org.shanerx.tradeshop.shop.ShopSign;
import org.yaml.snakeyaml.Yaml;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

public enum Setting {

Expand Down Expand Up @@ -81,7 +82,8 @@ public enum Setting {
ALLOW_USER_PURCHASING(SettingSection.SHOP_OPTIONS, "allow-user-purchasing", false),
MULTIPLE_ITEMS_ON_SIGN(SettingSection.SHOP_OPTIONS, "multiple-items-on-sign", "Use '/ts what'"),

//Shop Item Default Setting Options
//region Shop Item Settings
//------------------------------------------------------------------------------------------------------------------
COMPARE_DURABILITY_DEFAULT(SettingSection.COMPARE_DURABILITY, "default", 1),
COMPARE_ENCHANTMENTS_DEFAULT(SettingSection.COMPARE_ENCHANTMENTS, "default", true),
COMPARE_NAME_DEFAULT(SettingSection.COMPARE_NAME, "default", true),
Expand Down Expand Up @@ -113,6 +115,16 @@ public enum Setting {
COMPARE_FIREWORK_DURATION_USER_EDITABLE(SettingSection.COMPARE_FIREWORK_DURATION, "user-editable", true),
COMPARE_FIREWORK_EFFECTS_USER_EDITABLE(SettingSection.COMPARE_FIREWORK_EFFECTS, "user-editable", true),

//------------------------------------------------------------------------------------------------------------------
//endregion

//region Shop Sign Settings
//------------------------------------------------------------------------------------------------------------------
SHOP_SIGN_DEFAULT_COLOURS(SettingSection.SHOP_SIGN_OPTIONS, "sign-default-colours", ShopSign.getDefaultColourMap()),

//------------------------------------------------------------------------------------------------------------------
//endregion

// Trade Shop Options
TRADESHOP_HEADER(SettingSection.TRADE_SHOP_OPTIONS, "header", "Trade"),
TRADESHOP_EXPLODE(SettingSection.TRADE_SHOP_OPTIONS, "allow-explode", false),
Expand Down Expand Up @@ -140,7 +152,7 @@ public enum Setting {
PRODUCT_ILLEGAL_ITEMS_TYPE(SettingSection.PRODUCT_ILLEGAL_ITEMS, "type", IllegalItemList.ListType.DISABLED.toString()),
PRODUCT_ILLEGAL_ITEMS_LIST(SettingSection.PRODUCT_ILLEGAL_ITEMS, "list", new String[]{});

public static final TradeShop PLUGIN = Objects.requireNonNull((TradeShop) Bukkit.getPluginManager().getPlugin("TradeShop"));
public static final TradeShop PLUGIN = (TradeShop) Bukkit.getPluginManager().getPlugin("TradeShop");

private final String key, path;
private final Object defaultValue;
Expand Down Expand Up @@ -239,6 +251,10 @@ public Object getDefaultValue() {
return defaultValue;
}

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

public String getPostComment() {
return PLUGIN.getLanguage().getPostComment(Language.LangSection.SETTING, path);
}
Expand All @@ -262,7 +278,14 @@ public String getFileString() {
keyOutput.append(section.getSectionLead()).append("# ").append(PLUGIN.getSettingManager().fixCommentNewLines(section.getSectionLead(), getPreComment())).append("\n");
}

keyOutput.append(section.getSectionLead()).append(getKey()).append(": ").append(new Yaml().dump(getSetting()));
if (defaultValue instanceof Map) {
keyOutput.append(section.getSectionLead()).append(getKey()).append(":\n");
for (Map.Entry entry : ((Map<?, ?>) defaultValue).entrySet()) {
keyOutput.append(section.getSectionLead() + " ").append(entry.getKey().toString()).append(": ").append(new Yaml().dump(entry.getValue()));
}
} else {
keyOutput.append(section.getSectionLead()).append(getKey()).append(": ").append(new Yaml().dump(getSetting()));
}

if (!getPostComment().isEmpty()) {
if (getPostComment().equals(" ") || getPostComment().equals("\n"))
Expand Down
47 changes: 28 additions & 19 deletions src/main/java/org/shanerx/tradeshop/data/config/SettingSection.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,44 @@

public enum SettingSection {

//Weight for primary Sections should start at 0 and each sub-section should start at increments of 50

NONE(0, ""),
SYSTEM_OPTIONS(1, "system-options"),
LANGUAGE_OPTIONS(2, "language-options"),
GLOBAL_OPTIONS(3, "global-options"),
GLOBAL_MULTI_TRADE(0, GLOBAL_OPTIONS, "multi-trade"),
GLOBAL_MULTI_TRADE(50, GLOBAL_OPTIONS, "multi-trade"),
SHOP_OPTIONS(4, "shop-options"),
SHOP_ITEM_DEFAULT_SETTINGS_OPTIONS(0, SHOP_OPTIONS, "shop-item-default-settings-options"),
COMPARE_DURABILITY(0, SHOP_ITEM_DEFAULT_SETTINGS_OPTIONS, "compare-durability"),
COMPARE_ENCHANTMENTS(1, SHOP_ITEM_DEFAULT_SETTINGS_OPTIONS, "compare-enchantments"),
COMPARE_NAME(2, SHOP_ITEM_DEFAULT_SETTINGS_OPTIONS, "compare-name"),
COMPARE_LORE(3, SHOP_ITEM_DEFAULT_SETTINGS_OPTIONS, "compare-lore"),
COMPARE_CUSTOM_MODEL_DATA(4, SHOP_ITEM_DEFAULT_SETTINGS_OPTIONS, "compare-custom-data-model"),
COMPARE_ITEM_FLAGS(5, SHOP_ITEM_DEFAULT_SETTINGS_OPTIONS, "compare-item-flags"),
COMPARE_UNBREAKABLE(6, SHOP_ITEM_DEFAULT_SETTINGS_OPTIONS, "compare-unbreakable"),
COMPARE_ATTRIBUTE_MODIFIER(7, SHOP_ITEM_DEFAULT_SETTINGS_OPTIONS, "compare-attribute-modifier"),
COMPARE_BOOK_AUTHOR(8, SHOP_ITEM_DEFAULT_SETTINGS_OPTIONS, "compare-book-author"),
COMPARE_BOOK_PAGES(9, SHOP_ITEM_DEFAULT_SETTINGS_OPTIONS, "compare-book-pages"),
COMPARE_SHULKER_INVENTORY(10, SHOP_ITEM_DEFAULT_SETTINGS_OPTIONS, "compare-shulker-inventory"),
COMPARE_BUNDLE_INVENTORY(11, SHOP_ITEM_DEFAULT_SETTINGS_OPTIONS, "compare-bundle-inventory"),
COMPARE_FIREWORK_DURATION(12, SHOP_ITEM_DEFAULT_SETTINGS_OPTIONS, "compare-firework-duration"),
COMPARE_FIREWORK_EFFECTS(13, SHOP_ITEM_DEFAULT_SETTINGS_OPTIONS, "compare-firework-effects"),

//region Shop Item Settings
//------------------------------------------------------------------------------------------------------------------
SHOP_ITEM_SETTINGS(50, SHOP_OPTIONS, "shop-item-default-settings-options"),
COMPARE_DURABILITY(100, SHOP_ITEM_SETTINGS, "compare-durability"),
COMPARE_ENCHANTMENTS(101, SHOP_ITEM_SETTINGS, "compare-enchantments"),
COMPARE_NAME(102, SHOP_ITEM_SETTINGS, "compare-name"),
COMPARE_LORE(103, SHOP_ITEM_SETTINGS, "compare-lore"),
COMPARE_CUSTOM_MODEL_DATA(104, SHOP_ITEM_SETTINGS, "compare-custom-data-model"),
COMPARE_ITEM_FLAGS(105, SHOP_ITEM_SETTINGS, "compare-item-flags"),
COMPARE_UNBREAKABLE(106, SHOP_ITEM_SETTINGS, "compare-unbreakable"),
COMPARE_ATTRIBUTE_MODIFIER(107, SHOP_ITEM_SETTINGS, "compare-attribute-modifier"),
COMPARE_BOOK_AUTHOR(108, SHOP_ITEM_SETTINGS, "compare-book-author"),
COMPARE_BOOK_PAGES(109, SHOP_ITEM_SETTINGS, "compare-book-pages"),
COMPARE_SHULKER_INVENTORY(110, SHOP_ITEM_SETTINGS, "compare-shulker-inventory"),
COMPARE_BUNDLE_INVENTORY(111, SHOP_ITEM_SETTINGS, "compare-bundle-inventory"),
COMPARE_FIREWORK_DURATION(112, SHOP_ITEM_SETTINGS, "compare-firework-duration"),
COMPARE_FIREWORK_EFFECTS(113, SHOP_ITEM_SETTINGS, "compare-firework-effects"),
//------------------------------------------------------------------------------------------------------------------
//endregion

SHOP_SIGN_OPTIONS(5, "shop-sign-options"),

TRADE_SHOP_OPTIONS(6, "trade-shop-options"),
ITRADE_SHOP_OPTIONS(7, "itrade-shop-options"),
BITRADE_SHOP_OPTIONS(8, "bitrade-shop-options"),
ILLEGAL_ITEM_OPTIONS(9, "illegal-item-options"),
GLOBAL_ILLEGAL_ITEMS(0, ILLEGAL_ITEM_OPTIONS, "global-illegal-items"),
COST_ILLEGAL_ITEMS(1, ILLEGAL_ITEM_OPTIONS, "cost-illegal-items"),
PRODUCT_ILLEGAL_ITEMS(2, ILLEGAL_ITEM_OPTIONS, "product-illegal-items");
GLOBAL_ILLEGAL_ITEMS(50, ILLEGAL_ITEM_OPTIONS, "global-illegal-items"),
COST_ILLEGAL_ITEMS(51, ILLEGAL_ITEM_OPTIONS, "cost-illegal-items"),
PRODUCT_ILLEGAL_ITEMS(52, ILLEGAL_ITEM_OPTIONS, "product-illegal-items");

public static final TradeShop PLUGIN = Objects.requireNonNull((TradeShop) Bukkit.getPluginManager().getPlugin("TradeShop"));

Expand Down
53 changes: 31 additions & 22 deletions src/main/java/org/shanerx/tradeshop/shop/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
public class Shop implements Serializable {

private ShopUser owner;
private List<UUID> managers, members;
private Set<UUID> managers, members;
private ShopType shopType;
private final ShopLocation shopLoc;
private List<ShopItemStack> product, cost;
Expand All @@ -79,7 +79,7 @@ public class Shop implements Serializable {
* @param items Items to go into the shop as Tuple, left = Product, right = Cost
* @param players Users to be added to the shop as Tuple, left = Managers, right = Members
*/
public Shop(Tuple<Location, Location> locations, ShopType shopType, ShopUser owner, Tuple<List<UUID>, List<UUID>> players, Tuple<ItemStack, ItemStack> items) {
public Shop(Tuple<Location, Location> locations, ShopType shopType, ShopUser owner, Tuple<Set<UUID>, Set<UUID>> players, Tuple<ItemStack, ItemStack> items) {
shopLoc = new ShopLocation(locations.getLeft());
this.owner = owner;

Expand All @@ -90,8 +90,8 @@ public Shop(Tuple<Location, Location> locations, ShopType shopType, ShopUser own

this.shopType = shopType;

managers = players.getLeft() == null ? Collections.emptyList() : players.getLeft();
members = players.getRight() == null ? Collections.emptyList() : players.getRight();
managers = players.getLeft() == null ? Collections.emptySet() : players.getLeft();
members = players.getRight() == null ? Collections.emptySet() : players.getRight();

product = new ArrayList<>();
cost = new ArrayList<>();
Expand Down Expand Up @@ -320,6 +320,15 @@ public void saveShop() {
updateUserFiles();
}

/**
* Saves the shop to file if passed boolean is true
*
* @param shouldSave true if save should proceed
*/
public void saveShop(boolean shouldSave) {
if (shouldSave) saveShop();
}

/**
* Returns the shops sign as a Sign
*
Expand All @@ -344,7 +353,7 @@ public void updateSign() {
if (s == null)
return;

String[] signLines = updateSignLines();
String[] signLines = updateSignLines(Setting.SHOP_SIGN_DEFAULT_COLOURS.getMappedString(Signs.match(s.getType()).name()));

for (int i = 0; i < 4; i++) {
s.setLine(i, signLines[i]);
Expand All @@ -360,7 +369,7 @@ public void updateSign(SignChangeEvent event) {
if (event == null || !event.getBlock().getLocation().equals(getShopLocation()))
return;

String[] signLines = updateSignLines();
String[] signLines = updateSignLines(Setting.SHOP_SIGN_DEFAULT_COLOURS.getMappedString(Signs.match(event.getBlock().getType()).name()));

for (int i = 0; i < 4; i++) {
event.setLine(i, signLines[i]);
Expand All @@ -374,7 +383,7 @@ public void updateSign(Sign sign) {
if (sign == null || !sign.getLocation().equals(getShopLocation()))
return;

String[] signLines = updateSignLines();
String[] signLines = updateSignLines(Setting.SHOP_SIGN_DEFAULT_COLOURS.getMappedString(Signs.match(sign.getType()).name()));

for (int i = 0; i < 4; i++) {
sign.setLine(i, signLines[i]);
Expand All @@ -383,12 +392,12 @@ public void updateSign(Sign sign) {
sign.update();
}

/**
* Updates the text for the shop signs
*
* @return String array containing updated sign lines to be set
*/
private String[] updateSignLines() {
/**
* Updates the text for the shop signs
*
* @return String array containing updated sign lines to be set
*/
private String[] updateSignLines(String defaultColour) {
String[] signLines = new String[4];

signLines[0] = utils.colorize((isMissingItems() ? Setting.SHOP_INCOMPLETE_COLOUR : Setting.SHOP_GOOD_COLOUR).getString() + shopType.toHeader());
Expand All @@ -405,10 +414,10 @@ private String[] updateSignLines() {

sb.append(item.getCleanItemName());

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

} else {
signLines[1] = Setting.MULTIPLE_ITEMS_ON_SIGN.getString().replace("%amount%", "");
} else {
signLines[1] = utils.colorize(defaultColour + Setting.MULTIPLE_ITEMS_ON_SIGN.getString().replace("%amount%", ""));
}

if (cost.isEmpty()) {
Expand All @@ -428,8 +437,8 @@ private String[] updateSignLines() {
signLines[2] = Setting.MULTIPLE_ITEMS_ON_SIGN.getString();
}

signLines[1] = ChatColor.stripColor(signLines[1]);
signLines[2] = ChatColor.stripColor(signLines[2]);
signLines[1] = utils.colorize(defaultColour + ChatColor.stripColor(signLines[1]));
signLines[2] = utils.colorize(defaultColour + ChatColor.stripColor(signLines[2]));

updateStatus();

Expand Down Expand Up @@ -742,7 +751,7 @@ public boolean addUser(UUID newUser, ShopRole role) {
}
}

if (ret) saveShop();
saveShop(ret);

return ret;
}
Expand Down Expand Up @@ -794,7 +803,7 @@ public boolean removeUser(UUID oldUser) {
ret = true;
}

saveShop();
saveShop(ret);

return ret;
}
Expand All @@ -804,7 +813,7 @@ public boolean removeUser(UUID oldUser) {
*
* @param users the managers to be set to the shop
*/
public boolean setUsers(List<UUID> users, ShopRole role) {
public boolean setUsers(Set<UUID> users, ShopRole role) {
boolean ret = false;
switch (role) {
case MANAGER:
Expand All @@ -817,7 +826,7 @@ public boolean setUsers(List<UUID> users, ShopRole role) {
ret = true;
}

if (ret) saveShop();
saveShop(ret);

return ret;
}
Expand Down
Loading

0 comments on commit 37f8891

Please sign in to comment.