Skip to content

Commit

Permalink
Bug Fixes
Browse files Browse the repository at this point in the history
- Added conditional saving for Setting and Message files
- Added thorough change detection for setting defaults and upgrades to prevent unnecessary saves and fix bug preventing changes to default sign colours to be changed from their default values
- Fixed bug preventing setUser commands from working due to failed removal(which wasn't necessary for the set to process)
- Added missing return in ShopTradeListener which caused `OOS` code to run after `Incomplete` code in a switch
  • Loading branch information
KillerOfPie committed Jun 13, 2022
1 parent 37f8891 commit b4d1b8f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 18 deletions.
42 changes: 32 additions & 10 deletions src/main/java/org/shanerx/tradeshop/data/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
Expand All @@ -41,8 +42,10 @@
import java.io.Writer;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;

public class ConfigManager {
Expand Down Expand Up @@ -78,7 +81,7 @@ public String colour(String toColour) {
return ChatColor.translateAlternateColorCodes('&', toColour);
}

public void load() {
public boolean load() {
try {
if (!PLUGIN.getDataFolder().isDirectory()) {
PLUGIN.getDataFolder().mkdirs();
Expand All @@ -93,23 +96,24 @@ public void load() {

config = YamlConfiguration.loadConfiguration(file);

setDefaults();
return setDefaults();
}

public void reload() {
load();
Set<Boolean> hasUpgraded = new HashSet<>();
hasUpgraded.add(load());

switch (configType) {
case CONFIG:
Setting.upgrade();
hasUpgraded.add(Setting.upgrade());
PLUGIN.setUseInternalPerms(Setting.USE_INTERNAL_PERMISSIONS.getBoolean());
break;
case MESSAGES:
Message.upgrade();
hasUpgraded.add(Message.upgrade());
break;
}

save();
save(hasUpgraded.contains(true));

PLUGIN.setSkipHopperProtection(
Setting.BITRADESHOP_HOPPER_EXPORT.getBoolean() &&
Expand All @@ -118,15 +122,28 @@ public void reload() {
Setting.TRADESHOP_HOPPER_EXPORT.getBoolean());
}

public void setDefaults() {
public boolean setDefaults() {
Set<Boolean> hasUpgraded = new HashSet<>();

switch (configType) {
case CONFIG:
Arrays.stream(Setting.values()).forEach((setting) -> addKeyValue(setting.getPath(), setting.getDefaultValue()));
Arrays.stream(Setting.values()).forEach((setting) -> hasUpgraded.add(addKeyValue(setting.getPath(), setting.getDefaultValue())));
break;
case MESSAGES:
Arrays.stream(Message.values()).forEach((message) -> addKeyValue(message.getPath(), message.getDefaultValue()));
Arrays.stream(Message.values()).forEach((message) -> hasUpgraded.add(addKeyValue(message.getPath(), message.getDefaultValue())));
break;
}

return hasUpgraded.contains(true);
}

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

public void save() {
Expand Down Expand Up @@ -181,17 +198,22 @@ public void save() {
config = YamlConfiguration.loadConfiguration(file);
}

private void addKeyValue(String node, Object value) {
private boolean addKeyValue(String node, Object value) {
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())) {
Bukkit.getLogger().log(Level.WARNING, "TS reload issue ->\n" + newNode + " = " + config.get(newNode).toString() + " | " + entry.getValue().toString());
config.set(newNode, entry.getValue().toString());
return true;
}
}
} else if (config.get(node) == null || (config.get(node) != null && config.get(node).toString().isEmpty())) {
config.set(node, value);
return true;
}

return false;
}

public enum ConfigType {
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/org/shanerx/tradeshop/data/config/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
import org.yaml.snakeyaml.Yaml;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -117,13 +119,15 @@ public enum Message {
}

// Method to fix any values that have changed with updates
static void upgrade() {
static boolean upgrade() {
double version = MESSAGE_VERSION.getDouble();
Set<Boolean> hasUpgraded = new HashSet<>(); // Uses this instead of a boolean to later replace below ifs with boolean return methods...

//Changes if CONFIG_VERSION is below 1.1, then update to 1.1
if (checkVersion(version, 1.1)) {
if (TOO_MANY_ITEMS.getString().equals("&cThis trade can not take any more %side%!")) {
TOO_MANY_ITEMS.setValue(PLUGIN.getLanguage().getDefault(Language.LangSection.MESSAGE, TOO_MANY_ITEMS.getPath()));
hasUpgraded.add(true);
}
version = 1.1;
}
Expand All @@ -133,8 +137,10 @@ static void upgrade() {
Arrays.stream(values()).forEach((message) -> {
String str = message.getString().replace("{", "%").replace("}", "%");

if (!str.equals(message.getString()))
if (!str.equals(message.getString())) {
message.setValue(str);
hasUpgraded.add(true);
}

});
version = 1.2;
Expand All @@ -144,19 +150,24 @@ static void upgrade() {
if (checkVersion(version, 1.3)) {
if (INSUFFICIENT_ITEMS.getString().equals("&cYou do not have &e%AMOUNT% %ITEM%&c!")) {
INSUFFICIENT_ITEMS.setValue(PLUGIN.getLanguage().getDefault(Language.LangSection.MESSAGE, INSUFFICIENT_ITEMS.getPath()));
hasUpgraded.add(true);
}
if (SHOP_INSUFFICIENT_ITEMS.getString().equals("&cThis shop does not have enough &e%AMOUNT% %ITEM%&c to trade!")) {
SHOP_INSUFFICIENT_ITEMS.setValue(PLUGIN.getLanguage().getDefault(Language.LangSection.MESSAGE, SHOP_INSUFFICIENT_ITEMS.getPath()));
hasUpgraded.add(true);
}
if (ON_TRADE.getString().equals("&aYou have traded your &e%AMOUNT2% %ITEM2% &afor &e%AMOUNT1% %ITEM1% &awith %SELLER%")) {
ON_TRADE.setValue(PLUGIN.getLanguage().getDefault(Language.LangSection.MESSAGE, ON_TRADE.getPath()));
hasUpgraded.add(true);
}


version = 1.3;
}

MESSAGE_VERSION.setValue(version != 0.0 ? version : 1.3);

return hasUpgraded.contains(true);
}

private static boolean checkVersion(double version, double maxVersion) {
Expand Down
30 changes: 27 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 @@ -32,8 +32,10 @@
import org.yaml.snakeyaml.Yaml;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public enum Setting {

Expand Down Expand Up @@ -170,8 +172,9 @@ public static Setting findSetting(String search) {
}

// Method to fix any values that have changed with updates
static void upgrade() {
static boolean upgrade() {
double version = CONFIG_VERSION.getDouble();
Set<Boolean> hasUpgraded = new HashSet<>(); // Uses this instead of a boolean to later replace below ifs with boolean return methods...
ConfigManager configManager = PLUGIN.getSettingManager();

// 2.2.2 Changed enable debug from true/false to integer
Expand All @@ -184,46 +187,55 @@ static void upgrade() {
if (configManager.getConfig().contains("itradeshop.owner")) {
configManager.getConfig().set(ITRADESHOP_OWNER.path, configManager.getConfig().get("itradeshop.owner"));
configManager.getConfig().set("itradeshop.owner", null);
hasUpgraded.add(true);
}

if (configManager.getConfig().contains("itradeshop.header")) {
configManager.getConfig().set(ITRADESHOP_HEADER.path, configManager.getConfig().get("itradeshop.header"));
configManager.getConfig().set("itradeshop.header", null);
hasUpgraded.add(true);
}

if (configManager.getConfig().contains("itradeshop.allow-explode")) {
configManager.getConfig().set(ITRADESHOP_EXPLODE.path, configManager.getConfig().get("itradeshop.allow-explode"));
configManager.getConfig().set("itradeshop.allow-explode", null);
hasUpgraded.add(true);
}

if (configManager.getConfig().contains("tradeshop.header")) {
configManager.getConfig().set(TRADESHOP_HEADER.path, configManager.getConfig().get("tradeshop.header"));
configManager.getConfig().set("tradeshop.header", null);
hasUpgraded.add(true);
}

if (configManager.getConfig().contains("tradeshop.allow-explode")) {
configManager.getConfig().set(TRADESHOP_EXPLODE.path, configManager.getConfig().get("tradeshop.allow-explode"));
configManager.getConfig().set("tradeshop.allow-explode", null);
hasUpgraded.add(true);
}

if (configManager.getConfig().contains("tradeshop.allow-hopper-export")) {
configManager.getConfig().set(TRADESHOP_HOPPER_EXPORT.path, configManager.getConfig().get("tradeshop.allow-hopper-export"));
configManager.getConfig().set("tradeshop.allow-hopper-export", null);
hasUpgraded.add(true);
}

if (configManager.getConfig().contains("bitradeshop.header")) {
configManager.getConfig().set(BITRADESHOP_HEADER.path, configManager.getConfig().get("bitradeshop.header"));
configManager.getConfig().set("bitradeshop.header", null);
hasUpgraded.add(true);
}

if (configManager.getConfig().contains("bitradeshop.allow-explode")) {
configManager.getConfig().set(BITRADESHOP_EXPLODE.path, configManager.getConfig().get("bitradeshop.allow-explode"));
configManager.getConfig().set("bitradeshop.allow-explode", null);
hasUpgraded.add(true);
}

if (configManager.getConfig().contains("bitradeshop.allow-hopper-export")) {
configManager.getConfig().set(BITRADESHOP_HOPPER_EXPORT.path, configManager.getConfig().get("bitradeshop.allow-hopper-export"));
configManager.getConfig().set("bitradeshop.allow-hopper-export", null);
hasUpgraded.add(true);
}


Expand All @@ -235,12 +247,15 @@ static void upgrade() {
configManager.getConfig().set(GLOBAL_ILLEGAL_ITEMS_LIST.path, configManager.getConfig().get("global-options.illegal-items"));
GLOBAL_ILLEGAL_ITEMS_LIST.setValue(configManager.getConfig().getStringList("global-options.illegal-items").removeAll(Arrays.asList("Air", "Void_Air", "Cave_Air")));
configManager.getConfig().set("global-options.illegal-items", null);
hasUpgraded.add(true);
}

version = 1.2;
}

CONFIG_VERSION.setValue(version);

return hasUpgraded.contains(true);
}

public String getKey() {
Expand All @@ -255,6 +270,10 @@ public String getMappedString(String subKey) {
return PLUGIN.getSettingManager().getConfig().getConfigurationSection(getPath()).getString(subKey.toLowerCase().replace("_", "-"));
}

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

public String getPostComment() {
return PLUGIN.getLanguage().getPostComment(Language.LangSection.SETTING, path);
}
Expand All @@ -281,7 +300,7 @@ public String getFileString() {
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()));
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()));
Expand All @@ -301,6 +320,11 @@ public void setValue(Object obj) {
PLUGIN.getSettingManager().getConfig().set(getPath(), obj);
}

public void setMappedValue(String subKey, Object obj) {
String newNode = getPath() + "." + subKey;
PLUGIN.getSettingManager().getConfig().set(newNode, obj);
}

public void clearSetting() {
PLUGIN.getSettingManager().getConfig().set(getPath(), null);
}
Expand All @@ -309,7 +333,7 @@ public Object getSetting() {
return PLUGIN.getSettingManager().getConfig().get(getPath());
}

public String getString() {
public String getString() {
return PLUGIN.getSettingManager().getConfig().getString(getPath());
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/shanerx/tradeshop/shop/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,8 @@ public void setOwner(ShopUser owner) {
* @return true if player has been set
*/
public boolean setUser(UUID newUser, ShopRole role) {
return removeUser(newUser) && addUser(newUser, role);
removeUser(newUser);
return addUser(newUser, role);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ public void onBlockInteract(PlayerInteractEvent e) {
return;
case INCOMPLETE:
Message.SHOP_EMPTY.sendMessage(buyer);
return;
case OUT_OF_STOCK:
if (shop.getShopType() == ShopType.ITRADE) {
break;
Expand All @@ -142,8 +143,6 @@ public void onBlockInteract(PlayerInteractEvent e) {
if (shop.hasSide(ShopItemSide.PRODUCT)) {
List<ItemStack> searchResult = getItems(shop.getChestAsSC().getInventory().getStorageContents(), shop.getSideList(ShopItemSide.PRODUCT, doBiTradeAlternate), multiplier);
Message.SHOP_INSUFFICIENT_ITEMS.sendItemMultiLineMessage(buyer, Collections.singletonMap(Variable.MISSING_ITEMS, searchResult));
} else {
Message.SHOP_EMPTY.sendMessage(buyer);
}
return;
case OPEN:
Expand Down

0 comments on commit b4d1b8f

Please sign in to comment.