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 { public interface SelectEnchantmentContainer {
List<Enchantment> getSelectedEnchantments(); List<Enchantment> getSelectedEnchantments();
void setSelectedEnchantments(List<Enchantment> enchantments);
boolean setSelectedEnchantments(List<Enchantment> enchantments);
Set<Enchantment> illegalEnchantments(); Set<Enchantment> illegalEnchantments();

View file

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

View file

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

View file

@ -5,6 +5,7 @@ import java.util.*
abstract class AbstractMaterialGroup(private val name: String) { abstract class AbstractMaterialGroup(private val name: String) {
protected val includedMaterial by lazy {createDefaultSet()} protected val includedMaterial by lazy {createDefaultSet()}
protected var groupChangeNotified = false
/** /**
* Get the group default set * Get the group default set
@ -33,6 +34,11 @@ abstract class AbstractMaterialGroup(private val name: String) {
*/ */
abstract fun addToPolicy(other : AbstractMaterialGroup) abstract fun addToPolicy(other : AbstractMaterialGroup)
/**
* Get the group as a set
*/
abstract fun getMaterials(): MutableSet<Material>
/** /**
* Get the group name in case something is wrong * Get the group name in case something is wrong
*/ */
@ -41,10 +47,13 @@ abstract class AbstractMaterialGroup(private val name: String) {
} }
/** /**
* Get the group as a set * Update the contained groups of this group
*/ */
fun getSet(): Set<Material> { abstract fun setGroups(groups: MutableSet<AbstractMaterialGroup>)
return includedMaterial
} /**
* Get the contained group of this material group
*/
abstract fun getGroups(): MutableSet<AbstractMaterialGroup>
} }

View file

@ -8,7 +8,8 @@ class ExcludeGroup(name: String): AbstractMaterialGroup(name) {
return EnumSet.allOf(Material::class.java) return EnumSet.allOf(Material::class.java)
} }
private val includedGroup = HashSet<AbstractMaterialGroup>() private var includedGroup: MutableSet<AbstractMaterialGroup> = HashSet()
private val groupItems: MutableSet<Material> by lazy {createDefaultSet()}
override fun isReferencing(other: AbstractMaterialGroup): Boolean { override fun isReferencing(other: AbstractMaterialGroup): Boolean {
for (materialGroup in includedGroup.iterator()) { for (materialGroup in includedGroup.iterator()) {
@ -21,11 +22,33 @@ class ExcludeGroup(name: String): AbstractMaterialGroup(name) {
override fun addToPolicy(mat: Material) { override fun addToPolicy(mat: Material) {
includedMaterial.remove(mat) includedMaterial.remove(mat)
groupItems.remove(mat)
} }
override fun addToPolicy(other: AbstractMaterialGroup) { override fun addToPolicy(other: AbstractMaterialGroup) {
includedGroup.add(other) includedGroup.add(other)
includedMaterial.removeAll(other.getSet()) groupItems.removeAll(other.getMaterials());
}
override fun setGroups(groups: MutableSet<AbstractMaterialGroup>) {
groupItems.clear()
groupItems.addAll(includedMaterial)
includedGroup.clear();
groups.forEach { group ->
if(!group.isReferencing(this)) {
includedGroup.add(group);
groupItems.removeAll(group.getMaterials())
}
}
}
override fun getGroups(): MutableSet<AbstractMaterialGroup> {
return includedGroup;
}
override fun getMaterials(): MutableSet<Material> {
return groupItems;
} }

View file

@ -8,7 +8,8 @@ class IncludeGroup(name: String): AbstractMaterialGroup(name) {
return EnumSet.noneOf(Material::class.java) return EnumSet.noneOf(Material::class.java)
} }
private val includedGroup = HashSet<AbstractMaterialGroup>() private var includedGroup: MutableSet<AbstractMaterialGroup> = HashSet()
private val groupItems: MutableSet<Material> by lazy {createDefaultSet()}
override fun isReferencing(other: AbstractMaterialGroup): Boolean { override fun isReferencing(other: AbstractMaterialGroup): Boolean {
for (materialGroup in includedGroup.iterator()) { for (materialGroup in includedGroup.iterator()) {
@ -21,11 +22,34 @@ class IncludeGroup(name: String): AbstractMaterialGroup(name) {
override fun addToPolicy(mat: Material) { override fun addToPolicy(mat: Material) {
includedMaterial.add(mat) includedMaterial.add(mat)
groupItems.add(mat)
} }
override fun addToPolicy(other: AbstractMaterialGroup) { override fun addToPolicy(other: AbstractMaterialGroup) {
includedGroup.add(other) includedGroup.add(other)
includedMaterial.addAll(other.getSet()) groupItems.addAll(other.getMaterials());
} }
override fun setGroups(groups: MutableSet<AbstractMaterialGroup>) {
groupItems.clear();
groupItems.addAll(includedMaterial)
includedGroup.clear();
groups.forEach { group ->
if(!group.isReferencing(this)){
includedGroup.add(group);
groupItems.addAll(group.getMaterials())
}
}
}
override fun getGroups(): MutableSet<AbstractMaterialGroup> {
return includedGroup
}
override fun getMaterials(): MutableSet<Material> {
return groupItems
}
} }

View file

@ -18,7 +18,7 @@ class ItemGroupManager {
private val FUTURE_MATERIAL = setOf("PIGLIN_HEAD","BRUSH") private val FUTURE_MATERIAL = setOf("PIGLIN_HEAD","BRUSH")
} }
private lateinit var groupMap : HashMap<String, AbstractMaterialGroup> lateinit var groupMap : HashMap<String, AbstractMaterialGroup>
// Read and create material groups // Read and create material groups
fun prepareGroups(config: ConfigurationSection){ fun prepareGroups(config: ConfigurationSection){