Skip to content

Commit

Permalink
Give portals more context about click, user PortalUserContext instead…
Browse files Browse the repository at this point in the history
… of player entity
  • Loading branch information
Patbox committed Dec 31, 2024
1 parent 274e227 commit cfbd454
Show file tree
Hide file tree
Showing 19 changed files with 172 additions and 291 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.logging.LogUtils;
import eu.pb4.sgui.api.ClickType;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.NbtCompoundArgumentType;
import net.minecraft.nbt.NbtOps;
Expand All @@ -31,6 +32,7 @@
import xyz.nucleoid.plasmid.api.game.player.GamePlayerJoiner;
import xyz.nucleoid.plasmid.api.game.player.JoinIntent;
import xyz.nucleoid.plasmid.api.util.Scheduler;
import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext;
import xyz.nucleoid.plasmid.impl.portal.config.ActiveGamePortalConfig;
import xyz.nucleoid.plasmid.impl.portal.config.GamePortalConfig;

Expand Down Expand Up @@ -253,7 +255,7 @@ private static int proposeGame(ServerCommandSource source, GameSpace gameSpace)

private static int joinGame(CommandContext<ServerCommandSource> context, JoinIntent intent) throws CommandSyntaxException {
GamePortalConfig.create(context.getSource().getServer(), Identifier.of("plasmid", "join_command"),
ActiveGamePortalConfig.of(Text.translatable("text.plasmid.ui.game_join.title"), intent)).applyTo(context.getSource().getPlayerOrThrow(), false);
ActiveGamePortalConfig.of(Text.translatable("text.plasmid.ui.game_join.title"), intent)).applyTo(PortalUserContext.of(context.getSource().getPlayerOrThrow()), ClickType.MOUSE_RIGHT);
return Command.SINGLE_SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import eu.pb4.sgui.api.ClickType;
import net.minecraft.command.argument.BlockPosArgumentType;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import xyz.nucleoid.plasmid.impl.command.argument.GamePortalArgument;
import xyz.nucleoid.plasmid.impl.portal.GamePortalInterface;
import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext;

