mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 08:14:00 +02:00
Add group select gui and edit group logic to be able to handle group set.
This commit is contained in:
parent
b4cb6c2848
commit
41235d3024
8 changed files with 216 additions and 19 deletions
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<Material>
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
return includedMaterial
|
||||
}
|
||||
abstract fun setGroups(groups: MutableSet<AbstractMaterialGroup>)
|
||||
|
||||
/**
|
||||
* Get the contained group of this material group
|
||||
*/
|
||||
abstract fun getGroups(): MutableSet<AbstractMaterialGroup>
|
||||
|
||||
}
|
||||
|
|
@ -8,7 +8,8 @@ class ExcludeGroup(name: String): AbstractMaterialGroup(name) {
|
|||
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 {
|
||||
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<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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,8 @@ class IncludeGroup(name: String): AbstractMaterialGroup(name) {
|
|||
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 {
|
||||
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<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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -18,7 +18,7 @@ class ItemGroupManager {
|
|||
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
|
||||
fun prepareGroups(config: ConfigurationSection){
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue