added LocatorBar preferences

This commit is contained in:
2025-10-01 19:11:16 +02:00
parent c88c2ab6aa
commit d4a3c798f8
5 changed files with 132 additions and 2 deletions

View File

@@ -17,6 +17,7 @@ import org.jetbrains.annotations.NotNull;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class Settings extends Appliance {
@@ -34,7 +35,8 @@ public class Settings extends Appliance {
ChatMentions,
DoubleDoors,
KnockDoors,
BorderWarning
BorderWarning,
LocatorBarConfig
}
public static Settings instance() {
@@ -58,6 +60,16 @@ public class Settings extends Appliance {
private final WeakHashMap<Player, OpenSettingsInventory> openSettingsInventories = new WeakHashMap<>();
private final WeakHashMap<Player, List<Setting<?>>> settingsCache = new WeakHashMap<>();
protected final Map<Class<? extends Setting<?>>, Consumer<Player>> changeListeners = new WeakHashMap<>();
public <TDataType extends Setting<?>> void addChangeListener(Class<TDataType> setting, Consumer<Player> listener) {
this.changeListeners.merge(setting, listener, Consumer::andThen);
}
public <TDataType extends Setting<?>> void invokeChangeListener(Player player, Class<TDataType> setting) {
Optional.ofNullable(this.changeListeners.get(setting))
.ifPresent(listener -> listener.accept(player));
}
private List<Setting<?>> getSettings(Player player) {
if(this.settingsCache.containsKey(player)) return this.settingsCache.get(player);

View File

@@ -34,7 +34,24 @@ public abstract class Setting<TDataType> {
}
public void initializeFromPlayer(Player p) {
this.fromStorage(p.getPersistentDataContainer());
PersistentDataContainer dataContainer = p.getPersistentDataContainer();
try {
this.fromStorage(dataContainer);
} catch(IllegalArgumentException e) {
Main.logger().warning(String.format(
"Could not load state of setting %s from player %s: '%s'\n Did the datatype of the setting change?",
this.getNamespacedKey(),
e.getMessage(),
p.getName()
));
dataContainer.remove(this.getNamespacedKey());
this.fromStorage(dataContainer);
Main.logger().info(String.format(
"Restoring defaults of setting %s of player %s",
this.getNamespacedKey(),
p.getName()
));
}
}
public void triggerChange(Player p, ClickType clickType) {
@@ -42,6 +59,7 @@ public abstract class Setting<TDataType> {
this.change(p, clickType);
InteractSounds.of(p).click();
this.toStorage(p.getPersistentDataContainer(), this.state());
Settings.instance().invokeChangeListener(p, this.getClass());
}
public ItemStack buildItem() {