Fix an issue where config.yml was no longer being created...

This commit is contained in:
Mathias Lystbæk 2024-03-11 01:16:49 +01:00
parent a564da45be
commit e6f2e2c25a
4 changed files with 46 additions and 2 deletions

View File

@ -6,7 +6,7 @@
<groupId>eu.mathiasl</groupId> <groupId>eu.mathiasl</groupId>
<artifactId>LegacyChatFix</artifactId> <artifactId>LegacyChatFix</artifactId>
<version>0.1-SNAPSHOT</version> <version>0.5-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>LegacyChatFix</name> <name>LegacyChatFix</name>

View File

@ -6,12 +6,16 @@ import org.bukkit.plugin.java.JavaPlugin;
public final class LegacyChatFix extends JavaPlugin { public final class LegacyChatFix extends JavaPlugin {
ConfigManager configManager = new ConfigManager(this); ConfigManager configManager;
LegacyChatFixPacketAdapter adapter; LegacyChatFixPacketAdapter adapter;
@Override @Override
public void onEnable() { public void onEnable() {
configManager = new ConfigManager(this);
configManager.saveDefaultConfig("config.yml");
configManager.checkAndUpdateConfig("config.yml");
adapter = new LegacyChatFixPacketAdapter(this, configManager); adapter = new LegacyChatFixPacketAdapter(this, configManager);
ProtocolLibrary.getProtocolManager().addPacketListener(adapter); ProtocolLibrary.getProtocolManager().addPacketListener(adapter);
} }
@ -20,5 +24,8 @@ public final class LegacyChatFix extends JavaPlugin {
public void onDisable() { public void onDisable() {
// Plugin shutdown logic // Plugin shutdown logic
ProtocolLibrary.getProtocolManager().removePacketListener(adapter); ProtocolLibrary.getProtocolManager().removePacketListener(adapter);
configManager.saveConfig("config.yml");
} }
} }

View File

@ -103,6 +103,8 @@ public class LegacyChatFixPacketAdapter extends PacketAdapter {
*/ */
@Override @Override
public void onPacketSending(PacketEvent event) { public void onPacketSending(PacketEvent event) {
if (legacyColors == null) return;
// Get the chat components from the packet // Get the chat components from the packet
StructureModifier<WrappedChatComponent> chatComponents = event.getPacket().getChatComponents(); StructureModifier<WrappedChatComponent> chatComponents = event.getPacket().getChatComponents();
// If there are no chat components, return // If there are no chat components, return

View File

@ -6,6 +6,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -61,4 +62,38 @@ public class ConfigManager {
} }
return configFiles.get(configName); return configFiles.get(configName);
} }
public void checkAndUpdateConfig(String configName) {
File configFile = new File(plugin.getDataFolder(), configName);
if (!configFile.exists()) {
plugin.saveResource(configName, false);
return;
}
FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
String configVersion = config.getString("ConfigVersion", "0"); // Default to "0" if not found
if (!"1.0".equals(configVersion)) {
backupAndReplaceConfig(configName);
// After backing up and copying new ones, you might want to reload these configs into your ConfigManager
// assuming you have such a method in your ConfigManager
reloadConfig(configName);
}
}
private void backupAndReplaceConfig(String fileName) {
File configFile = new File(plugin.getDataFolder(), fileName);
File backupFile = new File(plugin.getDataFolder(), fileName + ".BACKUP");
// Backup the old file
try {
Files.move(configFile.toPath(), backupFile.toPath());
} catch (IOException e) {
plugin.getLogger().warning("Could not backup " + fileName + ": " + e.getMessage());
}
// Copy new file from resources
plugin.saveResource(fileName, false);
}
} }