added linebreaks for settings descriptions
This commit is contained in:
parent
35b40262a4
commit
78ba105291
src/main/java/eu/mhsl/craftattack/spawn
@ -7,6 +7,7 @@ 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;
|
||||
|
||||
@ -18,6 +19,7 @@ public abstract class BoolSetting extends Setting<Boolean> {
|
||||
}
|
||||
|
||||
protected abstract String title();
|
||||
|
||||
protected abstract String description();
|
||||
|
||||
@Override
|
||||
@ -35,13 +37,17 @@ public abstract class BoolSetting extends Setting<Boolean> {
|
||||
@Override
|
||||
public ItemMeta buildMeta(ItemMeta meta) {
|
||||
meta.displayName(Component.text(title(), NamedTextColor.WHITE));
|
||||
meta.lore(List.of(
|
||||
List<Component> lore = new ArrayList<>(List.of(
|
||||
Component.empty()
|
||||
.append(Component.text("Status: ", NamedTextColor.DARK_GRAY))
|
||||
.append(Component.text(this.state ? "An" : "Aus", this.state ? NamedTextColor.GREEN : NamedTextColor.RED)),
|
||||
Component.empty(),
|
||||
Component.text(description(), NamedTextColor.GRAY)
|
||||
.append(Component.text(
|
||||
this.state ? "Aktiviert" : "Deaktiviert",
|
||||
this.state ? NamedTextColor.GREEN : NamedTextColor.RED)
|
||||
),
|
||||
Component.empty()
|
||||
));
|
||||
lore.addAll(buildDescription(description()));
|
||||
meta.lore(lore);
|
||||
return meta;
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,10 @@ package eu.mhsl.craftattack.spawn.appliances.settings.datatypes;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.Main;
|
||||
import eu.mhsl.craftattack.spawn.appliances.settings.Settings;
|
||||
import eu.mhsl.craftattack.spawn.util.text.ComponentUtil;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.Player;
|
||||
@ -9,6 +13,8 @@ import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class Setting<TDataType> {
|
||||
private final Settings.Key key;
|
||||
|
||||
@ -39,6 +45,12 @@ public abstract class Setting<TDataType> {
|
||||
return stack;
|
||||
}
|
||||
|
||||
protected List<TextComponent> buildDescription(String description) {
|
||||
return ComponentUtil.lineBreak(description, 50)
|
||||
.map(s -> Component.text(s, NamedTextColor.GRAY))
|
||||
.toList();
|
||||
}
|
||||
|
||||
protected abstract Material icon();
|
||||
public abstract ItemMeta buildMeta(ItemMeta meta);
|
||||
protected abstract void change();
|
||||
|
@ -10,13 +10,42 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ComponentUtil {
|
||||
public static TextComponent appendWithNewline(Component a, Component b) {
|
||||
return Component.text().append(a.appendNewline().append(b)).build();
|
||||
}
|
||||
|
||||
public static Stream<String> lineBreak(String text, int charactersPerLine) {
|
||||
List<String> lines = new ArrayList<>();
|
||||
String[] words = text.split(" ");
|
||||
StringBuilder line = new StringBuilder();
|
||||
|
||||
for (String word : words) {
|
||||
if (line.length() + word.length() + 1 > charactersPerLine) {
|
||||
lines.add(line.toString().trim());
|
||||
line = new StringBuilder();
|
||||
}
|
||||
line.append(word).append(" ");
|
||||
}
|
||||
|
||||
if (!line.isEmpty()) {
|
||||
lines.add(line.toString().trim());
|
||||
}
|
||||
|
||||
return lines.stream();
|
||||
}
|
||||
|
||||
public static String lineBreakNL(String text, int charactersPerLine) {
|
||||
Stream<String> lines = lineBreak(text, charactersPerLine);
|
||||
return lines.collect(Collectors.joining("\n"));
|
||||
}
|
||||
|
||||
public static Component getFormattedTPS() {
|
||||
double[] tpsValues = Bukkit.getTPS();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user