diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java index 9a468de..668aace 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectEnchantmentContainer.java @@ -9,7 +9,8 @@ import java.util.Set; public interface SelectEnchantmentContainer { List getSelectedEnchantments(); - void setSelectedEnchantments(List enchantments); + + boolean setSelectedEnchantments(List enchantments); Set illegalEnchantments(); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java index 7574f85..33d76f9 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectGroupContainer.java @@ -7,8 +7,9 @@ import java.util.Set; public interface SelectGroupContainer { - List getSelectedGroups(); - void setSelectedGroups(List groups); + Set getSelectedGroups(); + + boolean setSelectedGroups(Set groups); Set illegalGroups(); diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java index a510371..bd78db4 100644 --- a/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/SelectMaterialContainer.java @@ -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 getSelectedMaterials(); - void setSelectedMaterials(List materials); + EnumSet getSelectedMaterials(); - Set illegalMaterials(); + boolean setSelectedMaterials(EnumSet materials); + + EnumSet illegalMaterials(); } diff --git a/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java new file mode 100644 index 0000000..5ed1a6d --- /dev/null +++ b/src/main/java/xyz/alexcrea/cuanvil/gui/config/settings/GroupSelectSettingGui.java @@ -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 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 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 TRUE_LORE = Collections.singletonList("\u00A77Value: \u00A7aSelected"); + private static final List 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 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 baseGroup = this.groupContainer.getSelectedGroups(); + return baseGroup.size() != this.selectedGroups.size() || + !baseGroup.containsAll(this.selectedGroups); + } + +} diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt index ffcaab4..2030741 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/AbstractMaterialGroup.kt @@ -5,6 +5,7 @@ import java.util.* abstract class AbstractMaterialGroup(private val name: String) { protected val includedMaterial by lazy {createDefaultSet()} + protected var groupChangeNotified = false /** * Get the group default set @@ -33,6 +34,11 @@ abstract class AbstractMaterialGroup(private val name: String) { */ abstract fun addToPolicy(other : AbstractMaterialGroup) + /** + * Get the group as a set + */ + abstract fun getMaterials(): MutableSet + /** * 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 { - return includedMaterial - } + abstract fun setGroups(groups: MutableSet) + + /** + * Get the contained group of this material group + */ + abstract fun getGroups(): MutableSet } \ No newline at end of file diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt index d8ee38b..24eec47 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ExcludeGroup.kt @@ -8,7 +8,8 @@ class ExcludeGroup(name: String): AbstractMaterialGroup(name) { return EnumSet.allOf(Material::class.java) } - private val includedGroup = HashSet() + private var includedGroup: MutableSet = HashSet() + private val groupItems: MutableSet by lazy {createDefaultSet()} override fun isReferencing(other: AbstractMaterialGroup): Boolean { for (materialGroup in includedGroup.iterator()) { @@ -21,11 +22,33 @@ class ExcludeGroup(name: String): AbstractMaterialGroup(name) { override fun addToPolicy(mat: Material) { includedMaterial.remove(mat) + groupItems.remove(mat) } override fun addToPolicy(other: AbstractMaterialGroup) { includedGroup.add(other) - includedMaterial.removeAll(other.getSet()) + groupItems.removeAll(other.getMaterials()); + } + + override fun setGroups(groups: MutableSet) { + 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 { + return includedGroup; + } + + override fun getMaterials(): MutableSet { + return groupItems; } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt index bd962c2..20e9ef3 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/IncludeGroup.kt @@ -8,7 +8,8 @@ class IncludeGroup(name: String): AbstractMaterialGroup(name) { return EnumSet.noneOf(Material::class.java) } - private val includedGroup = HashSet() + private var includedGroup: MutableSet = HashSet() + private val groupItems: MutableSet by lazy {createDefaultSet()} override fun isReferencing(other: AbstractMaterialGroup): Boolean { for (materialGroup in includedGroup.iterator()) { @@ -21,11 +22,34 @@ class IncludeGroup(name: String): AbstractMaterialGroup(name) { override fun addToPolicy(mat: Material) { includedMaterial.add(mat) + groupItems.add(mat) } override fun addToPolicy(other: AbstractMaterialGroup) { includedGroup.add(other) - includedMaterial.addAll(other.getSet()) + groupItems.addAll(other.getMaterials()); } + override fun setGroups(groups: MutableSet) { + 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 { + return includedGroup + } + + override fun getMaterials(): MutableSet { + return groupItems + } + + } \ No newline at end of file diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt index a7c927a..5d57dfa 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/group/ItemGroupManager.kt @@ -18,7 +18,7 @@ class ItemGroupManager { private val FUTURE_MATERIAL = setOf("PIGLIN_HEAD","BRUSH") } - private lateinit var groupMap : HashMap + lateinit var groupMap : HashMap // Read and create material groups fun prepareGroups(config: ConfigurationSection){