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 @@ -16,6 +16,7 @@
import net.minestom.server.instance.Instance;
import net.onelitefeather.cygnus.setup.event.MapSetupSaveEvent;
import net.onelitefeather.cygnus.setup.event.MapSetupSelectEvent;
import net.onelitefeather.cygnus.setup.event.PlayerRemoveDataEvent;
import net.onelitefeather.cygnus.setup.event.PositionSetEvent;
import net.onelitefeather.cygnus.setup.event.dialog.DialogRequestEvent;
import net.onelitefeather.cygnus.setup.inventory.MapSetupInventory;
Expand All @@ -26,6 +27,7 @@
import net.onelitefeather.cygnus.setup.listener.PlayerSpawnListener;
import net.onelitefeather.cygnus.setup.listener.SetupItemListener;
import net.onelitefeather.cygnus.setup.listener.SpawnCreationListener;
import net.onelitefeather.cygnus.setup.listener.data.PlayerRemoveDataListener;
import net.onelitefeather.cygnus.setup.listener.dialog.DialogPayloadListener;
import net.onelitefeather.cygnus.setup.listener.dialog.DialogRequestListener;
import net.onelitefeather.cygnus.setup.listener.map.MapSetupSaveListener;
Expand Down Expand Up @@ -55,7 +57,6 @@ public SetupExtension() {

private void registerSetupComponents() {
var manager = MinecraftServer.getGlobalEventHandler();
var commandManager = MinecraftServer.getCommandManager();
var spawnPos = new Pos(0, 150, 0);

Supplier<Instance> instanceSupplier = this.mapProvider.getActiveInstance();
Expand Down Expand Up @@ -84,6 +85,7 @@ private void registerSetupComponents() {
manager.addListener(PlayerCustomClickEvent.class, new DialogPayloadListener(this.dataService));

manager.addListener(PositionSetEvent.class, new PositionSetListener(this.dataService));
manager.addListener(PlayerRemoveDataEvent.class, new PlayerRemoveDataListener());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,21 @@
super.handleItemInteraction(player, tagValue);
}

/**
* {@inheritDoc}
*/
@Override
public void handleDataDelete(MapDataCategory category) {
switch (category) {
case SPAWN -> gameMapBuilder.spawn(null);
case NAME -> gameMapBuilder.name(null);
case AUTHOR -> gameMapBuilder.builders("");
case SLENDER -> gameMapBuilder.setSlenderSpawn(null);
default -> throw new IllegalArgumentException("Unknown inventory category: " + category);
}
this.triggerUpdate(InventoryTarget.GENERAL);
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -201,7 +216,7 @@
}
// this.inventory = new LobbyViewInventory(this.gameMapBuilder);
this.instance = MinecraftServer.getInstanceManager().createInstanceContainer();
AnvilLoader anvilLoader = new AnvilLoader(this.mapEntry.getDirectoryRoot());

Check warning on line 219 in setup/src/main/java/net/onelitefeather/cygnus/setup/data/GameData.java

View workflow job for this annotation

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

[removal] AnvilLoader(Path) in AnvilLoader has been deprecated and marked for removal

Check warning on line 219 in setup/src/main/java/net/onelitefeather/cygnus/setup/data/GameData.java

View workflow job for this annotation

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

[removal] AnvilLoader(Path) in AnvilLoader has been deprecated and marked for removal
this.instance.setChunkLoader(anvilLoader);
this.updateTitle();
MinecraftServer.getInstanceManager().registerInstance(this.instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public void handleItemInteraction(Player player, byte tagValue) {
openInventory(InventoryTarget.GENERAL);
}

public abstract void handleDataDelete(MapDataCategory category);

/**
* Checks whether a map file is available for this setup.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.theevilreaper.aves.map.MapEntry;

import java.nio.file.Files;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

Expand Down Expand Up @@ -62,6 +63,21 @@
this.viewInventory.invalidateDataLayout();
}


/**
* {@inheritDoc}
*/
@Override
public void handleDataDelete(MapDataCategory category) {
switch (category) {
case SPAWN -> mapBuilder.spawn(null);
case NAME -> mapBuilder.name(null);
case AUTHOR -> mapBuilder.builders("");
default -> throw new IllegalArgumentException("Unknown inventory category: " + category);
}
this.triggerUpdate(InventoryTarget.GENERAL);
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -110,7 +126,7 @@
}

this.instance = MinecraftServer.getInstanceManager().createInstanceContainer();
AnvilLoader anvilLoader = new AnvilLoader(this.mapEntry.getDirectoryRoot());

Check warning on line 129 in setup/src/main/java/net/onelitefeather/cygnus/setup/data/LobbyData.java

View workflow job for this annotation

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

[removal] AnvilLoader(Path) in AnvilLoader has been deprecated and marked for removal

Check warning on line 129 in setup/src/main/java/net/onelitefeather/cygnus/setup/data/LobbyData.java

View workflow job for this annotation

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

[removal] AnvilLoader(Path) in AnvilLoader has been deprecated and marked for removal
this.instance.setChunkLoader(anvilLoader);
this.updateTitle();
MinecraftServer.getInstanceManager().registerInstance(this.instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public final class MapDialogs extends DialogBase {
public static final Key MAP_KEY = create("map_name");
public static final Key AUTHOR_AMOUNT_KEY = create("author_amount_dialog");
public static final Key AUTHOR_INPUT_ENTRY_KEY = create("author_input_dialog");
public static final Key NON_DYNAMIC_DELETE_KEY = create("non_dynamic_delete_dialog");


/**
Expand Down Expand Up @@ -73,7 +74,7 @@ public static void openNameUpdateDialog(Player player, String name) {
}

public static void openDeleteDialog(Player player, MapDataCategory mapDataCategory) {
DialogTemplate dialogTemplate = DialogType.confirm(MAP_KEY)
DialogTemplate dialogTemplate = DialogType.confirm(NON_DYNAMIC_DELETE_KEY)
.meta(dialogMeta -> {
dialogMeta.closeWithEscape(false);
dialogMeta.pause(false);
Expand All @@ -86,7 +87,7 @@ public static void openDeleteDialog(Player player, MapDataCategory mapDataCatego
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())))
.action(new DialogAction.DynamicCustom(NON_DYNAMIC_DELETE_KEY, getCategoryPayload(mapDataCategory.ordinal())))
)
.noButton(button -> button.width(101).label(NO_COMPONENT))
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.onelitefeather.cygnus.setup.event;

import net.minestom.server.entity.Player;
import net.minestom.server.event.trait.PlayerEvent;
import net.onelitefeather.cygnus.setup.map.MapDataCategory;

/**
* Event will be fired during the setup when a player wants to delete data from a map.
*
* @author Joltra
* @version 1.0.0
* @since 2.5.1
*/
public final class PlayerRemoveDataEvent implements PlayerEvent {

private final Player player;
private final MapDataCategory mapDataCategory;

/**
* Creates a new instance of the event with the given values from the constructor
*
* @param player which is involved
* @param mapDataCategory which data should be deleted
*/
public PlayerRemoveDataEvent(Player player, MapDataCategory mapDataCategory) {
this.player = player;
this.mapDataCategory = mapDataCategory;
}

/**
* {@inheritDoc}
*/
@Override
public Player getPlayer() {
return this.player;
}

/**
* Returns the involved category.
*
* @return the given category
*/
public MapDataCategory getMapDataCategory() {
return mapDataCategory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,4 @@ public enum DialogTarget {
CREATE_AUTHORS,
AUTHOR_INPUT,
UPDATE_NAME,
DELETE_NAME,
DELETE_SPAWN,
DELETE_SLENDER,
DELETE_PAGE_FACE,
DELETE_SURVIVOR_SPAWN
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import net.minestom.server.event.EventDispatcher;
import net.minestom.server.inventory.click.Click;
import net.minestom.server.item.ItemStack;
import net.onelitefeather.cygnus.setup.event.PlayerRemoveDataEvent;
import net.onelitefeather.cygnus.setup.event.dialog.DialogRequestEvent;
import net.onelitefeather.cygnus.setup.event.dialog.DialogTarget;
import net.onelitefeather.cygnus.setup.map.MapDataCategory;
import net.theevilreaper.aves.inventory.click.ClickHolder;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.function.Consumer;
Expand Down Expand Up @@ -58,7 +58,7 @@ protected void click(Player player, int slot, Click click, ItemStack stack, Cons
}

if (click instanceof Click.Right) {
// EventDispatcher.call(new PlayerDeletePromptEvent(player, type));
EventDispatcher.call(new PlayerRemoveDataEvent(player, type));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import net.minestom.server.event.EventDispatcher;
import net.minestom.server.inventory.click.Click;
import net.minestom.server.item.ItemStack;
import net.onelitefeather.cygnus.setup.event.PlayerRemoveDataEvent;
import net.onelitefeather.cygnus.setup.event.PositionSetEvent;
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 net.theevilreaper.aves.inventory.click.ClickHolder;
import net.theevilreaper.aves.util.Components;
Expand Down Expand Up @@ -78,8 +76,7 @@ protected void click(Player player, int slot, Click click, ItemStack stack, Cons
}

if (click instanceof Click.Right) {
EventDispatcher.call(new DialogRequestEvent(player, DialogTarget.DELETE_SPAWN, new DialogContext.PositionContent(position)));

EventDispatcher.call(new PlayerRemoveDataEvent(player, type));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minestom.server.event.EventDispatcher;
import net.minestom.server.inventory.click.Click;
import net.minestom.server.item.ItemStack;
import net.onelitefeather.cygnus.setup.event.PlayerRemoveDataEvent;
import net.onelitefeather.cygnus.setup.event.dialog.DialogRequestEvent;
import net.onelitefeather.cygnus.setup.event.dialog.DialogTarget;
import net.onelitefeather.cygnus.setup.map.MapDataCategory;
Expand Down Expand Up @@ -49,7 +50,7 @@ protected void click(Player player, int slot, Click click, ItemStack stack, Cons
}

if (click instanceof Click.Right) {
EventDispatcher.call(new DialogRequestEvent(player, DialogTarget.DELETE_NAME));
EventDispatcher.call(new PlayerRemoveDataEvent(player, type));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.onelitefeather.cygnus.setup.listener.data;

import net.minestom.server.entity.Player;
import net.onelitefeather.cygnus.setup.dialogs.MapDialogs;
import net.onelitefeather.cygnus.setup.event.PlayerRemoveDataEvent;
import net.onelitefeather.cygnus.setup.map.MapDataCategory;

import java.util.function.Consumer;

public final class PlayerRemoveDataListener implements Consumer<PlayerRemoveDataEvent> {
@Override
public void accept(PlayerRemoveDataEvent event) {
final Player player = event.getPlayer();
final MapDataCategory category = event.getMapDataCategory();

MapDialogs.openDeleteDialog(player, category);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@NotNullByDefault
package net.onelitefeather.cygnus.setup.listener.data;

import org.jetbrains.annotations.NotNullByDefault;
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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 net.onelitefeather.guira.SetupDataService;

import java.util.ArrayList;
Expand Down Expand Up @@ -76,5 +77,16 @@ public void accept(PlayerCustomClickEvent event) {
instanceSetupData.triggerUpdate(InstanceSetupData.InventoryTarget.GENERAL);
});
}

if (key.equals(MapDialogs.NON_DYNAMIC_DELETE_KEY)) {
IntBinaryTag idTag = (IntBinaryTag) castedPayload.get("category_id");
if (idTag == null) return;
int categoryId = idTag.value();
MapDataCategory category = MapDataCategory.byId(categoryId);

dataService.get(event.getPlayer().getUuid()).ifPresent(data -> {
((InstanceSetupData)data).handleDataDelete(category);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,11 @@
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();
Expand All @@ -32,19 +23,13 @@ public void accept(DialogRequestEvent event) {
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);
}
case CREATE_AUTHORS -> AuthorDialogs.openAuthorRequestDialog(player);
case AUTHOR_INPUT -> {
if (context == null) return;
AuthorDialogs.openAuthorInput(player, ((DialogContext.AuthorAmount)context).amount());
}
default -> {
MapDataCategory category = DELETE_TARGETS.get(target);
if (category == null || context == null) return;
MapDialogs.openDeleteDialog(player, category);
// Nothing to do here
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,11 @@ public static ItemStack getDefaultItem(MapDataCategory category) {
.set(SetupTags.MAP_DATA_CATEGORY_TAG, category)
.build());
}

public static MapDataCategory byId(int id) {
if (id < 0 || id >= VALUES.length) {
throw new IllegalArgumentException("Invalid map data category ID: " + id);
}
return VALUES[id];
}
}
Loading