diff --git a/src/main/java/xyz/nucleoid/plasmid/game/GameAttachment.java b/src/main/java/xyz/nucleoid/plasmid/game/GameAttachment.java new file mode 100644 index 00000000..e6e5a0ee --- /dev/null +++ b/src/main/java/xyz/nucleoid/plasmid/game/GameAttachment.java @@ -0,0 +1,20 @@ +package xyz.nucleoid.plasmid.game; + +import net.minecraft.util.Identifier; + +public class GameAttachment { + private final Identifier id; + + private GameAttachment(Identifier id) { + this.id = id; + } + + public static GameAttachment create(Identifier id) { + return new GameAttachment<>(id); + } + + @Override + public String toString() { + return this.id.toString(); + } +} diff --git a/src/main/java/xyz/nucleoid/plasmid/game/GameAttachmentHolder.java b/src/main/java/xyz/nucleoid/plasmid/game/GameAttachmentHolder.java new file mode 100644 index 00000000..07da427f --- /dev/null +++ b/src/main/java/xyz/nucleoid/plasmid/game/GameAttachmentHolder.java @@ -0,0 +1,18 @@ +package xyz.nucleoid.plasmid.game; + +import org.jetbrains.annotations.Nullable; + +public interface GameAttachmentHolder { + @Nullable + T getAttachment(GameAttachment attachment); + + default T getAttachmentOrThrow(GameAttachment attachment) { + T value = this.getAttachment(attachment); + if (value == null) { + throw new IllegalArgumentException("Missing attachment " + attachment + " on " + this); + } + return value; + } + + void setAttachment(GameAttachment attachment, @Nullable T value); +} diff --git a/src/main/java/xyz/nucleoid/plasmid/game/GameSpace.java b/src/main/java/xyz/nucleoid/plasmid/game/GameSpace.java index 24474036..bcb2ac9c 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/GameSpace.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/GameSpace.java @@ -23,7 +23,7 @@ * @see GameType * @see GameActivity */ -public interface GameSpace { +public interface GameSpace extends GameAttachmentHolder { /** * @return the host server of this {@link GameSpace} */ @@ -109,8 +109,4 @@ public interface GameSpace { * @return true if this GameSpace is closed, false otherwise */ boolean isClosed(); - - @Nullable - T getAttachment(String key); - void setAttachment(String key, @Nullable Object obj); } diff --git a/src/main/java/xyz/nucleoid/plasmid/game/manager/ManagedGameSpace.java b/src/main/java/xyz/nucleoid/plasmid/game/manager/ManagedGameSpace.java index b9f1130e..894642a1 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/manager/ManagedGameSpace.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/manager/ManagedGameSpace.java @@ -1,6 +1,7 @@ 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; @@ -8,6 +9,7 @@ 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; @@ -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; @@ -40,7 +41,7 @@ public final class ManagedGameSpace implements GameSpace { private boolean closed; private final GameSpaceStatistics statistics = new GameSpaceStatistics(); - private final Map attachments = new HashMap<>(); + private final Map, Object> attachments = new Reference2ObjectOpenHashMap<>(); ManagedGameSpace(MinecraftServer server, GameSpaceManager manager, GameSpaceMetadata metadata) { this.server = server; @@ -171,18 +172,20 @@ public GameSpaceStatistics getStatistics() { public boolean isClosed() { return this.closed; } + @Override - public T getAttachment(String key) { - //noinspection unchecked - return (T) this.attachments.get(key); + @Nullable + @SuppressWarnings("unchecked") + public T getAttachment(GameAttachment attachment) { + return (T) this.attachments.get(attachment); } @Override - public void setAttachment(String key, Object obj) { - if (obj == null) { - this.attachments.remove(key); + public void setAttachment(GameAttachment attachment, @Nullable T value) { + if (value == null) { + this.attachments.remove(attachment); } else { - this.attachments.put(key, obj); + this.attachments.put(attachment, value); } } diff --git a/src/testmod/java/xyz/nucleoid/plasmid/test/TestGame.java b/src/testmod/java/xyz/nucleoid/plasmid/test/TestGame.java index 92e4b69d..a6acbc8b 100644 --- a/src/testmod/java/xyz/nucleoid/plasmid/test/TestGame.java +++ b/src/testmod/java/xyz/nucleoid/plasmid/test/TestGame.java @@ -81,8 +81,10 @@ public static GameOpenProcedure open(GameOpenContext context) { }); } + private static final GameAttachment 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(); @@ -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);