mirror of
https://github.com/FancyInnovations/FancyPlugins.git
synced 2025-12-06 07:43:36 +00:00
packets, fancydialogs: Refactor custom action handling to use a map for additional data
This commit is contained in:
@@ -245,7 +245,7 @@ public class ClientboundShowDialogPacketImpl extends FS_ClientboundShowDialogPac
|
||||
Optional<CompoundTag> additions;
|
||||
if (customAction.getAdditions() != null) {
|
||||
CompoundTag tag = new CompoundTag();
|
||||
tag.putString("data", customAction.getAdditions());
|
||||
customAction.getAdditions().forEach(tag::putString);
|
||||
additions = Optional.of(tag);
|
||||
} else {
|
||||
additions = Optional.empty();
|
||||
|
||||
@@ -13,8 +13,9 @@ import net.minecraft.server.level.ServerPlayer;
|
||||
import org.bukkit.craftbukkit.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Map;
|
||||
|
||||
public class PacketListenerImpl extends FS_PacketListener {
|
||||
|
||||
@@ -75,12 +76,16 @@ public class PacketListenerImpl extends FS_PacketListener {
|
||||
switch (type) {
|
||||
case CUSTOM_CLICK_ACTION -> {
|
||||
ServerboundCustomClickActionPacket customClickActionPacket = (ServerboundCustomClickActionPacket) packet;
|
||||
|
||||
Map<String, String> payload = new HashMap<>();
|
||||
customClickActionPacket.payload().get().asCompound().get().forEach((k, t) -> {
|
||||
payload.put(k, t.asString().get());
|
||||
});
|
||||
|
||||
return new FS_ServerboundCustomClickActionPacket(
|
||||
type,
|
||||
PaperAdventure.asAdventure(customClickActionPacket.id()),
|
||||
customClickActionPacket.payload().isPresent() ?
|
||||
customClickActionPacket.payload().get().asCompound().get().getString("data") :
|
||||
Optional.empty()
|
||||
payload
|
||||
);
|
||||
}
|
||||
// Add more cases for other packet types as needed
|
||||
|
||||
@@ -3,12 +3,14 @@ package de.oliver.fancysitula.api.dialogs.actions;
|
||||
import org.intellij.lang.annotations.Subst;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class FS_DialogCustomAction implements FS_DialogActionButtonAction {
|
||||
|
||||
private String id;
|
||||
private @Nullable String additions;
|
||||
private Map<String, String> additions;
|
||||
|
||||
public FS_DialogCustomAction(String id, String additions) {
|
||||
public FS_DialogCustomAction(String id, Map<String, String> additions) {
|
||||
this.id = id;
|
||||
this.additions = additions;
|
||||
}
|
||||
@@ -22,11 +24,11 @@ public class FS_DialogCustomAction implements FS_DialogActionButtonAction {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public @Nullable String getAdditions() {
|
||||
public @Nullable Map<String, String> getAdditions() {
|
||||
return additions;
|
||||
}
|
||||
|
||||
public void setAdditions(@Nullable String additions) {
|
||||
public void setAdditions(@Nullable Map<String, String> additions) {
|
||||
this.additions = additions;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@ package de.oliver.fancysitula.api.packets;
|
||||
|
||||
import net.kyori.adventure.key.Key;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Map;
|
||||
|
||||
public class FS_ServerboundCustomClickActionPacket extends FS_ServerboundPacket {
|
||||
|
||||
private final Key id;
|
||||
private final Optional<String> payload;
|
||||
private final Map<String, String> payload;
|
||||
|
||||
public FS_ServerboundCustomClickActionPacket(Type type, Key id, Optional<String> payload) {
|
||||
public FS_ServerboundCustomClickActionPacket(Type type, Key id, Map<String, String> payload) {
|
||||
super(type);
|
||||
this.id = id;
|
||||
this.payload = payload;
|
||||
@@ -19,7 +19,7 @@ public class FS_ServerboundCustomClickActionPacket extends FS_ServerboundPacket
|
||||
return id;
|
||||
}
|
||||
|
||||
public Optional<String> getPayload() {
|
||||
public Map<String, String> getPayload() {
|
||||
return payload;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class FancySitulaCMD extends Command {
|
||||
@@ -105,7 +106,7 @@ public class FancySitulaCMD extends Command {
|
||||
),
|
||||
new FS_DialogCustomAction(
|
||||
"my-custom-action-1",
|
||||
"someAdditionalData1"
|
||||
Map.of("foo", "bar")
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
@@ -35,12 +35,14 @@ public class ConfirmationDialog {
|
||||
new DialogButton(
|
||||
this.confirmText,
|
||||
this.confirmText,
|
||||
"confirm"
|
||||
"confirm",
|
||||
""
|
||||
),
|
||||
new DialogButton(
|
||||
this.cancelText,
|
||||
this.cancelText,
|
||||
"cancel"
|
||||
"cancel",
|
||||
""
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.fancyinnovations.fancydialogs.api.data;
|
||||
public record DialogButton(
|
||||
String label,
|
||||
String tooltip,
|
||||
String action
|
||||
String action,
|
||||
String actionData
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class DialogImpl extends Dialog {
|
||||
|
||||
@@ -46,7 +47,14 @@ public class DialogImpl extends Dialog {
|
||||
button.tooltip(),
|
||||
150 // default button width
|
||||
),
|
||||
new FS_DialogCustomAction("fancydialogs_dialog_action", button.action())
|
||||
new FS_DialogCustomAction(
|
||||
"fancydialogs_dialog_action",
|
||||
Map.of(
|
||||
"dialog_id", id,
|
||||
"action_id", button.action(),
|
||||
"action_data", button.actionData()
|
||||
)
|
||||
)
|
||||
);
|
||||
actions.add(fsDialogActionButton);
|
||||
}
|
||||
|
||||
@@ -31,6 +31,12 @@ public class CustomClickActionPacketListener {
|
||||
if (!packet.getId().namespace().equals("fancysitula") && !packet.getId().namespace().equals("fancydialogs_dialog_action")) {
|
||||
return; // Ignore packets not related to FancyDialogs
|
||||
}
|
||||
|
||||
String dialogId = packet.getPayload().get("dialog_id");
|
||||
String actionId = packet.getPayload().get("action_id");
|
||||
String actionData = packet.getPayload().get("action_data");
|
||||
|
||||
// TODO process the dialog action
|
||||
}
|
||||
|
||||
public FS_PacketListener getPacketListener() {
|
||||
|
||||
@@ -44,12 +44,14 @@ public class DefaultDialogs {
|
||||
new DialogButton(
|
||||
"<color:#ff4f19>Close</color>",
|
||||
"<color:#ff4f19>Enjoy using FancyDialogs</color>",
|
||||
"close_dialog"
|
||||
"close_dialog",
|
||||
""
|
||||
),
|
||||
new DialogButton(
|
||||
"<color:#ffd000>Run command</color>",
|
||||
"<color:#ff4f19>Click to give yourself an apple :)</color>",
|
||||
"run_command:/give {player} apple 1"
|
||||
"run_command:",
|
||||
"give {player} apple 1"
|
||||
)
|
||||
)
|
||||
);
|
||||
@@ -72,22 +74,26 @@ public class DefaultDialogs {
|
||||
new DialogButton(
|
||||
"<color:red>Read the rules</color>",
|
||||
"<color:red>Click to read our rules!</color>",
|
||||
"message:<click:open_url:'{LINK TO RULES}'>Visit our rules</click>!"
|
||||
"message",
|
||||
"<click:open_url:'{LINK TO RULES}'>Visit our rules</click>!"
|
||||
),
|
||||
new DialogButton(
|
||||
"<color:#00ff5e>Start playing</color>",
|
||||
"<color:#00ff5e>Click to start playing!</color>",
|
||||
"close_dialog"
|
||||
"close_dialog",
|
||||
""
|
||||
),
|
||||
new DialogButton(
|
||||
"<color:#1787ff>Join our Discord</color>",
|
||||
"<color:#1787ff>Click to join our Discord server!</color>",
|
||||
"message:<click:open_url:'{LINK TO DISC SERVER}'>Join our Discord server</click>!"
|
||||
"message",
|
||||
"<click:open_url:'{LINK TO DISC SERVER}'>Join our Discord server</click>!"
|
||||
),
|
||||
new DialogButton(
|
||||
"<color:#ffee00>Visit our website</color>",
|
||||
"<color:#ffee00>Click to visit our website!</color>",
|
||||
"message:<click:open_url:'{LINK TO WEBSITE}'>Visit our website</click>!"
|
||||
"message",
|
||||
"<click:open_url:'{LINK TO WEBSITE}'>Visit our website</click>!"
|
||||
)
|
||||
)
|
||||
);
|
||||
@@ -107,27 +113,32 @@ public class DefaultDialogs {
|
||||
new DialogButton(
|
||||
"<color:#ffee00>Visit our website</color>",
|
||||
"<color:#ffee00>Click to visit our website!</color>",
|
||||
"message:<click:open_url:'{LINK TO WEBSITE}'>Visit our website</click>!"
|
||||
"message",
|
||||
"<click:open_url:'{LINK TO WEBSITE}'>Visit our website</click>!"
|
||||
),
|
||||
new DialogButton(
|
||||
"<color:#ffee00>Read the rules</color>",
|
||||
"<color:#ffee00>Click to read our rules!</color>",
|
||||
"message:<click:open_url:'{LINK TO RULES}'>Visit our rules</click>!"
|
||||
"message",
|
||||
"<click:open_url:'{LINK TO RULES}'>Visit our rules</click>!"
|
||||
),
|
||||
new DialogButton(
|
||||
"<color:#ffee00>Join our Discord</color>",
|
||||
"<color:#ffee00>Click to join our Discord server!</color>",
|
||||
"message:<click:open_url:'{LINK TO DISCORD}'>Join our Discord server</click>!"
|
||||
"message",
|
||||
"<click:open_url:'{LINK TO DISCORD}'>Join our Discord server</click>!"
|
||||
),
|
||||
new DialogButton(
|
||||
"<color:#ffee00>Support us</color>",
|
||||
"<color:#ffee00>Click to support us!</color>",
|
||||
"message:<click:open_url:'{LINK TO SUPPORT}'>Support us</click>!"
|
||||
"message",
|
||||
"<click:open_url:'{LINK TO SUPPORT}'>Support us</click>!"
|
||||
),
|
||||
new DialogButton(
|
||||
"<color:red>Close</color>",
|
||||
"<color:red>Click to close this dialog!</color>",
|
||||
"close_dialog"
|
||||
"close_dialog",
|
||||
""
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user