Add group select gui and edit group logic to be able to handle group set.

This commit is contained in:
alexcrea 2024-03-20 22:32:44 +01:00
parent b4cb6c2848
commit 41235d3024
8 changed files with 216 additions and 19 deletions

View file

@ -9,7 +9,8 @@ import java.util.Set;
public interface SelectEnchantmentContainer {
List<Enchantment> getSelectedEnchantments();
void setSelectedEnchantments(List<Enchantment> enchantments);
boolean setSelectedEnchantments(List<Enchantment> enchantments);
Set<Enchantment> illegalEnchantments();

View file

@ -7,8 +7,9 @@ import java.util.Set;
public interface SelectGroupContainer {
List<AbstractMaterialGroup> getSelectedGroups();
void setSelectedGroups(List<AbstractMaterialGroup> groups);
Set<AbstractMaterialGroup> getSelectedGroups();
boolean setSelectedGroups(Set<AbstractMaterialGroup> groups);
Set<AbstractMaterialGroup> illegalGroups();

View file

@ -1,16 +1,15 @@
package xyz.alexcrea.cuanvil.gui.config;
import javafx.scene.paint.Material;
import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup;
import org.bukkit.Material;
import java.util.List;
import java.util.Set;
import java.util.EnumSet;
public interface SelectMaterialContainer {
List<Material> getSelectedMaterials();
void setSelectedMaterials(List<Material> materials);
EnumSet<Material> getSelectedMaterials();
Set<Material> illegalMaterials();
boolean setSelectedMaterials(EnumSet<Material> materials);
EnumSet<Material> illegalMaterials();
}

View file

@ -0,0 +1,140 @@
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.pane.util.Pattern;
import io.delilaheve.CustomAnvil;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
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.config.ConfigHolder;
import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.SelectGroupContainer;
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.function.Consumer;
public class GroupSelectSettingGui extends AbstractSettingGui{
SelectGroupContainer groupContainer;
int page;
HashSet<AbstractMaterialGroup> selectedGroups;
public GroupSelectSettingGui(@NotNull String title, ValueUpdatableGui parent, SelectGroupContainer groupContainer, int page) {
super(6, title, parent);
this.groupContainer = groupContainer;
//Not used but planned
this.page = page;
this.selectedGroups = new HashSet<>(groupContainer.getSelectedGroups());
initGroups();
}
@Override
protected Pattern getGuiPattern() {
return new Pattern(
"000000000",
"000000000",
"000000000",
"000000000",
"000000000",
"B1111111S"
);
}
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);
Set<AbstractMaterialGroup> illegalGroup = this.groupContainer.illegalGroups();
ConfigHolder.ITEM_GROUP_HOLDER.getItemGroupsManager().getGroupMap().forEach((name, group)->{
if(illegalGroup.contains(group)) {
return;
}
filledEnchant.addItem(getGuiItemFromGroup(group));
});
addPane(filledEnchant);
}
private GuiItem getGuiItemFromGroup(AbstractMaterialGroup group){
boolean isIn = this.selectedGroups.contains(group);
Material usedMaterial = Material.PAPER;
ItemStack item = new ItemStack(usedMaterial);
setGroupItemMeta(item, group.getName(), isIn);
GuiItem guiItem = new GuiItem(item, CustomAnvil.instance);
guiItem.setAction(getGrpupItemConsumer(group, guiItem));
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");
public void setGroupItemMeta(ItemStack item, String name, boolean isIn){
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("\u00A7"+(isIn ? 'a' : 'c')+ CasedStringUtil.snakeToUpperSpacedCase(name));
if(isIn){
meta.addEnchant(Enchantment.DAMAGE_UNDEAD, 1, true);
meta.setLore(TRUE_LORE);
}else{
meta.removeEnchant(Enchantment.DAMAGE_UNDEAD);
meta.setLore(FALSE_LORE);
}
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES, ItemFlag.HIDE_ENCHANTS);
item.setItemMeta(meta);
}
private Consumer<InventoryClickEvent> getGrpupItemConsumer(AbstractMaterialGroup group, GuiItem guiItem){
return event -> {
event.setCancelled(true);
boolean isIn = this.selectedGroups.contains(group);
if(isIn){
this.selectedGroups.remove(group);
}else{
this.selectedGroups.add(group);
}
ItemStack item = guiItem.getItem();
setGroupItemMeta(item, group.getName(), !isIn);
guiItem.setItem(item);// Just in case
update();
};
}
@Override
public boolean onSave() {
return this.groupContainer.setSelectedGroups(this.selectedGroups);
}
@Override
public boolean hadChange() {
Set<AbstractMaterialGroup> baseGroup = this.groupContainer.getSelectedGroups();
return baseGroup.size() != this.selectedGroups.size() ||
!baseGroup.containsAll(this.selectedGroups);
}
}