mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
add custom item to boolean setting button
This commit is contained in:
parent
48f60bc38e
commit
0b11f5b66f
6 changed files with 80 additions and 13 deletions
|
|
@ -9,6 +9,9 @@ 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.gui.config.settings.AbstractSettingGui;
|
import xyz.alexcrea.cuanvil.gui.config.settings.AbstractSettingGui;
|
||||||
|
import xyz.alexcrea.cuanvil.gui.config.settings.BoolSettingsGui;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
// maybe use builder patern ?
|
// maybe use builder patern ?
|
||||||
public class GuiGlobalItems {
|
public class GuiGlobalItems {
|
||||||
|
|
@ -75,4 +78,39 @@ public class GuiGlobalItems {
|
||||||
return new GuiItem(item, GuiGlobalActions.openSettingGuiAction(factory), CustomAnvil.instance);
|
return new GuiItem(item, GuiGlobalActions.openSettingGuiAction(factory), CustomAnvil.instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final String SETTING_ITEM_LORE_PREFIX = "\u00A77value: ";
|
||||||
|
public static GuiItem boolSettingGuiItem(
|
||||||
|
@NotNull BoolSettingsGui.BoolSettingFactory factory
|
||||||
|
){
|
||||||
|
// Get item properties
|
||||||
|
boolean value = factory.getConfiguredValue();
|
||||||
|
|
||||||
|
Material itemMat;
|
||||||
|
StringBuilder itemName = new StringBuilder("\u00A7");
|
||||||
|
if(value){
|
||||||
|
itemMat = Material.GREEN_TERRACOTTA;
|
||||||
|
itemName.append("a");
|
||||||
|
}else{
|
||||||
|
itemMat = Material.RED_TERRACOTTA;
|
||||||
|
itemName.append("c");
|
||||||
|
}
|
||||||
|
itemName.append(getConfigNameFromPath(factory.getConfigPath()));
|
||||||
|
|
||||||
|
// Create item
|
||||||
|
ItemStack item = new ItemStack(itemMat);
|
||||||
|
ItemMeta itemMeta = item.getItemMeta();
|
||||||
|
|
||||||
|
itemMeta.setDisplayName(itemName.toString());
|
||||||
|
itemMeta.setLore(Collections.singletonList(SETTING_ITEM_LORE_PREFIX+value));
|
||||||
|
|
||||||
|
item.setItemMeta(itemMeta);
|
||||||
|
return openSettingGuiItem(item, factory);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getConfigNameFromPath(String path){
|
||||||
|
int indexOfDot = path.indexOf(".");
|
||||||
|
//if(indexOfDot == -1) return path;
|
||||||
|
// indexOfDot == -1 (not fond) imply indexOfDot+1 = 0. substring will keep the full path as expected
|
||||||
|
return path.substring(indexOfDot+1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
package xyz.alexcrea.cuanvil.gui;
|
||||||
|
|
||||||
|
public interface UpdatableGlobalGui {
|
||||||
|
|
||||||
|
void updateGuiForAll();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -43,9 +43,8 @@ public class BasicConfigGui extends ChestGui {
|
||||||
GuiItem setting1 = GuiGlobalItems.openSettingGuiItem(setting1Item, factory1);
|
GuiItem setting1 = GuiGlobalItems.openSettingGuiItem(setting1Item, factory1);
|
||||||
pane.bindItem('1', setting1);
|
pane.bindItem('1', setting1);
|
||||||
|
|
||||||
ItemStack setting2Item = new ItemStack(Material.STONE);
|
BoolSettingsGui.BoolSettingFactory factory2 = BoolSettingsGui.factory("Test Gui bool",this, "test2", CustomAnvil.instance.getConfig(), false);
|
||||||
AbstractSettingGui.SettingGuiFactory factory2 = BoolSettingsGui.factory("Test Gui bool",this, "test2", CustomAnvil.instance.getConfig(), false);
|
GuiItem setting2 = GuiGlobalItems.boolSettingGuiItem(factory2);
|
||||||
GuiItem setting2 = GuiGlobalItems.openSettingGuiItem(setting2Item, factory2);
|
|
||||||
pane.bindItem('2', setting2);
|
pane.bindItem('2', setting2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
|
||||||
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.gui.GuiGlobalItems;
|
import xyz.alexcrea.cuanvil.gui.GuiGlobalItems;
|
||||||
|
|
||||||
|
|
@ -50,6 +51,23 @@ public abstract class AbstractSettingGui extends ChestGui {
|
||||||
|
|
||||||
|
|
||||||
public abstract static class SettingGuiFactory{
|
public abstract static class SettingGuiFactory{
|
||||||
public abstract AbstractSettingGui create();
|
protected String configPath;
|
||||||
};
|
protected ConfigurationSection section;
|
||||||
|
|
||||||
|
protected SettingGuiFactory(String configPath, ConfigurationSection section){
|
||||||
|
this.configPath = configPath;
|
||||||
|
this.section = section;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getConfigPath() {
|
||||||
|
return configPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConfigurationSection getSection() {
|
||||||
|
return section;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public abstract AbstractSettingGui create();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ public class BoolSettingsGui extends AbstractSettingGui{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SettingGuiFactory factory(@NotNull String title, Gui parent,
|
public static BoolSettingFactory factory(@NotNull String title, Gui parent,
|
||||||
String configPath, ConfigurationSection section,
|
String configPath, ConfigurationSection section,
|
||||||
boolean defaultVal){
|
boolean defaultVal){
|
||||||
return new BoolSettingFactory(
|
return new BoolSettingFactory(
|
||||||
|
|
@ -91,24 +91,27 @@ public class BoolSettingsGui extends AbstractSettingGui{
|
||||||
|
|
||||||
public static class BoolSettingFactory extends SettingGuiFactory{
|
public static class BoolSettingFactory extends SettingGuiFactory{
|
||||||
@NotNull String title; Gui parent;
|
@NotNull String title; Gui parent;
|
||||||
String configPath; ConfigurationSection section;
|
|
||||||
boolean defaultVal;
|
boolean defaultVal;
|
||||||
|
|
||||||
private BoolSettingFactory(@NotNull String title, Gui parent,
|
private BoolSettingFactory(@NotNull String title, Gui parent,
|
||||||
String configPath, ConfigurationSection section,
|
String configPath, ConfigurationSection section,
|
||||||
boolean defaultVal){
|
boolean defaultVal){
|
||||||
|
super(configPath, section);
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
this.configPath = configPath;
|
|
||||||
this.section = section;
|
|
||||||
this.defaultVal = defaultVal;
|
this.defaultVal = defaultVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean getConfiguredValue(){
|
||||||
|
return this.section.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 ?)
|
//TODO maybe get section dynamically (and maybe same for save ?)
|
||||||
boolean now = section.getBoolean(this.configPath, this.defaultVal);
|
boolean now = getConfiguredValue();
|
||||||
// create new gui
|
// create new gui
|
||||||
return new BoolSettingsGui(this, now);
|
return new BoolSettingsGui(this, now);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -118,27 +118,29 @@ public class IntSettingsGui extends AbstractSettingGui{
|
||||||
|
|
||||||
public static class IntSettingFactory extends SettingGuiFactory{
|
public static class IntSettingFactory extends SettingGuiFactory{
|
||||||
@NotNull String title; Gui parent;
|
@NotNull String title; Gui parent;
|
||||||
String configPath; ConfigurationSection section;
|
|
||||||
int min; int max; int defaultVal; int[] steps;
|
int min; int max; int defaultVal; int[] steps;
|
||||||
|
|
||||||
private IntSettingFactory(@NotNull String title, Gui parent,
|
private IntSettingFactory(@NotNull String title, Gui parent,
|
||||||
String configPath, ConfigurationSection section,
|
String configPath, ConfigurationSection section,
|
||||||
int min, int max, int defaultVal, int... steps){
|
int min, int max, int defaultVal, int... steps){
|
||||||
|
super(configPath, section);
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
this.configPath = configPath;
|
|
||||||
this.section = section;
|
|
||||||
this.min = min;
|
this.min = min;
|
||||||
this.max = max;
|
this.max = max;
|
||||||
this.defaultVal = defaultVal;
|
this.defaultVal = defaultVal;
|
||||||
this.steps = steps;
|
this.steps = steps;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getConfiguredValue(){
|
||||||
|
return this.section.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 ?)
|
//TODO maybe get section dynamically (and maybe same for save ?)
|
||||||
int now = section.getInt(this.configPath, this.defaultVal);
|
int now = getConfiguredValue();
|
||||||
// create new gui
|
// create new gui
|
||||||
return new IntSettingsGui(this, now);
|
return new IntSettingsGui(this, now);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue