mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 08:14:00 +02:00
Optimized list gui mapped to gui initialisation
This commit is contained in:
parent
36e26d762d
commit
e36f3a1694
13 changed files with 119 additions and 66 deletions
|
|
@ -9,6 +9,7 @@ import org.jetbrains.annotations.NotNull;
|
|||
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
||||
import xyz.alexcrea.cuanvil.dependency.DependencyManager;
|
||||
import xyz.alexcrea.cuanvil.gui.config.global.UnitRepairConfigGui;
|
||||
import xyz.alexcrea.cuanvil.gui.config.list.MappedGuiListConfigGui;
|
||||
import xyz.alexcrea.cuanvil.gui.config.list.UnitRepairElementListGui;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -94,9 +95,9 @@ public class UnitRepairApi {
|
|||
// Add to gui
|
||||
UnitRepairConfigGui repairConfigGui = UnitRepairConfigGui.getCurrentInstance();
|
||||
if(repairConfigGui != null) {
|
||||
UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit);
|
||||
UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit).getStored();
|
||||
|
||||
elementGui.updateValueForGeneric(repairableName, true);
|
||||
if(elementGui != null) elementGui.updateValueForGeneric(repairableName, true);
|
||||
repairConfigGui.updateValueForGeneric(unit, true);
|
||||
}
|
||||
|
||||
|
|
@ -124,14 +125,15 @@ public class UnitRepairApi {
|
|||
boolean lastValue = false;
|
||||
if(config.isConfigurationSection(unitName.toLowerCase())) {
|
||||
ConfigurationSection section = config.getConfigurationSection(unitName.toLowerCase());
|
||||
if(section.getKeys(false).isEmpty()) {
|
||||
|
||||
if(section != null && section.getKeys(false).isEmpty()) {
|
||||
lastValue = true;
|
||||
config.set(unitName.toLowerCase(), null);
|
||||
}
|
||||
|
||||
} else if (config.isConfigurationSection(unitName.toUpperCase())) {
|
||||
ConfigurationSection section = config.getConfigurationSection(unitName.toUpperCase());
|
||||
if(section.getKeys(false).isEmpty()) {
|
||||
if(section != null && section.getKeys(false).isEmpty()) {
|
||||
lastValue = true;
|
||||
config.set(unitName.toUpperCase(), null);
|
||||
}
|
||||
|
|
@ -146,9 +148,9 @@ public class UnitRepairApi {
|
|||
// Remove from gui
|
||||
UnitRepairConfigGui repairConfigGui = UnitRepairConfigGui.getCurrentInstance();
|
||||
if(repairConfigGui != null) {
|
||||
UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit);
|
||||
UnitRepairElementListGui elementGui = repairConfigGui.getInstanceOrCreate(unit).getStored();
|
||||
|
||||
elementGui.removeGeneric(repairableName);
|
||||
if(elementGui != null) elementGui.removeGeneric(repairableName);
|
||||
if(lastValue){
|
||||
repairConfigGui.removeGeneric(unit);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,8 @@ import xyz.alexcrea.cuanvil.util.CasedStringUtil;
|
|||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
public class CustomRecipeConfigGui extends MappedGuiListConfigGui<AnvilCustomRecipe, CustomRecipeSubSettingGui> {
|
||||
public class CustomRecipeConfigGui extends MappedGuiListConfigGui<AnvilCustomRecipe,
|
||||
MappedGuiListConfigGui.LazyElement<CustomRecipeSubSettingGui>> {
|
||||
|
||||
|
||||
private static CustomRecipeConfigGui INSTANCE = new CustomRecipeConfigGui();
|
||||
|
|
@ -44,15 +45,15 @@ public class CustomRecipeConfigGui extends MappedGuiListConfigGui<AnvilCustomRec
|
|||
protected ItemStack createItemForGeneric(AnvilCustomRecipe recipe) {
|
||||
// Get base item to display
|
||||
ItemStack craftResultItem = recipe.getResultItem();
|
||||
ItemStack displaydItem;
|
||||
ItemStack displayedItem;
|
||||
if(craftResultItem == null){
|
||||
displaydItem = new ItemStack(Material.BARRIER);
|
||||
displayedItem = new ItemStack(Material.BARRIER);
|
||||
}else{
|
||||
displaydItem = craftResultItem.clone();
|
||||
displayedItem = craftResultItem.clone();
|
||||
}
|
||||
|
||||
// edit displayed item
|
||||
ItemMeta meta = displaydItem.getItemMeta();
|
||||
ItemMeta meta = displayedItem.getItemMeta();
|
||||
assert meta != null;
|
||||
|
||||
meta.setDisplayName("§e" + CasedStringUtil.snakeToUpperSpacedCase(recipe.toString()) + " §fCustom recipe");
|
||||
|
|
@ -67,13 +68,13 @@ public class CustomRecipeConfigGui extends MappedGuiListConfigGui<AnvilCustomRec
|
|||
|
||||
));
|
||||
|
||||
displaydItem.setItemMeta(meta);
|
||||
return displaydItem;
|
||||
displayedItem.setItemMeta(meta);
|
||||
return displayedItem;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CustomRecipeSubSettingGui newInstanceOfGui(AnvilCustomRecipe generic, GuiItem item) {
|
||||
return new CustomRecipeSubSettingGui(this, generic, item);
|
||||
protected LazyElement<CustomRecipeSubSettingGui> newInstanceOfGui(AnvilCustomRecipe generic, GuiItem item) {
|
||||
return new LazyElement<>(item, () -> new CustomRecipeSubSettingGui(this, generic));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -18,7 +18,8 @@ import xyz.alexcrea.cuanvil.util.CasedStringUtil;
|
|||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
public class EnchantConflictGui extends MappedGuiListConfigGui<EnchantConflictGroup, EnchantConflictSubSettingGui> {
|
||||
public class EnchantConflictGui extends MappedGuiListConfigGui<EnchantConflictGroup,
|
||||
MappedGuiListConfigGui.LazyElement<EnchantConflictSubSettingGui>> {
|
||||
|
||||
private static EnchantConflictGui INSTANCE;
|
||||
|
||||
|
|
@ -86,8 +87,8 @@ public class EnchantConflictGui extends MappedGuiListConfigGui<EnchantConflictGr
|
|||
}
|
||||
|
||||
@Override
|
||||
protected EnchantConflictSubSettingGui newInstanceOfGui(EnchantConflictGroup conflict, GuiItem item) {
|
||||
return new EnchantConflictSubSettingGui(this, conflict, item);
|
||||
protected LazyElement<EnchantConflictSubSettingGui> newInstanceOfGui(EnchantConflictGroup conflict, GuiItem item) {
|
||||
return new LazyElement<>(item, () -> new EnchantConflictSubSettingGui(this, conflict));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -15,12 +15,13 @@ import xyz.alexcrea.cuanvil.group.ItemGroupManager;
|
|||
import xyz.alexcrea.cuanvil.gui.config.list.MappedGuiListConfigGui;
|
||||
import xyz.alexcrea.cuanvil.gui.config.list.elements.GroupConfigSubSettingGui;
|
||||
import xyz.alexcrea.cuanvil.util.CasedStringUtil;
|
||||
import xyz.alexcrea.cuanvil.util.LazyValue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
public class GroupConfigGui extends MappedGuiListConfigGui<IncludeGroup, GroupConfigSubSettingGui> {
|
||||
public class GroupConfigGui extends MappedGuiListConfigGui<IncludeGroup, MappedGuiListConfigGui.LazyElement<GroupConfigSubSettingGui>> {
|
||||
|
||||
private static GroupConfigGui INSTANCE;
|
||||
|
||||
|
|
@ -73,8 +74,8 @@ public class GroupConfigGui extends MappedGuiListConfigGui<IncludeGroup, GroupCo
|
|||
}
|
||||
|
||||
@Override
|
||||
protected GroupConfigSubSettingGui newInstanceOfGui(IncludeGroup group, GuiItem item) {
|
||||
return new GroupConfigSubSettingGui(this, group, item);
|
||||
protected LazyElement<GroupConfigSubSettingGui> newInstanceOfGui(IncludeGroup group, GuiItem item) {
|
||||
return new LazyElement<>(item, () -> new GroupConfigSubSettingGui(this, group));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -18,7 +18,8 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
public class UnitRepairConfigGui extends MappedGuiListConfigGui<Material, UnitRepairElementListGui> {
|
||||
public class UnitRepairConfigGui extends
|
||||
MappedGuiListConfigGui<Material, MappedGuiListConfigGui.LazyElement<UnitRepairElementListGui>> {
|
||||
|
||||
private static UnitRepairConfigGui INSTANCE;
|
||||
|
||||
|
|
@ -41,10 +42,12 @@ public class UnitRepairConfigGui extends MappedGuiListConfigGui<Material, UnitRe
|
|||
}
|
||||
|
||||
@Override
|
||||
protected UnitRepairElementListGui newInstanceOfGui(Material material, GuiItem item) {
|
||||
UnitRepairElementListGui element = new UnitRepairElementListGui(material, this, item);
|
||||
element.init();
|
||||
return element;
|
||||
protected LazyElement<UnitRepairElementListGui> newInstanceOfGui(Material material, GuiItem item) {
|
||||
return new LazyElement<>(item, () -> {
|
||||
UnitRepairElementListGui element = new UnitRepairElementListGui(material, this);
|
||||
element.init();
|
||||
return element;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -115,7 +118,7 @@ public class UnitRepairConfigGui extends MappedGuiListConfigGui<Material, UnitRe
|
|||
updateValueForGeneric(type, true);
|
||||
|
||||
// Display material edit setting
|
||||
this.elementGuiMap.get(type).getMappedGui().show(player);
|
||||
this.elementGuiMap.get(type).get().getMappedGui().show(player);
|
||||
},
|
||||
true
|
||||
).show(clickEvent.getWhoClicked());
|
||||
|
|
@ -123,8 +126,8 @@ public class UnitRepairConfigGui extends MappedGuiListConfigGui<Material, UnitRe
|
|||
}
|
||||
|
||||
@NotNull
|
||||
public UnitRepairElementListGui getInstanceOrCreate(Material mat){
|
||||
UnitRepairElementListGui element = this.elementGuiMap.get(mat);
|
||||
public LazyElement<UnitRepairElementListGui> getInstanceOrCreate(Material mat){
|
||||
LazyElement<UnitRepairElementListGui> element = this.elementGuiMap.get(mat);
|
||||
if(element == null){
|
||||
updateValueForGeneric(mat, false);
|
||||
|
||||
|
|
|
|||
|
|
@ -3,15 +3,19 @@ package xyz.alexcrea.cuanvil.gui.config.list;
|
|||
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
|
||||
import io.delilaheve.CustomAnvil;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import xyz.alexcrea.cuanvil.gui.config.list.elements.ElementMappedToListGui;
|
||||
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
|
||||
import xyz.alexcrea.cuanvil.util.LazyValue;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public abstract class MappedGuiListConfigGui< T, S extends ElementMappedToListGui> extends MappedElementListConfigGui< T, S > {
|
||||
public abstract class MappedGuiListConfigGui< T, S extends MappedGuiListConfigGui.LazyElement<?>>
|
||||
extends MappedElementListConfigGui< T, S > {
|
||||
|
||||
protected MappedGuiListConfigGui(@NotNull String title) {
|
||||
super(title);
|
||||
|
|
@ -20,7 +24,10 @@ public abstract class MappedGuiListConfigGui< T, S extends ElementMappedToListGu
|
|||
|
||||
@Override
|
||||
public void reloadValues() {
|
||||
this.elementGuiMap.forEach((conflict, gui) -> gui.cleanAndBeUnusable());
|
||||
this.elementGuiMap.forEach((conflict, element) -> {
|
||||
ElementMappedToListGui gui = element.getStored();
|
||||
if(gui != null) gui.cleanAndBeUnusable();
|
||||
});
|
||||
this.elementGuiMap.clear();
|
||||
|
||||
super.reloadValues();
|
||||
|
|
@ -30,23 +37,24 @@ public abstract class MappedGuiListConfigGui< T, S extends ElementMappedToListGu
|
|||
protected S newElementRequested(T generic, GuiItem newItem) {
|
||||
S element = newInstanceOfGui(generic, newItem);
|
||||
|
||||
newItem.setAction(GuiGlobalActions.openGuiAction(element.getMappedGui()));
|
||||
newItem.setAction(element.openAction());
|
||||
return element;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GuiItem findItemFromElement(T generic, S element) {
|
||||
return element.getParentItemForThisGui();
|
||||
return element.getParentItem();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateElement(T generic, S element) {
|
||||
element.updateLocal();
|
||||
ElementMappedToListGui gui = element.getStored();
|
||||
if(gui != null) gui.updateLocal();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GuiItem findGuiItemForRemoval(T generic, S element) {
|
||||
return element.getParentItemForThisGui();
|
||||
return element.getParentItem();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -90,7 +98,7 @@ public abstract class MappedGuiListConfigGui< T, S extends ElementMappedToListGu
|
|||
updateValueForGeneric(generic, true);
|
||||
|
||||
// show the new conflict config to the player
|
||||
this.elementGuiMap.get(generic).getMappedGui().show(player);
|
||||
this.elementGuiMap.get(generic).get().getMappedGui().show(player);
|
||||
|
||||
update();
|
||||
};
|
||||
|
|
@ -105,4 +113,28 @@ public abstract class MappedGuiListConfigGui< T, S extends ElementMappedToListGu
|
|||
|
||||
protected abstract T createAndSaveNewEmptyGeneric(String name);
|
||||
|
||||
public static class LazyElement<T extends ElementMappedToListGui> extends LazyValue<T> {
|
||||
|
||||
private final GuiItem parentItem;
|
||||
private final LazyValue<Consumer<InventoryClickEvent>> lazyOpenConsumer;
|
||||
public LazyElement(GuiItem parentItem, Supplier<T> valueSupplier) {
|
||||
super(valueSupplier);
|
||||
this.parentItem = parentItem;
|
||||
|
||||
this.lazyOpenConsumer = new LazyValue<>(() ->
|
||||
GuiGlobalActions.openGuiAction(this.get().getMappedGui()))
|
||||
;
|
||||
}
|
||||
|
||||
public GuiItem getParentItem() {
|
||||
return parentItem;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Consumer<InventoryClickEvent> openAction(){
|
||||
return event -> lazyOpenConsumer.get().accept(event);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,17 +26,14 @@ import java.util.function.Consumer;
|
|||
|
||||
public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, DoubleSettingGui.DoubleSettingFactory> implements ElementMappedToListGui {
|
||||
|
||||
private final GuiItem parentItem;
|
||||
private final Material parentMaterial;
|
||||
private final UnitRepairConfigGui parentGui;
|
||||
private final String materialName;
|
||||
|
||||
private boolean shouldWork = true;
|
||||
public UnitRepairElementListGui(@NotNull Material parentMaterial,
|
||||
@NotNull UnitRepairConfigGui parentGui,
|
||||
@NotNull GuiItem parentItem) {
|
||||
@NotNull UnitRepairConfigGui parentGui) {
|
||||
super("§e" + CasedStringUtil.snakeToUpperSpacedCase(parentMaterial.name().toLowerCase()) + " §rUnit repair");
|
||||
this.parentItem = parentItem;
|
||||
this.parentMaterial = parentMaterial;
|
||||
this.parentGui = parentGui;
|
||||
this.materialName = CasedStringUtil.snakeToUpperSpacedCase(parentMaterial.name().toLowerCase());
|
||||
|
|
@ -165,11 +162,6 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
|
|||
|
||||
// ElementMappedToListGui methods
|
||||
|
||||
@Override
|
||||
public GuiItem getParentItemForThisGui() {
|
||||
return this.parentItem;
|
||||
}
|
||||
|
||||
@Override // Not used in this implementation
|
||||
public void updateLocal() {}
|
||||
|
||||
|
|
|
|||
|
|
@ -35,9 +35,8 @@ public class CustomRecipeSubSettingGui extends MappedToListSubSettingGui {
|
|||
|
||||
public CustomRecipeSubSettingGui(
|
||||
@NotNull CustomRecipeConfigGui parent,
|
||||
@NotNull AnvilCustomRecipe anvilRecipe,
|
||||
@NotNull GuiItem parentItemForThisGui) {
|
||||
super(parentItemForThisGui, 3, "§e" + CasedStringUtil.snakeToUpperSpacedCase(anvilRecipe.toString()) + " §8Config");
|
||||
@NotNull AnvilCustomRecipe anvilRecipe) {
|
||||
super(3, "§e" + CasedStringUtil.snakeToUpperSpacedCase(anvilRecipe.toString()) + " §8Config");
|
||||
this.parent = parent;
|
||||
this.anvilRecipe = anvilRecipe;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,9 @@
|
|||
package xyz.alexcrea.cuanvil.gui.config.list.elements;
|
||||
|
||||
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
|
||||
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
|
||||
|
||||
public interface ElementMappedToListGui {
|
||||
|
||||
GuiItem getParentItemForThisGui();
|
||||
|
||||
void updateLocal();
|
||||
|
||||
void cleanAndBeUnusable();
|
||||
|
|
|
|||
|
|
@ -39,10 +39,8 @@ public class EnchantConflictSubSettingGui extends MappedToListSubSettingGui impl
|
|||
|
||||
public EnchantConflictSubSettingGui(
|
||||
@NotNull EnchantConflictGui parent,
|
||||
@NotNull EnchantConflictGroup enchantConflict,
|
||||
@NotNull GuiItem parentItemForThisGui) {
|
||||
super(parentItemForThisGui,
|
||||
3,
|
||||
@NotNull EnchantConflictGroup enchantConflict) {
|
||||
super(3,
|
||||
"§e" + CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.toString()) + " §8Config");
|
||||
this.parent = parent;
|
||||
this.enchantConflict = enchantConflict;
|
||||
|
|
|
|||
|
|
@ -37,9 +37,8 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen
|
|||
|
||||
public GroupConfigSubSettingGui(
|
||||
@NotNull GroupConfigGui parent,
|
||||
@NotNull IncludeGroup group,
|
||||
@NotNull GuiItem item) {
|
||||
super(item, 3,
|
||||
@NotNull IncludeGroup group) {
|
||||
super(3,
|
||||
CasedStringUtil.snakeToUpperSpacedCase(group.getName()) + " Config");
|
||||
this.parent = parent;
|
||||
this.group = group;
|
||||
|
|
|
|||
|
|
@ -9,18 +9,10 @@ import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
|
|||
|
||||
public abstract class MappedToListSubSettingGui extends ChestGui implements ValueUpdatableGui, ElementMappedToListGui {
|
||||
|
||||
private final GuiItem item;
|
||||
protected MappedToListSubSettingGui(
|
||||
GuiItem item,
|
||||
int rows,
|
||||
@NotNull String title) {
|
||||
super(rows, title, CustomAnvil.instance);
|
||||
this.item = item;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GuiItem getParentItemForThisGui() {
|
||||
return item;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
36
src/main/java/xyz/alexcrea/cuanvil/util/LazyValue.java
Normal file
36
src/main/java/xyz/alexcrea/cuanvil/util/LazyValue.java
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package xyz.alexcrea.cuanvil.util;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class LazyValue<T> {
|
||||
|
||||
private final Supplier<T> valueSupplier;
|
||||
private T storedValue;
|
||||
|
||||
public LazyValue(Supplier<T> valueSupplier) {
|
||||
this.valueSupplier = valueSupplier;
|
||||
this.storedValue = null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public T getStored(){
|
||||
return storedValue;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public T get(){
|
||||
if (storedValue != null) return storedValue;
|
||||
|
||||
synchronized(this) {
|
||||
if(storedValue == null) {
|
||||
storedValue = valueSupplier.get();
|
||||
}
|
||||
}
|
||||
|
||||
return storedValue;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue