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

Update minigame to Minecraft 1.21.3 #2

Merged
merged 1 commit into from
Dec 22, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
with:
cache: gradle
distribution: zulu
java-version: 17
java-version: 21
- name: Build with Gradle
run: gradle build
- name: Upload Artifacts
Expand Down
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id "fabric-loom" version "1.8.12"
id "fabric-loom" version "1.9.2"
id "maven-publish"
}

Expand Down Expand Up @@ -44,12 +44,12 @@ processResources {
}

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}

tasks.withType(JavaCompile) {
options.release = 17
options.release = 21
options.encoding = "UTF-8"
}

Expand Down
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ mod_version = 1.0.0
org.gradle.jvmargs = -Xmx1G

# Versions
minecraft_version = 1.20.4
yarn_mappings = 1.20.4+build.3
minecraft_version = 1.21.3
yarn_mappings = 1.21.3+build.2
loader_version = 0.16.9
fabric_version = 0.97.2+1.20.4
fabric_version = 0.110.0+1.21.3

plasmid_version = 0.5.102-SNAPSHOT+1.20.4
plasmid_version = 0.6.1+1.21.3
wasmtime_version = 0.18.0
8 changes: 6 additions & 2 deletions src/main/java/io/github/haykam821/consolebox/ConsoleBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
import io.github.haykam821.consolebox.resource.ConsoleGameManager;
import net.fabricmc.api.ModInitializer;
import net.minecraft.util.Identifier;
import xyz.nucleoid.plasmid.game.GameType;
import xyz.nucleoid.plasmid.api.game.GameType;

