Skip to content

Commit

Permalink
Add Quick Portal Entry for menus (#141)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Add custom message

---------

Co-authored-by: haykam821 <[email protected]>
  • Loading branch information
Hugman76 and haykam821 authored Sep 4, 2023
1 parent 5eebab8 commit 1810762
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/xyz/nucleoid/extras/NucleoidExtras.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -43,6 +44,7 @@ public void onInitialize() {
ExtrasErrorReporter.register();
ExtraPlaceholders.register();
ExtrasGamePortals.register();
ExtraMenuEntries.register();
ExtraCommands.register();

PlayerDataApi.register(PlayerLobbyState.STORAGE);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Text> 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<GameSpace> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<Text> name,
Optional<List<Text>> description,
Optional<ItemStack> icon
) implements MenuEntryConfig {
public static final Codec<QuickPortalEntryConfig> 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<? extends MenuEntryConfig> codec() {
return CODEC;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/data/nucleoid_extras/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 1810762

Please sign in to comment.