Last active
January 4, 2023 22:54
-
-
Save Xezard/55b81fce657d3492c2b099521976b4f9 to your computer and use it in GitHub Desktop.
Simple yml configurations manager for bukkit / spigot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.AccessLevel; | |
import lombok.AllArgsConstructor; | |
import lombok.NonNull; | |
import lombok.SneakyThrows; | |
import lombok.experimental.FieldDefaults; | |
import org.bukkit.configuration.file.FileConfiguration; | |
import org.bukkit.configuration.file.YamlConfiguration; | |
import org.bukkit.plugin.Plugin; | |
import org.jetbrains.annotations.NotNull; | |
import java.io.File; | |
import java.util.*; | |
@AllArgsConstructor | |
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) | |
public class Configurations { | |
Map<ConfigurationType, Map.Entry<FileConfiguration, File>> configurations = new HashMap<> (); | |
Plugin plugin; | |
private @NotNull File generateDefaultFile(@NonNull String name) { | |
File file = new File(this.plugin.getDataFolder(), name); | |
if (!file.exists()) { | |
this.plugin.saveResource(name, false); | |
} | |
return file; | |
} | |
public void load() { | |
for (ConfigurationType configurationType : ConfigurationType.values()) { | |
if (this.configurations.containsKey(configurationType)) { | |
continue; | |
} | |
var configurationFile = this.generateDefaultFile(configurationType.getFileName()); | |
var configuration = YamlConfiguration.loadConfiguration(configurationFile); | |
this.configurations.put(configurationType, | |
new AbstractMap.SimpleEntry<> (configuration, configurationFile)); | |
} | |
} | |
public void reload() { | |
this.configurations.clear(); | |
this.load(); | |
} | |
private Map.Entry<FileConfiguration, File> getEntry(@NonNull ConfigurationType configurationType) { | |
return this.configurations.get(configurationType); | |
} | |
public @NotNull FileConfiguration get(@NonNull ConfigurationType configurationType) { | |
return this.getEntry(configurationType).getKey(); | |
} | |
@SneakyThrows | |
public void save(@NonNull ConfigurationType configurationType) { | |
Map.Entry<FileConfiguration, File> entry = this.getEntry(configurationType); | |
entry.getKey().save(entry.getValue()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.AllArgsConstructor; | |
import lombok.Getter; | |
@Getter | |
@AllArgsConstructor | |
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) | |
public enum ConfigurationType { | |
CONFIG("config.yml"), | |
MESSAGES("messages.yml"); | |
String fileName; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.bukkit.plugin.java.JavaPlugin; | |
import org.bukkit.configuration.file.FileConfiguration; | |
@FieldDefaults(level = AccessLevel.PRIVATE) | |
public class ExamplePlugin | |
extends JavaPlugin { | |
Configurations configurations = new Configurations(this); | |
@Override | |
public void onEnable() { | |
this.configurations.load(); | |
} | |
@Override | |
public void onDisable() { | |
this.configurations = null; | |
} | |
public int getValueFromConfig() { | |
return this.configurations.get(ConfigurationType.CONFIG).getInt("test"); | |
} | |
public void saveValueToConfig() { | |
FileConfiguration config = this.configurations.get(ConfigurationType.CONFIG); | |
config.set("test", 1); | |
this.configurations.save(ConfigurationType.CONFIG); | |
} | |
} |
xapow
postav zvezdochku togda :)
Пасибо, буду пользовать
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
xapow