import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
Expand Down Expand Up @@ -51,7 +53,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {

private static int openPortal(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var portal = GamePortalArgument.get(context, "portal");
portal.requestJoin(context.getSource().getPlayer(), false);
portal.applyTo(PortalUserContext.of(context.getSource().getPlayerOrThrow()), ClickType.MOUSE_RIGHT);
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package xyz.nucleoid.plasmid.impl.portal;

import eu.pb4.sgui.api.ClickType;
import eu.pb4.sgui.api.elements.GuiElementInterface;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.game.config.CustomValuesConfig;
import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext;
import xyz.nucleoid.plasmid.impl.portal.backend.GamePortalBackend;
import xyz.nucleoid.plasmid.impl.portal.config.GamePortalConfig;

import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -67,7 +66,7 @@ public int getMaxPlayerCount() {
return this.backend.getMaxPlayerCount();
}

public void requestJoin(ServerPlayerEntity player, boolean alt) {
public void applyTo(PortalUserContext player, ClickType alt) {
this.backend.applyTo(player, alt);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package xyz.nucleoid.plasmid.impl.portal.backend;

import eu.pb4.sgui.api.ClickType;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
Expand All @@ -23,8 +23,6 @@ default void populateDisplay(GamePortalDisplay display) {
display.set(GamePortalDisplay.SPECTATOR_COUNT, this.getSpectatorCount());
}

void applyTo(ServerPlayerEntity player, boolean alt);

default Text getName() {
return Text.literal("༼ つ ◕_◕ ༽つ (Unnamed)");
}
Expand All @@ -49,22 +47,20 @@ default int getSpectatorCount() {
return -1;
}

default ActionType getActionType() {
return ActionType.NONE;
}
default ActionType getAltActionType() {
return ActionType.NONE;
}
void applyTo(PortalUserContext context, ClickType clickType);

default List<Action> getActions(PortalUserContext context) {
return List.of();
}
default void provideGameSpaces(Consumer<GameSpace> consumer) {}

interface Factory<T extends GamePortalConfig> {
GamePortalBackend create(MinecraftServer server, Identifier id, T config);
}

record ActionType(Text text, Text textAlt) {
public static ActionType NONE = new ActionType(Text.empty(), Text.empty());
public static ActionType PLAY = new ActionType(Text.translatable("text.plasmid.ui.game_join.action.play"), Text.translatable("text.plasmid.ui.game_join.action.play.alt"));
public static ActionType SPECTATE = new ActionType(Text.translatable("text.plasmid.ui.game_join.action.spectate"), Text.translatable("text.plasmid.ui.game_join.action.spectate.alt"));
record Action(Text text, Text textAlt) {
public static Action NONE = new Action(Text.empty(), Text.empty());
public static Action PLAY = new Action(Text.translatable("text.plasmid.ui.game_join.action.play"), Text.translatable("text.plasmid.ui.game_join.action.play.alt"));
public static Action SPECTATE = new Action(Text.translatable("text.plasmid.ui.game_join.action.spectate"), Text.translatable("text.plasmid.ui.game_join.action.spectate.alt"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package xyz.nucleoid.plasmid.impl.portal.backend;

import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Formatting;
import xyz.nucleoid.plasmid.api.game.GameResult;
import xyz.nucleoid.plasmid.api.game.GameSpace;
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.game.manager.GameSpaceManagerImpl;

import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;

public interface PortalUserContext {
static PortalUserContext of(ServerPlayerEntity serverPlayer) {
return new Player(serverPlayer);
}

void openUi(Consumer<ServerPlayerEntity> guiOpener);

boolean canJoinExisting();

GameResult tryJoin(GameSpace gameSpace, JoinIntent joinIntent);

void tryOpening(RegistryEntry<GameConfig<?>> game);

record Player(ServerPlayerEntity player) implements PortalUserContext {
@Override
public void openUi(Consumer<ServerPlayerEntity> guiOpener) {
guiOpener.accept(this.player);
}

@Override
public boolean canJoinExisting() {
return true;
}

@Override
public GameResult tryJoin(GameSpace gameSpace, JoinIntent joinIntent) {
return GamePlayerJoiner.tryJoin(this.player, gameSpace, joinIntent);
}

@Override
public void tryOpening(RegistryEntry<GameConfig<?>> game) {
CompletableFuture.supplyAsync(() -> GameSpaceManagerImpl.get().open(game))
.thenCompose(Function.identity())
.handleAsync((gameSpace, throwable) -> {
GameResult result;
if (gameSpace != null) {
result = GamePlayerJoiner.tryJoin(this.player, gameSpace, JoinIntent.PLAY);
} else {
result = GamePlayerJoiner.handleJoinException(throwable);
}

if (result.isError()) {
this.player.sendMessage(result.errorCopy().formatted(Formatting.RED), false);
}

return null;
}, this.player.server);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,72 +1,27 @@
package xyz.nucleoid.plasmid.impl.portal.backend.game;

import eu.pb4.sgui.api.ClickType;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Formatting;
import xyz.nucleoid.plasmid.api.game.GameResult;
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.game.config.GameConfig;
import xyz.nucleoid.plasmid.impl.game.manager.GameSpaceManagerImpl;
import xyz.nucleoid.plasmid.api.game.player.GamePlayerJoiner;
import xyz.nucleoid.plasmid.api.game.player.JoinIntent;
import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext;

import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

public final class ConcurrentGamePortalBackend implements GameConfigGamePortalBackend {
private final RegistryEntry<GameConfig<?>> game;
private CompletableFuture<GameSpace> gameFuture;

public ConcurrentGamePortalBackend(RegistryEntry<GameConfig<?>> game) {
this.game = game;
}

public record ConcurrentGamePortalBackend(RegistryEntry<GameConfig<?>> game) implements GameConfigGamePortalBackend {
@Override
public RegistryEntry<GameConfig<?>> game() {
return this.game;
}

@Override
public void applyTo(ServerPlayerEntity player, boolean alt) {
for (var gameSpace : GameSpaceManagerImpl.get().getOpenGameSpaces()) {
if (gameSpace.getMetadata().sourceConfig().equals(this.game)) {
var result = GamePlayerJoiner.tryJoin(player, gameSpace, alt ? JoinIntent.SPECTATE : JoinIntent.PLAY);

if (result.isOk()) {
return;
public void applyTo(PortalUserContext context, ClickType type) {
if (context.canJoinExisting()) {
for (var gameSpace : GameSpaceManagerImpl.get().getOpenGameSpaces()) {
if (gameSpace.getMetadata().sourceConfig().equals(this.game)) {
var result = context.tryJoin(gameSpace, JoinIntent.PLAY);

if (result.isOk()) {
return;
}
}
}
}

CompletableFuture.supplyAsync(() -> this.getOrOpenNew(player.server))
.thenCompose(Function.identity())
.handleAsync((gameSpace, throwable) -> {
this.gameFuture = null;
GameResult result;
if (gameSpace != null) {
result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.PLAY);
} else {
result = GamePlayerJoiner.handleJoinException(throwable);
}

if (result.isError()) {
player.sendMessage(result.errorCopy().formatted(Formatting.RED), false);
}

return null;
}, player.server);
}

public CompletableFuture<GameSpace> getOrOpenNew(MinecraftServer server) {
var future = this.gameFuture;
if (future == null || future.isCompletedExceptionally()) {
this.gameFuture = future = this.openGame(server);
}
return future;
}

private CompletableFuture<GameSpace> openGame(MinecraftServer server) {
return GameSpaceManagerImpl.get().open(this.game);
context.tryOpening(this.game);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.game.config.GameConfig;
import xyz.nucleoid.plasmid.impl.game.manager.GameSpaceManagerImpl;
import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext;
import xyz.nucleoid.plasmid.impl.portal.backend.GamePortalBackend;

import java.util.List;
Expand Down Expand Up @@ -61,7 +62,7 @@ default Text getName() {
}

@Override
default ActionType getActionType() {
return ActionType.PLAY;
default List<Action> getActions(PortalUserContext context) {
return List.of(Action.PLAY);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package xyz.nucleoid.plasmid.impl.portal.backend.game;

import com.mojang.serialization.MapCodec;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import eu.pb4.sgui.api.ClickType;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import xyz.nucleoid.plasmid.api.game.config.CustomValuesConfig;
import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext;
import xyz.nucleoid.plasmid.impl.portal.backend.GamePortalBackend;
import xyz.nucleoid.plasmid.impl.portal.config.GamePortalConfig;

Expand All @@ -28,7 +28,7 @@ public Text getName() {
}

@Override
public void applyTo(ServerPlayerEntity player, boolean alt) {
public void applyTo(PortalUserContext context, ClickType clickType) {

}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,13 @@
package xyz.nucleoid.plasmid.impl.portal.backend.game;

import eu.pb4.sgui.api.ClickType;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Formatting;
import xyz.nucleoid.plasmid.api.game.GameResult;
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.game.config.GameConfig;
import xyz.nucleoid.plasmid.impl.game.manager.GameSpaceManagerImpl;
import xyz.nucleoid.plasmid.api.game.player.GamePlayerJoiner;
import xyz.nucleoid.plasmid.api.game.player.JoinIntent;

import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import xyz.nucleoid.plasmid.impl.portal.backend.PortalUserContext;

public record NewGamePortalBackend(RegistryEntry<GameConfig<?>> game) implements GameConfigGamePortalBackend {
@Override
public void applyTo(ServerPlayerEntity player, boolean alt) {
CompletableFuture.supplyAsync(() -> this.openGame(player.server))
.thenCompose(Function.identity())
.handleAsync((gameSpace, throwable) -> {
GameResult result;
if (gameSpace != null) {
result = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.PLAY);
} else {
result = GamePlayerJoiner.handleJoinException(throwable);
}

if (result.isError()) {
player.sendMessage(result.errorCopy().formatted(Formatting.RED), false);
}

return null;
}, player.server);
}

private CompletableFuture<GameSpace> openGame(MinecraftServer server) {
return GameSpaceManagerImpl.get().open(this.game);
public void applyTo(PortalUserContext context, ClickType clickType) {
context.tryOpening(this.game);
}
}
Loading

0 comments on commit cfbd454

Please sign in to comment.