public class ConsoleBox implements ModInitializer {
public static final String MOD_ID = "consolebox";

private static final Identifier CONSOLE_BOX_ID = new Identifier(MOD_ID, "console_box");
private static final Identifier CONSOLE_BOX_ID = ConsoleBox.identifier("console_box");
public static final GameType<ConsoleBoxConfig> CONSOLE_BOX = GameType.register(CONSOLE_BOX_ID, ConsoleBoxConfig.CODEC, ConsoleBoxGame::open);

@Override
public void onInitialize() {
ConsoleGameManager.register();
}

public static Identifier identifier(String path) {
return Identifier.of(MOD_ID, path);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package io.github.haykam821.consolebox.game;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import io.github.haykam821.consolebox.resource.ConsoleGameManager;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.dynamic.Codecs;
import net.minecraft.util.math.Vec3d;
import xyz.nucleoid.plasmid.game.GameOpenException;
import xyz.nucleoid.plasmid.api.game.GameOpenException;

public record ConsoleBoxConfig(
Identifier game,
Expand All @@ -18,7 +19,7 @@ public record ConsoleBoxConfig(
) {
private static final Vec3d DEFAULT_SPECTATOR_SPAWN_OFFSET = new Vec3d(0, 2, 0);

public static final Codec<ConsoleBoxConfig> CODEC = RecordCodecBuilder.create(instance -> {
public static final MapCodec<ConsoleBoxConfig> CODEC = RecordCodecBuilder.mapCodec(instance -> {
return instance.group(
Identifier.CODEC.fieldOf("game").forGetter(ConsoleBoxConfig::game),
Codecs.VECTOR_3F.xmap(Vec3d::new, Vec3d::toVector3f).optionalFieldOf("spectator_spawn_offset", DEFAULT_SPECTATOR_SPAWN_OFFSET).forGetter(ConsoleBoxConfig::spectatorSpawnOffset),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,44 @@
import io.github.haykam821.consolebox.game.audio.BaseAudioController;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.SpawnReason;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.passive.MuleEntity;
import net.minecraft.entity.player.PlayerPosition;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.play.PlayerInputC2SPacket;
import net.minecraft.network.packet.s2c.play.GameStateChangeS2CPacket;
import net.minecraft.network.packet.s2c.play.PlayerPositionLookS2CPacket;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.network.packet.s2c.play.PositionFlag;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.PlayerInput;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.GameMode;
import xyz.nucleoid.fantasy.RuntimeWorldConfig;
import xyz.nucleoid.fantasy.util.VoidChunkGenerator;
import xyz.nucleoid.plasmid.game.*;
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.PlayerOfferResult;
import xyz.nucleoid.plasmid.game.rule.GameRuleType;
import xyz.nucleoid.plasmid.api.game.*;
import xyz.nucleoid.plasmid.api.game.common.PlayerLimiter;
import xyz.nucleoid.plasmid.api.game.common.config.PlayerLimiterConfig;
import xyz.nucleoid.plasmid.api.game.event.GameActivityEvents;
import xyz.nucleoid.plasmid.api.game.event.GamePlayerEvents;
import xyz.nucleoid.plasmid.api.game.player.JoinAcceptor;
import xyz.nucleoid.plasmid.api.game.player.JoinAcceptorResult;
import xyz.nucleoid.plasmid.api.game.rule.GameRuleType;
import xyz.nucleoid.stimuli.event.EventResult;
import xyz.nucleoid.stimuli.event.player.PlayerC2SPacketEvent;
import xyz.nucleoid.stimuli.event.player.PlayerDamageEvent;
import xyz.nucleoid.stimuli.event.player.PlayerDeathEvent;

import java.util.Set;

public class ConsoleBoxGame implements GamePlayerEvents.Add, GameActivityEvents.Destroy, GameActivityEvents.Tick, GameActivityEvents.Enable, GamePlayerEvents.Remove, GamePlayerEvents.Offer, PlayerDamageEvent, PlayerDeathEvent, PlayerC2SPacketEvent {
public class ConsoleBoxGame implements GamePlayerEvents.Add, GameActivityEvents.Destroy, GameActivityEvents.Tick, GameActivityEvents.Enable, GamePlayerEvents.Remove, GamePlayerEvents.Accept, PlayerDamageEvent, PlayerDeathEvent, PlayerC2SPacketEvent {
private final Thread thread;

private final GameSpace gameSpace;
Expand Down Expand Up @@ -86,7 +92,7 @@ public static GameOpenProcedure open(GameOpenContext<ConsoleBoxConfig> context)
ConsoleBoxConfig config = context.config();

RuntimeWorldConfig worldConfig = new RuntimeWorldConfig()
.setGenerator(new VoidChunkGenerator(context.server().getRegistryManager().get(RegistryKeys.BIOME)));
.setGenerator(new VoidChunkGenerator(context.server()));


var audioController = new BaseAudioController();
Expand All @@ -101,12 +107,14 @@ public static GameOpenProcedure open(GameOpenContext<ConsoleBoxConfig> context)
audioController.setOutput(phase);
ConsoleBoxGame.setRules(activity);

PlayerLimiter.addTo(activity, new PlayerLimiterConfig(phase.players.length));

// Listeners
activity.listen(GamePlayerEvents.ADD, phase);
activity.listen(GameActivityEvents.ENABLE, phase);
activity.listen(GameActivityEvents.DESTROY, phase);
activity.listen(GameActivityEvents.TICK, phase);
activity.listen(GamePlayerEvents.OFFER, phase);
activity.listen(GamePlayerEvents.ACCEPT, phase);
activity.listen(PlayerDamageEvent.EVENT, phase);
activity.listen(PlayerDeathEvent.EVENT, phase);
activity.listen(GamePlayerEvents.REMOVE, phase);
Expand All @@ -132,7 +140,7 @@ public void onDestroy(GameCloseReason reason) {
}

@Override
public ActionResult onPacket(ServerPlayerEntity player, Packet<?> packet) {
public EventResult onPacket(ServerPlayerEntity player, Packet<?> packet) {
int id = -1;
for (int i = 0; i < 4; i++) {
if (this.players[i] == player) {
Expand All @@ -142,27 +150,31 @@ public ActionResult onPacket(ServerPlayerEntity player, Packet<?> packet) {
}

if (id == -1) {
return ActionResult.PASS;
return EventResult.PASS;
}

if (packet instanceof PlayerInputC2SPacket playerInputC2SPacket) {
var isJumping = this.config.swapXZ() ? playerInputC2SPacket.isSneaking() : playerInputC2SPacket.isJumping();
var isSneaking = !this.config.swapXZ() ? playerInputC2SPacket.isSneaking() : playerInputC2SPacket.isJumping();
PlayerInput input = playerInputC2SPacket.input();

var isJumping = this.config.swapXZ() ? input.sneak() : input.jump();
var isSneaking = !this.config.swapXZ() ? input.sneak() : input.jump();

this.canvas.updateGamepad(id, playerInputC2SPacket.getSideways(), playerInputC2SPacket.getForward(),
this.canvas.updateGamepad(id, input.forward(), input.left(), input.backward(), input.right(),
isSneaking, isJumping);
}

return ActionResult.PASS;
return EventResult.PASS;
}

@Override
public void onTick() {
for (var player : this.players) {
if (player != null) {
PlayerPosition pos = new PlayerPosition(Vec3d.ZERO, Vec3d.ZERO, 180, 0);
Set<PositionFlag> flags = Set.of();

player.networkHandler.sendPacket(
new PlayerPositionLookS2CPacket(player.getX(), player.getY(), player.getZ(), 180f, 0f,
Set.of(), 0));
new PlayerPositionLookS2CPacket(0, pos, flags));
}
}
}
Expand Down Expand Up @@ -191,15 +203,15 @@ private void runThread() {

// /game open {type:"consolebox:console_box", game:"consolebox:cart"}
@Override
public PlayerOfferResult onOfferPlayer(PlayerOffer offer) {
public JoinAcceptorResult onAcceptPlayers(JoinAcceptor acceptor) {
Vec3d spawnPos = this.canvas.getSpawnPos();

if (this.playerCount < this.config.playerCount()) {
if (acceptor.intent().canPlay()) {
for (int i = 0; i < players.length; i++) {
final var x = i;
if (this.players[x] == null) {
return offer.accept(this.world, spawnPos).and(() -> {
this.players[x] = offer.player();
return acceptor.teleport(this.world, spawnPos).thenRunForEach(player -> {
this.players[x] = player;
this.playerCount++;

this.spawnMount(spawnPos, this.players[x]);
Expand All @@ -210,19 +222,19 @@ public PlayerOfferResult onOfferPlayer(PlayerOffer offer) {
}

Vec3d pos = spawnPos.add(this.config.spectatorSpawnOffset());
return offer.accept(this.world, pos).and(() -> {
this.initializePlayer(offer.player(), GameMode.SPECTATOR);
return acceptor.teleport(this.world, pos).thenRunForEach(player -> {
this.initializePlayer(player, GameMode.SPECTATOR);
});
}

@Override
public ActionResult onDamage(ServerPlayerEntity player, DamageSource source, float damage) {
return ActionResult.FAIL;
public EventResult onDamage(ServerPlayerEntity player, DamageSource source, float damage) {
return EventResult.DENY;
}

@Override
public ActionResult onDeath(ServerPlayerEntity player, DamageSource source) {
return ActionResult.FAIL;
public EventResult onDeath(ServerPlayerEntity player, DamageSource source) {
return EventResult.DENY;
}

@Override
Expand All @@ -240,7 +252,7 @@ public void onRemovePlayer(ServerPlayerEntity player) {
for (int i = 0; i < 4; i++) {
if (this.players[i] == player) {
this.players[i] = null;
this.canvas.updateGamepad(i, 0, 0, false, false);
this.canvas.updateGamepad(i, false, false, false, false, false, false);
this.playerCount--;
break;
}
Expand All @@ -250,7 +262,7 @@ public void onRemovePlayer(ServerPlayerEntity player) {

// Utilities
private Entity spawnMount(Vec3d playerPos, ServerPlayerEntity player) {
MuleEntity mount = EntityType.MULE.create(this.world);
MuleEntity mount = EntityType.MULE.create(this.world, SpawnReason.JOCKEY);
mount.calculateDimensions();
double y = playerPos.getY() - 1.25f;
mount.setPos(playerPos.getX(), y, playerPos.getZ());
Expand All @@ -266,7 +278,7 @@ private Entity spawnMount(Vec3d playerPos, ServerPlayerEntity player) {
mount.setInvisible(true);

// Remove mount hearts from HUD
mount.getAttributeInstance(EntityAttributes.GENERIC_MAX_HEALTH).setBaseValue(0);
mount.getAttributeInstance(EntityAttributes.MAX_HEALTH).setBaseValue(0);

this.world.spawnEntity(mount);
player.startRiding(mount, true);
Expand All @@ -285,7 +297,7 @@ private void initializePlayer(ServerPlayerEntity player, GameMode gameMode) {
player.setPitch(Float.MIN_VALUE);
}

private StatusEffectInstance createInfiniteStatusEffect(StatusEffect statusEffect) {
return new StatusEffectInstance(statusEffect, Integer.MAX_VALUE, 0, true, false);
private StatusEffectInstance createInfiniteStatusEffect(RegistryEntry<StatusEffect> statusEffect) {
return new StatusEffectInstance(statusEffect, StatusEffectInstance.INFINITE, 0, true, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,9 @@ public void render() {
}
}

public void updateGamepad(int id, float leftRight, float upDown, boolean isSneaking, boolean isJumping) {
public void updateGamepad(int id, boolean forward, boolean left, boolean backward, boolean right, boolean isSneaking, boolean isJumping) {
synchronized (this) {
this.memory.updateGamepad(id, leftRight, upDown, isSneaking, isJumping);
this.memory.updateGamepad(id, forward, left, backward, right, isSneaking, isJumping);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ public ByteBuffer readSprite(int start, int width, int height, int bit) {
return this.buffer.slice(start, width * height * bit);
}

public void updateGamepad(int id, float leftRight, float upDown, boolean isSneaking, boolean isJumping) {
public void updateGamepad(int id, boolean forward, boolean left, boolean backward, boolean right, boolean isSneaking, boolean isJumping) {
byte gamepad = 0;

if (isJumping) gamepad |= 1; // Z
if (isSneaking) gamepad |= 2; // X

if (leftRight > 0) gamepad |= 16; // Left
if (leftRight < 0) gamepad |= 32; // Right
if (upDown > 0) gamepad |= 64; // Up
if (upDown < 0) gamepad |= 128; // Down
if (left) gamepad |= 16; // Left
if (right) gamepad |= 32; // Right
if (forward) gamepad |= 64; // Up
if (backward) gamepad |= 128; // Down

this.buffer.put(GAMEPADS_ADDRESS + id, gamepad);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import net.minecraft.util.Identifier;

public class ConsoleGameManager implements SimpleSynchronousResourceReloadListener {
private static final Identifier ID = new Identifier(ConsoleBox.MOD_ID, "console_games");
private static final Identifier ID = ConsoleBox.identifier("console_games");
private static final Logger LOGGER = LoggerFactory.getLogger("ConsoleGameManager");

private static final String GAME_PREFIX = "console_games";
Expand Down Expand Up @@ -50,9 +50,10 @@ private boolean isGamePath(Identifier path) {

private Identifier parsePath(Identifier id) {
String prefix = GAME_PREFIX + "/";
String path = id.getPath().substring(prefix.length(), id.getPath().length() - GAME_EXTENSION.length());

return new Identifier(id.getNamespace(), path);
return id.withPath(path -> {
return path.substring(prefix.length(), path.length() - GAME_EXTENSION.length());
});
}

public static byte[] getGameData(Identifier id) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"license": "MIT",
"depends": {
"fabricloader": ">=0.4.0",
"java": ">=17",
"plasmid": ">=0.5.0"
"java": ">=21",
"plasmid": ">=0.6.0"
}
}
Loading