removed InfoBarCommand and integrated InfoBars with Settings preference handling
This commit is contained in:
@@ -13,6 +13,7 @@ import java.time.temporal.ChronoUnit;
|
||||
public abstract class Bar {
|
||||
private BossBar bossBar;
|
||||
private final BukkitTask updateTask;
|
||||
public static String name;
|
||||
|
||||
public Bar() {
|
||||
long refreshRateInTicks = this.refresh().get(ChronoUnit.SECONDS) * Ticks.TICKS_PER_SECOND;
|
||||
|
@@ -1,32 +0,0 @@
|
||||
package eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.infoBars;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceCommand;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class InfoBarCommand extends ApplianceCommand.PlayerChecked<InfoBars> {
|
||||
public InfoBarCommand() {
|
||||
super("infobar");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void execute(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) throws Exception {
|
||||
if(args.length == 0) throw new Error("<show|hide|hideall> [bar name]");
|
||||
switch(args[0]) {
|
||||
case "hideAll" -> this.getAppliance().hideAll(this.getPlayer());
|
||||
case "show" -> this.getAppliance().show(this.getPlayer(), args[1]);
|
||||
case "hide" -> this.getAppliance().hide(this.getPlayer(), args[1]);
|
||||
default -> throw new Error("Erlaubte Optionen sind 'show', 'hide', 'hideAll'!");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
|
||||
if(args.length == 1) return List.of("show", "hide", "hideAll");
|
||||
return this.getAppliance().getInfoBars().stream().map(Bar::name).toList();
|
||||
}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
package eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.infoBars;
|
||||
|
||||
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.MultiBoolSetting;
|
||||
import org.bukkit.Material;
|
||||
|
||||
public class InfoBarSetting extends MultiBoolSetting<InfoBarSetting.InfoBarConfiguration> implements CategorizedSetting {
|
||||
public InfoBarSetting() {
|
||||
super(Settings.Key.InfoBars);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SettingCategory category() {
|
||||
return SettingCategory.Misc;
|
||||
}
|
||||
|
||||
public record InfoBarConfiguration(
|
||||
@DisplayName("mspt") boolean mspt,
|
||||
@DisplayName("Player counter") boolean playerCounter,
|
||||
@DisplayName("TPS") boolean tps
|
||||
) {}
|
||||
|
||||
@Override
|
||||
protected String title() {
|
||||
return "Informationsleisten";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String description() {
|
||||
return "Wähle anzuzeigende Informationsleisten aus";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Material icon() {
|
||||
return Material.COMMAND_BLOCK;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InfoBarConfiguration defaultValue() {
|
||||
return new InfoBarConfiguration(false, false, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> dataType() {
|
||||
return InfoBarConfiguration.class;
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
package eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.infoBars;
|
||||
|
||||
import eu.mhsl.craftattack.spawn.common.appliances.metaGameplay.settings.Settings;
|
||||
import eu.mhsl.craftattack.spawn.core.Main;
|
||||
import eu.mhsl.craftattack.spawn.core.appliance.Appliance;
|
||||
import eu.mhsl.craftattack.spawn.core.appliance.ApplianceCommand;
|
||||
@@ -24,38 +25,38 @@ public class InfoBars extends Appliance {
|
||||
new PlayerCounterBar()
|
||||
);
|
||||
|
||||
public void showAll(Player player) {
|
||||
this.getStoredBars(player).forEach(bar -> this.show(player, bar));
|
||||
public void showAllEnabled(Player player) {
|
||||
this.getEnabledBars(player).forEach(bar -> this.show(player, bar));
|
||||
}
|
||||
|
||||
public void hideAll(Player player) {
|
||||
this.getStoredBars(player).forEach(bar -> this.hide(player, bar));
|
||||
this.setStoredBars(player, List.of());
|
||||
public void hideAllEnabled(Player player) {
|
||||
this.getEnabledBars(player).forEach(bar -> this.hide(player, bar));
|
||||
this.setEnabledBars(player, List.of());
|
||||
}
|
||||
|
||||
public void show(Player player, String bar) {
|
||||
this.validateBarName(bar);
|
||||
List<String> existingBars = new ArrayList<>(this.getStoredBars(player));
|
||||
List<String> existingBars = new ArrayList<>(this.getEnabledBars(player));
|
||||
existingBars.add(bar);
|
||||
player.showBossBar(this.getBarByName(bar).getBossBar());
|
||||
this.setStoredBars(player, existingBars);
|
||||
this.setEnabledBars(player, existingBars);
|
||||
}
|
||||
|
||||
public void hide(Player player, String bar) {
|
||||
this.validateBarName(bar);
|
||||
List<String> existingBars = new ArrayList<>(this.getStoredBars(player));
|
||||
List<String> existingBars = new ArrayList<>(this.getEnabledBars(player));
|
||||
existingBars.remove(bar);
|
||||
player.hideBossBar(this.getBarByName(bar).getBossBar());
|
||||
this.setStoredBars(player, existingBars);
|
||||
this.setEnabledBars(player, existingBars);
|
||||
}
|
||||
|
||||
private List<String> getStoredBars(Player player) {
|
||||
private List<String> getEnabledBars(Player player) {
|
||||
PersistentDataContainer container = player.getPersistentDataContainer();
|
||||
if(!container.has(this.infoBarKey)) return List.of();
|
||||
return container.get(this.infoBarKey, PersistentDataType.LIST.strings());
|
||||
}
|
||||
|
||||
private void setStoredBars(Player player, List<String> bars) {
|
||||
private void setEnabledBars(Player player, List<String> bars) {
|
||||
player.getPersistentDataContainer().set(this.infoBarKey, PersistentDataType.LIST.strings(), bars);
|
||||
}
|
||||
|
||||
@@ -71,8 +72,16 @@ public class InfoBars extends Appliance {
|
||||
throw new ApplianceCommand.Error(String.format("Ungültiger infobar name '%s'", name));
|
||||
}
|
||||
|
||||
public List<Bar> getInfoBars() {
|
||||
return this.infoBars;
|
||||
@Override
|
||||
public void onEnable() {
|
||||
Settings.instance().declareSetting(InfoBarSetting.class);
|
||||
Settings.instance().addChangeListener(InfoBarSetting.class, player -> {
|
||||
this.hideAllEnabled(player);
|
||||
InfoBarSetting.InfoBarConfiguration config = Settings.instance().getSetting(player, Settings.Key.InfoBars, InfoBarSetting.InfoBarConfiguration.class);
|
||||
if(config.mspt()) this.show(player, MsptBar.name);
|
||||
if(config.playerCounter()) this.show(player, PlayerCounterBar.name);
|
||||
if(config.tps()) this.show(player, TpsBar.name);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -84,9 +93,4 @@ public class InfoBars extends Appliance {
|
||||
protected @NotNull List<Listener> listeners() {
|
||||
return List.of(new ShowPreviousBarsListener());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull List<ApplianceCommand<?>> commands() {
|
||||
return List.of(new InfoBarCommand());
|
||||
}
|
||||
}
|
||||
|
@@ -7,6 +7,6 @@ import org.bukkit.event.player.PlayerJoinEvent;
|
||||
class ShowPreviousBarsListener extends ApplianceListener<InfoBars> {
|
||||
@EventHandler
|
||||
public void onJoin(PlayerJoinEvent event) {
|
||||
// this.getAppliance().showAll(event.getPlayer());
|
||||
this.getAppliance().showAllEnabled(event.getPlayer());
|
||||
}
|
||||
}
|
||||
|
@@ -10,6 +10,8 @@ import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import java.time.Duration;
|
||||
|
||||
public class MsptBar extends Bar {
|
||||
public static String name = "msptd";
|
||||
|
||||
@Override
|
||||
protected Duration refresh() {
|
||||
return Duration.ofSeconds(3);
|
||||
@@ -17,7 +19,7 @@ public class MsptBar extends Bar {
|
||||
|
||||
@Override
|
||||
protected String name() {
|
||||
return "mspt";
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -12,6 +12,8 @@ import org.bukkit.Bukkit;
|
||||
import java.time.Duration;
|
||||
|
||||
public class PlayerCounterBar extends Bar {
|
||||
public static String name = "playerCounter";
|
||||
|
||||
@Override
|
||||
protected Duration refresh() {
|
||||
return Duration.ofSeconds(3);
|
||||
@@ -19,7 +21,7 @@ public class PlayerCounterBar extends Bar {
|
||||
|
||||
@Override
|
||||
protected String name() {
|
||||
return "playerCounter";
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -10,6 +10,8 @@ import org.bukkit.Bukkit;
|
||||
import java.time.Duration;
|
||||
|
||||
public class TpsBar extends Bar {
|
||||
public static String name = "tps";
|
||||
|
||||
@Override
|
||||
protected Duration refresh() {
|
||||
return Duration.ofSeconds(3);
|
||||
@@ -17,7 +19,7 @@ public class TpsBar extends Bar {
|
||||
|
||||
@Override
|
||||
protected String name() {
|
||||
return "tps";
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -36,7 +36,8 @@ public class Settings extends Appliance {
|
||||
DoubleDoors,
|
||||
KnockDoors,
|
||||
BorderWarning,
|
||||
LocatorBarConfig
|
||||
LocatorBar,
|
||||
InfoBars
|
||||
}
|
||||
|
||||
public static Settings instance() {
|
||||
|
@@ -26,7 +26,7 @@ public abstract class Setting<TDataType> {
|
||||
}
|
||||
|
||||
public NamespacedKey getNamespacedKey() {
|
||||
return new NamespacedKey(Main.instance(), this.key.name());
|
||||
return new NamespacedKey(Settings.class.getSimpleName().toLowerCase(), this.key.name().toLowerCase());
|
||||
}
|
||||
|
||||
public Settings.Key getKey() {
|
||||
|
@@ -29,7 +29,7 @@ public class LocatorBar extends Appliance {
|
||||
}
|
||||
|
||||
public void updateLocatorBar(Player player) {
|
||||
boolean enabled = Settings.instance().getSetting(player, Settings.Key.LocatorBarConfig, Boolean.class);
|
||||
boolean enabled = Settings.instance().getSetting(player, Settings.Key.LocatorBar, Boolean.class);
|
||||
|
||||
AttributeInstance receive = player.getAttribute(Attribute.WAYPOINT_RECEIVE_RANGE);
|
||||
AttributeInstance transmit = player.getAttribute(Attribute.WAYPOINT_TRANSMIT_RANGE);
|
||||
|
@@ -13,7 +13,7 @@ public class LocatorBarSettings extends BoolSetting implements CategorizedSettin
|
||||
}
|
||||
|
||||
public LocatorBarSettings() {
|
||||
super(Settings.Key.LocatorBarConfig);
|
||||
super(Settings.Key.LocatorBar);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user