mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
Finished unit repair config gui.
This commit is contained in:
parent
cfab871838
commit
dee7862e82
4 changed files with 132 additions and 14 deletions
|
|
@ -4,12 +4,19 @@ import com.github.stefvanschie.inventoryframework.gui.GuiItem;
|
||||||
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
|
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
import org.bukkit.entity.HumanEntity;
|
||||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||||
|
import org.bukkit.inventory.meta.Damageable;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import xyz.alexcrea.cuanvil.config.ConfigHolder;
|
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.config.settings.DoubleSettingGui;
|
||||||
import xyz.alexcrea.cuanvil.gui.util.GuiGlobalItems;
|
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.CasedStringUtil;
|
||||||
|
import xyz.alexcrea.cuanvil.util.MetricsUtil;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
@ -20,14 +27,17 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
|
||||||
|
|
||||||
private final GuiItem parentItem;
|
private final GuiItem parentItem;
|
||||||
private final Material material;
|
private final Material material;
|
||||||
|
private final UnitRepairConfigGui parentGui;
|
||||||
private final String materialName;
|
private final String materialName;
|
||||||
|
|
||||||
|
private boolean shouldWork = true;
|
||||||
public UnitRepairElementListGui(@NotNull Material material,
|
public UnitRepairElementListGui(@NotNull Material material,
|
||||||
@NotNull Gui parentGui,
|
@NotNull UnitRepairConfigGui parentGui,
|
||||||
@NotNull GuiItem parentItem) {
|
@NotNull GuiItem parentItem) {
|
||||||
super("\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(material.name().toLowerCase()) + " \u00A7rUnit repair");
|
super("\u00A7e" + CasedStringUtil.snakeToUpperSpacedCase(material.name().toLowerCase()) + " \u00A7rUnit repair");
|
||||||
this.parentItem = parentItem;
|
this.parentItem = parentItem;
|
||||||
this.material = material;
|
this.material = material;
|
||||||
|
this.parentGui = parentGui;
|
||||||
this.materialName = CasedStringUtil.snakeToUpperSpacedCase(material.name().toLowerCase());
|
this.materialName = CasedStringUtil.snakeToUpperSpacedCase(material.name().toLowerCase());
|
||||||
|
|
||||||
GuiGlobalItems.addBackItem(this.backgroundPane, parentGui);
|
GuiGlobalItems.addBackItem(this.backgroundPane, parentGui);
|
||||||
|
|
@ -37,9 +47,8 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
|
||||||
@Override
|
@Override
|
||||||
protected List<String> getCreateItemLore() {
|
protected List<String> getCreateItemLore() {
|
||||||
return Arrays.asList(
|
return Arrays.asList(
|
||||||
"a",
|
"\u00A77Select a new item to be repairable.",
|
||||||
"b",
|
"\u00A77You will be asked the material to use."
|
||||||
"c"
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -47,8 +56,50 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
|
||||||
protected Consumer<InventoryClickEvent> getCreateClickConsumer() {
|
protected Consumer<InventoryClickEvent> getCreateClickConsumer() {
|
||||||
return event -> {
|
return event -> {
|
||||||
event.setCancelled(true);
|
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(
|
return DoubleSettingGui.doubleFactory(
|
||||||
"\u00A70%\u00A78" +CasedStringUtil.snakeToUpperSpacedCase(materialName)+" Repair",
|
"\u00A70%\u00A78" +CasedStringUtil.snakeToUpperSpacedCase(materialName)+" Repair",
|
||||||
this,
|
this,
|
||||||
material.name()+"."+materialName,
|
material.name().toLowerCase()+"."+materialName,
|
||||||
ConfigHolder.UNIT_REPAIR_HOLDER,
|
ConfigHolder.UNIT_REPAIR_HOLDER,
|
||||||
2,
|
2,
|
||||||
true, true,
|
true, true,
|
||||||
|
|
@ -70,7 +121,6 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
|
||||||
1,
|
1,
|
||||||
0.25,
|
0.25,
|
||||||
0.01, 0.05, 0.25
|
0.01, 0.05, 0.25
|
||||||
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -83,6 +133,9 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
|
||||||
@Override
|
@Override
|
||||||
protected List<String> getEveryDisplayableInstanceOfGeneric() {
|
protected List<String> getEveryDisplayableInstanceOfGeneric() {
|
||||||
ArrayList<String> keys = new ArrayList<>();
|
ArrayList<String> keys = new ArrayList<>();
|
||||||
|
if(!this.shouldWork){
|
||||||
|
return keys;
|
||||||
|
}
|
||||||
|
|
||||||
ConfigurationSection materialSection = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getConfigurationSection(material.name().toLowerCase());
|
ConfigurationSection materialSection = ConfigHolder.UNIT_REPAIR_HOLDER.getConfig().getConfigurationSection(material.name().toLowerCase());
|
||||||
if(materialSection == null){
|
if(materialSection == null){
|
||||||
|
|
@ -98,6 +151,12 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
|
||||||
return mat;
|
return mat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateGuiValues() {
|
||||||
|
super.updateGuiValues();
|
||||||
|
this.parentGui.updateValueForGeneric(this.material, true);
|
||||||
|
}
|
||||||
|
|
||||||
// ElementMappedToListGui methods
|
// ElementMappedToListGui methods
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -110,7 +169,15 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void cleanAndBeUnusable() {
|
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
|
@Override
|
||||||
|
|
@ -118,4 +185,13 @@ public class UnitRepairElementListGui extends SettingGuiListConfigGui<String, Do
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void show(@NotNull HumanEntity humanEntity) {
|
||||||
|
if(!this.shouldWork){
|
||||||
|
humanEntity.closeInventory();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
super.show(humanEntity);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,7 @@ public abstract class AbstractSettingGui extends ChestGui {
|
||||||
*
|
*
|
||||||
* @param parent Parent gui to go back when completed.
|
* @param parent Parent gui to go back when completed.
|
||||||
*/
|
*/
|
||||||
private void initBase(ValueUpdatableGui parent) {
|
protected void initBase(ValueUpdatableGui parent) {
|
||||||
Pattern pattern = getGuiPattern();
|
Pattern pattern = getGuiPattern();
|
||||||
pane = new PatternPane(0, 0, pattern.getLength(), pattern.getHeight(), pattern);
|
pane = new PatternPane(0, 0, pattern.getLength(), pattern.getHeight(), pattern);
|
||||||
addPane(pane);
|
addPane(pane);
|
||||||
|
|
|
||||||
|
|
@ -20,9 +20,9 @@ import xyz.alexcrea.cuanvil.util.MetricsUtil;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class DoubleSettingGui extends AbstractSettingGui {
|
public class DoubleSettingGui extends AbstractSettingGui {
|
||||||
|
|
@ -61,6 +61,43 @@ public class DoubleSettingGui extends AbstractSettingGui {
|
||||||
updateValueDisplay();
|
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
|
@Override
|
||||||
public Pattern getGuiPattern() {
|
public Pattern getGuiPattern() {
|
||||||
|
|
@ -81,7 +118,7 @@ public class DoubleSettingGui extends AbstractSettingGui {
|
||||||
ItemMeta meta = item.getItemMeta();
|
ItemMeta meta = item.getItemMeta();
|
||||||
|
|
||||||
meta.setDisplayName("\u00A7eReset to default value");
|
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);
|
item.setItemMeta(meta);
|
||||||
returnToDefault = new GuiItem(item, event -> {
|
returnToDefault = new GuiItem(item, event -> {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
|
|
@ -98,7 +135,6 @@ public class DoubleSettingGui extends AbstractSettingGui {
|
||||||
|
|
||||||
PatternPane pane = getPane();
|
PatternPane pane = getPane();
|
||||||
|
|
||||||
|
|
||||||
//minus item
|
//minus item
|
||||||
GuiItem minusItem;
|
GuiItem minusItem;
|
||||||
if (now.compareTo(holder.min) > 0) {
|
if (now.compareTo(holder.min) > 0) {
|
||||||
|
|
@ -139,7 +175,6 @@ public class DoubleSettingGui extends AbstractSettingGui {
|
||||||
}
|
}
|
||||||
pane.bindItem('D', returnToDefault);
|
pane.bindItem('D', returnToDefault);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private GuiItem getSetValueItem(Material mat, BigDecimal planned, String numberPrefix){
|
private GuiItem getSetValueItem(Material mat, BigDecimal planned, String numberPrefix){
|
||||||
|
|
@ -262,7 +297,7 @@ public class DoubleSettingGui extends AbstractSettingGui {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onSave() {
|
public boolean onSave() {
|
||||||
if(this.nullOnZero && (this.now.compareTo(BigDecimal.ZERO) == 0)){
|
if(isNull()){
|
||||||
this.holder.config.getConfig().set(this.holder.configPath, null);
|
this.holder.config.getConfig().set(this.holder.configPath, null);
|
||||||
}else{
|
}else{
|
||||||
this.holder.config.getConfig().set(this.holder.configPath, now.doubleValue());
|
this.holder.config.getConfig().set(this.holder.configPath, now.doubleValue());
|
||||||
|
|
@ -280,6 +315,10 @@ public class DoubleSettingGui extends AbstractSettingGui {
|
||||||
return now.compareTo(before) != 0;
|
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);
|
private static final BigDecimal PERCENTAGE_OFFSET = BigDecimal.valueOf(100);
|
||||||
public String displayValue(BigDecimal value){
|
public String displayValue(BigDecimal value){
|
||||||
return displayValue(value, this.asPercentage);
|
return displayValue(value, this.asPercentage);
|
||||||
|
|
|
||||||
|
|
@ -127,7 +127,10 @@ object MetricsUtil {
|
||||||
isDefaultItemGroupsConfig = false
|
isDefaultItemGroupsConfig = false
|
||||||
} else if (ConfigHolder.UNIT_REPAIR_HOLDER.equals(holder)) {
|
} else if (ConfigHolder.UNIT_REPAIR_HOLDER.equals(holder)) {
|
||||||
isDefaultUnitRepairItemConfig = false
|
isDefaultUnitRepairItemConfig = false
|
||||||
|
} else if (ConfigHolder.CUSTOM_RECIPE_HOLDER.equals(holder)) {
|
||||||
|
isDefaultCustomAnvilCraftConfig = false
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addCustomMetric(metric: Metrics) {
|
fun addCustomMetric(metric: Metrics) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue