Skip to content

Commit

Permalink
Fix light updates not being sent sometimes
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Dec 15, 2023
1 parent 2323531 commit 113bfbd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import eu.pb4.polymer.common.impl.CommonNetworkHandlerExt;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.packet.s2c.common.ResourcePackSendS2CPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;

import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
Expand All @@ -33,6 +35,7 @@ public class AutoHost implements ModInitializer {
public static void init(MinecraftServer server) {
var config = CommonImpl.loadConfig("auto-host", AutoHostConfig.class);
AutoHost.config = config;
AutoHost.config.enabled |= Files.exists(FabricLoader.getInstance().getGameDir().resolve("polymer/.force_autohost"));

if (!config.enabled) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.google.gson.JsonPrimitive;
import com.google.gson.annotations.SerializedName;
import eu.pb4.polymer.common.impl.CommonImpl;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;


public class AutoHostConfig {
public String _c1 = "Enables Polymer's ResourcePack Auto Hosting";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
import eu.pb4.polymer.common.impl.compat.FloodGateUtils;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.packet.s2c.play.PlaySoundFromEntityS2CPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerCommonNetworkHandler;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;
Expand All @@ -29,7 +27,7 @@ private PolymerCommonUtils(){}

public static final SimpleEvent<ResourcePackChangeCallback> ON_RESOURCE_PACK_STATUS_CHANGE = new SimpleEvent<>();
private static Path cachedClientPath;
private final static String SAFE_CLIENT_SHA1 = "224c056f3cc6c6e1b5b90e63a0c621811f6a0681";
private final static String SAFE_CLIENT_SHA1 = "fd19469fed4a4b4c15b2d5133985f0e3e7816a8a";
private final static String SAFE_CLIENT_URL = "https://piston-data.mojang.com/v1/objects/" + SAFE_CLIENT_SHA1 + "/client.jar";
private static Path cachedClientJarRoot;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,54 +31,63 @@
@Mixin(ServerChunkManager.class)
public abstract class ServerChunkManagerMixin {

@Unique
private final Object2LongMap<ChunkSectionPos> polymer$lastUpdates = new Object2LongArrayMap<>();
@Shadow
@Final
public ThreadedAnvilChunkStorage threadedAnvilChunkStorage;

@Shadow
@Final
private ServerWorld world;
@Shadow
@Final
private ServerLightingProvider lightingProvider;

@Shadow
@Nullable
public abstract WorldChunk getWorldChunk(int chunkX, int chunkZ);

@Shadow @Final private ServerLightingProvider lightingProvider;

@Shadow public abstract ServerLightingProvider getLightingProvider();

@Unique
private final Object2LongMap<ChunkSectionPos> polymer$lastUpdates = new Object2LongArrayMap<>();
@Shadow
public abstract ServerLightingProvider getLightingProvider();

@Inject(method = "tickChunks", at = @At("TAIL"))
private void polymer$sendChunkUpdates(CallbackInfo ci) {
this.world.getServer().execute(() -> {
if (this.polymer$lastUpdates.size() != 0) {
var t = this.world.getServer().getTicks();
for (var entry : new ArrayList<>(this.polymer$lastUpdates.object2LongEntrySet())) {
var pos = entry.getKey();
var time = entry.getLongValue();
if (!this.polymer$lastUpdates.isEmpty()) {
var t = this.world.getServer().getTicks();
for (var entry : new ArrayList<>(this.polymer$lastUpdates.object2LongEntrySet())) {
var pos = entry.getKey();
var time = entry.getLongValue();

if (t - time > PolymerImpl.LIGHT_UPDATE_TICK_DELAY) {
BitSet bitSet = new BitSet();
int i = this.lightingProvider.getBottomY();
int j = this.lightingProvider.getTopY();
int y = pos.getSectionY();
if (y >= i && y <= j) {
bitSet.set(y - i);
}
if (t - time > PolymerImpl.LIGHT_UPDATE_TICK_DELAY) {
this.polymer$lastUpdates.removeLong(pos);

var chunk = this.getWorldChunk(pos.getX(), pos.getZ());
if (chunk == null) {
continue;
}
BitSet bitSet = new BitSet();
int i = this.lightingProvider.getBottomY();
int j = this.lightingProvider.getTopY();
int y = pos.getSectionY();
if (y >= i && y <= j) {
bitSet.set(y - i);
}

var section = chunk.getSection(chunk.sectionCoordToIndex(pos.getSectionY()));
if (section != null) {
((PolymerBlockPosStorage) section).polymer$setRequireLights(false);
}

List<ServerPlayerEntity> players = this.threadedAnvilChunkStorage.getPlayersWatchingChunk(pos.toChunkPos(), false);
if (!players.isEmpty()) {
Packet<?> packet = new LightUpdateS2CPacket(pos.toChunkPos(), this.getLightingProvider(), new BitSet(this.world.getTopSectionCoord() + 2), bitSet);
List<ServerPlayerEntity> players = this.threadedAnvilChunkStorage.getPlayersWatchingChunk(pos.toChunkPos(), false);
if (players.size() > 0) {
for (ServerPlayerEntity player : players) {
player.networkHandler.sendPacket(packet);
}
for (ServerPlayerEntity player : players) {
player.networkHandler.sendPacket(packet);
}
this.polymer$lastUpdates.removeLong(pos);
}
}
}
});
}
}

@Inject(method = "onLightUpdate", at = @At("TAIL"))
Expand All @@ -96,7 +105,6 @@ public abstract class ServerChunkManagerMixin {
var section = sections[i];
if (section != null && !section.isEmpty() && ((PolymerBlockPosStorage) section).polymer$requireLights()) {
this.polymer$lastUpdates.put(pos, this.world.getServer().getTicks());
((PolymerBlockPosStorage) section).polymer$setRequireLights(false);
return;
}
}
Expand Down

0 comments on commit 113bfbd

Please sign in to comment.