From 18107623d1ffe73d2614edacde778e293f2a420a Mon Sep 17 00:00:00 2001 From: Hugo Landrin Date: Mon, 4 Sep 2023 20:37:24 +0200 Subject: [PATCH] Add Quick Portal Entry for menus (#141) * Add Detailed Game Entry for menus * rename to option portal and make it support a main portal instead of a game id * Update OptionPortalEntryConfig.java * Rename to quick portal * Update QuickPortalEntry.java * Update src/main/resources/data/nucleoid_extras/lang/en_us.json Co-authored-by: haykam821 <24855774+haykam821@users.noreply.github.com> * Add custom message --------- Co-authored-by: haykam821 <24855774+haykam821@users.noreply.github.com> --- .../xyz/nucleoid/extras/NucleoidExtras.java | 2 + .../game_portal/entry/ExtraMenuEntries.java | 10 ++ .../game_portal/entry/QuickPortalEntry.java | 112 ++++++++++++++++++ .../entry/QuickPortalEntryConfig.java | 56 +++++++++ .../data/nucleoid_extras/lang/en_us.json | 2 + 5 files changed, 182 insertions(+) create mode 100644 src/main/java/xyz/nucleoid/extras/game_portal/entry/ExtraMenuEntries.java create mode 100644 src/main/java/xyz/nucleoid/extras/game_portal/entry/QuickPortalEntry.java create mode 100644 src/main/java/xyz/nucleoid/extras/game_portal/entry/QuickPortalEntryConfig.java diff --git a/src/main/java/xyz/nucleoid/extras/NucleoidExtras.java b/src/main/java/xyz/nucleoid/extras/NucleoidExtras.java index 4c8ea76..93c3aad 100644 --- a/src/main/java/xyz/nucleoid/extras/NucleoidExtras.java +++ b/src/main/java/xyz/nucleoid/extras/NucleoidExtras.java @@ -13,6 +13,7 @@ import xyz.nucleoid.extras.error.ExtrasErrorReporter; import xyz.nucleoid.extras.game_portal.ExtrasGamePortals; import xyz.nucleoid.extras.game_portal.ServerChangePortalBackend; +import xyz.nucleoid.extras.game_portal.entry.ExtraMenuEntries; import xyz.nucleoid.extras.integrations.NucleoidIntegrations; import xyz.nucleoid.extras.integrations.http.NucleoidHttpClient; import xyz.nucleoid.extras.lobby.*; @@ -43,6 +44,7 @@ public void onInitialize() { ExtrasErrorReporter.register(); ExtraPlaceholders.register(); ExtrasGamePortals.register(); + ExtraMenuEntries.register(); ExtraCommands.register(); PlayerDataApi.register(PlayerLobbyState.STORAGE); diff --git a/src/main/java/xyz/nucleoid/extras/game_portal/entry/ExtraMenuEntries.java b/src/main/java/xyz/nucleoid/extras/game_portal/entry/ExtraMenuEntries.java new file mode 100644 index 0000000..c23d754 --- /dev/null +++ b/src/main/java/xyz/nucleoid/extras/game_portal/entry/ExtraMenuEntries.java @@ -0,0 +1,10 @@ +package xyz.nucleoid.extras.game_portal.entry; + +import xyz.nucleoid.extras.NucleoidExtras; +import xyz.nucleoid.plasmid.game.portal.menu.MenuEntryConfig; + +public class ExtraMenuEntries { + public static void register() { + MenuEntryConfig.register(NucleoidExtras.identifier("quick_portal"), QuickPortalEntryConfig.CODEC); + } +} diff --git a/src/main/java/xyz/nucleoid/extras/game_portal/entry/QuickPortalEntry.java b/src/main/java/xyz/nucleoid/extras/game_portal/entry/QuickPortalEntry.java new file mode 100644 index 0000000..5ce16b8 --- /dev/null +++ b/src/main/java/xyz/nucleoid/extras/game_portal/entry/QuickPortalEntry.java @@ -0,0 +1,112 @@ +package xyz.nucleoid.extras.game_portal.entry; + +import eu.pb4.sgui.api.elements.GuiElement; +import eu.pb4.sgui.api.elements.GuiElementBuilder; +import net.minecraft.item.ItemStack; +import net.minecraft.screen.ScreenTexts; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Style; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; +import xyz.nucleoid.plasmid.game.GameSpace; +import xyz.nucleoid.plasmid.game.portal.GamePortal; +import xyz.nucleoid.plasmid.game.portal.GamePortalBackend; +import xyz.nucleoid.plasmid.game.portal.menu.MenuEntry; + +import java.util.List; +import java.util.function.Consumer; + +public record QuickPortalEntry( + GamePortal portal, + GamePortal quickPortal, + Text message, + Text name, + List description, + ItemStack icon +) implements MenuEntry { + @Override + public void click(ServerPlayerEntity player) { + this.quickPortal.requestJoin(player); + } + + public void secondaryClick(ServerPlayerEntity player) { + this.portal.requestJoin(player); + } + + @Override + public int getPlayerCount() { + return this.portal.getPlayerCount(); + } + + @Override + public void provideGameSpaces(Consumer consumer) { + portal.provideGameSpaces(consumer); + } + + @Override + public GamePortalBackend.ActionType getActionType() { + return this.quickPortal.getBackend().getActionType(); + } + + public GuiElement createGuiElement() { + var element = GuiElementBuilder.from(this.icon().copy()).hideFlags() + .setName(Text.empty().append(this.name())); + + for (var line : this.description()) { + var text = line.copy(); + + if (line.getStyle().getColor() == null) { + text.setStyle(line.getStyle().withFormatting(Formatting.GRAY)); + } + + element.addLoreLine(text); + } + + var playerCount = this.getPlayerCount(); + var spectatorCount = this.getSpectatorCount(); + boolean allowSpace = true; + + if (playerCount > -1) { + if (allowSpace) { + element.addLoreLine(ScreenTexts.EMPTY); + allowSpace = false; + } + element.addLoreLine(Text.empty() + .append(Text.literal("» ").formatted(Formatting.DARK_GRAY)) + .append(Text.translatable("text.plasmid.ui.game_join.players", + Text.literal(playerCount + "").formatted(Formatting.YELLOW)).formatted(Formatting.GOLD)) + ); + } + + if (spectatorCount > -1) { + if (allowSpace) { + element.addLoreLine(ScreenTexts.EMPTY); + allowSpace = false; + } + + element.addLoreLine(Text.empty() + .append(Text.literal("» ").formatted(Formatting.DARK_GRAY)) + .append(Text.translatable("text.plasmid.ui.game_join.spectators", + Text.literal(playerCount + "").formatted(Formatting.YELLOW)).formatted(Formatting.GOLD)) + ); + } + + var actionType = this.getActionType(); + + if (actionType != GamePortalBackend.ActionType.NONE) { + element.addLoreLine(Text.empty().append(Text.literal(" [ ").formatted(Formatting.GRAY)) + .append(actionType.text()) + .append(Text.literal(" ]").formatted(Formatting.GRAY)).setStyle(Style.EMPTY.withColor(0x76ed6f))); + } + element.addLoreLine(Text.empty().append(Text.literal(" [ ").formatted(Formatting.GRAY)) + .append(this.message().copy()) + .append(Text.literal(" ]").formatted(Formatting.GRAY)).setStyle(Style.EMPTY.withColor(0x5e8ad6))); + + element.setCallback((index, clickType, slotActionType, gui) -> { + if (clickType.isRight) this.secondaryClick(gui.getPlayer()); + else this.click(gui.getPlayer()); + }); + + return element.build(); + } +} diff --git a/src/main/java/xyz/nucleoid/extras/game_portal/entry/QuickPortalEntryConfig.java b/src/main/java/xyz/nucleoid/extras/game_portal/entry/QuickPortalEntryConfig.java new file mode 100644 index 0000000..db0d9cb --- /dev/null +++ b/src/main/java/xyz/nucleoid/extras/game_portal/entry/QuickPortalEntryConfig.java @@ -0,0 +1,56 @@ +package xyz.nucleoid.extras.game_portal.entry; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.item.ItemStack; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; +import xyz.nucleoid.codecs.MoreCodecs; +import xyz.nucleoid.plasmid.game.portal.GamePortalManager; +import xyz.nucleoid.plasmid.game.portal.menu.*; +import xyz.nucleoid.plasmid.util.PlasmidCodecs; + +import java.util.List; +import java.util.Optional; + +public record QuickPortalEntryConfig( + Identifier portal, + Identifier quickPortal, + Text message, + Optional name, + Optional> description, + Optional icon +) implements MenuEntryConfig { + public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( + Identifier.CODEC.fieldOf("portal").forGetter(QuickPortalEntryConfig::portal), + Identifier.CODEC.fieldOf("quick_portal").forGetter(QuickPortalEntryConfig::quickPortal), + PlasmidCodecs.TEXT.fieldOf("message").orElse(Text.translatable("text.nucleoid_extras.ui.action.more")).forGetter(QuickPortalEntryConfig::message), + PlasmidCodecs.TEXT.optionalFieldOf("name").forGetter(QuickPortalEntryConfig::name), + MoreCodecs.listOrUnit(PlasmidCodecs.TEXT).optionalFieldOf("description").forGetter(QuickPortalEntryConfig::description), + MoreCodecs.ITEM_STACK.optionalFieldOf("icon").forGetter(QuickPortalEntryConfig::icon) + ).apply(instance, QuickPortalEntryConfig::new)); + + @Override + public MenuEntry createEntry() { + var portal = GamePortalManager.INSTANCE.byId(this.portal); + var quickPortal = GamePortalManager.INSTANCE.byId(this.quickPortal); + + if (portal != null && quickPortal != null) { + return new QuickPortalEntry( + portal, + quickPortal, + this.message, + this.name.orElse(portal.getName()), + this.description.orElse(portal.getDescription()), + this.icon.orElse(portal.getIcon()) + ); + } + + return new InvalidMenuEntry(this.name); + } + + @Override + public Codec codec() { + return CODEC; + } +} diff --git a/src/main/resources/data/nucleoid_extras/lang/en_us.json b/src/main/resources/data/nucleoid_extras/lang/en_us.json index ebacd48..c8dda33 100644 --- a/src/main/resources/data/nucleoid_extras/lang/en_us.json +++ b/src/main/resources/data/nucleoid_extras/lang/en_us.json @@ -370,6 +370,8 @@ "text.nucleoid_extras.statistics.waiting": "Waiting for statistics data", "text.nucleoid_extras.seconds": "%s second(s)", + "text.nucleoid_extras.ui.action.more": "Right-click for more...", + "advancements.nucleoid_extras.root.title": "Nucleoid", "advancements.nucleoid_extras.root.description": "Advancements for Nucleoid", "advancements.nucleoid_extras.first_tater.title": "My First Tater",