Skip to content

Commit

Permalink
Don't expose player entity in offer event until joined
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Jun 23, 2024
1 parent dc8d18a commit d2172b6
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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.LocalPlayerOffer;
import xyz.nucleoid.plasmid.game.player.PlayerOffer;
import xyz.nucleoid.plasmid.game.player.PlayerOfferResult;

Expand Down Expand Up @@ -211,7 +212,7 @@ private GameResult attemptScreenJoins(Collection<ServerPlayerEntity> players) {
return this.state.invoker(GamePlayerEvents.SCREEN_JOINS).screenJoins(players);
}

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
@@ -1,5 +1,6 @@
package xyz.nucleoid.plasmid.game.player;

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,8 +9,14 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public record LocalPlayerOffer(ServerPlayerEntity player) implements PlayerOffer {
@Override
public GameProfile profile() {
return this.player.getGameProfile();
}

@Override
public PlayerOfferResult.Accept accept(ServerWorld world, Vec3d position) {
return new Accept(world, position);
Expand All @@ -24,24 +31,26 @@ public static class Accept implements PlayerOfferResult.Accept {
private final ServerWorld world;
private final Vec3d position;

private final List<Runnable> and = new ArrayList<>();
private final List<Consumer<ServerPlayerEntity>> thenRun = new ArrayList<>();

Accept(ServerWorld world, Vec3d position) {
this.world = world;
this.position = position;
}

@Override
public Accept and(Runnable and) {
this.and.add(and);
public Accept thenRun(Consumer<ServerPlayerEntity> consumer) {
this.thenRun.add(consumer);
return this;
}

public ServerWorld applyJoin(ServerPlayerEntity player) {
player.changeGameMode(GameMode.SURVIVAL);
player.refreshPositionAndAngles(this.position.x, this.position.y, this.position.z, 0.0F, 0.0F);

this.and.forEach(Runnable::run);
for (Consumer<ServerPlayerEntity> consumer : this.thenRun) {
consumer.accept(player);
}

return this.world;
}
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/xyz/nucleoid/plasmid/game/player/PlayerOffer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.nucleoid.plasmid.game.player;

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,8 @@
import xyz.nucleoid.plasmid.game.GameTexts;
import xyz.nucleoid.plasmid.game.event.GamePlayerEvents;

import java.util.UUID;

/**
* Represents a request for a {@link ServerPlayerEntity} to join a {@link GameSpace}.
* <p>
Expand All @@ -19,9 +22,23 @@
*/
public interface PlayerOffer {
/**
* @return the player that is requesting access to this {@link GameSpace}.
* @return the {@link GameProfile} of the player that is requesting access to this {@link GameSpace}
*/
ServerPlayerEntity player();
GameProfile profile();

/**
* @return the {@link UUID profile UUID} of the player that is requesting access to this {@link GameSpace}
*/
default UUID playerId() {
return this.profile().getId();
}

/**
* @return the username of the player that is requesting access to this {@link GameSpace}
*/
default String playerName() {
return this.profile().getName();
}

/**
* Returns an offer result that accepts this player offer and allows the player into this {@link GameSpace}.
Expand All @@ -32,7 +49,7 @@ public interface PlayerOffer {
* @param world the world that the player should be teleported to when accepted
* @param position the position that the player should be teleported to when accepted
* @return an "accept" offer result
* @see PlayerOfferResult.Accept#and(Runnable)
* @see PlayerOfferResult.Accept#thenRun(java.util.function.Consumer)
*/
PlayerOfferResult.Accept accept(ServerWorld world, Vec3d position);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package xyz.nucleoid.plasmid.game.player;

import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;

import java.util.function.Consumer;

public sealed interface PlayerOfferResult permits PlayerOfferResult.Pass, PlayerOfferResult.Accept, PlayerOfferResult.Reject {
Pass PASS = new Pass();

Expand All @@ -11,7 +14,7 @@ private Pass() {
}

non-sealed interface Accept extends PlayerOfferResult {
PlayerOfferResult.Accept and(Runnable and);
PlayerOfferResult.Accept thenRun(Consumer<ServerPlayerEntity> consumer);
}

non-sealed interface Reject extends PlayerOfferResult {
Expand Down
20 changes: 8 additions & 12 deletions src/testmod/java/xyz/nucleoid/plasmid/test/JankGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import eu.pb4.polymer.common.api.PolymerCommonUtils;
import eu.pb4.polymer.virtualentity.api.VirtualEntityUtils;
import eu.pb4.sidebars.api.SidebarUtils;
import it.unimi.dsi.fastutil.ints.IntList;
import net.minecraft.block.BlockState;
import net.minecraft.entity.MovementType;
Expand All @@ -13,9 +12,7 @@
import net.minecraft.network.packet.c2s.play.VehicleMoveC2SPacket;
import net.minecraft.network.packet.s2c.play.*;
import net.minecraft.particle.ParticleTypes;
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.ActionResult;
import net.minecraft.util.math.Vec3d;
Expand Down Expand Up @@ -61,11 +58,10 @@ public static GameOpenProcedure open(GameOpenContext<TestConfig> context) {
.setGameRule(GameRules.KEEP_INVENTORY, true);

return context.openWithWorld(worldConfig, (activity, world) -> {
activity.listen(GamePlayerEvents.OFFER, offer -> {
var player = offer.player();
return offer.accept(world, new Vec3d(0.0, 65.0, 0.0))
.and(() -> player.changeGameMode(GameMode.ADVENTURE));
});
activity.listen(GamePlayerEvents.OFFER, offer ->
offer.accept(world, new Vec3d(0.0, 65.0, 0.0))
.thenRun(player -> player.changeGameMode(GameMode.ADVENTURE))
);

GameWaitingLobby.addTo(activity, new PlayerConfig(1, 99));

Expand Down Expand Up @@ -174,10 +170,10 @@ private static GameResult startGame(GameSpace gameSpace) {
player.networkHandler.sendPacket(new PlayerPositionLookS2CPacket(0, 0, 0, 0, 0f, Set.of(), 0));
});

activity.listen(GamePlayerEvents.OFFER, offer -> {
return offer.accept(gameSpace.getWorlds().iterator().next(), new Vec3d(0.0, 65.0, 0.0))
.and(() -> offer.player().changeGameMode(GameMode.ADVENTURE));
});
activity.listen(GamePlayerEvents.OFFER, offer ->
offer.accept(gameSpace.getWorlds().iterator().next(), new Vec3d(0.0, 65.0, 0.0))
.thenRun(joiningPlayer -> joiningPlayer.changeGameMode(GameMode.ADVENTURE))
);
});

return GameResult.ok();
Expand Down
10 changes: 4 additions & 6 deletions src/testmod/java/xyz/nucleoid/plasmid/test/TestGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import xyz.nucleoid.plasmid.util.WoodType;
import xyz.nucleoid.stimuli.event.player.PlayerDeathEvent;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
Expand All @@ -59,11 +58,10 @@ public static GameOpenProcedure open(GameOpenContext<TestConfig> context) {
.setGameRule(GameRules.KEEP_INVENTORY, true);

return context.openWithWorld(worldConfig, (activity, world) -> {
activity.listen(GamePlayerEvents.OFFER, offer -> {
var player = offer.player();
return offer.accept(world, new Vec3d(0.0, 65.0, 0.0))
.and(() -> player.changeGameMode(GameMode.ADVENTURE));
});
activity.listen(GamePlayerEvents.OFFER, offer ->
offer.accept(world, new Vec3d(0.0, 65.0, 0.0))
.thenRun(player -> player.changeGameMode(GameMode.ADVENTURE))
);

GameWaitingLobby.addTo(activity, new PlayerConfig(1, 99));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;
import net.minecraft.util.Unit;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.GameMode;
import net.minecraft.world.GameRules;
Expand Down Expand Up @@ -52,11 +51,10 @@ public static GameOpenProcedure open(GameOpenContext<TestConfig> context) {
.setGameRule(GameRules.KEEP_INVENTORY, true);

return context.openWithWorld(worldConfig, (activity, world) -> {
activity.listen(GamePlayerEvents.OFFER, offer -> {
var player = offer.player();
return offer.accept(world, new Vec3d(0.0, 65.0, 0.0))
.and(() -> player.changeGameMode(GameMode.ADVENTURE));
});
activity.listen(GamePlayerEvents.OFFER, offer ->
offer.accept(world, new Vec3d(0.0, 65.0, 0.0))
.thenRun(player -> player.changeGameMode(GameMode.ADVENTURE))
);

GameWaitingLobby.addTo(activity, new PlayerConfig(1, 99));

Expand Down Expand Up @@ -129,11 +127,10 @@ private static GameResult startGame(GameSpace gameSpace, int iter) {
return ActionResult.FAIL;
});

activity.listen(GamePlayerEvents.OFFER, offer -> {
var player = offer.player();
return offer.accept(gameSpace.getWorlds().iterator().next(), new Vec3d(0.0, 65.0, 0.0))
.and(() -> player.changeGameMode(GameMode.ADVENTURE));
});
activity.listen(GamePlayerEvents.OFFER, offer ->
offer.accept(gameSpace.getWorlds().iterator().next(), new Vec3d(0.0, 65.0, 0.0))
.thenRun(player -> player.changeGameMode(GameMode.ADVENTURE))
);
});

return GameResult.ok();
Expand Down

0 comments on commit d2172b6

Please sign in to comment.