added integer setting
This commit is contained in:
parent
b0414ae6ab
commit
ca99e6cfef
common/src/main/java/eu/mhsl/craftattack/spawn/common/appliances/metaGameplay/settings
varo/src/main/java/eu/mhsl/craftattack/spawn/varo/appliances/metaGameplay/shrinkingBorder
12
common/src/main/java/eu/mhsl/craftattack/spawn/common/appliances/metaGameplay/settings/Settings.java
12
common/src/main/java/eu/mhsl/craftattack/spawn/common/appliances/metaGameplay/settings/Settings.java
@ -143,9 +143,9 @@ public class Settings extends Appliance {
|
||||
int countOfUncategorized = (int) settings.stream()
|
||||
.filter(setting -> !(setting instanceof CategorizedSetting))
|
||||
.count();
|
||||
int rowsOfUncategorized = (int) Math.ceil((double) countOfUncategorized / 9);
|
||||
int invSizeForUncategorized = (int) Math.ceil((double) countOfUncategorized / 9) * 9;
|
||||
|
||||
int rowsOfCategorized = Arrays.stream(SettingCategory.values())
|
||||
int invSizeForCategorized = Arrays.stream(SettingCategory.values())
|
||||
.map(settingCategory -> settings.stream()
|
||||
.filter(setting -> setting instanceof CategorizedSetting)
|
||||
.map(setting -> (CategorizedSetting) setting)
|
||||
@ -155,11 +155,11 @@ public class Settings extends Appliance {
|
||||
.reduce(Integer::sum)
|
||||
.orElse(1) * 9;
|
||||
|
||||
int rows = rowsOfUncategorized + rowsOfCategorized;
|
||||
if(rows % 9 != 0) throw new IllegalStateException(
|
||||
String.format("Failed to calculate settings inventory size. %d is not an multiple of 9", rows)
|
||||
int invSize = invSizeForUncategorized + invSizeForCategorized;
|
||||
if(invSize % 9 != 0) throw new IllegalStateException(
|
||||
String.format("Failed to calculate settings inventory size. %d is not an multiple of 9", invSize)
|
||||
);
|
||||
return rows;
|
||||
return invSize;
|
||||
}
|
||||
|
||||
public void onSettingsClose(Player player) {
|
||||
|
101
common/src/main/java/eu/mhsl/craftattack/spawn/common/appliances/metaGameplay/settings/datatypes/IntegerSetting.java
Normal file
101
common/src/main/java/eu/mhsl/craftattack/spawn/common/appliances/metaGameplay/settings/datatypes/IntegerSetting.java
Normal file
@ -0,0 +1,101 @@
|
||||
package eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.datatypes;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.Settings;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.inventory.ClickType;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public abstract class IntegerSetting extends Setting<Integer> {
|
||||
private final List<Integer> options;
|
||||
|
||||
public IntegerSetting(Settings.Key key, int minimum, int maximum) {
|
||||
this(key, IntStream.range(minimum, maximum).boxed().toList());
|
||||
}
|
||||
|
||||
public IntegerSetting(Settings.Key key, List<Integer> options) {
|
||||
super(key);
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemMeta buildMeta(ItemMeta meta) {
|
||||
Component componentBefore = Component.text(" " + this.fillWithSpaces(this.options.getLast()));
|
||||
Component componentAfter = Component.text(" " + this.fillWithSpaces(this.options.getFirst()));
|
||||
int listIndex = this.options.indexOf(this.state);
|
||||
if(listIndex > 0) componentBefore = Component.text(" " + this.fillWithSpaces(this.options.get(listIndex-1)));
|
||||
if(listIndex < this.options.size()-1) componentAfter = Component.text(" " + this.fillWithSpaces(this.options.get(listIndex+1)));
|
||||
|
||||
meta.displayName(Component.text(this.title(), NamedTextColor.WHITE));
|
||||
List<Component> lore = new ArrayList<>(Stream.of(
|
||||
Component.empty()
|
||||
.append(Component.text("Wert: ", NamedTextColor.DARK_GRAY)),
|
||||
Component.empty()
|
||||
.append(componentBefore.color(NamedTextColor.DARK_GRAY))
|
||||
.append(Component.text(" " + this.fillWithSpaces(this.state), NamedTextColor.GREEN))
|
||||
.append(componentAfter.color(NamedTextColor.DARK_GRAY)),
|
||||
Component.empty()
|
||||
).toList());
|
||||
lore.addAll(this.buildDescription(this.description()));
|
||||
meta.lore(lore);
|
||||
return meta;
|
||||
}
|
||||
|
||||
private String fillWithSpaces(Integer option) {
|
||||
String optionString = option.toString();
|
||||
int optionLength = optionString.length();
|
||||
int maxInteger = this.options.stream().mapToInt(value -> value).max().orElse(0);
|
||||
int maxLength = String.valueOf(maxInteger).length();
|
||||
int padding = maxLength - optionLength;
|
||||
|
||||
int padEnd = padding / 2;
|
||||
int padStart = padding - padEnd;
|
||||
|
||||
optionString = " ".repeat(padStart) + optionString + " ".repeat(padEnd);
|
||||
return optionString;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void change(Player player, ClickType clickType) {
|
||||
int elementBefore = this.options.getLast();
|
||||
int elementAfter = this.options.getFirst();
|
||||
int listIndex = this.options.indexOf(this.state);
|
||||
if(listIndex > 0) elementBefore = this.options.get(listIndex-1);
|
||||
if(listIndex < this.options.size()-1) elementAfter = this.options.get(listIndex+1);
|
||||
|
||||
if(clickType.equals(ClickType.LEFT)) this.state = elementBefore;
|
||||
if(clickType.equals(ClickType.RIGHT)) this.state = elementAfter;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void fromStorage(PersistentDataContainer container) {
|
||||
this.state = container.has(this.getNamespacedKey())
|
||||
? Integer.valueOf(Objects.requireNonNull(container.get(this.getNamespacedKey(), PersistentDataType.STRING)))
|
||||
: this.defaultValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void toStorage(PersistentDataContainer container, Integer value) {
|
||||
container.set(this.getNamespacedKey(), PersistentDataType.STRING, new Gson().toJson(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> dataType() {
|
||||
return Integer.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer state() {
|
||||
return this.state;
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@ package eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.shrinkingBorder;
|
||||
|
||||
import eu.mhsl.craftattack.core.appliance.ApplianceListener;
|
||||
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.Settings;
|
||||
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.datatypes.SelectSetting;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.bukkit.Location;
|
||||
@ -19,11 +18,7 @@ public class ShrinkingBorderListener extends ApplianceListener<ShrinkingBorder>
|
||||
if(this.isSave(event.getTo())) return;
|
||||
int remainingDays = this.daysUntilBorder(event.getTo());
|
||||
|
||||
SelectSetting.Options.Option setting = Settings.instance().getSetting(event.getPlayer(), Settings.Key.BorderWarning, SelectSetting.Options.Option.class);
|
||||
int maxDaysShown = 0;
|
||||
if(setting.is(ShrinkingBorderSetting.oneDay)) maxDaysShown = 1;
|
||||
if(setting.is(ShrinkingBorderSetting.twoDays)) maxDaysShown = 2;
|
||||
if(setting.is(ShrinkingBorderSetting.threeDays)) maxDaysShown = 3;
|
||||
int maxDaysShown = Settings.instance().getSetting(event.getPlayer(), Settings.Key.BorderWarning, Integer.class);
|
||||
|
||||
if(remainingDays > maxDaysShown) return;
|
||||
|
||||
|
@ -3,24 +3,16 @@ package eu.mhsl.craftattack.spawn.varo.appliances.metaGameplay.shrinkingBorder;
|
||||
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.CategorizedSetting;
|
||||
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.SettingCategory;
|
||||
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.Settings;
|
||||
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.datatypes.SelectSetting;
|
||||
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.datatypes.IntegerSetting;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class ShrinkingBorderSetting extends SelectSetting implements CategorizedSetting {
|
||||
private static final String namespace = ShrinkingBorderSetting.class.getSimpleName().toLowerCase(Locale.ROOT);
|
||||
public static Options.Option noWarning = new Options.Option("Keine Warnung", new NamespacedKey(namespace, "disabled"));
|
||||
public static Options.Option oneDay = new Options.Option("Ein Tag", new NamespacedKey(namespace, "one"));
|
||||
public static Options.Option twoDays = new Options.Option("Zwei Tage", new NamespacedKey(namespace, "two"));
|
||||
public static Options.Option threeDays = new Options.Option("Drei Tage", new NamespacedKey(namespace, "three"));
|
||||
|
||||
public class ShrinkingBorderSetting extends IntegerSetting implements CategorizedSetting {
|
||||
public ShrinkingBorderSetting() {
|
||||
super(
|
||||
Settings.Key.BorderWarning,
|
||||
new Options(List.of(noWarning, oneDay, twoDays, threeDays))
|
||||
List.of(0, 1, 2, 3)
|
||||
);
|
||||
}
|
||||
|
||||
@ -45,7 +37,7 @@ public class ShrinkingBorderSetting extends SelectSetting implements Categorized
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Options.Option defaultValue() {
|
||||
return oneDay;
|
||||
protected Integer defaultValue() {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user