Finished the MaterialSelectSettingGui.

This commit is contained in:
alexcrea 2024-04-25 22:27:50 +02:00
parent d622511e3c
commit ebe3708eca
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
6 changed files with 332 additions and 191 deletions

View file

@ -19,11 +19,14 @@ import java.util.logging.Level;
public class ConfirmActionGui extends AbstractAskGui { public class ConfirmActionGui extends AbstractAskGui {
public ConfirmActionGui(@NotNull String title, String actionDescription, public ConfirmActionGui(@NotNull String title, String actionDescription,
Gui backOnCancel, Gui backOnConfirm, Supplier<Boolean> onConfirm) { Gui backOnCancel, Gui backOnConfirm, Supplier<Boolean> onConfirm,
boolean permanent) {
super(3, title, backOnCancel); super(3, title, backOnCancel);
// Save item // Save item
this.pane.bindItem('S', new GuiItem(GuiSharedConstant.CONFIRM_ITEM, event -> { this.pane.bindItem('S', new GuiItem(
(permanent ? GuiSharedConstant.CONFIRM_PERMANENT_ITEM : GuiSharedConstant.CONFIRM_ITEM),
event -> {
event.setCancelled(true); event.setCancelled(true);
HumanEntity player = event.getWhoClicked(); HumanEntity player = event.getWhoClicked();
@ -53,12 +56,19 @@ public class ConfirmActionGui extends AbstractAskGui {
ItemMeta infoMeta = infoItem.getItemMeta(); ItemMeta infoMeta = infoItem.getItemMeta();
infoMeta.setDisplayName("\u00A7eAre you sure ?"); infoMeta.setDisplayName("\u00A7eAre you sure ?");
if(actionDescription != null){
infoMeta.setLore(Arrays.asList(actionDescription.split("\n"))); infoMeta.setLore(Arrays.asList(actionDescription.split("\n")));
}
infoItem.setItemMeta(infoMeta); infoItem.setItemMeta(infoMeta);
pane.bindItem('I', new GuiItem(infoItem, GuiGlobalActions.stayInPlace, CustomAnvil.instance)); pane.bindItem('I', new GuiItem(infoItem, GuiGlobalActions.stayInPlace, CustomAnvil.instance));
} }
public ConfirmActionGui(@NotNull String title, String actionDescription,
Gui backOnCancel, Gui backOnConfirm, Supplier<Boolean> onConfirm){
this(title, actionDescription, backOnCancel, backOnConfirm, onConfirm, true);
}
@Override @Override
protected Pattern getGuiPattern() { protected Pattern getGuiPattern() {

View file

@ -1,181 +0,0 @@
package xyz.alexcrea.cuanvil.gui.config.list;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import com.github.stefvanschie.inventoryframework.pane.util.Pattern;
import io.delilaheve.CustomAnvil;
import io.delilaheve.util.ConfigOptions;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
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.gui.config.SelectMaterialContainer;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.logging.Level;
public class MaterialSelectSettingGui extends MappedElementListConfigGui<Material, GuiItem> {
private final SelectMaterialContainer selector;
private final List<Material> defaultMaterials;
private final int defaultMaterialHash;
private int nowMaterialHash;
public MaterialSelectSettingGui(
@NotNull SelectMaterialContainer selector,
@NotNull String title,
@NotNull Gui backGui) {
super(title);
this.selector = selector;
this.defaultMaterials = new ArrayList<>(this.selector.getSelectedMaterials());
this.defaultMaterialHash = hashFromMaterialList(this.defaultMaterials);
this.nowMaterialHash = this.defaultMaterialHash;
init();
// Change back item
this.backgroundPane.bindItem('B', GuiGlobalItems.backItem(backGui));
}
@Override
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,
"BT1LAR11S"
);
}
private GuiItem saveItem;
private GuiItem noChangeItem;
@Override
protected void prepareStaticValues() {
super.prepareStaticValues();
// Temporary leave item
GuiItem temporaryLeave = GuiGlobalItems.temporaryCloseGuiToSelectItem(Material.YELLOW_TERRACOTTA, this);
this.backgroundPane.bindItem('T', temporaryLeave);
// Select new mat item
ItemStack selectItem = new ItemStack(Material.LIME_STAINED_GLASS_PANE);
ItemMeta selectMeta = selectItem.getItemMeta();
selectMeta.setDisplayName("aaaaaaa");
selectItem.setItemMeta(selectMeta);
this.backgroundPane.bindItem('A', new GuiItem(selectItem, setItemAsCursor(), CustomAnvil.instance));
// Save item
this.saveItem = GuiGlobalItems.noChangeItem();
this.noChangeItem = GuiGlobalItems.noChangeItem();
this.backgroundPane.bindItem('S', this.noChangeItem);
}
/**
* @return A consumer to update the current setting's value.
*/
protected Consumer<InventoryClickEvent> setItemAsCursor() {
return event -> {
event.setCancelled(true);
HumanEntity player = event.getWhoClicked();
ItemStack cursor = player.getItemOnCursor();
if(cursor.getType().isAir()) return;
Material cursorMat = cursor.getType();
if(!this.elementGuiMap.containsKey(cursorMat)){
updateValueForGeneric(cursorMat, true);
this.nowMaterialHash ^= cursorMat.hashCode();
testCanSave();
}
update();
};
}
@Override
protected ItemStack createItemForGeneric(Material material) {
return new ItemStack(material); //this is temp TODO the function
}
@Override
protected List<Material> getEveryDisplayableInstanceOfGeneric() {
return this.defaultMaterials;
}
@Override
protected void updateElement(Material generic, GuiItem element) {
if(ConfigOptions.INSTANCE.getDebugLog()){
CustomAnvil.instance.getLogger().log(Level.INFO,
"Call that should not happen happened...",
new IllegalStateException());
}
}
@Override
protected GuiItem newElementRequested(Material generic, GuiItem newItem) {
newItem.setAction(GuiGlobalActions.stayInPlace); //TODO ask to remove the item on click (or instant remove)
return newItem;
}
@Override
protected GuiItem findItemFromElement(Material generic, GuiItem element) {
return element;
}
@Override
protected GuiItem findGuiItemForRemoval(Material generic, GuiItem element) {
return element;
}
private static int hashFromMaterialList(List<Material> materialList){
int defaultMaterialHash = 0;
for (Material material : materialList) {
defaultMaterialHash ^= material.hashCode();
}
return defaultMaterialHash;
}
private void testCanSave() {
if(this.defaultMaterialHash == this.nowMaterialHash){
this.backgroundPane.bindItem('S', this.noChangeItem);
}else{
this.backgroundPane.bindItem('S', this.saveItem);
}
}
// Unused functions.
@Override
protected GuiItem prepareCreateNewItem() {// Not used
return null;
}
@Override
protected Consumer<String> prepareCreateItemConsumer(HumanEntity player) {// Not used
return null;
}
@Override
protected String genericDisplayedName() {// Not Used
return null;
}
}

View file

@ -17,8 +17,8 @@ import xyz.alexcrea.cuanvil.gui.config.SelectGroupContainer;
import xyz.alexcrea.cuanvil.gui.config.SelectMaterialContainer; import xyz.alexcrea.cuanvil.gui.config.SelectMaterialContainer;
import xyz.alexcrea.cuanvil.gui.config.ask.ConfirmActionGui; import xyz.alexcrea.cuanvil.gui.config.ask.ConfirmActionGui;
import xyz.alexcrea.cuanvil.gui.config.global.GroupConfigGui; import xyz.alexcrea.cuanvil.gui.config.global.GroupConfigGui;
import xyz.alexcrea.cuanvil.gui.config.list.MaterialSelectSettingGui;
import xyz.alexcrea.cuanvil.gui.config.settings.GroupSelectSettingGui; import xyz.alexcrea.cuanvil.gui.config.settings.GroupSelectSettingGui;
import xyz.alexcrea.cuanvil.gui.config.settings.MaterialSelectSettingGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems; import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant;
@ -233,7 +233,7 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen
@Override @Override
public void cleanAndBeUnusable() { public void cleanAndBeUnusable() {
//TODO
} }
// ---------------------------- // ----------------------------
@ -294,6 +294,7 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen
// ---------------------------- // ----------------------------
// End of SelectGroupContainer related methods // End of SelectGroupContainer related methods
// ----------------------------
// SelectGroupContainer related methods // SelectGroupContainer related methods
// ---------------------------- // ----------------------------
@ -320,14 +321,14 @@ public class GroupConfigSubSettingGui extends MappedToListSubSettingGui implemen
// Save file configuration to disk // Save file configuration to disk
if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) { if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) {
return ConfigHolder.CONFLICT_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE); return ConfigHolder.ITEM_GROUP_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE);
} }
return true; return true;
} }
@Override @Override
public EnumSet<Material> illegalMaterials() { public EnumSet<Material> illegalMaterials() {
return EnumSet.noneOf(Material.class); return EnumSet.of(Material.AIR);
} }
// ---------------------------- // ----------------------------

View file

@ -0,0 +1,304 @@
package xyz.alexcrea.cuanvil.gui.config.settings;
import com.github.stefvanschie.inventoryframework.gui.GuiItem;
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;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.InventoryClickEvent;
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.gui.config.SelectMaterialContainer;
import xyz.alexcrea.cuanvil.gui.config.ask.ConfirmActionGui;
import xyz.alexcrea.cuanvil.gui.config.list.MappedElementListConfigGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalActions;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant;
import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import java.util.*;
import java.util.function.Consumer;
public class MaterialSelectSettingGui extends MappedElementListConfigGui<Material, GuiItem> {
private final SelectMaterialContainer selector;
private final Gui backGui;
private boolean instantRemove;
private final List<Material> defaultMaterials;
private final EnumSet<Material> illegalMaterials;
private final int defaultMaterialHash;
private int nowMaterialHash;
public MaterialSelectSettingGui(
@NotNull SelectMaterialContainer selector,
@NotNull String title,
@NotNull Gui backGui) {
super(title);
this.selector = selector;
this.backGui = backGui;
this.instantRemove = false;
this.defaultMaterials = new ArrayList<>(this.selector.getSelectedMaterials());
this.illegalMaterials = this.selector.illegalMaterials();
this.defaultMaterialHash = hashFromMaterialList(this.defaultMaterials);
this.nowMaterialHash = this.defaultMaterialHash;
init();
// Change back item
this.backgroundPane.bindItem('B', GuiGlobalItems.backItem(backGui));
}
@Override
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,
"BT1LAR1IS"
);
}
private GuiItem saveItem;
private GuiItem noChangeItem;
private GuiItem instantRemoveOn;
private GuiItem instantRemoveOff;
@Override
protected void prepareStaticValues() {
super.prepareStaticValues();
// Temporary leave item
GuiItem temporaryLeave = GuiGlobalItems.temporaryCloseGuiToSelectItem(Material.YELLOW_TERRACOTTA, this);
this.backgroundPane.bindItem('T', temporaryLeave);
// Select new mat item
ItemStack selectItem = new ItemStack(Material.BLUE_STAINED_GLASS_PANE);
ItemMeta selectMeta = selectItem.getItemMeta();
selectMeta.setDisplayName("\u00A7aAdd Item");
selectMeta.setLore(Arrays.asList(
"\u00A77Click here with an item to add",
"\u00A77it's Material to the list."));
selectItem.setItemMeta(selectMeta);
this.backgroundPane.bindItem('A', new GuiItem(selectItem, setItemAsCursor(), CustomAnvil.instance));
// Save item
this.saveItem = prepareSaveItem();
this.noChangeItem = GuiGlobalItems.noChangeItem();
this.backgroundPane.bindItem('S', this.noChangeItem);
// Instant Remove On item
ItemStack instantRemoveOnItem = new ItemStack(Material.LIME_STAINED_GLASS_PANE);
ItemMeta instantRemoveOnMeta = instantRemoveOnItem.getItemMeta();
instantRemoveOnMeta.setDisplayName("\u00A7eInstant remove is \u00A7aEnabled \u00A7e!");
instantRemoveOnMeta.setLore(
Collections.singletonList("\u00A77Click here to disable the instant remove"));
instantRemoveOnItem.setItemMeta(instantRemoveOnMeta);
// Instant Remove Off item
ItemStack instantRemoveOffItem = new ItemStack(Material.RED_STAINED_GLASS_PANE);
ItemMeta instantRemoveOffMeta = instantRemoveOffItem.getItemMeta();
instantRemoveOffMeta.setDisplayName("\u00A7eInstant remove is \u00A7cDisabled \u00A7e!");
instantRemoveOffMeta.setLore(
Collections.singletonList("\u00A77Click here to enable the instant remove"));
instantRemoveOffItem.setItemMeta(instantRemoveOffMeta);
// Instant Remove gui items
this.instantRemoveOn = new GuiItem(instantRemoveOnItem, (event -> {
this.instantRemove = false;
this.backgroundPane.bindItem('I', this.instantRemoveOff);
update();
}), CustomAnvil.instance);
this.instantRemoveOff = new GuiItem(instantRemoveOffItem, (event -> {
this.instantRemove = true;
this.backgroundPane.bindItem('I', this.instantRemoveOn);
update();
}), CustomAnvil.instance);
this.backgroundPane.bindItem('I', this.instantRemoveOff);
}
private GuiItem prepareSaveItem() {
ItemStack saveItemStack = new ItemStack(GuiGlobalItems.DEFAULT_SAVE_ITEM);
ItemMeta saveMeta = saveItemStack.getItemMeta();
saveMeta.setDisplayName("\u00A7aSave");
saveItemStack.setItemMeta(saveMeta);
return new GuiItem(saveItemStack, event -> {
event.setCancelled(true);
HumanEntity player = event.getWhoClicked();
// Do not allow to save configuration if player do not have edit configuration permission
if (!player.hasPermission(CustomAnvil.editConfigPermission)) {
player.closeInventory();
player.sendMessage(GuiGlobalActions.NO_EDIT_PERM);
return;
}
if(!testCanSave()) return;
// Save setting
EnumSet<Material> result = EnumSet.noneOf(Material.class);
result.addAll(this.elementGuiMap.keySet());
if(!this.selector.setSelectedMaterials(result)){
player.sendMessage("\u00A7cSomething went wrong while saving the change of value.");
}
// Return to parent
this.backGui.show(player);
}, CustomAnvil.instance);
}
/**
* @return A consumer to update the current setting's value.
*/
protected Consumer<InventoryClickEvent> setItemAsCursor() {
return event -> {
event.setCancelled(true);
HumanEntity player = event.getWhoClicked();
ItemStack cursor = player.getItemOnCursor();
// Test if cursor material allowed
Material cursorMat = cursor.getType();
if(cursorMat.isAir()) return;
if(this.illegalMaterials.contains(cursorMat)) return;
// Update gui only if item did not exist before.
if(!this.elementGuiMap.containsKey(cursorMat)){
updateValueForGeneric(cursorMat, true);
this.nowMaterialHash ^= cursorMat.hashCode();
setSaveItem();
update();
}
};
}
@Override
protected ItemStack createItemForGeneric(Material material) {
ItemStack item = new ItemStack(material);
ItemMeta meta = item.getItemMeta();
if(meta == null) return item;
meta.setDisplayName("\u00A7f" + CasedStringUtil.snakeToUpperSpacedCase(material.name()));
meta.setLore(Collections.singletonList("\u00A77Click here to remove this material from the list"));
meta.addItemFlags(ItemFlag.values());
item.setItemMeta(meta);
return item;
}
@Override
protected List<Material> getEveryDisplayableInstanceOfGeneric() {
return this.defaultMaterials;
}
@Override
protected void updateElement(Material material, GuiItem element) {
// Nothing happen here I think
}
@Override
protected GuiItem newElementRequested(Material material, GuiItem newItem) {
newItem.setAction(event -> {
if(this.instantRemove){
removeMaterial(material);
}else {
String materialName = CasedStringUtil.snakeToUpperSpacedCase(material.name().toLowerCase());
// Create and show confirm remove gui.
ConfirmActionGui confirmGui = new ConfirmActionGui(
"Remove " + materialName,
"\u00A77Confirm Remove " + materialName.toLowerCase() + " from this list.",
this, this,
() -> {
removeMaterial(material);
return true;
}, false
);
confirmGui.show(event.getWhoClicked());
}
});
return newItem;
}
private void removeMaterial(Material material) {
if(this.elementGuiMap.containsKey(material)){
this.nowMaterialHash ^= material.hashCode();
setSaveItem();
removeGeneric(material);
}
}
@Override
protected GuiItem findItemFromElement(Material generic, GuiItem element) {
return element;
}
@Override
protected GuiItem findGuiItemForRemoval(Material generic, GuiItem element) {
return element;
}
private static int hashFromMaterialList(List<Material> materialList){
int defaultMaterialHash = 0;
for (Material material : materialList) {
defaultMaterialHash ^= material.hashCode();
}
return defaultMaterialHash;
}
private void setSaveItem() {
if(!testCanSave()){
this.backgroundPane.bindItem('S', this.noChangeItem);
}else{
this.backgroundPane.bindItem('S', this.saveItem);
}
}
private boolean testCanSave() {
return this.defaultMaterialHash != this.nowMaterialHash;
}
// Unused functions.
@Override
protected GuiItem prepareCreateNewItem() {// Not used
return null;
}
@Override
protected Consumer<String> prepareCreateItemConsumer(HumanEntity player) {// Not used
return null;
}
@Override
protected String genericDisplayedName() {// Not Used
return null;
}
}

View file

@ -117,8 +117,8 @@ public class GuiGlobalItems {
addBackgroundItem(target, DEFAULT_BACKGROUND_MAT); addBackgroundItem(target, DEFAULT_BACKGROUND_MAT);
} }
private static final Material DEFAULT_SAVE_ITEM = Material.LIME_DYE; public static final Material DEFAULT_SAVE_ITEM = Material.LIME_DYE;
private static final Material DEFAULT_NO_CHANGE_ITEM = Material.GRAY_DYE; public static final Material DEFAULT_NO_CHANGE_ITEM = Material.GRAY_DYE;
/** /**
* Create a new save setting GuiItem. * Create a new save setting GuiItem.

View file

@ -55,6 +55,7 @@ public class GuiSharedConstant {
public static final ItemStack CANCEL_ITEM; public static final ItemStack CANCEL_ITEM;
public static final ItemStack CONFIRM_ITEM; public static final ItemStack CONFIRM_ITEM;
public static final ItemStack CONFIRM_PERMANENT_ITEM;
static { static {
CANCEL_ITEM = new ItemStack(Material.RED_TERRACOTTA); CANCEL_ITEM = new ItemStack(Material.RED_TERRACOTTA);
@ -66,9 +67,15 @@ public class GuiSharedConstant {
CONFIRM_ITEM = new ItemStack(Material.GREEN_TERRACOTTA); CONFIRM_ITEM = new ItemStack(Material.GREEN_TERRACOTTA);
meta = CONFIRM_ITEM.getItemMeta(); meta = CONFIRM_ITEM.getItemMeta();
meta.setDisplayName("\u00A7aConfirm"); meta.setDisplayName("\u00A7aConfirm");
meta.setLore(Collections.singletonList("\u00A77Confirm current action."));
CONFIRM_ITEM.setItemMeta(meta);
CONFIRM_PERMANENT_ITEM = new ItemStack(Material.GREEN_TERRACOTTA);
meta = CONFIRM_PERMANENT_ITEM.getItemMeta();
meta.setDisplayName("\u00A7aConfirm");
meta.setLore(Arrays.asList("\u00A77Confirm current action.", meta.setLore(Arrays.asList("\u00A77Confirm current action.",
"\u00A74Cation: This action can't be canceled.")); "\u00A74Cation: This action can't be canceled."));
CONFIRM_ITEM.setItemMeta(meta); CONFIRM_PERMANENT_ITEM.setItemMeta(meta);
} }