Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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 @@ -100,6 +100,16 @@ public void addPage(Vec pos, Direction face) {
this.pageFaces.add(new PageResource(pos, face));
}

/**
* Removes a page from the map.
*
* @param pageResource the page resource to remove
* @return true if the page was removed
*/
public boolean removePage(PageResource pageResource) {
return this.pageFaces.remove(pageResource);
}

/**
* Returns the {@link Set} of {@link PageResource} which are used for the game.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,43 @@
import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.Player;
import net.minestom.server.instance.anvil.AnvilLoader;
import net.minestom.server.utils.Direction;
import net.minestom.server.world.DimensionType;
import net.minestom.server.inventory.InventoryType;
import net.onelitefeather.cygnus.common.map.GameMap;
import net.onelitefeather.cygnus.common.map.GameMapBuilder;
import net.onelitefeather.cygnus.common.util.GsonHelper;
import net.onelitefeather.cygnus.setup.inventory.page.PageHeaderFormatter;
import net.onelitefeather.cygnus.setup.inventory.slot.PageSlot;
import net.onelitefeather.cygnus.setup.inventory.view.InventoryMode;
import net.onelitefeather.cygnus.setup.inventory.view.MapDataOverviewInventory;
import net.onelitefeather.cygnus.setup.inventory.view.SurvivorViewInventory;
import net.onelitefeather.cygnus.setup.item.SetupItemId;
import net.onelitefeather.cygnus.setup.item.SetupItems;
import net.onelitefeather.cygnus.setup.map.MapDataCategory;
import net.onelitefeather.cygnus.common.page.PageResource;
import net.onelitefeather.cygnus.setup.player.SetupPlayer;
import net.onelitefeather.cygnus.setup.util.SetupMessages;
import net.theevilreaper.aves.inventory.layout.InventoryLayout;
import net.theevilreaper.aves.inventory.pageable.PageableInventory;
import net.theevilreaper.aves.inventory.pageable.TitleData;
import net.theevilreaper.aves.inventory.slot.ISlot;
import net.theevilreaper.aves.inventory.util.LayoutCalculator;
import net.theevilreaper.aves.map.BaseMapBuilder;
import net.theevilreaper.aves.map.MapEntry;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class GameData extends InstanceSetupData {

private final MapDataOverviewInventory inventory;
private final SurvivorViewInventory survivorInventory;
private final PageableInventory pageInventory;
private GameMapBuilder gameMapBuilder;
private boolean pageMode;
private boolean survivorMode;
Expand All @@ -43,6 +58,59 @@ public GameData(Player player, MapEntry mapEntry) {

this.inventory = new MapDataOverviewInventory(player, this.gameMapBuilder, InventoryMode.GAME);
this.survivorInventory = new SurvivorViewInventory(player, this.gameMapBuilder);
this.pageInventory = createPageInventory(player);
}

/**
* Creates the [{@link PageableInventory} to display the given {@link PageResource}s.
*
* @param player who owns the inventory
* @return the created inventory
*/
private PageableInventory createPageInventory(Player player) {
InventoryLayout layout = InventoryLayout.fromType(InventoryType.CHEST_6_ROW);
layout.setItems(LayoutCalculator.fillRow(InventoryType.CHEST_1_ROW), SetupItems.DECORATION_PANE);
layout.setItems(LayoutCalculator.fillRow(InventoryType.CHEST_6_ROW), SetupItems.DECORATION_PANE);

return PageableInventory
.builder()
.titleData(
TitleData
.builder()
.title(Component.text("Page positions - "))
.pageMapper(PageHeaderFormatter::format)
.showPageNumbers(true)
.build()
)
.player(player)
.slotRange(LayoutCalculator.quad(InventoryType.CHEST_1_ROW.getSize(), InventoryType.CHEST_5_ROW.getSize() - 1))
.layout(layout)
.values(getPageSlots())
.build();
}

/**
* Adds a new {@link PageResource} to the builder and inventory
*
* @param pos of the resource
* @param face of the resource
*/
public void addPage(Vec pos, Direction face) {
PageResource pageResource = new PageResource(pos, face);
this.gameMapBuilder.addPage(pos, face);
this.pageInventory.add(new PageSlot(pageResource));
}

