Skip to content

Commit

Permalink
Make GameSpace attachment API strictly typed
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Jun 22, 2024
1 parent dd73b0c commit ead412f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
20 changes: 20 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/game/GameAttachment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package xyz.nucleoid.plasmid.game;

import net.minecraft.util.Identifier;

public class GameAttachment<T> {
private final Identifier id;

private GameAttachment(Identifier id) {
this.id = id;
}

public static <T> GameAttachment<T> create(Identifier id) {
return new GameAttachment<>(id);
}

@Override
public String toString() {
return this.id.toString();
}
}
18 changes: 18 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/game/GameAttachmentHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package xyz.nucleoid.plasmid.game;

import org.jetbrains.annotations.Nullable;

public interface GameAttachmentHolder {
@Nullable
<T> T getAttachment(GameAttachment<? extends T> attachment);

default <T> T getAttachmentOrThrow(GameAttachment<? extends T> attachment) {
T value = this.getAttachment(attachment);
if (value == null) {
throw new IllegalArgumentException("Missing attachment " + attachment + " on " + this);
}
return value;
}

<T> void setAttachment(GameAttachment<? super T> attachment, @Nullable T value);
}
6 changes: 1 addition & 5 deletions src/main/java/xyz/nucleoid/plasmid/game/GameSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @see GameType
* @see GameActivity
*/
public interface GameSpace {
public interface GameSpace extends GameAttachmentHolder {
/**
* @return the host server of this {@link GameSpace}
*/
Expand Down Expand Up @@ -109,8 +109,4 @@ public interface GameSpace {
* @return true if this GameSpace is closed, false otherwise
*/
boolean isClosed();

@Nullable
<T> T getAttachment(String key);
void setAttachment(String key, @Nullable Object obj);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package xyz.nucleoid.plasmid.game.manager;

import com.google.common.collect.Lists;
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.registry.RegistryKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.fantasy.RuntimeWorldHandle;
import xyz.nucleoid.plasmid.Plasmid;
import xyz.nucleoid.plasmid.event.GameEvents;
Expand All @@ -19,7 +21,6 @@
import xyz.nucleoid.plasmid.game.player.PlayerOfferResult;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

Expand All @@ -40,7 +41,7 @@ public final class ManagedGameSpace implements GameSpace {
private boolean closed;

private final GameSpaceStatistics statistics = new GameSpaceStatistics();
private final Map<String, Object> attachments = new HashMap<>();
private final Map<GameAttachment<?>, Object> attachments = new Reference2ObjectOpenHashMap<>();

ManagedGameSpace(MinecraftServer server, GameSpaceManager manager, GameSpaceMetadata metadata) {
this.server = server;
Expand Down Expand Up @@ -171,18 +172,20 @@ public GameSpaceStatistics getStatistics() {
public boolean isClosed() {
return this.closed;
}

@Override
public <T> T getAttachment(String key) {
//noinspection unchecked
return (T) this.attachments.get(key);
@Nullable
@SuppressWarnings("unchecked")
public <T> T getAttachment(GameAttachment<? extends T> attachment) {
return (T) this.attachments.get(attachment);
}

@Override
public void setAttachment(String key, Object obj) {
if (obj == null) {
this.attachments.remove(key);
public <T> void setAttachment(GameAttachment<? super T> attachment, @Nullable T value) {
if (value == null) {
this.attachments.remove(attachment);
} else {
this.attachments.put(key, obj);
this.attachments.put(attachment, value);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/testmod/java/xyz/nucleoid/plasmid/test/TestGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ public static GameOpenProcedure open(GameOpenContext<TestConfig> context) {
});
}

private static final GameAttachment<Item> TEST = GameAttachment.create(new Identifier("plasmid", "test"));

private static GameResult startGame(GameSpace gameSpace) {
gameSpace.setAttachment("test", Items.POTATO);
gameSpace.setAttachment(TEST, Items.POTATO);

gameSpace.setActivity((activity) -> {
long currentTime = gameSpace.getTime();
Expand All @@ -92,7 +94,7 @@ private static GameResult startGame(GameSpace gameSpace) {

activity.deny(GameRuleType.INTERACTION).allow(GameRuleType.USE_BLOCKS);

Item potato = gameSpace.getAttachment("test");
Item potato = gameSpace.getAttachment(TEST);

var teamManager = TeamManager.addTo(activity);
teamManager.addTeam(TEAM);
Expand Down

0 comments on commit ead412f

Please sign in to comment.