| 1 | package me.fridtjof.minecarttrains.managers; |
| 2 | |
| 3 | import me.fridtjof.puddingapi.bukkit.utils.YamlConfig; |
| 4 | import org.bukkit.Material; |
| 5 | import org.bukkit.plugin.java.JavaPlugin; |
| 6 | |
| 7 | import java.io.File; |
| 8 | import java.util.Collections; |
| 9 | import java.util.Objects; |
| 10 | import java.util.logging.Logger; |
| 11 | |
| 12 | public class ConfigManager |
| 13 | { |
| 14 | |
| 15 | JavaPlugin plugin; |
| 16 | |
| 17 | private Logger logger; |
| 18 | public YamlConfig mainConfig, physicsConfig, messagesFile; |
| 19 | |
| 20 | public static int mainConfigVersion = 4; |
| 21 | public static int physicsConfigVersion = 4; |
| 22 | public static String versionStringName = "config_version"; |
| 23 | |
| 24 | public ConfigManager(JavaPlugin plugin) |
| 25 | { |
| 26 | this.plugin = plugin; |
| 27 | logger = plugin.getLogger(); |
| 28 | setupConfigs(); |
| 29 | checkConfigVersions(); |
| 30 | //2nd time to overwrite renamed ones |
| 31 | setupConfigs(); |
| 32 | reloadConfigs(); |
| 33 | } |
| 34 | |
| 35 | private void setupConfigs() |
| 36 | { |
| 37 | mainConfig = new YamlConfig(plugin.getDataFolder(), "config"); |
| 38 | physicsConfig = new YamlConfig(plugin.getDataFolder(), "physics"); |
| 39 | messagesFile = new YamlConfig(plugin.getDataFolder(), "messages"); |
| 40 | } |
| 41 | |
| 42 | private void checkConfigVersions() |
| 43 | { |
| 44 | checkConfigVersion(mainConfig, versionStringName, mainConfigVersion); |
| 45 | checkConfigVersion(physicsConfig, versionStringName, physicsConfigVersion); |
| 46 | } |
| 47 | |
| 48 | //TODO move to PuddingAPI |
| 49 | private void checkConfigVersion(YamlConfig config, String versionStringName, int wantedVersion) |
| 50 | { |
| 51 | if(!config.getConfig().contains(versionStringName)) |
| 52 | { |
| 53 | return; |
| 54 | } |
| 55 | if(config.getConfig().getInt(versionStringName) == wantedVersion) |
| 56 | { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | String filePath = config.getFile().getAbsolutePath(); |
| 62 | |
| 63 | File renamedFile = new File(filePath + "_backup"); |
| 64 | |
| 65 | int backupCount = 0; |
| 66 | |
| 67 | while(renamedFile.exists()) |
| 68 | { |
| 69 | backupCount++; |
| 70 | renamedFile = new File(filePath + "_backup_" + backupCount); |
| 71 | } |
| 72 | |
| 73 | config.getFile().renameTo(renamedFile); |
| 74 | |
| 75 | logger.warning("The old " + config.getName() + " was replaced by a new version of the default file due to an update"); |
| 76 | } |
| 77 | |
| 78 | private void reloadConfigs() |
| 79 | { |
| 80 | loadMainConfig(); |
| 81 | checkMaterial(); |
| 82 | loadPhysicsConfig(); |
| 83 | loadMessagesFile(); |
| 84 | } |
| 85 | |
| 86 | private void loadMainConfig() |
| 87 | { |
| 88 | mainConfig.getConfig().options().setHeader(Collections.singletonList("This is the main configuration file")); |
| 89 | |
| 90 | mainConfig.getConfig().addDefault(versionStringName, mainConfigVersion); |
| 91 | |
| 92 | mainConfig.getConfig().addDefault("trains.run_over.entities", true); |
| 93 | mainConfig.getConfig().addDefault("trains.run_over.min_velocity", 1.8D); |
| 94 | mainConfig.getConfig().addDefault("trains.run_over.damage", 10000); |
| 95 | |
| 96 | mainConfig.getConfig().addDefault("trains.coupling.tool", "IRON_CHAIN"); |
| 97 | |
| 98 | mainConfig.getConfig().addDefault("trains.can_get_hit_by_arrows", false); |
| 99 | |
| 100 | mainConfig.getConfig().addDefault("trains.fuel.consumption_per_tick", 1); |
| 101 | mainConfig.getConfig().addDefault("trains.fuel.refill_only_from_fuel_cart", true); |
| 102 | mainConfig.getConfig().addDefault("trains.fuel.cart_name", "Coal Cart"); |
| 103 | mainConfig.getConfig().addDefault("trains.fuel.do_hopper_logic", true); |
| 104 | |
| 105 | mainConfig.getConfig().addDefault("tracks.high_speed_tracks.enable", true); |
| 106 | mainConfig.getConfig().addDefault("tracks.high_speed_tracks.bed_block", Material.REDSTONE_BLOCK.toString()); |
| 107 | |
| 108 | mainConfig.getConfig().options().copyDefaults(true); |
| 109 | mainConfig.save(); |
| 110 | logger.info("Successfully (re)loaded config.yml"); |
| 111 | } |
| 112 | |
| 113 | private void loadPhysicsConfig() |
| 114 | { |
| 115 | physicsConfig.getConfig().options().setHeader(Collections.singletonList("This is the physics configuration file - Handle with care! - Default values by @Wallaceman105")); |
| 116 | |
| 117 | physicsConfig.getConfig().addDefault(versionStringName, physicsConfigVersion); |
| 118 | |
| 119 | physicsConfig.getConfig().addDefault("coupling.max_distance", 3.5); |
| 120 | |
| 121 | physicsConfig.getConfig().addDefault("link.distance.max", 9.5); |
| 122 | physicsConfig.getConfig().addDefault("link.distance.min", 0.7); |
| 123 | |
| 124 | physicsConfig.getConfig().addDefault("link.low_speed.speed.pull", 1); |
| 125 | physicsConfig.getConfig().addDefault("link.low_speed.speed.push", 32); |
| 126 | physicsConfig.getConfig().addDefault("link.low_speed.distance.aimed", 1.3); |
| 127 | physicsConfig.getConfig().addDefault("link.low_speed.distance.aimed_tolerance", 0.001); |
| 128 | |
| 129 | physicsConfig.getConfig().addDefault("link.high_speed.speed.pull", 4); |
| 130 | physicsConfig.getConfig().addDefault("link.high_speed.speed.push", 4); |
| 131 | physicsConfig.getConfig().addDefault("link.high_speed.distance.aimed", 1.5); |
| 132 | physicsConfig.getConfig().addDefault("link.high_speed.distance.aimed_tolerance", 0.001); |
| 133 | |
| 134 | physicsConfig.getConfig().addDefault("link.high_speed.gate_speed", 1.5); |
| 135 | |
| 136 | physicsConfig.getConfig().addDefault("track.high_speed.max_speed", 4); |
| 137 | physicsConfig.getConfig().addDefault("track.high_speed.acceleration", 0.008); |
| 138 | |
| 139 | physicsConfig.getConfig().options().copyDefaults(true); |
| 140 | physicsConfig.save(); |
| 141 | logger.info("Successfully (re)loaded physics.yml"); |
| 142 | } |
| 143 | |
| 144 | private void loadMessagesFile() |
| 145 | { |
| 146 | messagesFile.getConfig().options().header("This is the localization file.");; |
| 147 | |
| 148 | messagesFile.getConfig().addDefault("trains.coupling_successful", "Coupling successful!"); |
| 149 | messagesFile.getConfig().addDefault("trains.coupling_failed_distance", "Coupling failed! - The minecarts are too far apart!"); |
| 150 | messagesFile.getConfig().addDefault("trains.coupling_failed_self", "Coupling failed! - Can't be coupled with itself!"); |
| 151 | messagesFile.getConfig().addDefault("trains.coupling_already_coupled_with_itself", "Minecart is coupled with itself! - this shouldn't be possible!"); |
| 152 | |
| 153 | messagesFile.getConfig().addDefault("trains.coupling_1-2", "Coupling (1/2) activated for "); |
| 154 | messagesFile.getConfig().addDefault("trains.coupling_2-2", "Coupling (2/2) activated for "); |
| 155 | |
| 156 | messagesFile.getConfig().options().copyDefaults(true); |
| 157 | messagesFile.save(); |
| 158 | logger.info("Successfully (re)loaded messages.yml"); |
| 159 | } |
| 160 | |
| 161 | public void checkMaterial() |
| 162 | { |
| 163 | //check is Material of tool is valid |
| 164 | String toolMaterialConfig = mainConfig.getConfig().getString("trains.coupling.tool"); |
| 165 | if((toolMaterialConfig == null) || (Material.getMaterial(toolMaterialConfig) == null)) |
| 166 | { |
| 167 | String msg = "Could not resolve coupling tool Material! - Replacing with default!"; |
| 168 | Material newToolMaterialDefault = Material.STICK; |
| 169 | |
| 170 | // server lost support for the legacy chain naming |
| 171 | if(Material.getMaterial("CHAIN") == null) |
| 172 | { |
| 173 | newToolMaterialDefault = Material.getMaterial("IRON_CHAIN"); |
| 174 | } |
| 175 | |
| 176 | // server uses legacy naming for the iron chain |
| 177 | if(Material.getMaterial("IRON_CHAIN") == null) |
| 178 | { |
| 179 | newToolMaterialDefault = Material.getMaterial("CHAIN"); |
| 180 | } |
| 181 | |
| 182 | logger.warning(msg); |
| 183 | mainConfig.getConfig().set("trains.coupling.tool", newToolMaterialDefault.toString()); |
| 184 | mainConfig.save(); |
| 185 | } |
| 186 | } |
| 187 | } |