Finished unit repair config gui.

This commit is contained in:
alexcrea 2024-04-11 19:31:43 +02:00
parent cfab871838
commit dee7862e82
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
4 changed files with 132 additions and 14 deletions

View file

@ -4,12 +4,19 @@ import com.github.stefvanschie.inventoryframework.gui.GuiItem;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.gui.config.ask.SelectItemTypeGui;
import xyz.alexcrea.cuanvil.gui.config.global.UnitRepairConfigGui;
import xyz.alexcrea.cuanvil.gui.config.settings.DoubleSettingGui;
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
import xyz.alexcrea.cuanvil.gui.util.GuiSharedConstant;
import xyz.alexcrea.cuanvil.util.CasedStringUtil;
import xyz.alexcrea.cuanvil.util.MetricsUtil;
import java.util.ArrayList;
import java.util.Arrays;
@ -20,14 +27,17 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
private final GuiItem parentItem;
private final Material material;
private final UnitRepairConfigGui parentGui;
private final String materialName;
private boolean shouldWork = true;
public UnitRepairElementListGui(@NotNull Material material,
@NotNull Gui parentGui,
@NotNull UnitRepairConfigGui parentGui,
@NotNull GuiItem parentItem) {
super("\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(material.name().toLowerCase()) + " \u00A7rUnit repair");
this.parentItem = parentItem;
this.material = material;
this.parentGui = parentGui;
this.materialName = CasedStringUtil.snakeToUpperSpacedCase(material.name().toLowerCase());
GuiGlobalItems.addBackItem(this.backgroundPane, parentGui);
@ -37,9 +47,8 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
@Override
protected List<String> getCreateItemLore() {
return Arrays.asList(
"a",
"b",
"c"
"\u00A77Select a new item to be repairable.",
"\u00A77You will be asked the material to use."
);
}
@ -47,8 +56,50 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
protected Consumer<InventoryClickEvent> getCreateClickConsumer() {
return event -> {
event.setCancelled(true);
if(!this.shouldWork){
return;
}
event.setCancelled(true);
new SelectItemTypeGui(
"Select item to be repaired.",
"\u00A77Click here with an item to set the item\n" +
"\u00A77You like to be repaired by " + this.materialName,
this,
(itemStack, player) -> {
ItemMeta meta = itemStack.getItemMeta();
Material type = itemStack.getType();
if(!(meta instanceof Damageable) || (type.getMaxDurability() <= 0)) {
player.sendMessage("\u00A7cThis item can't be damaged, so it can't be repaired.");
return;
}
if(type == this.material){
player.sendMessage("\u00A7cItem can't repair something of the same type.");
return;
}
String materialName = type.name().toLowerCase();
// Add new material
ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().set(material.name().toLowerCase()+"."+materialName,0.25);
MetricsUtil.INSTANCE.notifyChange(ConfigHolder.UNIT_REPAIR_HOLDER, "");
if (GuiSharedConstant.TEMPORARY_DO_SAVE_TO_DISK_EVERY_CHANGE) {
ConfigHolder.UNIT_REPAIR_HOLDER.saveToDisk(GuiSharedConstant.TEMPORARY_DO_BACKUP_EVERY_SAVE);
}
// Update gui
updateValueForGeneric(materialName, true);
this.parentGui.updateValueForGeneric(this.material, true);
// Display material edit setting
this.factoryMap.get(materialName).create().show(player);
},
true
).show(event.getWhoClicked());
event.getWhoClicked().sendMessage("todo");
};
}
@ -62,7 +113,7 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
return DoubleSettingGui.doubleFactory(
"\u00A70%\u00A78" +CasedStringUtil.snakeToUpperSpacedCase(materialName)+" Repair",
this,
material.name()+"."+materialName,
material.name().toLowerCase()+"."+materialName,
ConfigHolder.UNIT_REPAIR_HOLDER,
2,
true, true,
@ -70,7 +121,6 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
1,
0.25,
0.01, 0.05, 0.25
);
}
@ -83,6 +133,9 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
@Override
protected List<String> getEveryDisplayableInstanceOfGeneric() {
ArrayList<String> keys = new ArrayList<>();
if(!this.shouldWork){
return keys;
}
ConfigurationSection materialSection = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getConfigurationSection(material.name().toLowerCase());
if(materialSection == null){
@ -98,6 +151,12 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
return mat;
}
@Override
public void updateGuiValues() {
super.updateGuiValues();
this.parentGui.updateValueForGeneric(this.material, true);
}
// ElementMappedToListGui methods
@Override
@ -110,7 +169,15 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
@Override
public void cleanAndBeUnusable() {
// TODO
this.shouldWork = false;
this.backgroundPane.bindItem('S', GuiGlobalItems.backgroundItem(Material.BLACK_STAINED_GLASS_PANE));
this.backgroundPane.bindItem('L', GuiGlobalItems.backgroundItem(Material.BLACK_STAINED_GLASS_PANE));
this.backgroundPane.bindItem('R', GuiGlobalItems.backgroundItem(Material.BLACK_STAINED_GLASS_PANE));
for (HumanEntity viewer : getViewers()) {
viewer.sendMessage("This config do not exist anymore");
this.parentGui.show(viewer);
}
}
@Override
@ -118,4 +185,13 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
return this;
}
@Override
public void show(@NotNull HumanEntity humanEntity) {
if(!this.shouldWork){
humanEntity.closeInventory();
return;
}
super.show(humanEntity);
}
}