/**
* Returns the list of slots for the given {@link PageResource}s
*
* @return the created list
*/
private List<ISlot> getPageSlots() {
if (this.gameMapBuilder.getPageFaces().isEmpty()) return new ArrayList<>();
List<ISlot> pageSlots = new ArrayList<>(this.gameMapBuilder.getPageFaces().size());
this.gameMapBuilder.getPageFaces().forEach(pageFace -> pageSlots.add(new PageSlot(pageFace)));
return pageSlots;
}

/**
Expand All @@ -52,6 +120,9 @@ public void swapPageMode() {
this.pageMode = !this.pageMode;
}

/**
* Swaps to the survivor mode.
*/
public void swapSurvivorMode() {
this.survivorMode = !this.survivorMode;
}
Expand All @@ -64,9 +135,7 @@ public void openInventory(InventoryTarget target) {
switch (target) {
case GENERAL -> this.inventory.open();
case SURVIVOR -> this.survivorInventory.open();
case PAGE -> {

}
case PAGE -> this.pageInventory.open();
}
}

Expand All @@ -79,7 +148,6 @@ public void triggerUpdate(InventoryTarget target) {
case GENERAL -> this.inventory.invalidateDataLayout();
case SURVIVOR -> this.survivorInventory.invalidateDataLayout();
case PAGE -> {
// Nothing to do here
}
}
}
Expand All @@ -89,7 +157,6 @@ public void triggerUpdate(InventoryTarget target) {
*/
@Override
public void updateTitle() {
System.out.println("TItle is " + (title == null));
if (getMapBuilder().getName().equalsIgnoreCase("Map")) {
this.title = null;
super.updateTitle();
Expand Down Expand Up @@ -168,6 +235,11 @@ public void handleItemInteraction(Player player, byte tagValue) {
return;
}

if (SetupItemId.PAGES == tagValue) {
this.openInventory(InventoryTarget.PAGE);
return;
}

super.handleItemInteraction(player, tagValue);
}

Expand All @@ -194,9 +266,19 @@ public void handleDataDelete(MapDataCategory category) {
*/
@Override
public void handleDataContextDelete(MapDataCategory category, Point point) {
if (point instanceof Pos pos) {
if (category == MapDataCategory.SURVIVOR && point instanceof Pos pos) {
this.gameMapBuilder.removeSurvivorSpawn(pos);
this.triggerUpdate(InventoryTarget.SURVIVOR);
} else if (category == MapDataCategory.PAGE) {
Player player = MinecraftServer.getConnectionManager().getOnlinePlayerByUuid(this.uuid);
PageResource pageResource = null;
if (player instanceof SetupPlayer setupPlayer) {
pageResource = setupPlayer.getPageResource();
}
if (pageResource != null) {
this.gameMapBuilder.removePage(pageResource);
this.pageInventory.remove(new PageSlot(pageResource));
}
}
}

Expand Down Expand Up @@ -228,6 +310,7 @@ public void reset() {
super.reset();
this.survivorInventory.unregister();
this.inventory.unregister();
this.pageInventory.unregister();
}

/**
Expand Down Expand Up @@ -262,6 +345,11 @@ public boolean hasPageMode() {
return pageMode;
}

/**
* Returns an indication if the survivor mode is active or not.
*
* @return true for yes otherwise false
*/
public boolean hasSurvivorMode() {
return this.survivorMode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import net.minestom.server.dialog.DialogAction;
import net.minestom.server.dialog.DialogAfterAction;
import net.minestom.server.entity.Player;
import net.minestom.server.coordinate.Point;
import net.onelitefeather.cygnus.common.page.PageResource;
import net.onelitefeather.cygnus.setup.event.dialog.DialogContext;
import net.onelitefeather.cygnus.setup.map.MapDataCategory;
import net.onelitefeather.cygnus.setup.util.DialogBase;
Expand All @@ -27,7 +29,6 @@ public final class MapDialogs extends DialogBase {
public static final Key NON_DYNAMIC_DELETE_KEY = create("non_dynamic_delete_dialog");
public static final Key DYNAMIC_DELETE_KEY = create("dynamic_delete_dialog");


/**
* Opens the dialog to allow the input of a name.
*
Expand Down Expand Up @@ -97,7 +98,11 @@ public static void openDeleteDialog(Player player, MapDataCategory mapDataCatego
}

public static void openDeleteDialog(Player player, MapDataCategory mapDataCategory, @Nullable DialogContext context) {
DialogContext.PositionContent positionContent = (DialogContext.PositionContent) context;
Point point = switch (context) {
case DialogContext.PositionContent(Point p) -> p;
case DialogContext.PageContent(PageResource resource) -> resource.position();
default -> null;
};
DialogTemplate dialogTemplate = DialogType.confirm(DYNAMIC_DELETE_KEY)
.meta(dialogMeta -> {
dialogMeta.closeWithEscape(false);
Expand All @@ -109,16 +114,18 @@ public static void openDeleteDialog(Player player, MapDataCategory mapDataCatego
template.contents(Component.text("Do you want to delete the following data?")));
dialogMeta.emptyMessage();
dialogMeta.messageBody(template -> template.contents(Component.text("Category: ").append(Component.text(mapDataCategory.getName()))));
dialogMeta.emptyMessage();
dialogMeta.messageBody(componentTemplate -> {
componentTemplate.contents(Component.text("x: ", NamedTextColor.WHITE).append(Component.text(positionContent.point().x())));
});
dialogMeta.messageBody(componentTemplate -> {
componentTemplate.contents(Component.text("y: ", NamedTextColor.WHITE).append(Component.text(positionContent.point().y())));
});
dialogMeta.messageBody(componentTemplate -> {
componentTemplate.contents(Component.text("z: ", NamedTextColor.WHITE).append(Component.text(positionContent.point().z())));
});
if (point != null) {
dialogMeta.emptyMessage();
dialogMeta.messageBody(componentTemplate -> {
componentTemplate.contents(Component.text("x: ", NamedTextColor.WHITE).append(Component.text(point.x())));
});
dialogMeta.messageBody(componentTemplate -> {
componentTemplate.contents(Component.text("y: ", NamedTextColor.WHITE).append(Component.text(point.y())));
});
dialogMeta.messageBody(componentTemplate -> {
componentTemplate.contents(Component.text("z: ", NamedTextColor.WHITE).append(Component.text(point.z())));
});
}
})
.yesButton(button -> button.width(101).label(Component.text("Save"))
.action(new DialogAction.DynamicCustom(DYNAMIC_DELETE_KEY, getCategoryPayload(mapDataCategory.ordinal())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ public void handle(PlayerCustomClickEvent event, CompoundBinaryTag payload) {
point = player.getSurvivorToDelete();
}
if (category == MapDataCategory.PAGE) {
point = player.getPageToDelete();
point = player.getPageToDelete() != null ? player.getPageToDelete() : (player.getPageResource() != null ? player.getPageResource().position() : null);
}
((InstanceSetupData)data).handleDataContextDelete(category, point);
if (category == MapDataCategory.SURVIVOR) {
player.setSurvivorToDelete(null);
}
if (category == MapDataCategory.PAGE) {
player.setPageToDelete(null);
player.setPageResource(null);
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package net.onelitefeather.cygnus.setup.event.dialog;

import net.minestom.server.coordinate.Point;
import net.onelitefeather.cygnus.common.page.PageResource;

/**
* Represents the context for a dialog event which contains all necessary data
* to open the correct dialog for a specific {@link DialogTarget}.
*
* @author Joltra
* @version 1.0.0
* @version 1.5.0
* @see DialogTarget
* @since 0.1.0
*/
public sealed interface DialogContext permits
DialogContext.NameContext,
DialogContext.PositionContent,
DialogContext.AuthorAmount {
DialogContext.AuthorAmount,
DialogContext.PageContent {

/**
* Specific context for the update or deletion of a name.
Expand All @@ -34,5 +36,21 @@ record PositionContent(Point point) implements DialogContext {

}

record AuthorAmount(float amount) implements DialogContext {}
/**
* Specific context to indicate how many authors a map should have.
*
* @param amount of author
*/
record AuthorAmount(float amount) implements DialogContext {

}

/**
* Context implementation to bind a {@link PageResource} to a dialog.
*
* @param resource to bind
*/
record PageContent(PageResource resource) implements DialogContext {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.onelitefeather.cygnus.setup.inventory.page;

import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.Contract;

/**
* Small helper class to format the current number of pages and the total ones to a {@link Component}.
*
* @author theEvilReaper
* @since 2.6.0
* @version 1.0.0
*/
public final class PageHeaderFormatter {

/**
* Returns a {@link Component} that contains the current page and the total numbers of pages.
* @param current page count
* @param max page count
* @return the created component
*/
@Contract(value = "_, _ -> new", pure = true)
public static Component format(int current, int max) {
return Component.text("Page " + current + " of " + max);
}

private PageHeaderFormatter() {
// Nothing to do here
}
}
Loading
Loading