-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> * Add custom message --------- Co-authored-by: haykam821 <[email protected]>
- Loading branch information
Showing
5 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/xyz/nucleoid/extras/game_portal/entry/ExtraMenuEntries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/main/java/xyz/nucleoid/extras/game_portal/entry/QuickPortalEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/xyz/nucleoid/extras/game_portal/entry/QuickPortalEntryConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters