Skip to content

Commit

Permalink
Fix direct calls to GameConfig.(short)name(), causing NPE
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Nov 10, 2024
1 parent 6b68456 commit c48f23f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import xyz.nucleoid.plasmid.api.game.GameLifecycle;
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.game.config.GameConfig;
import xyz.nucleoid.plasmid.api.game.config.GameConfigs;

import java.io.IOException;
import java.io.PrintWriter;
Expand Down Expand Up @@ -52,7 +53,7 @@ public void onClosed(GameSpace gameSpace, List<ServerPlayerEntity> players, Game
}

private static String sourceName(RegistryEntry<GameConfig<?>> game) {
var name = game.value().name().getString();
var name = GameConfig.name(game).getString();
return name + " (" + GameConfig.sourceName(game) + ")";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import xyz.nucleoid.plasmid.api.game.config.GameConfig;
import xyz.nucleoid.plasmid.impl.portal.game.ConcurrentGamePortalBackend;
import xyz.nucleoid.plasmid.impl.portal.menu.*;

Expand All @@ -23,14 +24,14 @@ protected List<MenuEntry> getEntries() {
this.entries = new ArrayList<>(this.configEntries.size());
for (var configEntry : configEntries) {
var game = new ConcurrentGamePortalBackend(configEntry.game());
var gameConfig = configEntry.game().value();
var gameConfig = configEntry.game();

if (gameConfig != null) {
this.entries.add(new GameMenuEntry(
game,
configEntry.name().orElse(gameConfig.name()),
configEntry.description().orElse(gameConfig.description()),
configEntry.icon().orElse(gameConfig.icon())
configEntry.name().orElse(GameConfig.name(gameConfig)),
configEntry.description().orElse(gameConfig.value().description()),
configEntry.icon().orElse(gameConfig.value().icon())
));
} else if (ExtrasGamePortals.SHOW_INVALID) {
this.entries.add(new InvalidMenuEntry(game.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import xyz.nucleoid.extras.util.CommonGuiElements;
import xyz.nucleoid.extras.util.PagedGui;
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.game.GameSpaceState;
import xyz.nucleoid.plasmid.api.game.config.GameConfig;
import xyz.nucleoid.plasmid.api.game.player.GamePlayerJoiner;
import xyz.nucleoid.plasmid.api.game.player.JoinIntent;
import xyz.nucleoid.plasmid.impl.portal.GamePortalBackend;
Expand Down Expand Up @@ -180,7 +182,7 @@ private void fillOpen(ServerPlayerEntity player, SimpleGui gui, MutableInt page)
if (games.size() > pIndex) {
var portal = games.get(pIndex);
var m = portal.getMetadata().sourceConfig().value();
gui.setSlot(index, createIconFor(m.icon(), m.name(), m.description(), portal.getPlayers().size(), (p) -> {
gui.setSlot(index, createIconFor(m.icon(), GameConfig.name(portal.getMetadata().sourceConfig()), m.description(), portal.getState(), (p) -> {
PagedGui.playClickSound(player);
tryJoinGame(p, portal, JoinIntent.PLAY);
}));
Expand Down Expand Up @@ -259,7 +261,7 @@ protected void fillInterface(ServerPlayerEntity player, SimpleGui gui, MutableIn
}
}

protected GuiElementBuilder createIconFor(ItemStack icon, Text name, List<Text> description, int playerCount, Consumer<ServerPlayerEntity> click) {
protected GuiElementBuilder createIconFor(ItemStack icon, Text name, List<Text> description, GameSpaceState state, Consumer<ServerPlayerEntity> click) {
var element = GuiElementBuilder.from(icon)
.setItemName(name)
.hideDefaultTooltip();
Expand All @@ -274,15 +276,36 @@ protected GuiElementBuilder createIconFor(ItemStack icon, Text name, List<Text>
element.addLoreLine(text);
}

if (playerCount > -1) {
int playerCount = state.players();
int maxPlayerCount = state.maxPlayers();
int spectatorCount = state.spectators();
boolean allowSpace = true;
var stateText = state.state().display();
if (stateText != null) {
element.addLoreLine(ScreenTexts.EMPTY);
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))
);
element.addLoreLine(Text.literal(" ").append(stateText).formatted(Formatting.WHITE));
allowSpace = false;
}

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", new Object[]{Text.literal("" + playerCount + (maxPlayerCount > 0 ? " / " + maxPlayerCount : "")).formatted(Formatting.YELLOW)}).formatted(Formatting.GOLD)));
}

if (spectatorCount > 0) {
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", new Object[]{Text.literal("" + spectatorCount).formatted(Formatting.YELLOW)}).formatted(Formatting.GOLD)));
}


element.setCallback((a, b, c, gui) -> {
click.accept(gui.getPlayer());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void clear() {
}

void addGame(RegistryEntry<GameConfig<?>> game, int playerCount) {
this.games.add(new GameEntry(game.value().name().getString(), game.value().type().id(), playerCount));
this.games.add(new GameEntry(GameConfig.name(game).getString(), game.value().type().id(), playerCount));
}

JsonObject serialize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.game.GameSpaceManager;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.plasmid.api.game.config.GameConfig;

import java.util.stream.Stream;

Expand All @@ -32,7 +33,7 @@ public Text toVanilla(@Nullable ServerPlayerEntity player, Text text) {
.withColor(
TextColor.fromRgb(gameSpace == null ? 0x800080 : (int) (gameSpace.getMetadata().id().getLeastSignificantBits() & 0xFFFFFF)))
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
gameSpace == null ? Text.literal("Lobby") : gameSpace.getMetadata().sourceConfig().value().name())))
gameSpace == null ? Text.literal("Lobby") : GameConfig.name(gameSpace.getMetadata().sourceConfig()))))
).append(ScreenTexts.SPACE);

out.getSiblings().addAll(text.getSiblings());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.world.World;
import xyz.nucleoid.extras.NucleoidExtrasConfig;
import xyz.nucleoid.plasmid.api.game.GameSpaceManager;
import xyz.nucleoid.plasmid.api.game.config.GameConfig;
import xyz.nucleoid.plasmid.impl.game.manager.ManagedGameSpace;

import java.util.ArrayList;
Expand Down Expand Up @@ -150,7 +151,7 @@ private void writeGamesToSidebar(LineBuilder builder, Collection<ManagedGameSpac
.limit(4);

games.forEach(game -> {
var name = game.getMetadata().sourceConfig().value().shortName();
var name = GameConfig.shortName(game.getMetadata().sourceConfig());

int players = game.getPlayers().size();
var playersText = Text.translatable("nucleoid.sidebar.game.player." + (players < 2 ? "1" : "more"), players).setStyle(GAME_COUNT_STYLE);
Expand Down

0 comments on commit c48f23f

Please sign in to comment.