probably finished sub setting gui

This commit is contained in:
alexcrea 2024-03-23 14:34:13 +01:00
parent 277cf5bedb
commit a239a063ae
3 changed files with 194 additions and 58 deletions

View file

@ -18,13 +18,12 @@ import java.util.HashMap;
public class EnchantConflictGui extends ChestGui { public class EnchantConflictGui extends ChestGui {
public final static EnchantConflictGui INSTANCE = new EnchantConflictGui(); public final static EnchantConflictGui INSTANCE = new EnchantConflictGui();
private final HashMap<EnchantConflictGroup, EnchantConflictSubSettingGui> conflictGuiMap;
static { static {
INSTANCE.init(); INSTANCE.init();
} }
private final HashMap<EnchantConflictGroup, EnchantConflictSubSettingGui> conflictGuiMap;
private EnchantConflictGui() { private EnchantConflictGui() {
super(6, "§eConflict Config", CustomAnvil.instance); super(6, "§eConflict Config", CustomAnvil.instance);
this.conflictGuiMap = new HashMap<>(); this.conflictGuiMap = new HashMap<>();
@ -50,24 +49,54 @@ public class EnchantConflictGui extends ChestGui {
this.conflictGuiMap.clear(); this.conflictGuiMap.clear();
this.filledEnchant.clear(); this.filledEnchant.clear();
// Create new sub setting gui
for (EnchantConflictGroup conflict : ConfigHolder.CONFLICT_HOLDER.getConflictManager().getConflictList()) { for (EnchantConflictGroup conflict : ConfigHolder.CONFLICT_HOLDER.getConflictManager().getConflictList()) {
EnchantConflictSubSettingGui conflictGui = new EnchantConflictSubSettingGui(this, conflict); updateValueForConflict(conflict, false);
}
// Temporaire, il faut faire un item avec le conflict et donc un generateur arbitraire update();
}
public ItemStack createItemForConflict(EnchantConflictGroup conflict){
ItemStack item = new ItemStack(Material.ENCHANTED_BOOK); ItemStack item = new ItemStack(Material.ENCHANTED_BOOK);
filledEnchant.addItem(new GuiItem(item, GuiGlobalActions.openGuiAction(conflictGui), CustomAnvil.instance)); //TODO item
return item;
} }
public void updateValueForConflict(EnchantConflictGroup conflict, boolean shouldUpdate){
EnchantConflictSubSettingGui gui = this.conflictGuiMap.get(conflict);
ItemStack item = createItemForConflict(conflict);
GuiItem guiItem;
if(gui == null){
// Create new sub setting gui
guiItem = new GuiItem(item, CustomAnvil.instance);
gui = new EnchantConflictSubSettingGui(this, conflict, guiItem);
guiItem.setAction(GuiGlobalActions.openGuiAction(gui));
this.conflictGuiMap.put(conflict, gui);
this.filledEnchant.addItem(guiItem);
}else{
// replace item with the updated one
guiItem = gui.getParentItemForThisGui();
guiItem.setItem(item);
} }
public void updateValueForConflict(EnchantConflictGroup conflict){ gui.updateLocal();
if(shouldUpdate){
update();
}
} }
public void removeConflict(EnchantConflictGroup conflict){ public void removeConflict(EnchantConflictGroup conflict){
EnchantConflictSubSettingGui gui = this.conflictGuiMap.get(conflict);
if(gui == null) return;
this.filledEnchant.removeItem(gui.getParentItemForThisGui());
this.conflictGuiMap.remove(conflict);
update();
} }
} }

View file

@ -14,7 +14,6 @@ import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup; import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup;
import xyz.alexcrea.cuanvil.group.EnchantConflictGroup; import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;
import xyz.alexcrea.cuanvil.group.EnchantConflictManager; import xyz.alexcrea.cuanvil.group.EnchantConflictManager;
import xyz.alexcrea.cuanvil.gui.MainConfigGui;
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui; import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
import xyz.alexcrea.cuanvil.gui.config.ConfirmActionGui; import xyz.alexcrea.cuanvil.gui.config.ConfirmActionGui;
import xyz.alexcrea.cuanvil.gui.config.SelectEnchantmentContainer; import xyz.alexcrea.cuanvil.gui.config.SelectEnchantmentContainer;
@ -28,22 +27,31 @@ import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant; import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant;
import xyz.alexcrea.cuanvil.util.CasedStringUtil; import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import java.util.*; import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.logging.Level;
public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements SelectEnchantmentContainer, SelectGroupContainer { public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements SelectEnchantmentContainer, SelectGroupContainer {
private final EnchantConflictGui parent; private final EnchantConflictGui parent;
private final EnchantConflictGroup enchantConflict; private final EnchantConflictGroup enchantConflict;
private final GuiItem parentItemForThisGui;
private final PatternPane pane; private final PatternPane pane;
private boolean canOpen = true; private boolean shouldWorld = true;
public EnchantConflictSubSettingGui( public EnchantConflictSubSettingGui(
@NotNull EnchantConflictGui parent, @NotNull EnchantConflictGui parent,
@NotNull EnchantConflictGroup enchantConflict) { @NotNull EnchantConflictGroup enchantConflict,
super(3, "\u00A72Config for \u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()), CustomAnvil.instance); @NotNull GuiItem parentItemForThisGui) {
super(3,
"\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()) + " \u00A78Config",
CustomAnvil.instance);
this.parent = parent; this.parent = parent;
this.enchantConflict = enchantConflict; this.enchantConflict = enchantConflict;
this.parentItemForThisGui = parentItemForThisGui;
Pattern pattern = new Pattern( Pattern pattern = new Pattern(
GuiSharedConstant.EMPTY_GUI_FULL_LINE, GuiSharedConstant.EMPTY_GUI_FULL_LINE,
@ -62,8 +70,8 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S
private void prepareStaticValues() { private void prepareStaticValues() {
GuiGlobalItems.addBackItem(pane, MainConfigGui.INSTANCE); GuiGlobalItems.addBackItem(this.pane, this.parent);
GuiGlobalItems.addBackgroundItem(pane); GuiGlobalItems.addBackgroundItem(this.pane);
// Delete item // Delete item
ItemStack deleteItem = new ItemStack(Material.RED_TERRACOTTA); ItemStack deleteItem = new ItemStack(Material.RED_TERRACOTTA);
@ -73,33 +81,34 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S
deleteMeta.setLore(Collections.singletonList("\u00A7cCaution with this button !")); deleteMeta.setLore(Collections.singletonList("\u00A7cCaution with this button !"));
deleteItem.setItemMeta(deleteMeta); deleteItem.setItemMeta(deleteMeta);
pane.bindItem('D', new GuiItem(deleteItem, GuiGlobalActions.openGuiAction(createDeleteGui()), CustomAnvil.instance)); this.pane.bindItem('D', new GuiItem(deleteItem, GuiGlobalActions.openGuiAction(createDeleteGui()), CustomAnvil.instance));
// Displayed item will be updated later // Displayed item will be updated later
enchantSettingItem = new GuiItem(new ItemStack(Material.ENCHANTED_BOOK), (event)->{ this.enchantSettingItem = new GuiItem(new ItemStack(Material.ENCHANTED_BOOK), (event)->{
event.setCancelled(true); event.setCancelled(true);
EnchantSelectSettingGui enchantGui = new EnchantSelectSettingGui( EnchantSelectSettingGui enchantGui = new EnchantSelectSettingGui(
"\u00A7eEnchantments for \u00A78" +CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()), "\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()) + " \u00A75Enchantments",
this, this, 0); this, this, 0);
enchantGui.show(event.getWhoClicked()); enchantGui.show(event.getWhoClicked());
}, CustomAnvil.instance); }, CustomAnvil.instance);
groupSettingItem = new GuiItem(new ItemStack(Material.PAPER), (event)->{ this.groupSettingItem = new GuiItem(new ItemStack(Material.PAPER), (event)->{
event.setCancelled(true); event.setCancelled(true);
GroupSelectSettingGui enchantGui = new GroupSelectSettingGui( GroupSelectSettingGui enchantGui = new GroupSelectSettingGui(
"\u00A7eGroups for \u00A78" +CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()), "\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(this.enchantConflict.getName()) + " \u00A73Groups",
this, this, 0); this, this, 0);
enchantGui.show(event.getWhoClicked()); enchantGui.show(event.getWhoClicked());
}, CustomAnvil.instance); }, CustomAnvil.instance);
minBeforeActiveSettingFactory = IntSettingsGui.intFactory("\u00A7eMinimum enchantment before conflict is active", this, this.minBeforeActiveSettingFactory = IntSettingsGui.intFactory(
enchantConflict.getName()+".maxEnchantmentBeforeConflict", ConfigHolder.CONFLICT_HOLDER, "\u00A78Minimum enchantment count",
this, this.enchantConflict.getName()+".maxEnchantmentBeforeConflict", ConfigHolder.CONFLICT_HOLDER,
0, 255, 0, 1 0, 255, 0, 1
); );
pane.bindItem('E', enchantSettingItem); this.pane.bindItem('E', this.enchantSettingItem);
pane.bindItem('G', groupSettingItem); this.pane.bindItem('G', this.groupSettingItem);
// Now we update the items // Now we update the items
updateLocal(); updateLocal();
@ -111,13 +120,19 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S
EnchantConflictManager manager = ConfigHolder.CONFLICT_HOLDER.getConflictManager(); EnchantConflictManager manager = ConfigHolder.CONFLICT_HOLDER.getConflictManager();
// Remove from manager // Remove from manager
for (Enchantment enchantment : enchantConflict.getEnchants()) { for (Enchantment enchantment : this.enchantConflict.getEnchants()) {
manager.removeConflictFromMap(enchantment, enchantConflict); manager.removeConflictFromMap(enchantment, this.enchantConflict);
} }
manager.conflictList.remove(enchantConflict); manager.conflictList.remove(this.enchantConflict);
// Remove from parent
this.parent.removeConflict(this.enchantConflict);
// Remove self
cleanUnused();
// Update config file storage // Update config file storage
ConfigHolder.CONFLICT_HOLDER.getConfig().set(enchantConflict.getName(), null); ConfigHolder.CONFLICT_HOLDER.getConfig().set(this.enchantConflict.getName(), null);
// Save // Save
boolean success = true; boolean success = true;
@ -136,46 +151,83 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S
@Override @Override
public void updateGuiValues() { public void updateGuiValues() {
this.parent.updateValueForConflict(this.enchantConflict); this.parent.updateValueForConflict(this.enchantConflict, true);
// Parent should call updateLocal // Parent should call updateLocal
} }
public void updateLocal(){ public void updateLocal(){
if(!this.canOpen) return; if(!this.shouldWorld) return;
// Prepare enchantment lore // Prepare enchantment lore
ArrayList<String> enchantLore = new ArrayList<>(); ArrayList<String> enchantLore = new ArrayList<>();
enchantLore.add("\u00A77Allow you to select a list of \u00A75Enchantments \u00A77that this conflict should include"); enchantLore.add("\u00A77Allow you to select a list of \u00A75Enchantments \u00A77that this conflict should include");
Set<Enchantment> enchants = getSelectedEnchantments(); Set<Enchantment> enchants = getSelectedEnchantments();
if(enchants.isEmpty()){ if(enchants.isEmpty()){
enchantLore.add("\u00A77There is no enchantment for this conflict."); enchantLore.add("\u00A77There is no included enchantment for this conflict.");
}else{ }else{
enchantLore.add("\u00A77List of included enchantment for this conflict:"); enchantLore.add("\u00A77List of included enchantment for this conflict:");
Iterator<Enchantment> enchantIterator = enchants.iterator(); Iterator<Enchantment> enchantIterator = enchants.iterator();
int maxindex = (enchants.size() > 5 ? 4 : enchants.size()); boolean greaterThanMax = enchants.size() > 5;
int maxindex = (greaterThanMax ? 4 : enchants.size());
for (int i = 0; i < maxindex; i++) { for (int i = 0; i < maxindex; i++) {
// format string like "- Fire Protection" // format string like "- Fire Protection"
enchantLore.add("\u00A77- \u00A75"+CasedStringUtil.snakeToUpperSpacedCase(enchantIterator.next().getKey().getKey())); String formattedName = CasedStringUtil.snakeToUpperSpacedCase(enchantIterator.next().getKey().getKey());
enchantLore.add("\u00A77- \u00A75" + formattedName);
}
if(greaterThanMax){
enchantLore.add("\u00A77And "+(enchants.size()-4)+" more...");
}
}
// Prepare group lore
ArrayList<String> groupLore = new ArrayList<>();
groupLore.add("\u00A77Allow you to select a list of \u00A73Groups \u00A77that this conflict should include");
Set<AbstractMaterialGroup> grouos = getSelectedGroups();
if(grouos.isEmpty()){
groupLore.add("\u00A77There is no excluded groups for this conflict.");
}else{
groupLore.add("\u00A77List of excluded groups for this conflict:");
Iterator<AbstractMaterialGroup> groupIterator = grouos.iterator();
boolean greaterThanMax = grouos.size() > 5;
int maxindex = (greaterThanMax ? 4 : grouos.size());
for (int i = 0; i < maxindex; i++) {
// format string like "- Melee Weapons"
String formattedName = CasedStringUtil.snakeToUpperSpacedCase(groupIterator.next().getName());
groupLore.add("\u00A77- \u00A73" + formattedName);
}
if(greaterThanMax){
groupLore.add("\u00A77And "+(grouos.size()-4)+" more...");
} }
} }
// Configure enchant setting item // Configure enchant setting item
ItemStack enchantItem = enchantSettingItem.getItem(); ItemStack enchantItem = this.enchantSettingItem.getItem();
ItemMeta enchantMeta = enchantItem.getItemMeta(); ItemMeta enchantMeta = enchantItem.getItemMeta();
enchantMeta.setDisplayName("\u00A7aSelect \u00A75Enchantments \u00A7aSettings"); enchantMeta.setDisplayName("\u00A7aSelect included \u00A75Enchantments \u00A7aSettings");
enchantMeta.setLore(enchantLore); enchantMeta.setLore(enchantLore);
enchantItem.setItemMeta(enchantMeta); enchantItem.setItemMeta(enchantMeta);
enchantSettingItem.setItem(enchantItem); // Just in case this.enchantSettingItem.setItem(enchantItem); // Just in case
// Configure group setting item
ItemStack groupItem = this.groupSettingItem.getItem();
ItemMeta groupMeta = groupItem.getItemMeta();
groupMeta.setDisplayName("\u00A7aSelect excluded \u00A73Groups \u00A7aSettings");
groupMeta.setLore(groupLore);
groupItem.setItemMeta(groupMeta);
this.groupSettingItem.setItem(groupItem); // Just in case
//todo: groupSettingItem this.pane.bindItem('M', GuiGlobalItems.intSettingGuiItem(this.minBeforeActiveSettingFactory, Material.COMMAND_BLOCK));
pane.bindItem('M', GuiGlobalItems.intSettingGuiItem(minBeforeActiveSettingFactory, Material.COMMAND_BLOCK));
update(); update();
} }
@ -183,25 +235,29 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S
for (HumanEntity viewer : getViewers()) { for (HumanEntity viewer : getViewers()) {
this.parent.show(viewer); this.parent.show(viewer);
} }
this.canOpen = false; this.shouldWorld = false;
// Just in case something is extremely wrong // Just in case something is extremely wrong
GuiItem background = GuiGlobalItems.backgroundItem(); GuiItem background = GuiGlobalItems.backgroundItem();
pane.bindItem('E', background); this.pane.bindItem('E', background);
pane.bindItem('G', background); this.pane.bindItem('G', background);
pane.bindItem('M', background); this.pane.bindItem('M', background);
pane.bindItem('D', background); this.pane.bindItem('D', background);
} }
@Override @Override
public void show(@NotNull HumanEntity humanEntity) { public void show(@NotNull HumanEntity humanEntity) {
if(this.canOpen){ if(this.shouldWorld){
super.show(humanEntity); super.show(humanEntity);
}else{ }else{
this.parent.show(humanEntity); this.parent.show(humanEntity);
} }
} }
public GuiItem getParentItemForThisGui() {
return parentItemForThisGui;
}
// Select enchantment container methods // Select enchantment container methods
@Override @Override
@ -211,12 +267,40 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S
@Override @Override
public boolean setSelectedEnchantments(Set<Enchantment> enchantments) { public boolean setSelectedEnchantments(Set<Enchantment> enchantments) {
if(!this.shouldWorld) {
CustomAnvil.instance.getLogger().info("Trying to save "+enchantConflict.getName()+" enchants but sub config is destroyed");
return false; return false;
} }
// Set live configuration
this.enchantConflict.setEnchants(enchantments);
// Save on file configuration
String[] enchantKeys = new String[enchantments.size()];
int index = 0;
for (Enchantment enchantment : enchantments) {
enchantKeys[index++] = enchantment.getKey().getKey();
}
ConfigHolder.CONFLICT_HOLDER.getConfig().set(enchantConflict.getName()+".enchantments", enchantKeys);
try {
updateGuiValues();
}catch (Exception e){
CustomAnvil.instance.getLogger().log(Level.WARNING, "An error occurred while updating enchants for "+this.enchantConflict.getName(), e);
}
// Save file configuration to disk
if(GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){
return ConfigHolder.CONFLICT_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE);
}
return true;
}
@Override @Override
public Set<Enchantment> illegalEnchantments() { public Set<Enchantment> illegalEnchantments() {
return new HashSet<>(); return Collections.emptySet();
} }
// Select group container methods // Select group container methods
@ -228,19 +312,39 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S
@Override @Override
public boolean setSelectedGroups(Set<AbstractMaterialGroup> groups) { public boolean setSelectedGroups(Set<AbstractMaterialGroup> groups) {
if(!this.shouldWorld) {
CustomAnvil.instance.getLogger().info("Trying to save "+enchantConflict.getName()+" groups but sub config is destroyed");
return false;
}
// Set live configuration // Set live configuration
this.enchantConflict.getCantConflictGroup().setGroups(groups); this.enchantConflict.getCantConflictGroup().setGroups(groups);
// Save on file configuration // Save on file configuration
String[] groupsNames = new String[groups.size()];
int index = 0;
for (AbstractMaterialGroup group : groups) {
groupsNames[index++] = group.getName();
}
ConfigHolder.CONFLICT_HOLDER.getConfig().set(this.enchantConflict.getName()+".notAffectedGroups", groupsNames);
try {
updateGuiValues();
}catch (Exception e){
CustomAnvil.instance.getLogger().log(Level.WARNING, "An error occurred while updating group for "+this.enchantConflict.getName(), e);
}
// Save file configuration to disk // Save file configuration to disk
if(GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE){
return ConfigHolder.CONFLICT_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE);
}
return false; return true;
} }
@Override @Override
public Set<AbstractMaterialGroup> illegalGroups() { public Set<AbstractMaterialGroup> illegalGroups() {
return new HashSet<>(); return Collections.emptySet();
} }
} }

View file

@ -13,9 +13,7 @@ class EnchantConflictGroup(
fun addEnchantment(enchant: Enchantment){ fun addEnchantment(enchant: Enchantment){
enchantments.add(enchant) enchantments.add(enchant)
} }
fun removeEnchantment(enchant: Enchantment){
enchantments.remove(enchant)
}
fun allowed(enchants: Set<Enchantment>, mat: Material) : Boolean{ fun allowed(enchants: Set<Enchantment>, mat: Material) : Boolean{
if(enchantments.size < minBeforeBlock){ if(enchantments.size < minBeforeBlock){
return true return true
@ -45,4 +43,9 @@ class EnchantConflictGroup(
return enchantments return enchantments
} }
fun setEnchants(enchants: Set<Enchantment>) {
enchantments.clear()
enchantments.addAll(enchants)
}
} }