View file

@ -55,7 +55,7 @@ public abstract class AbstractSettingGui extends ChestGui {
*
* @param parent Parent gui to go back when completed.
*/
private void initBase(ValueUpdatableGui parent) {
protected void initBase(ValueUpdatableGui parent) {
Pattern pattern = getGuiPattern();
pane = new PatternPane(0, 0, pattern.getLength(), pattern.getHeight(), pattern);
addPane(pane);

View file

@ -20,9 +20,9 @@ import xyz.alexcrea.cuanvil.util.MetricsUtil;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
public class DoubleSettingGui extends AbstractSettingGui {
@ -61,6 +61,43 @@ public class DoubleSettingGui extends AbstractSettingGui {
updateValueDisplay();
}
private static final ItemStack DELETE_ITEM_STACK = new ItemStack(Material.RED_TERRACOTTA);
static {
ItemMeta meta = DELETE_ITEM_STACK.getItemMeta();
meta.setDisplayName("\u00A7cDisable item being repaired ?");
meta.setLore(Arrays.asList("\u00A77Confirm disabling unit repair for this item..",
"\u00A74Cation: This action can't be canceled."));
DELETE_ITEM_STACK.setItemMeta(meta);
}
private GuiItem askDelete;
@Override
protected void initBase(ValueUpdatableGui parent) {
super.initBase(parent);
this.askDelete = new GuiItem(DELETE_ITEM_STACK,
GuiGlobalActions.saveSettingAction(this, parent),
CustomAnvil.instance);
}
@Override
public void update() {
boolean shouldDelete = isNull() && hadChange();
GuiItem tempSaveItem = this.saveItem;
if(shouldDelete){
this.saveItem = this.askDelete;
}
super.update();
if(shouldDelete){
this.saveItem = tempSaveItem;
}
}
@Override
public Pattern getGuiPattern() {
@ -81,7 +118,7 @@ public class DoubleSettingGui extends AbstractSettingGui {
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("\u00A7eReset to default value");
meta.setLore(Collections.singletonList("\u00A77Default value is: " + displayValue(holder.defaultVal)));
meta.setLore(Collections.singletonList("\u00A77Default value is " + displayValue(holder.defaultVal)));
item.setItemMeta(meta);
returnToDefault = new GuiItem(item, event -> {
event.setCancelled(true);
@ -98,7 +135,6 @@ public class DoubleSettingGui extends AbstractSettingGui {
PatternPane pane = getPane();
//minus item
GuiItem minusItem;
if (now.compareTo(holder.min) > 0) {
@ -139,7 +175,6 @@ public class DoubleSettingGui extends AbstractSettingGui {
}
pane.bindItem('D', returnToDefault);
}
private GuiItem getSetValueItem(Material mat, BigDecimal planned, String numberPrefix){
@ -262,7 +297,7 @@ public class DoubleSettingGui extends AbstractSettingGui {
@Override
public boolean onSave() {
if(this.nullOnZero && (this.now.compareTo(BigDecimal.ZERO) == 0)){
if(isNull()){
this.holder.config.getConfig().set(this.holder.configPath, null);
}else{
this.holder.config.getConfig().set(this.holder.configPath, now.doubleValue());
@ -280,6 +315,10 @@ public class DoubleSettingGui extends AbstractSettingGui {
return now.compareTo(before) != 0;
}
public boolean isNull(){
return this.nullOnZero && (this.now.compareTo(BigDecimal.ZERO) == 0);
}
private static final BigDecimal PERCENTAGE_OFFSET = BigDecimal.valueOf(100);
public String displayValue(BigDecimal value){
return displayValue(value, this.asPercentage);