-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Give portals more context about click, user PortalUserContext instead…
… of player entity
- Loading branch information
Showing
19 changed files
with
172 additions
and
291 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
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
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
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
66 changes: 66 additions & 0 deletions
66
src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/PortalUserContext.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,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); | ||
} | ||
} | ||
} |
71 changes: 13 additions & 58 deletions
71
src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/ConcurrentGamePortalBackend.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 |
---|---|---|
@@ -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); | ||
} | ||
} |
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
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
36 changes: 4 additions & 32 deletions
36
src/main/java/xyz/nucleoid/plasmid/impl/portal/backend/game/NewGamePortalBackend.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 |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.