Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@
import net.minestom.server.event.instance.RemoveEntityFromInstanceEvent;
import net.minestom.server.event.player.AsyncPlayerConfigurationEvent;
import net.minestom.server.event.player.PlayerBlockBreakEvent;
import net.minestom.server.event.player.PlayerCustomClickEvent;
import net.minestom.server.event.player.PlayerSpawnEvent;
import net.minestom.server.event.player.PlayerUseItemEvent;
import net.onelitefeather.cygnus.common.ListenerHandling;
import net.minestom.server.instance.Instance;
import net.onelitefeather.cygnus.setup.command.SetupCommand;
import net.onelitefeather.cygnus.setup.event.MapSetupSaveEvent;
import net.onelitefeather.cygnus.setup.event.MapSetupSelectEvent;
import net.onelitefeather.cygnus.setup.event.dialog.DialogRequestEvent;
import net.onelitefeather.cygnus.setup.inventory.MapSetupInventory;
import net.onelitefeather.cygnus.setup.listener.InstanceAddListener;
import net.onelitefeather.cygnus.setup.listener.InstanceRemoveListener;
import net.onelitefeather.cygnus.setup.listener.MapSetupSelectListener;
import net.onelitefeather.cygnus.setup.listener.PageCreationListener;
import net.onelitefeather.cygnus.setup.listener.PlayerSpawnListener;
import net.onelitefeather.cygnus.setup.listener.SetupItemListener;
import net.onelitefeather.cygnus.setup.listener.dialog.DialogPayloadListener;
import net.onelitefeather.cygnus.setup.listener.dialog.DialogRequestListener;
import net.onelitefeather.cygnus.setup.listener.map.MapSetupSaveListener;
import net.onelitefeather.cygnus.setup.map.SetupMapProvider;
import net.onelitefeather.cygnus.setup.util.SetupData;
Expand All @@ -35,12 +39,12 @@
public class SetupExtension implements ListenerHandling {

private final SetupDataService dataService;
private final SetupData setupData;

Check warning on line 42 in setup/src/main/java/net/onelitefeather/cygnus/setup/SetupExtension.java

View workflow job for this annotation

GitHub Actions / Build Pull Request Branch (ubuntu-latest)

[removal] SetupData in net.onelitefeather.cygnus.setup.util has been deprecated and marked for removal
private final MapSetupInventory mapSetupInventory;
private final AbstractMapProvider mapProvider;

public SetupExtension() {
this.setupData = new SetupData();

Check warning on line 47 in setup/src/main/java/net/onelitefeather/cygnus/setup/SetupExtension.java

View workflow job for this annotation

GitHub Actions / Build Pull Request Branch (ubuntu-latest)

[removal] SetupData in net.onelitefeather.cygnus.setup.util has been deprecated and marked for removal
this.dataService = SetupDataService.create();
this.mapProvider = new SetupMapProvider(Paths.get(""));
this.mapSetupInventory = new MapSetupInventory(mapProvider.getEntries());
Expand Down Expand Up @@ -68,6 +72,10 @@
manager.addListener(AddEntityToInstanceEvent.class, new InstanceAddListener(instanceUUID));
manager.addListener(RemoveEntityFromInstanceEvent.class, new InstanceRemoveListener(instanceUUID));
registerCancelListener(manager);

//Dialog listener
manager.addListener(DialogRequestEvent.class, new DialogRequestListener());
manager.addListener(PlayerCustomClickEvent.class, new DialogPayloadListener(setupData));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package net.onelitefeather.cygnus.setup.dialogs;

import net.kyori.adventure.key.Key;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.text.Component;
import net.minestom.server.dialog.DialogAction;
import net.minestom.server.dialog.DialogAfterAction;
import net.minestom.server.entity.Player;
import net.onelitefeather.cygnus.setup.event.dialog.DialogContext;
import net.onelitefeather.cygnus.setup.map.MapDataCategory;
import net.onelitefeather.cygnus.setup.util.DialogBase;
import net.onelitefeather.pica.dialog.DialogTemplate;
import net.onelitefeather.pica.dialog.type.DialogType;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;

/**
*
*/
public final class MapDialogs extends DialogBase {

public static final Key MAP_KEY = create("map_name");

/**
* Opens the dialog to allow the input of a name.
*
* @param player who should see the dialog
*/
public static void openNameCreateDialog(Player player) {
DialogTemplate dialogTemplate = DialogType.confirm(MAP_KEY)
.meta(dialogMeta -> {
dialogMeta.closeWithEscape(false);
dialogMeta.pause(false);
dialogMeta.afterAction(DialogAfterAction.CLOSE);
dialogMeta.title(Component.text("Map setup"));
dialogMeta.emptyMessage();
dialogMeta.messageBody(template ->
template.contents(Component.text("Enter the name of the map")));
dialogMeta.text("name", textInputTemplate ->
textInputTemplate.maxLength(100).initial(""));
})
.yesButton(button -> button.width(101).label(Component.text("Save"))
.action(new DialogAction.DynamicCustom(MAP_KEY, getEmptyPayload()))
)
.noButton(button -> button.width(101).label(NO_COMPONENT))
.build();
dialogTemplate.open(player);
}

public static void openNameUpdateDialog(Player player, String name) {
DialogTemplate dialogTemplate = DialogType.confirm(MAP_KEY)
.meta(dialogMeta -> {
dialogMeta.closeWithEscape(false);
dialogMeta.pause(false);
dialogMeta.afterAction(DialogAfterAction.CLOSE);
dialogMeta.title(Component.text("Map setup"));
dialogMeta.emptyMessage();
dialogMeta.messageBody(template ->
template.contents(Component.text("Update name of the map")));
dialogMeta.text("name", textInputTemplate ->
textInputTemplate.maxLength(100).initial(name));
})
.yesButton(button -> button.width(101).label(Component.text("Save"))
.action(new DialogAction.DynamicCustom(MAP_KEY, getEmptyPayload()))
)
.noButton(button -> button.width(101).label(NO_COMPONENT))
.build();
dialogTemplate.open(player);
}

public static void openDeleteDialog(Player player, MapDataCategory mapDataCategory) {
DialogTemplate dialogTemplate = DialogType.confirm(MAP_KEY)
.meta(dialogMeta -> {
dialogMeta.closeWithEscape(false);
dialogMeta.pause(false);
dialogMeta.afterAction(DialogAfterAction.CLOSE);
dialogMeta.title(Component.text("Map setup"));
dialogMeta.emptyMessage();
dialogMeta.messageBody(template ->
template.contents(Component.text("Do you want to delete the following map data?")));
dialogMeta.emptyMessage();
dialogMeta.messageBody(template -> template.contents(Component.text(mapDataCategory.name())));
})
.yesButton(button -> button.width(101).label(Component.text("Save"))
.action(new DialogAction.DynamicCustom(MAP_KEY, getCategoryPayload(mapDataCategory.ordinal())))
)
.noButton(button -> button.width(101).label(NO_COMPONENT))
.build();
dialogTemplate.open(player);
}

public static void openDeleteDialog(Player player, MapDataCategory mapDataCategory, @Nullable DialogContext context) {

}

private MapDialogs() {
}

/**
* Returns a payload that contains the id of the frame to update.
*
* @param id the id of the frame
* @return a payload
*/
@Contract(pure = true)
private static CompoundBinaryTag getCategoryPayload(int id) {
return CompoundBinaryTag.builder().putInt("category_id", id).build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.onelitefeather.cygnus.setup.event.dialog;

import net.minestom.server.coordinate.Point;

/**
* Represents the context for a dialog event which contains all necessary data
* to open the correct dialog for a specific {@link DialogTarget}.
Expand All @@ -10,7 +12,8 @@
* @since 0.1.0
*/
public sealed interface DialogContext permits
DialogContext.NameContext {
DialogContext.NameContext,
DialogContext.PositionContent {

/**
* Specific context for the update or deletion of a name.
Expand All @@ -20,4 +23,13 @@ public sealed interface DialogContext permits
record NameContext(String name) implements DialogContext {

}

/**
* Specific context to delete a position from the map.
*
* @param point to which should be deleted
*/
record PositionContent(Point point) implements DialogContext {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
*/
public enum DialogTarget {


CREATE_NAME,
UPDATE_NAME,
DELETE_NAME,
CREATE_SPAWN,
DELETE_SPAWN,
DELETE_SLENDER,
DELETE_PAGE_FACE,
DELETE_SURVIVOR_SPAWN
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.onelitefeather.cygnus.setup.listener.dialog;

import net.kyori.adventure.key.Key;
import net.kyori.adventure.nbt.BinaryTag;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.nbt.StringBinaryTag;
import net.minestom.server.event.player.PlayerCustomClickEvent;
import net.onelitefeather.cygnus.setup.dialogs.MapDialogs;
import net.onelitefeather.cygnus.setup.util.SetupData;

import java.util.function.Consumer;

public class DialogPayloadListener implements Consumer<PlayerCustomClickEvent> {

private final SetupData setupData;

Check warning on line 15 in setup/src/main/java/net/onelitefeather/cygnus/setup/listener/dialog/DialogPayloadListener.java

View workflow job for this annotation

GitHub Actions / Build Pull Request Branch (macos-latest)

[removal] SetupData in net.onelitefeather.cygnus.setup.util has been deprecated and marked for removal

public DialogPayloadListener(SetupData setupData) {

Check warning on line 17 in setup/src/main/java/net/onelitefeather/cygnus/setup/listener/dialog/DialogPayloadListener.java

View workflow job for this annotation

GitHub Actions / Build Pull Request Branch (macos-latest)

[removal] SetupData in net.onelitefeather.cygnus.setup.util has been deprecated and marked for removal
this.setupData = setupData;
}

@Override
public void accept(PlayerCustomClickEvent event) {
Key key = event.getKey();
BinaryTag payload = event.getPayload();

if (payload == null) return;

CompoundBinaryTag castedPayload = (CompoundBinaryTag) payload;

if (key.equals(MapDialogs.MAP_KEY)) {
StringBinaryTag nameBinary = (StringBinaryTag) castedPayload.get("name");
String nameEntry = nameBinary.value();

if (nameEntry.trim().isBlank()) {
return;
}


this.setupData.getBaseMapBuilder().name(nameEntry);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.onelitefeather.cygnus.setup.listener.dialog;

import net.minestom.server.entity.Player;
import net.onelitefeather.cygnus.setup.dialogs.MapDialogs;
import net.onelitefeather.cygnus.setup.event.dialog.DialogContext;
import net.onelitefeather.cygnus.setup.event.dialog.DialogRequestEvent;
import net.onelitefeather.cygnus.setup.event.dialog.DialogTarget;
import net.onelitefeather.cygnus.setup.map.MapDataCategory;

import java.util.Map;
import java.util.function.Consumer;

public class DialogRequestListener implements Consumer<DialogRequestEvent> {

private static final Map<DialogTarget, MapDataCategory> DELETE_TARGETS = Map.of(
DialogTarget.DELETE_SPAWN, MapDataCategory.SPAWN,
DialogTarget.DELETE_SURVIVOR_SPAWN, MapDataCategory.SURVIVOR,
DialogTarget.DELETE_SLENDER, MapDataCategory.SLENDER,
DialogTarget.DELETE_NAME, MapDataCategory.NAME
);

@Override
public void accept(DialogRequestEvent event) {
DialogTarget target = event.getTarget();
Player player = event.getPlayer();
DialogContext context = event.getContext();

switch (target) {
case CREATE_NAME -> MapDialogs.openNameCreateDialog(player);
case UPDATE_NAME -> {
if (context == null) return;
MapDialogs.openNameUpdateDialog(player, ((DialogContext.NameContext) context).name());
}
case DELETE_PAGE_FACE -> {
if (context == null) return;
MapDialogs.openDeleteDialog(player, MapDataCategory.PAGE, context);
}
default -> {
MapDataCategory category = DELETE_TARGETS.get(target);
if (category == null || context == null) return;
MapDialogs.openDeleteDialog(player, category);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.onelitefeather.cygnus.setup.map;

public enum MapDataCategory {

NAME,
AUTHOR,
SPAWN,
SLENDER,
SURVIVOR,
PAGE
;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.onelitefeather.cygnus.setup.util;

import net.kyori.adventure.key.Key;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.Contract;
Expand All @@ -8,6 +9,15 @@ public abstract class DialogBase {

protected static final Component NO_COMPONENT = Component.text("No");

/**
* Creates a new instance of a {@link Key}.
*
* @param value the value of the key
* @return the created instance
*/
protected static Key create(String value) {
return Key.key("cygnus", value);
}

/**
* Creates a new {@link CompoundBinaryTag} with no data for the dialog
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.onelitefeather.cygnus.setup.util;

import net.minestom.server.tag.Tag;
import net.onelitefeather.cygnus.setup.map.MapDataCategory;

/**
* The {@link SetupTags} class is a utility class that contains all the tags used by the setup system.
Expand All @@ -12,6 +13,8 @@
public final class SetupTags {

public static final Tag<Integer> SETUP_ID_TAG = Tag.Integer("setup_id").defaultValue(-1);
public static final Tag<MapDataCategory> MAP_DATA_CATEGORY_TAG = Tag.String("category_tag")
.map(MapDataCategory::valueOf, MapDataCategory::name);

private SetupTags() {
throw new UnsupportedOperationException("This class cannot be instantiated");
Expand Down
Loading