mirror of
https://github.com/plexusorg/Plex.git
synced 2026-07-14 21:44:55 +00:00
Updater improvements, toggle pvp, Dialog API
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
package dev.plex.menu.dialog;
|
||||
|
||||
import dev.plex.Plex;
|
||||
import dev.plex.util.PlexUtils;
|
||||
import io.papermc.paper.dialog.Dialog;
|
||||
import io.papermc.paper.registry.data.dialog.ActionButton;
|
||||
import io.papermc.paper.registry.data.dialog.DialogBase;
|
||||
import io.papermc.paper.registry.data.dialog.action.DialogAction;
|
||||
import io.papermc.paper.registry.data.dialog.body.DialogBody;
|
||||
import io.papermc.paper.registry.data.dialog.type.DialogType;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.event.ClickCallback;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class ToggleDialog
|
||||
{
|
||||
private static final ClickCallback.Options CALLBACK_OPTIONS = ClickCallback.Options.builder()
|
||||
.uses(1)
|
||||
.lifetime(Duration.ofMinutes(5))
|
||||
.build();
|
||||
|
||||
private final Plex plugin;
|
||||
|
||||
public ToggleDialog(Plex plugin)
|
||||
{
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public void open(Player player)
|
||||
{
|
||||
player.showDialog(create());
|
||||
}
|
||||
|
||||
private Dialog create()
|
||||
{
|
||||
return Dialog.create(builder -> builder.empty()
|
||||
.base(DialogBase.builder(PlexUtils.messageComponent("toggleMenuTitle"))
|
||||
.canCloseWithEscape(true)
|
||||
.pause(false)
|
||||
.afterAction(DialogBase.DialogAfterAction.WAIT_FOR_RESPONSE)
|
||||
.body(List.of(
|
||||
DialogBody.plainMessage(statusLine("toggleExplosions", "explosions", true)),
|
||||
DialogBody.plainMessage(statusLine("toggleFluidSpread", "fluidspread", false)),
|
||||
DialogBody.plainMessage(statusLine("toggleDrops", "drops", false)),
|
||||
DialogBody.plainMessage(statusLine("toggleRedstone", "redstone", false)),
|
||||
DialogBody.plainMessage(statusLine("togglePvp", "pvp", false)),
|
||||
DialogBody.plainMessage(Component.text(PlexUtils.messageString("toggleChat") + ": " + PlexUtils.messageString(plugin.toggles.getBoolean("chat") ? "stateOn" : "stateOff")))))
|
||||
.build())
|
||||
.type(DialogType.multiAction(List.of(
|
||||
toggleButton("toggleExplosions", "toggleExplosionsLower", "explosions"),
|
||||
toggleButton("toggleFluidSpread", "toggleFluidSpreadLower", "fluidspread"),
|
||||
toggleButton("toggleDrops", "toggleDropsLower", "drops"),
|
||||
toggleButton("toggleRedstone", "toggleRedstoneLower", "redstone"),
|
||||
toggleButton("togglePvp", "togglePvpLower", "pvp"),
|
||||
toggleButton("toggleChat", "toggleChatLower", "chat")),
|
||||
closeButton(), 2)));
|
||||
}
|
||||
|
||||
private ActionButton toggleButton(String nameKey, String lowerNameKey, String toggle)
|
||||
{
|
||||
return ActionButton.builder(Component.text(PlexUtils.messageString(nameKey)))
|
||||
.tooltip(Component.text(PlexUtils.messageString(plugin.toggles.getBoolean(toggle) ? "stateEnabled" : "stateDisabled")))
|
||||
.width(150)
|
||||
.action(DialogAction.customClick((response, audience) -> toggle(audience, lowerNameKey, toggle), CALLBACK_OPTIONS))
|
||||
.build();
|
||||
}
|
||||
|
||||
private ActionButton closeButton()
|
||||
{
|
||||
return ActionButton.builder(Component.text("Close"))
|
||||
.width(150)
|
||||
.build();
|
||||
}
|
||||
|
||||
private void toggle(Audience audience, String lowerNameKey, String toggle)
|
||||
{
|
||||
if (!(audience instanceof Player player) || !player.hasPermission("plex.toggle"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
plugin.toggles.set(toggle, !plugin.toggles.getBoolean(toggle));
|
||||
if ("chat".equals(toggle))
|
||||
{
|
||||
PlexUtils.broadcast(PlexUtils.messageComponent("chatToggled", player.getName(), PlexUtils.messageString(plugin.toggles.getBoolean("chat") ? "stateOn" : "stateOff")));
|
||||
}
|
||||
player.sendMessage(PlexUtils.messageComponent("toggleToggled", PlexUtils.messageString(lowerNameKey)));
|
||||
open(player);
|
||||
}
|
||||
|
||||
private Component statusLine(String nameKey, String toggle, boolean enabledIsUnsafe)
|
||||
{
|
||||
return Component.text(PlexUtils.messageString(nameKey) + ": " + status(toggle, enabledIsUnsafe));
|
||||
}
|
||||
|
||||
private String status(String toggle, boolean enabledIsUnsafe)
|
||||
{
|
||||
if (enabledIsUnsafe)
|
||||
{
|
||||
return PlexUtils.messageString(plugin.toggles.getBoolean(toggle) ? "stateEnabledUnsafe" : "stateDisabledSafe");
|
||||
}
|
||||
return PlexUtils.messageString(plugin.toggles.getBoolean(toggle) ? "stateEnabled" : "stateDisabled");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user