Fix mcMMO attempting to copy out custom repair/salvage configs

This commit is contained in:
nossr50 2023-04-17 12:07:41 -07:00
parent 978ee4a9a3
commit 640f4b0a9b
17 changed files with 57 additions and 60 deletions

View File

@ -10,22 +10,22 @@ import java.io.IOException;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
public abstract class AutoUpdateConfigLoader extends ConfigLoader { public abstract class AutoUpdateLegacyConfigLoader extends LegacyConfigLoader {
public AutoUpdateConfigLoader(String relativePath, String fileName, File dataFolder) { public AutoUpdateLegacyConfigLoader(String relativePath, String fileName, File dataFolder) {
super(relativePath, fileName, dataFolder); super(relativePath, fileName, dataFolder);
} }
public AutoUpdateConfigLoader(String fileName, File dataFolder) { public AutoUpdateLegacyConfigLoader(String fileName, File dataFolder) {
super(fileName, dataFolder); super(fileName, dataFolder);
} }
@Deprecated @Deprecated
public AutoUpdateConfigLoader(String relativePath, String fileName) { public AutoUpdateLegacyConfigLoader(String relativePath, String fileName) {
super(relativePath, fileName); super(relativePath, fileName);
} }
@Deprecated @Deprecated
public AutoUpdateConfigLoader(String fileName) { public AutoUpdateLegacyConfigLoader(String fileName) {
super(fileName); super(fileName);
} }

View File

@ -19,17 +19,7 @@ public abstract class BukkitConfig {
protected YamlConfiguration defaultYamlConfig; protected YamlConfiguration defaultYamlConfig;
protected YamlConfiguration config; protected YamlConfiguration config;
protected @NotNull final File dataFolder; protected @NotNull final File dataFolder;
private boolean savedDefaults = false;
public BukkitConfig(@NotNull String fileName, @NotNull File dataFolder) {
mcMMO.p.getLogger().info("[config] Initializing config: " + fileName);
this.fileName = fileName;
this.dataFolder = dataFolder;
configFile = new File(dataFolder, fileName);
this.defaultYamlConfig = copyDefaultConfig();
this.config = initConfig();
updateFile();
mcMMO.p.getLogger().info("[config] Config initialized: " + fileName);
}
public BukkitConfig(@NotNull String fileName, @NotNull File dataFolder, boolean copyDefaults) { public BukkitConfig(@NotNull String fileName, @NotNull File dataFolder, boolean copyDefaults) {
mcMMO.p.getLogger().info("[config] Initializing config: " + fileName); mcMMO.p.getLogger().info("[config] Initializing config: " + fileName);
@ -37,12 +27,16 @@ public abstract class BukkitConfig {
this.fileName = fileName; this.fileName = fileName;
this.dataFolder = dataFolder; this.dataFolder = dataFolder;
configFile = new File(dataFolder, fileName); configFile = new File(dataFolder, fileName);
this.defaultYamlConfig = copyDefaultConfig(); this.defaultYamlConfig = saveDefaultConfigToDisk();
this.config = initConfig(); this.config = initConfig();
updateFile(); updateFile();
mcMMO.p.getLogger().info("[config] Config initialized: " + fileName); mcMMO.p.getLogger().info("[config] Config initialized: " + fileName);
} }
public BukkitConfig(@NotNull String fileName, @NotNull File dataFolder) {
this(fileName, dataFolder, true);
}
public BukkitConfig(@NotNull String fileName) { public BukkitConfig(@NotNull String fileName) {
this(fileName, mcMMO.p.getDataFolder()); this(fileName, mcMMO.p.getDataFolder());
} }
@ -55,10 +49,12 @@ public abstract class BukkitConfig {
*/ */
public void updateFile() { public void updateFile() {
try { try {
if(copyDefaults) {
copyMissingDefaultsFromResource();
}
config.save(configFile); config.save(configFile);
if(copyDefaults && !savedDefaults) {
copyMissingDefaultsFromResource();
savedDefaults = true;
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -84,7 +80,7 @@ public abstract class BukkitConfig {
/** /**
* Copies the config from the JAR to defaults/<fileName> * Copies the config from the JAR to defaults/<fileName>
*/ */
YamlConfiguration copyDefaultConfig() { YamlConfiguration saveDefaultConfigToDisk() {
mcMMO.p.getLogger().info("[config] Copying default config to disk: " + fileName + " to defaults/" + fileName); mcMMO.p.getLogger().info("[config] Copying default config to disk: " + fileName + " to defaults/" + fileName);
try(InputStream inputStream = mcMMO.p.getResource(fileName)) { try(InputStream inputStream = mcMMO.p.getResource(fileName)) {
if(inputStream == null) { if(inputStream == null) {

View File

@ -7,20 +7,21 @@ import org.jetbrains.annotations.NotNull;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
public abstract class ConfigLoader { @Deprecated
public abstract class LegacyConfigLoader {
protected final File configFile; protected final File configFile;
protected final @NotNull File dataFolder; protected final @NotNull File dataFolder;
protected String fileName; protected String fileName;
protected YamlConfiguration config; protected YamlConfiguration config;
public ConfigLoader(String relativePath, String fileName, @NotNull File dataFolder) { public LegacyConfigLoader(String relativePath, String fileName, @NotNull File dataFolder) {
this.fileName = fileName; this.fileName = fileName;
this.dataFolder = dataFolder; this.dataFolder = dataFolder;
configFile = new File(dataFolder, relativePath + File.separator + fileName); configFile = new File(dataFolder, relativePath + File.separator + fileName);
loadFile(); loadFile();
} }
public ConfigLoader(String fileName, @NotNull File dataFolder) { public LegacyConfigLoader(String fileName, @NotNull File dataFolder) {
this.fileName = fileName; this.fileName = fileName;
this.dataFolder = dataFolder; this.dataFolder = dataFolder;
configFile = new File(dataFolder, fileName); configFile = new File(dataFolder, fileName);
@ -28,7 +29,7 @@ public abstract class ConfigLoader {
} }
@Deprecated @Deprecated
public ConfigLoader(String relativePath, String fileName) { public LegacyConfigLoader(String relativePath, String fileName) {
this.fileName = fileName; this.fileName = fileName;
configFile = new File(mcMMO.p.getDataFolder(), relativePath + File.separator + fileName); configFile = new File(mcMMO.p.getDataFolder(), relativePath + File.separator + fileName);
this.dataFolder = mcMMO.p.getDataFolder(); this.dataFolder = mcMMO.p.getDataFolder();
@ -36,7 +37,7 @@ public abstract class ConfigLoader {
} }
@Deprecated @Deprecated
public ConfigLoader(String fileName) { public LegacyConfigLoader(String fileName) {
this.fileName = fileName; this.fileName = fileName;
configFile = new File(mcMMO.p.getDataFolder(), fileName); configFile = new File(mcMMO.p.getDataFolder(), fileName);
this.dataFolder = mcMMO.p.getDataFolder(); this.dataFolder = mcMMO.p.getDataFolder();

View File

@ -29,7 +29,7 @@ public class ArmorConfigManager {
continue; continue;
} }
modManager.registerCustomArmor(new CustomArmorConfig(fileName)); modManager.registerCustomArmor(new CustomArmorLegacyConfig(fileName));
} }
} }
} }

View File

@ -29,7 +29,7 @@ public class BlockConfigManager {
continue; continue;
} }
modManager.registerCustomBlocks(new CustomBlockConfig(fileName)); modManager.registerCustomBlocks(new CustomBlockLegacyConfig(fileName));
} }
} }
} }

View File

@ -1,6 +1,6 @@
package com.gmail.nossr50.config.mods; package com.gmail.nossr50.config.mods;
import com.gmail.nossr50.config.ConfigLoader; import com.gmail.nossr50.config.LegacyConfigLoader;
import com.gmail.nossr50.datatypes.skills.ItemType; import com.gmail.nossr50.datatypes.skills.ItemType;
import com.gmail.nossr50.datatypes.skills.MaterialType; import com.gmail.nossr50.datatypes.skills.MaterialType;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
@ -13,7 +13,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
public class CustomArmorConfig extends ConfigLoader { public class CustomArmorLegacyConfig extends LegacyConfigLoader {
public List<Material> customBoots = new ArrayList<>(); public List<Material> customBoots = new ArrayList<>();
public List<Material> customChestplates = new ArrayList<>(); public List<Material> customChestplates = new ArrayList<>();
public List<Material> customHelmets = new ArrayList<>(); public List<Material> customHelmets = new ArrayList<>();
@ -21,7 +21,7 @@ public class CustomArmorConfig extends ConfigLoader {
public List<Repairable> repairables = new ArrayList<>(); public List<Repairable> repairables = new ArrayList<>();
private boolean needsUpdate = false; private boolean needsUpdate = false;
protected CustomArmorConfig(String fileName) { protected CustomArmorLegacyConfig(String fileName) {
super("mods", fileName); super("mods", fileName);
loadKeys(); loadKeys();
} }

View File

@ -1,6 +1,6 @@
package com.gmail.nossr50.config.mods; package com.gmail.nossr50.config.mods;
import com.gmail.nossr50.config.ConfigLoader; import com.gmail.nossr50.config.LegacyConfigLoader;
import com.gmail.nossr50.datatypes.mods.CustomBlock; import com.gmail.nossr50.datatypes.mods.CustomBlock;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import org.bukkit.Material; import org.bukkit.Material;
@ -11,7 +11,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
public class CustomBlockConfig extends ConfigLoader { public class CustomBlockLegacyConfig extends LegacyConfigLoader {
public List<Material> customExcavationBlocks = new ArrayList<>(); public List<Material> customExcavationBlocks = new ArrayList<>();
public List<Material> customHerbalismBlocks = new ArrayList<>(); public List<Material> customHerbalismBlocks = new ArrayList<>();
public List<Material> customMiningBlocks = new ArrayList<>(); public List<Material> customMiningBlocks = new ArrayList<>();
@ -22,7 +22,7 @@ public class CustomBlockConfig extends ConfigLoader {
public HashMap<Material, CustomBlock> customBlockMap = new HashMap<>(); public HashMap<Material, CustomBlock> customBlockMap = new HashMap<>();
private boolean needsUpdate = false; private boolean needsUpdate = false;
protected CustomBlockConfig(String fileName) { protected CustomBlockLegacyConfig(String fileName) {
super("mods", fileName); super("mods", fileName);
loadKeys(); loadKeys();
} }

View File

@ -1,6 +1,6 @@
package com.gmail.nossr50.config.mods; package com.gmail.nossr50.config.mods;
import com.gmail.nossr50.config.ConfigLoader; import com.gmail.nossr50.config.LegacyConfigLoader;
import com.gmail.nossr50.datatypes.mods.CustomEntity; import com.gmail.nossr50.datatypes.mods.CustomEntity;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import org.apache.commons.lang.ClassUtils; import org.apache.commons.lang.ClassUtils;
@ -9,11 +9,11 @@ import org.bukkit.inventory.ItemStack;
import java.util.HashMap; import java.util.HashMap;
public class CustomEntityConfig extends ConfigLoader { public class CustomEntityLegacyConfig extends LegacyConfigLoader {
public HashMap<String, CustomEntity> customEntityClassMap = new HashMap<>(); public HashMap<String, CustomEntity> customEntityClassMap = new HashMap<>();
public HashMap<String, CustomEntity> customEntityTypeMap = new HashMap<>(); public HashMap<String, CustomEntity> customEntityTypeMap = new HashMap<>();
protected CustomEntityConfig(String fileName) { protected CustomEntityLegacyConfig(String fileName) {
super("mods", fileName); super("mods", fileName);
loadKeys(); loadKeys();
} }

View File

@ -1,6 +1,6 @@
package com.gmail.nossr50.config.mods; package com.gmail.nossr50.config.mods;
import com.gmail.nossr50.config.ConfigLoader; import com.gmail.nossr50.config.LegacyConfigLoader;
import com.gmail.nossr50.datatypes.mods.CustomTool; import com.gmail.nossr50.datatypes.mods.CustomTool;
import com.gmail.nossr50.datatypes.skills.ItemType; import com.gmail.nossr50.datatypes.skills.ItemType;
import com.gmail.nossr50.datatypes.skills.MaterialType; import com.gmail.nossr50.datatypes.skills.MaterialType;
@ -15,7 +15,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
public class CustomToolConfig extends ConfigLoader { public class CustomToolLegacyConfig extends LegacyConfigLoader {
public List<Material> customAxes = new ArrayList<>(); public List<Material> customAxes = new ArrayList<>();
public List<Material> customBows = new ArrayList<>(); public List<Material> customBows = new ArrayList<>();
public List<Material> customHoes = new ArrayList<>(); public List<Material> customHoes = new ArrayList<>();
@ -26,7 +26,7 @@ public class CustomToolConfig extends ConfigLoader {
public List<Repairable> repairables = new ArrayList<>(); public List<Repairable> repairables = new ArrayList<>();
private boolean needsUpdate = false; private boolean needsUpdate = false;
protected CustomToolConfig(String fileName) { protected CustomToolLegacyConfig(String fileName) {
super("mods", fileName); super("mods", fileName);
loadKeys(); loadKeys();
} }

View File

@ -29,7 +29,7 @@ public class EntityConfigManager {
continue; continue;
} }
modManager.registerCustomEntities(new CustomEntityConfig(fileName)); modManager.registerCustomEntities(new CustomEntityLegacyConfig(fileName));
} }
} }
} }

View File

@ -29,7 +29,7 @@ public class ToolConfigManager {
continue; continue;
} }
modManager.registerCustomTools(new CustomToolConfig(fileName)); modManager.registerCustomTools(new CustomToolLegacyConfig(fileName));
} }
} }
} }

View File

@ -1,6 +1,6 @@
package com.gmail.nossr50.config.skills.alchemy; package com.gmail.nossr50.config.skills.alchemy;
import com.gmail.nossr50.config.ConfigLoader; import com.gmail.nossr50.config.LegacyConfigLoader;
import com.gmail.nossr50.datatypes.skills.alchemy.AlchemyPotion; import com.gmail.nossr50.datatypes.skills.alchemy.AlchemyPotion;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -15,7 +15,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class PotionConfig extends ConfigLoader { public class PotionConfig extends LegacyConfigLoader {
private static PotionConfig instance; private static PotionConfig instance;
private final List<ItemStack> concoctionsIngredientsTierOne = new ArrayList<>(); private final List<ItemStack> concoctionsIngredientsTierOne = new ArrayList<>();

View File

@ -17,8 +17,8 @@ public class RepairConfig extends BukkitConfig {
private final HashSet<String> notSupported; private final HashSet<String> notSupported;
private List<Repairable> repairables; private List<Repairable> repairables;
public RepairConfig(String fileName) { public RepairConfig(String fileName, boolean copyDefaults) {
super(fileName, false); super(fileName, copyDefaults);
notSupported = new HashSet<>(); notSupported = new HashSet<>();
loadKeys(); loadKeys();
} }

View File

@ -16,7 +16,7 @@ public class RepairConfigManager {
Pattern pattern = Pattern.compile("repair\\.(?:.+)\\.yml"); Pattern pattern = Pattern.compile("repair\\.(?:.+)\\.yml");
File dataFolder = plugin.getDataFolder(); File dataFolder = plugin.getDataFolder();
RepairConfig mainRepairConfig = new RepairConfig(REPAIR_VANILLA_YML); RepairConfig mainRepairConfig = new RepairConfig(REPAIR_VANILLA_YML, true);
repairables.addAll(mainRepairConfig.getLoadedRepairables()); repairables.addAll(mainRepairConfig.getLoadedRepairables());
for (String fileName : dataFolder.list()) { for (String fileName : dataFolder.list()) {
@ -33,7 +33,7 @@ public class RepairConfigManager {
continue; continue;
} }
RepairConfig rConfig = new RepairConfig(fileName); RepairConfig rConfig = new RepairConfig(fileName, false);
repairables.addAll(rConfig.getLoadedRepairables()); repairables.addAll(rConfig.getLoadedRepairables());
} }
} }

View File

@ -20,8 +20,8 @@ public class SalvageConfig extends BukkitConfig {
private final HashSet<String> notSupported; private final HashSet<String> notSupported;
private Set<Salvageable> salvageables; private Set<Salvageable> salvageables;
public SalvageConfig(String fileName) { public SalvageConfig(String fileName, boolean copyDefaults) {
super(fileName); super(fileName, copyDefaults);
notSupported = new HashSet<>(); notSupported = new HashSet<>();
loadKeys(); loadKeys();
} }

View File

@ -17,7 +17,7 @@ public class SalvageConfigManager {
Pattern pattern = Pattern.compile("salvage\\.(?:.+)\\.yml"); Pattern pattern = Pattern.compile("salvage\\.(?:.+)\\.yml");
File dataFolder = plugin.getDataFolder(); File dataFolder = plugin.getDataFolder();
SalvageConfig mainSalvageConfig = new SalvageConfig(SALVAGE_VANILLA_YML); SalvageConfig mainSalvageConfig = new SalvageConfig(SALVAGE_VANILLA_YML, true);
salvageables.addAll(mainSalvageConfig.getLoadedSalvageables()); salvageables.addAll(mainSalvageConfig.getLoadedSalvageables());
for (String fileName : dataFolder.list()) { for (String fileName : dataFolder.list()) {
@ -34,7 +34,7 @@ public class SalvageConfigManager {
continue; continue;
} }
SalvageConfig salvageConfig = new SalvageConfig(fileName); SalvageConfig salvageConfig = new SalvageConfig(fileName, false);
salvageables.addAll(salvageConfig.getLoadedSalvageables()); salvageables.addAll(salvageConfig.getLoadedSalvageables());
} }
} }

View File

@ -1,9 +1,9 @@
package com.gmail.nossr50.util; package com.gmail.nossr50.util;
import com.gmail.nossr50.config.mods.CustomArmorConfig; import com.gmail.nossr50.config.mods.CustomArmorLegacyConfig;
import com.gmail.nossr50.config.mods.CustomBlockConfig; import com.gmail.nossr50.config.mods.CustomBlockLegacyConfig;
import com.gmail.nossr50.config.mods.CustomEntityConfig; import com.gmail.nossr50.config.mods.CustomEntityLegacyConfig;
import com.gmail.nossr50.config.mods.CustomToolConfig; import com.gmail.nossr50.config.mods.CustomToolLegacyConfig;
import com.gmail.nossr50.datatypes.mods.CustomBlock; import com.gmail.nossr50.datatypes.mods.CustomBlock;
import com.gmail.nossr50.datatypes.mods.CustomEntity; import com.gmail.nossr50.datatypes.mods.CustomEntity;
import com.gmail.nossr50.datatypes.mods.CustomTool; import com.gmail.nossr50.datatypes.mods.CustomTool;
@ -52,7 +52,7 @@ public class ModManager {
private final List<Material> customSwords = new ArrayList<>(); private final List<Material> customSwords = new ArrayList<>();
private final HashMap<Material, CustomTool> customToolMap = new HashMap<>(); private final HashMap<Material, CustomTool> customToolMap = new HashMap<>();
public void registerCustomArmor(CustomArmorConfig config) { public void registerCustomArmor(CustomArmorLegacyConfig config) {
customBoots.addAll(config.customBoots); customBoots.addAll(config.customBoots);
customChestplates.addAll(config.customChestplates); customChestplates.addAll(config.customChestplates);
customHelmets.addAll(config.customHelmets); customHelmets.addAll(config.customHelmets);
@ -60,7 +60,7 @@ public class ModManager {
repairables.addAll(config.repairables); repairables.addAll(config.repairables);
} }
public void registerCustomBlocks(CustomBlockConfig config) { public void registerCustomBlocks(CustomBlockLegacyConfig config) {
customExcavationBlocks.addAll(config.customExcavationBlocks); customExcavationBlocks.addAll(config.customExcavationBlocks);
customHerbalismBlocks.addAll(config.customHerbalismBlocks); customHerbalismBlocks.addAll(config.customHerbalismBlocks);
customMiningBlocks.addAll(config.customMiningBlocks); customMiningBlocks.addAll(config.customMiningBlocks);
@ -71,12 +71,12 @@ public class ModManager {
customBlockMap.putAll(config.customBlockMap); customBlockMap.putAll(config.customBlockMap);
} }
public void registerCustomEntities(CustomEntityConfig config) { public void registerCustomEntities(CustomEntityLegacyConfig config) {
customEntityClassMap.putAll(config.customEntityClassMap); customEntityClassMap.putAll(config.customEntityClassMap);
customEntityTypeMap.putAll(config.customEntityTypeMap); customEntityTypeMap.putAll(config.customEntityTypeMap);
} }
public void registerCustomTools(CustomToolConfig config) { public void registerCustomTools(CustomToolLegacyConfig config) {
customAxes.addAll(config.customAxes); customAxes.addAll(config.customAxes);
customBows.addAll(config.customBows); customBows.addAll(config.customBows);
customHoes.addAll(config.customHoes); customHoes.addAll(config.customHoes);