Files
block-restorer-ready
Code
21 972
block-restorer-ready/src/client/java/blockrestorer/modid/client/BlockRestorerClient.java
package blockrestorer.modid.client;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.level.chunk.LevelChunkSection;
import net.minecraft.world.level.chunk.PalettedContainer;
import org.lwjgl.glfw.GLFW;
import java.util.HashMap;
import java.util.Map;
/**
* Client-side "bugged stone" restorer.
*
* We keep snapshots of nearby chunk sections while they still contain real blocks.
* If a section later turns into only stone/air (what the bugged packets do), then while the
* keybind toggle is ON we put the snapshot blocks back into the CLIENT world (visual only).
* Toggling OFF puts the bugged state back.
*/
public class BlockRestorerClient implements ClientModInitializer {
private static final int RADIUS = 6; // chunks around the player to watch
private static final int SCAN_INTERVAL = 10; // ticks between scans
private record Key(int cx, int cz, int idx) {}
private static KeyMapping toggleKey;
private static boolean active = false;
private int tickCounter = 0;
private final Map<Key, PalettedContainer<BlockState>> good = new HashMap<>();
private final Map<Key, PalettedContainer<BlockState>> buggedCopy = new HashMap<>();
@Override
public void onInitializeClient() {
KeyMapping.Category category = KeyMapping.Category.register(Identifier.fromNamespaceAndPath("block-restorer", "main"));
toggleKey = KeyBindingHelper.registerKeyBinding(
new KeyMapping("key.block-restorer.toggle", GLFW.GLFW_KEY_B, category));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (toggleKey.consumeClick()) {
active = !active;
if (client.player != null) {
client.player.displayClientMessage(
Component.literal("Block Restorer: " + (active ? "ON" : "OFF")), true);
}
if (!active) {
undo(client);
}
reload(client);
}
if (client.level == null || client.player == null) {
good.clear();
buggedCopy.clear();
return;
}
if (++tickCounter % SCAN_INTERVAL != 0) return;
scan(client, client.level);
});
}
private static boolean isReal(BlockState s) {
return !s.isAir() && !s.is(Blocks.STONE);
}
private void scan(Minecraft client, ClientLevel level) {
int pcx = client.player.getBlockX() >> 4;
int pcz = client.player.getBlockZ() >> 4;
boolean changed = false;
for (int cx = pcx - RADIUS; cx <= pcx + RADIUS; cx++) {
for (int cz = pcz - RADIUS; cz <= pcz + RADIUS; cz++) {
ChunkAccess chunk = level.getChunk(cx, cz, ChunkStatus.FULL, false);
if (chunk == null) continue;
LevelChunkSection[] sections = chunk.getSections();
for (int i = 0; i < sections.length; i++) {
LevelChunkSection section = sections[i];
Key k = new Key(cx, cz, i);
boolean hasReal = section.maybeHas(BlockRestorerClient::isReal);
PalettedContainer<BlockState> snap = good.get(k);
if (hasReal) {
// Genuine data: keep a fresh snapshot (unless we are currently showing a restore)
if (!buggedCopy.containsKey(k)) {
good.put(k, section.getStates().copy());
}
} else if (snap != null && active) {
// Was real before, now only stone/air: bugged. Restore it.
if (!buggedCopy.containsKey(k)) {
buggedCopy.put(k, section.getStates().copy());
}
apply(section, snap);
changed = true;
}
}
}
}
if (changed) reload(client);
}
private void undo(Minecraft client) {
ClientLevel level = client.level;
if (level == null) {
buggedCopy.clear();
return;
}
for (Map.Entry<Key, PalettedContainer<BlockState>> e : buggedCopy.entrySet()) {
Key k = e.getKey();
ChunkAccess chunk = level.getChunk(k.cx(), k.cz(), ChunkStatus.FULL, false);
if (chunk == null) continue;
LevelChunkSection[] sections = chunk.getSections();
if (k.idx() < sections.length) apply(sections[k.idx()], e.getValue());
}
buggedCopy.clear();
}
private static void apply(LevelChunkSection section, PalettedContainer<BlockState> source) {
for (int x = 0; x < 16; x++)
for (int y = 0; y < 16; y++)
for (int z = 0; z < 16; z++)
section.setBlockState(x, y, z, source.get(x, y, z), false);
}
private static void reload(Minecraft client) {
if (client.levelRenderer != null) client.levelRenderer.allChanged();
}
}