mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-24 00:26:16 +02:00
add save to disk & backup to int & boolean setting gui.
This commit is contained in:
parent
643487e1a9
commit
c5dbbeb67c
8 changed files with 47 additions and 43 deletions
|
|
@ -72,7 +72,7 @@ public abstract class ConfigHolder {
|
|||
|
||||
// Save logic
|
||||
public boolean saveToDisk(boolean doBackup){
|
||||
if(!doBackup){
|
||||
if(doBackup){
|
||||
if(!saveBackup()){
|
||||
CustomAnvil.instance.getLogger().severe("Could not save backup. see above.");
|
||||
return false;
|
||||
|
|
@ -93,11 +93,10 @@ public abstract class ConfigHolder {
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean saveBackup(){
|
||||
protected boolean saveBackup(){
|
||||
File base = getConfigFile();
|
||||
if(!base.exists()) return true; // We did back up everything we had to (nothing in this case)
|
||||
boolean sufficientSuccess = false;
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
|
|||
import io.delilaheve.CustomAnvil;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
||||
import xyz.alexcrea.cuanvil.gui.MainConfigGui;
|
||||
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
|
||||
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
|
||||
|
|
@ -46,11 +47,11 @@ public class BasicConfigGui extends ValueUpdatableGui {
|
|||
public void updateGuiValues() {
|
||||
// Update item with value
|
||||
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);
|
||||
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);
|
||||
pane.bindItem('2', setting2);
|
||||
|
||||
|
|
|
|||
|
|
@ -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.util.Pattern;
|
||||
import io.delilaheve.CustomAnvil;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
||||
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
|
||||
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems;
|
||||
|
||||
|
|
@ -16,6 +16,10 @@ import java.util.List;
|
|||
|
||||
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");
|
||||
|
||||
private PatternPane pane;
|
||||
|
|
@ -48,27 +52,26 @@ public abstract class AbstractSettingGui extends ChestGui {
|
|||
// S, b, 0 is used by: save, back, background
|
||||
protected abstract Pattern getGuiPattern();
|
||||
|
||||
public abstract void onSave();
|
||||
public abstract boolean onSave();
|
||||
|
||||
|
||||
public abstract static class SettingGuiFactory{
|
||||
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.section = section;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public String getConfigPath() {
|
||||
return configPath;
|
||||
}
|
||||
|
||||
public ConfigurationSection getSection() {
|
||||
return section;
|
||||
public ConfigHolder getConfigHolder() {
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
public abstract AbstractSettingGui create();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,11 +5,11 @@ import com.github.stefvanschie.inventoryframework.pane.PatternPane;
|
|||
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
|
||||
import io.delilaheve.CustomAnvil;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
||||
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
|
@ -73,18 +73,20 @@ public class BoolSettingsGui extends AbstractSettingGui{
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onSave() {
|
||||
holder.section.set(holder.configPath, now);
|
||||
// TODO SAVE (also backup before)
|
||||
|
||||
public boolean onSave() {
|
||||
holder.config.getConfig().set(holder.configPath, now);
|
||||
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,
|
||||
String configPath, ConfigurationSection section,
|
||||
String configPath, ConfigHolder config,
|
||||
boolean defaultVal){
|
||||
return new BoolSettingFactory(
|
||||
title,parent,
|
||||
configPath, section,
|
||||
configPath, config,
|
||||
defaultVal);
|
||||
}
|
||||
|
||||
|
|
@ -94,9 +96,9 @@ public class BoolSettingsGui extends AbstractSettingGui{
|
|||
boolean defaultVal;
|
||||
|
||||
private BoolSettingFactory(@NotNull String title, ValueUpdatableGui parent,
|
||||
String configPath, ConfigurationSection section,
|
||||
String configPath, ConfigHolder config,
|
||||
boolean defaultVal){
|
||||
super(configPath, section);
|
||||
super(configPath, config);
|
||||
this.title = title;
|
||||
this.parent = parent;
|
||||
|
||||
|
|
@ -104,13 +106,12 @@ public class BoolSettingsGui extends AbstractSettingGui{
|
|||
}
|
||||
|
||||
public boolean getConfiguredValue(){
|
||||
return this.section.getBoolean(this.configPath, this.defaultVal);
|
||||
return this.config.getConfig().getBoolean(this.configPath, this.defaultVal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractSettingGui create() {
|
||||
// Get value or default
|
||||
//TODO maybe get section dynamically (and maybe same for save ?)
|
||||
boolean now = getConfiguredValue();
|
||||
// create new gui
|
||||
return new BoolSettingsGui(this, now);
|
||||
|
|
|
|||
|
|
@ -5,11 +5,11 @@ import com.github.stefvanschie.inventoryframework.pane.PatternPane;
|
|||
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
|
||||
import io.delilaheve.CustomAnvil;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
||||
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
|
||||
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalActions;
|
||||
import xyz.alexcrea.cuanvil.gui.utils.GuiGlobalItems;
|
||||
|
|
@ -100,18 +100,20 @@ public class IntSettingsGui extends AbstractSettingGui{
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onSave() {
|
||||
holder.section.set(holder.configPath, now);
|
||||
// TODO SAVE (also backup before)
|
||||
|
||||
public boolean onSave() {
|
||||
if(TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){
|
||||
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,
|
||||
String configPath, ConfigurationSection section,
|
||||
String configPath, ConfigHolder config,
|
||||
int min, int max, int defaultVal, int... steps){
|
||||
return new IntSettingFactory(
|
||||
title,parent,
|
||||
configPath, section,
|
||||
configPath, config,
|
||||
min, max, defaultVal, steps);
|
||||
}
|
||||
|
||||
|
|
@ -121,9 +123,9 @@ public class IntSettingsGui extends AbstractSettingGui{
|
|||
int min; int max; int defaultVal; int[] steps;
|
||||
|
||||
private IntSettingFactory(@NotNull String title, ValueUpdatableGui parent,
|
||||
String configPath, ConfigurationSection section,
|
||||
String configPath, ConfigHolder config,
|
||||
int min, int max, int defaultVal, int... steps){
|
||||
super(configPath, section);
|
||||
super(configPath, config);
|
||||
this.title = title;
|
||||
this.parent = parent;
|
||||
this.min = min;
|
||||
|
|
@ -133,13 +135,12 @@ public class IntSettingsGui extends AbstractSettingGui{
|
|||
}
|
||||
|
||||
public int getConfiguredValue(){
|
||||
return this.section.getInt(this.configPath, this.defaultVal);
|
||||
return this.config.getConfig().getInt(this.configPath, this.defaultVal);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractSettingGui create() {
|
||||
// Get value or default
|
||||
//TODO maybe get section dynamically (and maybe same for save ?)
|
||||
int now = getConfiguredValue();
|
||||
// create new gui
|
||||
return new IntSettingsGui(this, now);
|
||||
|
|
|
|||
|
|
@ -60,7 +60,9 @@ public class GuiGlobalActions {
|
|||
return event -> {
|
||||
event.setCancelled(true);
|
||||
// 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
|
||||
goal.updateGuiValues();
|
||||
// Then show
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue