add save to disk & backup to int & boolean setting gui.

This commit is contained in:
alexcrea 2024-02-29 19:36:42 +01:00 committed by alexcrea
parent 643487e1a9
commit c5dbbeb67c
8 changed files with 47 additions and 43 deletions

View file

@ -72,7 +72,7 @@ public abstract class ConfigHolder {
// Save logic // Save logic
public boolean saveToDisk(boolean doBackup){ public boolean saveToDisk(boolean doBackup){
if(!doBackup){ if(doBackup){
if(!saveBackup()){ if(!saveBackup()){
CustomAnvil.instance.getLogger().severe("Could not save backup. see above."); CustomAnvil.instance.getLogger().severe("Could not save backup. see above.");
return false; return false;
@ -93,11 +93,10 @@ public abstract class ConfigHolder {
return false; return false;
} }
return true; return true;
} }
public boolean saveBackup(){ protected boolean saveBackup(){
File base = getConfigFile(); File base = getConfigFile();
if(!base.exists()) return true; // We did back up everything we had to (nothing in this case) if(!base.exists()) return true; // We did back up everything we had to (nothing in this case)
boolean sufficientSuccess = false; boolean sufficientSuccess = false;

View file

@ -6,6 +6,7 @@ import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.MainConfigGui; import xyz.alexcrea.cuanvil.gui.MainConfigGui;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui; import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
@ -46,11 +47,11 @@ public class BasicConfigGui extends ValueUpdatableGui {
public void updateGuiValues() { public void updateGuiValues() {
// Update item with value // Update item with value
ItemStack setting1Item = new ItemStack(Material.STONE); ItemStack setting1Item = new ItemStack(Material.STONE);
AbstractSettingGui.SettingGuiFactory factory1 = IntSettingsGui.factory( "Test GUI", this, "test", CustomAnvil.instance.getConfig(), 0,42,2,1); AbstractSettingGui.SettingGuiFactory factory1 = IntSettingsGui.factory( "Test GUI", this, "test", ConfigHolder.DEFAULT_CONFIG, 0,42,2,1);
GuiItem setting1 = GuiGlobalItems.openSettingGuiItem(setting1Item, factory1); GuiItem setting1 = GuiGlobalItems.openSettingGuiItem(setting1Item, factory1);
pane.bindItem('1', setting1); pane.bindItem('1', setting1);
BoolSettingsGui.BoolSettingFactory factory2 = BoolSettingsGui.factory("Test Gui bool",this, "test2", CustomAnvil.instance.getConfig(), false); BoolSettingsGui.BoolSettingFactory factory2 = BoolSettingsGui.factory("Test Gui bool",this, "test2", ConfigHolder.DEFAULT_CONFIG, false);
GuiItem setting2 = GuiGlobalItems.boolSettingGuiItem(factory2); GuiItem setting2 = GuiGlobalItems.boolSettingGuiItem(factory2);
pane.bindItem('2', setting2); pane.bindItem('2', setting2);

View file

@ -6,8 +6,8 @@ import com.github.stefvanschie.inventoryframework.gui.type.ChestGui;
import com.github.stefvanschie.inventoryframework.pane.PatternPane; import com.github.stefvanschie.inventoryframework.pane.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems;
@ -16,6 +16,10 @@ import java.util.List;
public abstract class AbstractSettingGui extends ChestGui { public abstract class AbstractSettingGui extends ChestGui {
// Temporary values, until I get something better.
public static final boolean TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE = true;
public static final boolean TEMPORARY_DO_BACKUP_EVERY_SAVE = true;
protected final static List<String> CLICK_LORE = Collections.singletonList("\u00A77Click Here to change the value"); protected final static List<String> CLICK_LORE = Collections.singletonList("\u00A77Click Here to change the value");
private PatternPane pane; private PatternPane pane;
@ -48,27 +52,26 @@ public abstract class AbstractSettingGui extends ChestGui {
// S, b, 0 is used by: save, back, background // S, b, 0 is used by: save, back, background
protected abstract Pattern getGuiPattern(); protected abstract Pattern getGuiPattern();
public abstract void onSave(); public abstract boolean onSave();
public abstract static class SettingGuiFactory{ public abstract static class SettingGuiFactory{
protected String configPath; protected String configPath;
protected ConfigurationSection section; protected ConfigHolder config;
protected SettingGuiFactory(String configPath, ConfigurationSection section){ protected SettingGuiFactory(String configPath, ConfigHolder config){
this.configPath = configPath; this.configPath = configPath;
this.section = section; this.config = config;
} }
public String getConfigPath() { public String getConfigPath() {
return configPath; return configPath;
} }
public ConfigurationSection getSection() { public ConfigHolder getConfigHolder() {
return section; return config;
} }
public abstract AbstractSettingGui create(); public abstract AbstractSettingGui create();
} }
} }

View file

@ -5,11 +5,11 @@ import com.github.stefvanschie.inventoryframework.pane.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -73,18 +73,20 @@ public class BoolSettingsGui extends AbstractSettingGui{
} }
@Override @Override
public void onSave() { public boolean onSave() {
holder.section.set(holder.configPath, now); holder.config.getConfig().set(holder.configPath, now);
// TODO SAVE (also backup before) if(TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){
return holder.config.saveToDisk(TEMPORARY_DO_BACKUP_EVERY_SAVE);
}
return true;
} }
public static BoolSettingFactory factory(@NotNull String title, ValueUpdatableGui parent, public static BoolSettingFactory factory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigurationSection section, String configPath, ConfigHolder config,
boolean defaultVal){ boolean defaultVal){
return new BoolSettingFactory( return new BoolSettingFactory(
title,parent, title,parent,
configPath, section, configPath, config,
defaultVal); defaultVal);
} }
@ -94,9 +96,9 @@ public class BoolSettingsGui extends AbstractSettingGui{
boolean defaultVal; boolean defaultVal;
private BoolSettingFactory(@NotNull String title, ValueUpdatableGui parent, private BoolSettingFactory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigurationSection section, String configPath, ConfigHolder config,
boolean defaultVal){ boolean defaultVal){
super(configPath, section); super(configPath, config);
this.title = title; this.title = title;
this.parent = parent; this.parent = parent;
@ -104,13 +106,12 @@ public class BoolSettingsGui extends AbstractSettingGui{
} }
public boolean getConfiguredValue(){ public boolean getConfiguredValue(){
return this.section.getBoolean(this.configPath, this.defaultVal); return this.config.getConfig().getBoolean(this.configPath, this.defaultVal);
} }
@Override @Override
public AbstractSettingGui create() { public AbstractSettingGui create() {
// Get value or default // Get value or default
//TODO maybe get section dynamically (and maybe same for save ?)
boolean now = getConfiguredValue(); boolean now = getConfiguredValue();
// create new gui // create new gui
return new BoolSettingsGui(this, now); return new BoolSettingsGui(this, now);

View file

@ -5,11 +5,11 @@ import com.github.stefvanschie.inventoryframework.pane.PatternPane;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern; import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil; import io.delilaheve.CustomAnvil;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems;
@ -100,18 +100,20 @@ public class IntSettingsGui extends AbstractSettingGui{
} }
@Override @Override
public void onSave() { public boolean onSave() {
holder.section.set(holder.configPath, now); if(TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){
// TODO SAVE (also backup before) holder.config.getConfig().set(holder.configPath, now);
return holder.config.saveToDisk(TEMPORARY_DO_BACKUP_EVERY_SAVE);
}
return true;
} }
public static SettingGuiFactory factory(@NotNull String title, ValueUpdatableGui parent, public static SettingGuiFactory factory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigurationSection section, String configPath, ConfigHolder config,
int min, int max, int defaultVal, int... steps){ int min, int max, int defaultVal, int... steps){
return new IntSettingFactory( return new IntSettingFactory(
title,parent, title,parent,
configPath, section, configPath, config,
min, max, defaultVal, steps); min, max, defaultVal, steps);
} }
@ -121,9 +123,9 @@ public class IntSettingsGui extends AbstractSettingGui{
int min; int max; int defaultVal; int[] steps; int min; int max; int defaultVal; int[] steps;
private IntSettingFactory(@NotNull String title, ValueUpdatableGui parent, private IntSettingFactory(@NotNull String title, ValueUpdatableGui parent,
String configPath, ConfigurationSection section, String configPath, ConfigHolder config,
int min, int max, int defaultVal, int... steps){ int min, int max, int defaultVal, int... steps){
super(configPath, section); super(configPath, config);
this.title = title; this.title = title;
this.parent = parent; this.parent = parent;
this.min = min; this.min = min;
@ -133,13 +135,12 @@ public class IntSettingsGui extends AbstractSettingGui{
} }
public int getConfiguredValue(){ public int getConfiguredValue(){
return this.section.getInt(this.configPath, this.defaultVal); return this.config.getConfig().getInt(this.configPath, this.defaultVal);
} }
@Override @Override
public AbstractSettingGui create() { public AbstractSettingGui create() {
// Get value or default // Get value or default
//TODO maybe get section dynamically (and maybe same for save ?)
int now = getConfiguredValue(); int now = getConfiguredValue();
// create new gui // create new gui
return new IntSettingsGui(this, now); return new IntSettingsGui(this, now);

View file

@ -60,7 +60,9 @@ public class GuiGlobalActions {
return event -> { return event -> {
event.setCancelled(true); event.setCancelled(true);
// Save setting // Save setting
setting.onSave(); if(!setting.onSave()){
event.getWhoClicked().sendMessage("\u00A7cSomething wrong happen while saving the change of value.");
}
// Update gui for the one who have it open // Update gui for the one who have it open
goal.updateGuiValues(); goal.updateGuiValues();
// Then show // Then show

View file

@ -39,9 +39,6 @@ class CustomAnvil : JavaPlugin() {
// Current plugin instance // Current plugin instance
lateinit var instance: CustomAnvil lateinit var instance: CustomAnvil
// Configuration for unit repair
lateinit var unitRepairConfig: YamlConfiguration
/** /**
* Logging handler * Logging handler
*/ */
@ -68,7 +65,7 @@ class CustomAnvil : JavaPlugin() {
// Load config // Load config
val success = ConfigHolder.loadConfig(); val success = ConfigHolder.loadConfig()
if(!success) return if(!success) return
// Load metrics // Load metrics

View file

@ -30,11 +30,11 @@ class ReloadExecutor : CommandExecutor {
* Execute the command, return true if success or false otherwise * Execute the command, return true if success or false otherwise
*/ */
private fun commandBody(hardfail: Boolean): Boolean{ private fun commandBody(hardfail: Boolean): Boolean{
try { return try {
return ConfigHolder.reloadAllFromDisk(hardfail); ConfigHolder.reloadAllFromDisk(hardfail)
}catch (e: Exception){ }catch (e: Exception){
e.printStackTrace() e.printStackTrace()
return false false
} }
} }
} }