mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-24 00:26:16 +02:00
probably finished sub setting gui
This commit is contained in:
parent
277cf5bedb
commit
a239a063ae
3 changed files with 194 additions and 58 deletions
|
|
@ -18,13 +18,12 @@ import java.util.HashMap;
|
|||
public class EnchantConflictGui extends ChestGui {
|
||||
|
||||
public final static EnchantConflictGui INSTANCE = new EnchantConflictGui();
|
||||
|
||||
|
||||
private final HashMap<EnchantConflictGroup, EnchantConflictSubSettingGui> conflictGuiMap;
|
||||
static {
|
||||
INSTANCE.init();
|
||||
}
|
||||
|
||||
private final HashMap<EnchantConflictGroup, EnchantConflictSubSettingGui> conflictGuiMap;
|
||||
|
||||
private EnchantConflictGui() {
|
||||
super(6, "§eConflict Config", CustomAnvil.instance);
|
||||
this.conflictGuiMap = new HashMap<>();
|
||||
|
|
@ -50,24 +49,54 @@ public class EnchantConflictGui extends ChestGui {
|
|||
this.conflictGuiMap.clear();
|
||||
this.filledEnchant.clear();
|
||||
|
||||
// Create new sub setting gui
|
||||
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);
|
||||
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){
|
||||
EnchantConflictSubSettingGui gui = this.conflictGuiMap.get(conflict);
|
||||
if(gui == null) return;
|
||||
|
||||
this.filledEnchant.removeItem(gui.getParentItemForThisGui());
|
||||
this.conflictGuiMap.remove(conflict);
|
||||
update();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
|||
import xyz.alexcrea.cuanvil.group.AbstractMaterialGroup;
|
||||
import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;
|
||||
import xyz.alexcrea.cuanvil.group.EnchantConflictManager;
|
||||
import xyz.alexcrea.cuanvil.gui.MainConfigGui;
|
||||
import xyz.alexcrea.cuanvil.gui.ValueUpdatableGui;
|
||||
import xyz.alexcrea.cuanvil.gui.config.ConfirmActionGui;
|
||||
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.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.logging.Level;
|
||||
|
||||
public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements SelectEnchantmentContainer, SelectGroupContainer {
|
||||
|
||||
private final EnchantConflictGui parent;
|
||||
private final EnchantConflictGroup enchantConflict;
|
||||
private final GuiItem parentItemForThisGui;
|
||||
private final PatternPane pane;
|
||||
private boolean canOpen = true;
|
||||
private boolean shouldWorld = true;
|
||||
|
||||
public EnchantConflictSubSettingGui(
|
||||
@NotNull EnchantConflictGui parent,
|
||||
@NotNull EnchantConflictGroup enchantConflict) {
|
||||
super(3, "\u00A72Config for \u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()), CustomAnvil.instance);
|
||||
@NotNull EnchantConflictGroup enchantConflict,
|
||||
@NotNull GuiItem parentItemForThisGui) {
|
||||
super(3,
|
||||
"\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()) + " \u00A78Config",
|
||||
CustomAnvil.instance);
|
||||
this.parent = parent;
|
||||
this.enchantConflict = enchantConflict;
|
||||
this.parentItemForThisGui = parentItemForThisGui;
|
||||
|
||||
Pattern pattern = new Pattern(
|
||||
GuiSharedConstant.EMPTY_GUI_FULL_LINE,
|
||||
|
|
@ -62,8 +70,8 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S
|
|||
|
||||
private void prepareStaticValues() {
|
||||
|
||||
GuiGlobalItems.addBackItem(pane, MainConfigGui.INSTANCE);
|
||||
GuiGlobalItems.addBackgroundItem(pane);
|
||||
GuiGlobalItems.addBackItem(this.pane, this.parent);
|
||||
GuiGlobalItems.addBackgroundItem(this.pane);
|
||||
|
||||
// Delete item
|
||||
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 !"));
|
||||
|
||||
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
|
||||
|
||||
enchantSettingItem = new GuiItem(new ItemStack(Material.ENCHANTED_BOOK), (event)->{
|
||||
this.enchantSettingItem = new GuiItem(new ItemStack(Material.ENCHANTED_BOOK), (event)->{
|
||||
event.setCancelled(true);
|
||||
EnchantSelectSettingGui enchantGui = new EnchantSelectSettingGui(
|
||||
"\u00A7eEnchantments for \u00A78" +CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()),
|
||||
"\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()) + " \u00A75Enchantments",
|
||||
this, this, 0);
|
||||
enchantGui.show(event.getWhoClicked());
|
||||
}, CustomAnvil.instance);
|
||||
|
||||
groupSettingItem = new GuiItem(new ItemStack(Material.PAPER), (event)->{
|
||||
this.groupSettingItem = new GuiItem(new ItemStack(Material.PAPER), (event)->{
|
||||
event.setCancelled(true);
|
||||
GroupSelectSettingGui enchantGui = new GroupSelectSettingGui(
|
||||
"\u00A7eGroups for \u00A78" +CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName()),
|
||||
"\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(this.enchantConflict.getName()) + " \u00A73Groups",
|
||||
this, this, 0);
|
||||
enchantGui.show(event.getWhoClicked());
|
||||
}, CustomAnvil.instance);
|
||||
|
||||
minBeforeActiveSettingFactory = IntSettingsGui.intFactory("\u00A7eMinimum enchantment before conflict is active", this,
|
||||
enchantConflict.getName()+".maxEnchantmentBeforeConflict", ConfigHolder.CONFLICT_HOLDER,
|
||||
this.minBeforeActiveSettingFactory = IntSettingsGui.intFactory(
|
||||
"\u00A78Minimum enchantment count",
|
||||
this, this.enchantConflict.getName()+".maxEnchantmentBeforeConflict", ConfigHolder.CONFLICT_HOLDER,
|
||||
0, 255, 0, 1
|
||||
);
|
||||
|
||||
pane.bindItem('E', enchantSettingItem);
|
||||
pane.bindItem('G', groupSettingItem);
|
||||
this.pane.bindItem('E', this.enchantSettingItem);
|
||||
this.pane.bindItem('G', this.groupSettingItem);
|
||||
|
||||
// Now we update the items
|
||||
updateLocal();
|
||||
|
|
@ -111,13 +120,19 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S
|
|||
EnchantConflictManager manager = ConfigHolder.CONFLICT_HOLDER.getConflictManager();
|
||||
|
||||
// Remove from manager
|
||||
for (Enchantment enchantment : enchantConflict.getEnchants()) {
|
||||
manager.removeConflictFromMap(enchantment, enchantConflict);
|
||||
for (Enchantment enchantment : this.enchantConflict.getEnchants()) {
|
||||
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
|
||||
ConfigHolder.CONFLICT_HOLDER.getConfig().set(enchantConflict.getName(), null);
|
||||
ConfigHolder.CONFLICT_HOLDER.getConfig().set(this.enchantConflict.getName(), null);
|
||||
|
||||
// Save
|
||||
boolean success = true;
|
||||
|
|
@ -128,7 +143,7 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S
|
|||
return success;
|
||||
};
|
||||
|
||||
return new ConfirmActionGui("\u00A7cDelete \u00A7e"+CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName())+"\u00A7c ?",
|
||||
return new ConfirmActionGui("\u00A7cDelete \u00A7e"+CasedStringUtil.snakeToUpperSpacedCase(enchantConflict.getName())+"\u00A7c?",
|
||||
"\u00A77Confirm that you want to delete this conflict.",
|
||||
this, this.parent, deleteSupplier
|
||||
);
|
||||
|
|
@ -136,46 +151,83 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S
|
|||
|
||||
@Override
|
||||
public void updateGuiValues() {
|
||||
this.parent.updateValueForConflict(this.enchantConflict);
|
||||
this.parent.updateValueForConflict(this.enchantConflict, true);
|
||||
// Parent should call updateLocal
|
||||
}
|
||||
|
||||
public void updateLocal(){
|
||||
if(!this.canOpen) return;
|
||||
if(!this.shouldWorld) return;
|
||||
|
||||
// Prepare enchantment lore
|
||||
ArrayList<String> enchantLore = new ArrayList<>();
|
||||
enchantLore.add("\u00A77Allow you to select a list of \u00A75Enchantments \u00A77that this conflict should include");
|
||||
Set<Enchantment> enchants = getSelectedEnchantments();
|
||||
if(enchants.isEmpty()){
|
||||
enchantLore.add("\u00A77There is no enchantment for this conflict.");
|
||||
enchantLore.add("\u00A77There is no included enchantment for this conflict.");
|
||||
}else{
|
||||
enchantLore.add("\u00A77List of included enchantment for this conflict:");
|
||||
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++) {
|
||||
// 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
|
||||
ItemStack enchantItem = enchantSettingItem.getItem();
|
||||
ItemStack enchantItem = this.enchantSettingItem.getItem();
|
||||
ItemMeta enchantMeta = enchantItem.getItemMeta();
|
||||
|
||||
enchantMeta.setDisplayName("\u00A7aSelect \u00A75Enchantments \u00A7aSettings");
|
||||
enchantMeta.setDisplayName("\u00A7aSelect included \u00A75Enchantments \u00A7aSettings");
|
||||
enchantMeta.setLore(enchantLore);
|
||||
|
||||
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
|
||||
|
||||
|
||||
pane.bindItem('M', GuiGlobalItems.intSettingGuiItem(minBeforeActiveSettingFactory, Material.COMMAND_BLOCK));
|
||||
this.pane.bindItem('M', GuiGlobalItems.intSettingGuiItem(this.minBeforeActiveSettingFactory, Material.COMMAND_BLOCK));
|
||||
update();
|
||||
}
|
||||
|
||||
|
|
@ -183,25 +235,29 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S
|
|||
for (HumanEntity viewer : getViewers()) {
|
||||
this.parent.show(viewer);
|
||||
}
|
||||
this.canOpen = false;
|
||||
this.shouldWorld = false;
|
||||
|
||||
// Just in case something is extremely wrong
|
||||
GuiItem background = GuiGlobalItems.backgroundItem();
|
||||
pane.bindItem('E', background);
|
||||
pane.bindItem('G', background);
|
||||
pane.bindItem('M', background);
|
||||
pane.bindItem('D', background);
|
||||
this.pane.bindItem('E', background);
|
||||
this.pane.bindItem('G', background);
|
||||
this.pane.bindItem('M', background);
|
||||
this.pane.bindItem('D', background);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show(@NotNull HumanEntity humanEntity) {
|
||||
if(this.canOpen){
|
||||
if(this.shouldWorld){
|
||||
super.show(humanEntity);
|
||||
}else{
|
||||
this.parent.show(humanEntity);
|
||||
}
|
||||
}
|
||||
|
||||
public GuiItem getParentItemForThisGui() {
|
||||
return parentItemForThisGui;
|
||||
}
|
||||
|
||||
// Select enchantment container methods
|
||||
|
||||
@Override
|
||||
|
|
@ -211,12 +267,40 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S
|
|||
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
|
||||
// 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
|
||||
public Set<Enchantment> illegalEnchantments() {
|
||||
return new HashSet<>();
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
// Select group container methods
|
||||
|
|
@ -228,19 +312,39 @@ public class EnchantConflictSubSettingGui extends ValueUpdatableGui implements S
|
|||
|
||||
@Override
|
||||
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
|
||||
this.enchantConflict.getCantConflictGroup().setGroups(groups);
|
||||
|
||||
// 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
|
||||
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
|
||||
public Set<AbstractMaterialGroup> illegalGroups() {
|
||||
|
||||
return new HashSet<>();
|
||||
return Collections.emptySet();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,9 +13,7 @@ class EnchantConflictGroup(
|
|||
fun addEnchantment(enchant: Enchantment){
|
||||
enchantments.add(enchant)
|
||||
}
|
||||
fun removeEnchantment(enchant: Enchantment){
|
||||
enchantments.remove(enchant)
|
||||
}
|
||||
|
||||
fun allowed(enchants: Set<Enchantment>, mat: Material) : Boolean{
|
||||
if(enchantments.size < minBeforeBlock){
|
||||
return true
|
||||
|
|
@ -45,4 +43,9 @@ class EnchantConflictGroup(
|
|||
return enchantments
|
||||
}
|
||||
|
||||
fun setEnchants(enchants: Set<Enchantment>) {
|
||||
enchantments.clear()
|
||||
enchantments.addAll(enchants)
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue