Skip to content

Commit

Permalink
fully implement saving
Browse files Browse the repository at this point in the history
  • Loading branch information
sisby-folk committed Dec 22, 2023
1 parent 2c611c4 commit 1e0dae9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,20 @@ public void onInitializeClient() {

public static void groundStar(ClientWorld world, Star star) {
star.groundedTick = world.getTime();
reloadStars(world);
}

public static void freeStar(Star star) {
public static void freeStar(ClientWorld world, Star star) {
star.groundedTick = -1;
reloadStars(world);
}

public static void colorStar(PlayerEntity cause, Star star, int color) {
public static void colorStar(PlayerEntity cause, ClientWorld world, Star star, int color) {
star.color = color;
TextColor nameColor = cause.getDisplayName().getStyle().getColor();
star.editor = cause.getDisplayName().getString();
star.editorColor = nameColor != null ? nameColor.getRgb() : 0xFFFFFF;
reloadStars(world);
}

public static void reloadStars(ClientWorld world) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,39 @@ public static void init() {
}

private static void setInitialStarState(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) {
if (client.player != null && client.player.clientWorld instanceof StarcallerWorld scw) {
scw.starcaller$setSeed(buf.readLong());
scw.starcaller$setIterations(buf.readInt());
updateGrounded(client, handler, buf, responseSender);
updateColors(client, handler, buf, responseSender);
}
client.execute(() -> {
if (client.world instanceof StarcallerWorld scw) {
scw.starcaller$setSeed(buf.readLong());
scw.starcaller$setIterations(buf.readInt());
updateGrounded(client, handler, buf, responseSender);
updateColors(client, handler, buf, responseSender);
}
});
}

private static void updateGrounded(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) {
Map<Integer, Long> groundedMap = buf.readMap(PacketByteBuf::readInt, PacketByteBuf::readLong);
if (client.player != null && client.player.getWorld() instanceof StarcallerWorld scw) {
if (client.world instanceof StarcallerWorld scw) {
List<Star> stars = scw.starcaller$getStars();
groundedMap.forEach((index, groundedTick) -> {
if (index < stars.size()) {
stars.get(index).groundedTick = groundedTick;
}
});
StarcallerClient.reloadStars(client.player.clientWorld);
StarcallerClient.reloadStars(client.world);
}
}

private static void updateColors(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) {
Map<Integer, Integer> colorMap = buf.readMap(PacketByteBuf::readInt, PacketByteBuf::readInt);
if (client.player != null && client.player.getWorld() instanceof StarcallerWorld scw) {
if (client.world instanceof StarcallerWorld scw) {
List<Star> stars = scw.starcaller$getStars();
colorMap.forEach((index, color) -> {
if (index < stars.size()) {
stars.get(index).color = color;
}
});
StarcallerClient.reloadStars(client.player.clientWorld);
StarcallerClient.reloadStars(client.world);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public void fullBrightStarsWithSpear(float f, CallbackInfoReturnable<Float> cir)

@Override
public void starcaller$freeStar(PlayerEntity cause, Star star) {
StarcallerClient.freeStar(star);
StarcallerClient.freeStar(((ClientWorld) (Object) this), star);
}

@Override
public void starcaller$colorStar(PlayerEntity cause, Star star, int color) {
StarcallerClient.colorStar(cause, star, color);
StarcallerClient.colorStar(cause, ((ClientWorld) (Object) this), star, color);
}

@Override
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/folk/sisby/starcaller/StarcallerNetworking.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import net.fabricmc.fabric.api.entity.event.v1.ServerEntityWorldChangeEvents;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.PacketByteBuf;
Expand All @@ -21,7 +20,6 @@ public class StarcallerNetworking {
public static final Identifier S2C_UPDATE_COLORS = Starcaller.id("update_colors");

public static void init() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> sendInitialStarState(handler.player));
ServerEntityWorldChangeEvents.AFTER_PLAYER_CHANGE_WORLD.register((player, origin, destination) -> sendInitialStarState(player));
}

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/folk/sisby/starcaller/mixin/MixinPlayerManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package folk.sisby.starcaller.mixin;

import folk.sisby.starcaller.StarcallerNetworking;
import net.minecraft.network.ClientConnection;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ConnectedClientData;
import net.minecraft.server.network.ServerPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(PlayerManager.class)
public abstract class MixinPlayerManager {
@Inject(method = "onPlayerConnect", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/world/ServerWorld;onPlayerConnected(Lnet/minecraft/server/network/ServerPlayerEntity;)V", shift = At.Shift.AFTER))
public void syncStarState(ClientConnection connection, ServerPlayerEntity player, ConnectedClientData data, CallbackInfo ci) {
StarcallerNetworking.sendInitialStarState(player);
}
}
1 change: 1 addition & 0 deletions src/main/resources/starcaller.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"mixins": [
"MixinItemEntity",
"MixinPlayerManager",
"MixinServerWorld"
]
}

0 comments on commit 1e0dae9

Please sign in to comment.