Transform EnchantSelectSettingGui to be displayed as a list.

This commit is contained in:
alexcrea 2024-06-17 23:57:42 +02:00
parent 33fac3eed4
commit b1ca8bd91c
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
5 changed files with 71 additions and 50 deletions

View file

@ -57,7 +57,7 @@ public abstract class ElementListConfigGui< T > extends ChestGui implements Valu
protected ArrayList<OutlinePane> pages;
protected HashMap<UUID, Integer> pageMap;
public void init() { // Why I'm using an init function ?
public void init() { // Why I'm using an init function ? //TODO determine why is it using a init function and not used on constructor.
GuiGlobalItems.addBackgroundItem(this.backgroundPane);
this.backgroundPane.bindItem('1', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM);
addPane(this.backgroundPane);

View file

@ -38,16 +38,16 @@ public abstract class SettingGuiListConfigGui< T, S extends SettingGui.SettingGu
@Override
public void updateValueForGeneric(T generic, boolean shouldUpdate) {
S factory = this.factoryMap.get(generic);
if(factory == null){
if(!this.factoryMap.containsKey(generic)){
// Create new item & factory
factory = createFactory(generic);
S factory = createFactory(generic);
GuiItem newItem = itemFromFactory(generic, factory);
addToPage(newItem);
this.guiItemMap.put(generic, newItem);
this.factoryMap.put(generic, factory);
}else{
S factory = this.factoryMap.get(generic);
// Update old item
GuiItem oldItem = this.guiItemMap.get(generic);
@ -73,6 +73,7 @@ public abstract class SettingGuiListConfigGui< T, S extends SettingGui.SettingGu
oldITem.setProperties(newItem.getProperties());
oldITem.setVisible(newItem.isVisible());
}
@Override
protected GuiItem findGuiItemForRemoval(T generic) {
return this.guiItemMap.get(generic);

View file

@ -79,15 +79,15 @@ public class EnchantConflictSubSettingGui extends MappedToListSubSettingGui impl
this.pane.bindItem('D', new GuiItem(deleteItem, GuiGlobalActions.openGuiAction(createDeleteGui()), CustomAnvil.instance));
// Displayed item will be updated later
this.enchantSettingItem = new GuiItem(new ItemStack(Material.ENCHANTED_BOOK), (event) -> {
this.enchantSettingItem = new GuiItem(new ItemStack(Material.ENCHANTED_BOOK), event -> {
event.setCancelled(true);
EnchantSelectSettingGui enchantGui = new EnchantSelectSettingGui(
"\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.toString()) + " \u00A75Enchantments",
this, this, 0);
"\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.toString()) + "\u00A75",
this, this);
enchantGui.show(event.getWhoClicked());
}, CustomAnvil.instance);
this.groupSettingItem = new GuiItem(new ItemStack(Material.PAPER), (event) -> {
this.groupSettingItem = new GuiItem(new ItemStack(Material.PAPER), event -> {
event.setCancelled(true);
GroupSelectSettingGui enchantGui = new GroupSelectSettingGui(
"\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(this.enchantConflict.toString()) + " \u00A73Groups",

View file

@ -45,7 +45,6 @@ public abstract class AbstractSettingGui extends ChestGui implements SettingGui
}
protected GuiItem saveItem;
protected GuiItem noChangeItem;
/**
* Initialise and prepare value for this gui.
@ -61,15 +60,14 @@ public abstract class AbstractSettingGui extends ChestGui implements SettingGui
GuiGlobalItems.addBackgroundItem(pane);
saveItem = GuiGlobalItems.saveItem(this, parent);
noChangeItem = GuiGlobalItems.noChangeItem();
pane.bindItem('S', noChangeItem);
pane.bindItem('S', GuiGlobalItems.noChangeItem());
}
@Override
public void update() {
pane.bindItem('S', hadChange() ? saveItem : noChangeItem);
pane.bindItem('S', hadChange() ? saveItem : GuiGlobalItems.noChangeItem());
super.update();
}
@ -95,6 +93,7 @@ public abstract class AbstractSettingGui extends ChestGui implements SettingGui
*/
protected abstract Pattern getGuiPattern();
/**
* Most of the time a setting gui will be called from a global gui.
* <p>

View file

@ -1,9 +1,7 @@
package xyz.alexcrea.cuanvil.gui.config.settings;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.pane.Orientable;
import com.github.stefvanschie.inventoryframework.pane.OutlinePane;
import com.github.stefvanschie.inventoryframework.pane.Pane;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil;
import org.bukkit.Material;
@ -13,71 +11,67 @@ import org.bukkit.inventory.ItemFlag;
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.enchant.WrappedEnchantment;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.SelectEnchantmentContainer;
import xyz.alexcrea.cuanvil.gui.config.list.SettingGuiListConfigGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant;
import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class EnchantSelectSettingGui extends AbstractSettingGui {
public class EnchantSelectSettingGui extends SettingGuiListConfigGui<WrappedEnchantment, EnchantSelectSettingGui.DummyFactory> implements SettingGui {
SelectEnchantmentContainer enchantContainer;
int page;
private final SelectEnchantmentContainer enchantContainer;
Set<WrappedEnchantment> selectedEnchant;
private final Set<WrappedEnchantment> selectedEnchant;
private final GuiItem saveItem;
public EnchantSelectSettingGui(@NotNull String title, ValueUpdatableGui parent, SelectEnchantmentContainer enchantContainer, int page) {
super(6, title, parent);
public EnchantSelectSettingGui(@NotNull String title, ValueUpdatableGui parent, SelectEnchantmentContainer enchantContainer) {
super(title);
this.enchantContainer = enchantContainer;
// Not used and not planned rn
this.page = page;
this.selectedEnchant = new HashSet<>(enchantContainer.getSelectedEnchantments());
// Add secondary background item
this.getPane().bindItem('1', GuiSharedConstant.SECONDARY_BACKGROUND_ITEM);
this.saveItem = GuiGlobalItems.saveItem(this, parent);
this.backgroundPane.bindItem('S', GuiGlobalItems.noChangeItem());
initGroups();
init();
}
@Override
protected Pattern getGuiPattern() {
protected Pattern getBackgroundPattern() {
return new Pattern(
GuiSharedConstant.EMPTY_GUI_FULL_LINE,
GuiSharedConstant.EMPTY_GUI_FULL_LINE,
GuiSharedConstant.EMPTY_GUI_FULL_LINE,
GuiSharedConstant.EMPTY_GUI_FULL_LINE,
GuiSharedConstant.EMPTY_GUI_FULL_LINE,
"B1111111S"
"B11L1R11S"
);
}
protected void initGroups() {
// Add enchantment gui item
OutlinePane filledEnchant = new OutlinePane(0, 0, 9, 5);
filledEnchant.setPriority(Pane.Priority.HIGH);
filledEnchant.align(OutlinePane.Alignment.BEGIN);
filledEnchant.setOrientation(Orientable.Orientation.HORIZONTAL);
@Override
protected List<WrappedEnchantment> getEveryDisplayableInstanceOfGeneric() {
Set<WrappedEnchantment> illegalEnchantments = this.enchantContainer.illegalEnchantments();
Set<WrappedEnchantment> illegalEnchant = this.enchantContainer.illegalEnchantments();
for (WrappedEnchantment enchant : GuiSharedConstant.SORTED_ENCHANTMENT_LIST) {
if (illegalEnchant.contains(enchant)) {
return;
}
filledEnchant.addItem(getGuiItemFromEnchant(enchant));
return Arrays.stream(WrappedEnchantment.values())
.filter(enchantment -> !illegalEnchantments.contains(enchantment))
.collect(Collectors.toList());
}
addPane(filledEnchant);
@Override
public void update() {
this.backgroundPane.bindItem('S', hadChange() ? saveItem : GuiGlobalItems.noChangeItem());
super.update();
}
private GuiItem getGuiItemFromEnchant(WrappedEnchantment enchantment) {
@Override
protected GuiItem itemFromFactory(WrappedEnchantment enchantment, DummyFactory factory) {
boolean isIn = this.selectedEnchant.contains(enchantment);
Material usedMaterial;
@ -95,7 +89,6 @@ public class EnchantSelectSettingGui extends AbstractSettingGui {
return guiItem;
}
private static final List<String> TRUE_LORE = Collections.singletonList("\u00A77Value: \u00A7aSelected");
private static final List<String> FALSE_LORE = Collections.singletonList("\u00A77Value: \u00A7cNot Selected");
@ -157,4 +150,32 @@ public class EnchantSelectSettingGui extends AbstractSettingGui {
!baseGroup.containsAll(this.selectedEnchant);
}
// Unused methods and class
public static class DummyFactory extends AbstractSettingGui.SettingGuiFactory{
protected DummyFactory(@NotNull String configPath, @NotNull ConfigHolder config) {
super(configPath, config);
}
@Override
public Gui create() {
return null;
}
}
@Override
protected List<String> getCreateItemLore() {
return Collections.emptyList();
}
@Override
protected Consumer<InventoryClickEvent> getCreateClickConsumer() {
return null;
}
@Override
protected String createItemName() {
return null;
}
@Override
protected DummyFactory createFactory(WrappedEnchantment generic) {
return null;
}
}