Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove player entity context from player offers #298

Open
wants to merge 6 commits into
base: 1.21
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,25 @@ dependencies {

modApi "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

modApi include('xyz.nucleoid:server-translations-api:2.3.0+1.20.5-rc2')
modApi include('xyz.nucleoid:packet-tweaker:0.5.1+1.20.6')
modApi include('xyz.nucleoid:fantasy:0.6.2+1.20.6')
modApi include('xyz.nucleoid:more-codecs:0.3.3+1.20.2')
modApi include('xyz.nucleoid:stimuli:0.4.11+1.20.6')
modApi include('xyz.nucleoid:map-templates:0.1.9+1.20.4')
modApi include('xyz.nucleoid:substrate:0.2.2+1.20.1')
modApi 'eu.pb4:polymer-core:0.8.2+1.20.6'
modApi 'eu.pb4:polymer-resource-pack:0.8.2+1.20.6'
modApi 'eu.pb4:polymer-blocks:0.8.2+1.20.6'
modApi 'eu.pb4:polymer-virtual-entity:0.8.2+1.20.6'
modApi include('eu.pb4:sgui:1.5.1+1.20.5')
modApi include('eu.pb4:sidebar-api:0.4.0+1.20.5')
modApi include("eu.pb4:placeholder-api:2.4.0-pre.1+1.20.5")
modApi include("eu.pb4:map-canvas-api:0.3.0+1.20.6")
modApi include("eu.pb4:player-data-api:0.5.0+1.20.5")
modApi include("eu.pb4:predicate-api:0.4.0+1.20.5")

modImplementation include("me.lucko:fabric-permissions-api:0.2-SNAPSHOT")
modApi include("xyz.nucleoid:server-translations-api:${project.server_translations_version}")
modApi include("xyz.nucleoid:packet-tweaker:${project.packet_tweaker_version}")
modApi include("xyz.nucleoid:fantasy:${project.fantasy_version}")
modApi include("xyz.nucleoid:more-codecs:${project.more_codecs_version}")
modApi include("xyz.nucleoid:stimuli:${project.stimuli_version}")
modApi include("xyz.nucleoid:map-templates:${project.map_templates_version}")
modApi include("xyz.nucleoid:substrate:${project.substrate_version}")
modApi "eu.pb4:polymer-core:${project.polymer_version}"
modApi "eu.pb4:polymer-resource-pack:${project.polymer_version}"
modApi "eu.pb4:polymer-blocks:${project.polymer_version}"
modApi "eu.pb4:polymer-virtual-entity:${project.polymer_version}"
modApi include("eu.pb4:sgui:${project.sgui_version}")
modApi include("eu.pb4:sidebar-api:${project.sidebar_api_version}")
modApi include("eu.pb4:placeholder-api:${project.placeholder_api_version}")
modApi include("eu.pb4:map-canvas-api:${project.map_canvas_api_version}")
modApi include("eu.pb4:player-data-api:${project.player_data_api_version}")
modApi include("eu.pb4:predicate-api:${project.predicate_api_version}")

modImplementation include("me.lucko:fabric-permissions-api:${project.permission_api_version}")

modCompileOnly('xyz.nucleoid:disguiselib-fabric:1.3.2')
modCompileOnly('maven.modrinth:afkdisplay:1.1.0')
Expand Down
17 changes: 16 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@ minecraft_version=1.20.6
yarn_mappings=1.20.6+build.1
loader_version=0.15.10

#Fabric api
# Dependencies
fabric_version=0.99.0+1.20.6
polymer_version=0.8.2+1.20.6
server_translations_version=2.3.0+1.20.5-rc2
packet_tweaker_version=0.5.1+1.20.6
fantasy_version=0.6.2+1.20.6
more_codecs_version=0.3.3+1.20.2
stimuli_version=0.4.11+1.20.6
map_templates_version=0.1.9+1.20.4
substrate_version=0.2.2+1.20.1
sgui_version=1.5.1+1.20.5
sidebar_api_version=0.4.0+1.20.5
placeholder_api_version=2.4.0-pre.1+1.20.5
map_canvas_api_version=0.3.0+1.20.6
player_data_api_version=0.5.0+1.20.5
predicate_api_version=0.4.0+1.20.5
permission_api_version=0.2-SNAPSHOT

# Mod Properties
mod_version=0.6
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.mojang.logging.LogUtils;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.command.argument.NbtCompoundArgumentType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtOps;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.command.ServerCommandSource;
Expand All @@ -30,6 +29,7 @@
import xyz.nucleoid.plasmid.game.config.GameConfigs;
import xyz.nucleoid.plasmid.game.manager.GameSpaceManager;
import xyz.nucleoid.plasmid.game.player.GamePlayerJoiner;
import xyz.nucleoid.plasmid.game.player.JoinIntent;
import xyz.nucleoid.plasmid.util.Scheduler;

import java.util.Comparator;
Expand Down Expand Up @@ -286,18 +286,19 @@ private static void joinAllPlayersToGame(ServerCommandSource source, GameSpace g
.filter(player -> !GameSpaceManager.get().inGame(player))
.collect(Collectors.toList());

var screen = gameSpace.getPlayers().screenJoins(players);
var intent = JoinIntent.ANY;
var screen = gameSpace.getPlayers().screenJoins(players, intent);
if (screen.isOk()) {
for (var player : players) {
gameSpace.getPlayers().offer(player);
gameSpace.getPlayers().offer(player, intent);
}
} else {
source.sendError(screen.errorCopy().formatted(Formatting.RED));
}
}

private static void tryJoinGame(ServerPlayerEntity player, GameSpace gameSpace) {
var results = GamePlayerJoiner.tryJoin(player, gameSpace);
var results = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.ANY);
results.sendErrorsTo(player);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import xyz.nucleoid.plasmid.game.manager.GameSpaceManager;
import xyz.nucleoid.plasmid.game.manager.ManagedGameSpace;
import xyz.nucleoid.plasmid.game.player.GamePlayerJoiner;
import xyz.nucleoid.plasmid.game.player.JoinIntent;
import xyz.nucleoid.plasmid.util.Guis;

import java.util.ArrayList;
Expand All @@ -38,7 +39,7 @@ public GameJoinUi(ServerPlayerEntity player) {

private static void tryJoinGame(ServerPlayerEntity player, GameSpace gameSpace) {
player.server.execute(() -> {
var results = GamePlayerJoiner.tryJoin(player, gameSpace);
var results = GamePlayerJoiner.tryJoin(player, gameSpace, JoinIntent.ANY);
results.sendErrorsTo(player);
});
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/xyz/nucleoid/plasmid/game/GameSpacePlayers.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.server.network.ServerPlayerEntity;
import xyz.nucleoid.plasmid.game.event.GamePlayerEvents;
import xyz.nucleoid.plasmid.game.player.JoinIntent;
import xyz.nucleoid.plasmid.game.player.PlayerOps;
import xyz.nucleoid.plasmid.game.player.PlayerSet;

Expand All @@ -21,12 +22,13 @@ public interface GameSpacePlayers extends PlayerSet {
* This logic is controlled through the active {@link GameActivity} through {@link GamePlayerEvents#SCREEN_JOINS}.
*
* @param players the group of players trying to join
* @param intent the intent of the players trying to join, such as whether they want to participate or spectate
* @return a {@link GameResult} describing whether this group can join this game, or an error if not
* @see GamePlayerEvents#SCREEN_JOINS
* @see GameSpacePlayers#offer(ServerPlayerEntity)
* @see GameSpacePlayers#offer(ServerPlayerEntity, JoinIntent)
* @see xyz.nucleoid.plasmid.game.player.GamePlayerJoiner
*/
GameResult screenJoins(Collection<ServerPlayerEntity> players);
GameResult screenJoins(Collection<ServerPlayerEntity> players, JoinIntent intent);

/**
* Offers an individual player to join this game. If accepted, they will be teleported into the game, and if not
Expand All @@ -35,11 +37,12 @@ public interface GameSpacePlayers extends PlayerSet {
* This logic is controlled through the active {@link GameActivity} through {@link GamePlayerEvents#OFFER}.
*
* @param player the player trying to join
* @param intent the intent of the players trying to join, such as whether they want to participate or spectate
* @return a {@link GameResult} describing whether this player joined the game, or an error if not
* @see GamePlayerEvents#OFFER
* @see xyz.nucleoid.plasmid.game.player.GamePlayerJoiner
*/
GameResult offer(ServerPlayerEntity player);
GameResult offer(ServerPlayerEntity player, JoinIntent intent);

/**
* Attempts to remove the given {@link ServerPlayerEntity} from this {@link GameSpace}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.nucleoid.plasmid.game.common;

import com.mojang.authlib.GameProfile;
import net.minecraft.entity.boss.BossBar;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.server.network.ServerPlayerEntity;
Expand Down Expand Up @@ -88,7 +89,7 @@ public static GameWaitingLobby addTo(GameActivity activity, PlayerConfig playerC

activity.listen(GameActivityEvents.TICK, lobby::onTick);
activity.listen(GameActivityEvents.REQUEST_START, lobby::requestStart);
activity.listen(GamePlayerEvents.SCREEN_JOINS, lobby::screenJoins);
activity.listen(GamePlayerEvents.SCREEN_JOINS, (players, intent) -> lobby.screenJoins(players));
activity.listen(GamePlayerEvents.OFFER, lobby::offerPlayer);
activity.listen(GamePlayerEvents.REMOVE, lobby::onRemovePlayer);

Expand Down Expand Up @@ -169,7 +170,7 @@ private GameResult requestStart() {
}
}

private GameResult screenJoins(Collection<ServerPlayerEntity> players) {
private GameResult screenJoins(Collection<GameProfile> players) {
int newPlayerCount = this.gameSpace.getPlayers().size() + players.size();
if (newPlayerCount > this.playerConfig.maxPlayers()) {
return GameResult.error(GameTexts.Join.gameFull());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.nucleoid.plasmid.game.event;

import com.mojang.authlib.GameProfile;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
Expand All @@ -8,6 +9,7 @@
import xyz.nucleoid.plasmid.game.GameResult;
import xyz.nucleoid.plasmid.game.GameSpace;
import xyz.nucleoid.plasmid.game.GameTexts;
import xyz.nucleoid.plasmid.game.player.JoinIntent;
import xyz.nucleoid.plasmid.game.player.PlayerOffer;
import xyz.nucleoid.plasmid.game.player.PlayerOfferResult;
import xyz.nucleoid.stimuli.event.StimulusEvent;
Expand Down Expand Up @@ -107,10 +109,10 @@ public final class GamePlayerEvents {
*
* @see GamePlayerEvents#OFFER
*/
public static final StimulusEvent<ScreenJoins> SCREEN_JOINS = StimulusEvent.create(ScreenJoins.class, ctx -> players -> {
public static final StimulusEvent<ScreenJoins> SCREEN_JOINS = StimulusEvent.create(ScreenJoins.class, ctx -> (players, intent) -> {
try {
for (var listener : ctx.getListeners()) {
var result = listener.screenJoins(players);
var result = listener.screenJoins(players, intent);
if (result.isError()) {
return result;
}
Expand Down Expand Up @@ -138,7 +140,7 @@ public final class GamePlayerEvents {
try {
for (var listener : ctx.getListeners()) {
var result = listener.onOfferPlayer(offer);
if (result.isTerminal()) {
if (!(result instanceof PlayerOfferResult.Pass)) {
return result;
}
}
Expand Down Expand Up @@ -174,7 +176,7 @@ public interface Remove {
}

public interface ScreenJoins {
GameResult screenJoins(Collection<ServerPlayerEntity> players);
GameResult screenJoins(Collection<GameProfile> players, JoinIntent intent);
}

public interface Offer {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package xyz.nucleoid.plasmid.game.manager;

import com.google.common.collect.Lists;
import com.mojang.authlib.GameProfile;
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.registry.RegistryKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
Expand All @@ -17,7 +19,8 @@
import xyz.nucleoid.plasmid.game.config.GameConfig;
import xyz.nucleoid.plasmid.game.event.GameActivityEvents;
import xyz.nucleoid.plasmid.game.event.GamePlayerEvents;
import xyz.nucleoid.plasmid.game.player.PlayerOffer;
import xyz.nucleoid.plasmid.game.player.JoinIntent;
import xyz.nucleoid.plasmid.game.player.LocalPlayerOffer;
import xyz.nucleoid.plasmid.game.player.PlayerOfferResult;

import java.util.Collection;
Expand Down Expand Up @@ -193,8 +196,8 @@ public GameBehavior getBehavior() {
return this.state;
}

GameResult screenJoins(Collection<ServerPlayerEntity> players) {
var result = this.attemptScreenJoins(players);
GameResult screenJoins(Collection<ServerPlayerEntity> players, JoinIntent intent) {
var result = this.attemptScreenJoins(players.stream().map(PlayerEntity::getGameProfile).toList(), intent);

if (result.isError()) {
this.players.attemptGarbageCollection();
Expand All @@ -203,15 +206,15 @@ GameResult screenJoins(Collection<ServerPlayerEntity> players) {
return result;
}

private GameResult attemptScreenJoins(Collection<ServerPlayerEntity> players) {
private GameResult attemptScreenJoins(Collection<GameProfile> players, JoinIntent intent) {
if (this.closed) {
return GameResult.error(GameTexts.Join.gameClosed());
}

return this.state.invoker(GamePlayerEvents.SCREEN_JOINS).screenJoins(players);
return this.state.invoker(GamePlayerEvents.SCREEN_JOINS).screenJoins(players, intent);
}

PlayerOfferResult offerPlayer(PlayerOffer offer) {
PlayerOfferResult offerPlayer(LocalPlayerOffer offer) {
if (this.closed) {
return offer.reject(GameTexts.Join.gameClosed());
} else if (this.manager.inGame(offer.player())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import xyz.nucleoid.plasmid.game.GameResult;
import xyz.nucleoid.plasmid.game.GameSpacePlayers;
import xyz.nucleoid.plasmid.game.GameTexts;
import xyz.nucleoid.plasmid.game.player.JoinIntent;
import xyz.nucleoid.plasmid.game.player.LocalPlayerOffer;
import xyz.nucleoid.plasmid.game.player.MutablePlayerSet;
import xyz.nucleoid.plasmid.game.player.PlayerOffer;
import xyz.nucleoid.plasmid.game.player.PlayerOfferResult;
import xyz.nucleoid.plasmid.game.player.isolation.IsolatingPlayerTeleporter;

import java.util.Collection;
Expand All @@ -26,13 +28,13 @@ public final class ManagedGameSpacePlayers implements GameSpacePlayers {
}

@Override
public GameResult screenJoins(Collection<ServerPlayerEntity> players) {
return this.space.screenJoins(players);
public GameResult screenJoins(Collection<ServerPlayerEntity> players, JoinIntent intent) {
return this.space.screenJoins(players, intent);
}

@Override
public GameResult offer(ServerPlayerEntity player) {
var result = this.attemptOffer(player);
public GameResult offer(ServerPlayerEntity player, JoinIntent intent) {
var result = this.attemptOffer(player, intent);

if (result.isError()) {
this.attemptGarbageCollection();
Expand All @@ -41,32 +43,31 @@ public GameResult offer(ServerPlayerEntity player) {
return result;
}

private GameResult attemptOffer(ServerPlayerEntity player) {
private GameResult attemptOffer(ServerPlayerEntity player, JoinIntent intent) {
if (this.set.contains(player)) {
return GameResult.error(GameTexts.Join.alreadyJoined());
}

var offer = new PlayerOffer(player);
var result = this.space.offerPlayer(offer);
var offer = new LocalPlayerOffer(player, intent);

var reject = result.asReject();
if (reject != null) {
return GameResult.error(reject.reason());
}

var accept = result.asAccept();
if (accept != null) {
try {
this.teleporter.teleportIn(player, accept::applyJoin);
this.set.add(player);
this.space.onAddPlayer(player);
switch (this.space.offerPlayer(offer)) {
case LocalPlayerOffer.Accept accept -> {
try {
this.teleporter.teleportIn(player, accept::applyJoin);
this.set.add(player);
this.space.onAddPlayer(player);

return GameResult.ok();
} catch (Throwable throwable) {
return GameResult.error(GameTexts.Join.unexpectedError());
return GameResult.ok();
} catch (Throwable throwable) {
return GameResult.error(GameTexts.Join.unexpectedError());
}
}
case PlayerOfferResult.Reject reject -> {
return GameResult.error(reject.reason());
}
default -> {
return GameResult.error(GameTexts.Join.genericError());
}
} else {
return GameResult.error(GameTexts.Join.genericError());
}
}

Expand Down
Loading
Loading