Skip to content

Commit

Permalink
Allow to disable explosion blocks to limit desyncs
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jul 24, 2023
1 parent 663d94c commit 76247cf
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fabric_version=0.83.0+1.20.1

maven_group = eu.pb4

mod_version = 0.5.5
mod_version = 0.5.6-dev

minecraft_version_supported = ">=1.20-"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import eu.pb4.polymer.blocks.impl.PolymerBlocksInternal;
import eu.pb4.polymer.common.impl.CompatStatus;
import eu.pb4.polymer.core.api.block.BlockMapper;
import eu.pb4.polymer.core.api.block.PolymerBlockUtils;
import eu.pb4.polymer.core.impl.PolymerImpl;
import eu.pb4.polymer.resourcepack.api.ResourcePackCreator;
import eu.pb4.polymer.resourcepack.impl.generation.DefaultRPBuilder;
Expand Down Expand Up @@ -51,6 +52,7 @@ public BlockMapper getBlockMapper() {

protected void registerEvent() {
if (!this.registered) {
PolymerBlockUtils.requireStrictBlockUpdates();
creator.creationEvent.register((b) -> {
if (b instanceof DefaultRPBuilder defaultRPBuilder) {
defaultRPBuilder.buildEvent.register((c) -> this.generateResources(defaultRPBuilder, c));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import eu.pb4.polymer.common.api.events.BooleanEvent;
import eu.pb4.polymer.common.api.events.SimpleEvent;
import eu.pb4.polymer.core.api.utils.PolymerUtils;
import eu.pb4.polymer.core.impl.interfaces.BlockStateExtra;
import eu.pb4.polymer.core.mixin.block.BlockEntityUpdateS2CPacketAccessor;
import eu.pb4.polymer.rsm.api.RegistrySyncUtils;
Expand All @@ -18,6 +17,7 @@
import net.minecraft.util.Util;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkSectionPos;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
Expand Down Expand Up @@ -46,6 +46,8 @@ private PolymerBlockUtils() {
public static final BooleanEvent<BiPredicate<ServerWorld, ChunkSectionPos>> SEND_LIGHT_UPDATE_PACKET = new BooleanEvent<>();
private static final Set<BlockEntityType<?>> BLOCK_ENTITY_TYPES = new ObjectOpenCustomHashSet<>(Util.identityHashStrategy());

private static boolean requireStrictBlockUpdates = false;

/**
* Marks BlockEntity type as server-side only
*
Expand Down Expand Up @@ -248,6 +250,15 @@ public static BlockEntityUpdateS2CPacket createBlockEntityPacket(BlockPos pos, B
return BlockEntityUpdateS2CPacketAccessor.createBlockEntityUpdateS2CPacket(pos.toImmutable(), type, nbtCompound);
}

@ApiStatus.Experimental
public static void requireStrictBlockUpdates() {
requireStrictBlockUpdates = true;
}

public static boolean isStrictBlockUpdateRequired() {
return requireStrictBlockUpdates;
}

@FunctionalInterface
public interface MineEventListener {
boolean onBlockMine(BlockState state, BlockPos pos, ServerPlayerEntity player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import eu.pb4.polymer.core.mixin.block.PalettedContainerAccessor;
import eu.pb4.polymer.networking.impl.NetworkHandlerExtension;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.command.CommandSource;
import net.minecraft.command.argument.IdentifierArgumentType;
Expand Down Expand Up @@ -163,7 +164,7 @@ public static void registerDev(LiteralArgumentBuilder<ServerCommandSource> dev)
var chunk = ctx.getSource().getWorld().getChunk(ctx.getSource().getPlayer().getBlockPos());
var s = chunk.getSection(ctx.getSource().getWorld().getSectionIndex(ctx.getSource().getPlayer().getBlockY()));

var a = ((PalettedContainerAccessor) s.getBlockStateContainer()).getData();
var a = ((PalettedContainerAccessor<BlockState>) s.getBlockStateContainer()).getData();

ctx.getSource().sendFeedback(() -> Text.literal("Chunk: " + chunk.getPos() + " Palette: " + a.palette() + " | " + " Storage: " + a.storage() + " | Bits: " + a.storage().getElementBits()), false);
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public final class PolymerImpl {
public static final boolean USE_FULL_REI_COMPAT_CLIENT = true;
public static final boolean LOG_MORE_ERRORS;
public static final int LIGHT_UPDATE_TICK_DELAY;
public static final boolean FORCE_STRICT_UPDATES;

static {
var serverConfig = CommonImpl.loadConfig("server", ServerConfig.class);
Expand All @@ -37,6 +38,7 @@ public final class PolymerImpl {
LOG_MORE_ERRORS = CommonImpl.LOG_MORE_ERRORS;
SYNC_MODDED_ENTRIES_POLYMC = serverConfig.polyMcSyncModdedEntries && CompatStatus.POLYMC;
LIGHT_UPDATE_TICK_DELAY = serverConfig.lightUpdateTickDelay;
FORCE_STRICT_UPDATES = serverConfig.forceStrictUpdates;


if (PolymerImpl.IS_CLIENT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import eu.pb4.polymer.common.api.PolymerCommonUtils;
import eu.pb4.polymer.common.impl.CommonImplUtils;
import eu.pb4.polymer.common.impl.CompatStatus;
import eu.pb4.polymer.core.api.block.PolymerBlockUtils;
import eu.pb4.polymer.core.api.utils.PolymerUtils;
import eu.pb4.polymer.core.impl.client.compat.FabricFluids;
import eu.pb4.polymer.core.impl.client.compat.ReiCompatibility;
Expand All @@ -26,8 +27,11 @@ public void onInitialize() {
PolymerUtils.reloadWorld(handler.player);
}
}));
}

if (PolymerImpl.FORCE_STRICT_UPDATES) {
PolymerBlockUtils.requireStrictBlockUpdates();
}
}
@Override
public void onInitializeClient() {
PolymerClientProtocolHandler.register();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package eu.pb4.polymer.core.impl;

import com.google.gson.annotations.SerializedName;

public class ServerConfig {
public String _c7 = "Displays vanilla/modded creatives tabs in /polymer creative";
public boolean displayNonPolymerCreativeTabs = true;
Expand All @@ -13,4 +15,7 @@ public class ServerConfig {
public boolean polyMcSyncModdedEntries = true;
public String _c2 = "Delay from last light updates to syncing it to clients, in ticks";
public int lightUpdateTickDelay = 1;
public String _c3 = "Forcefully enables strict block updates, making client desyncs less likely to happen";
@SerializedName("force_strict_block_updates")
public boolean forceStrictUpdates = false;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.pb4.polymer.core.impl.networking;

import eu.pb4.polymer.common.impl.CompatStatus;
import eu.pb4.polymer.core.api.block.PolymerBlockUtils;
import eu.pb4.polymer.core.api.entity.PolymerEntity;
import eu.pb4.polymer.core.api.other.PolymerSoundEvent;
import eu.pb4.polymer.core.api.other.PolymerStatusEffect;
Expand All @@ -13,10 +14,7 @@
import net.minecraft.entity.Entity;
import net.minecraft.network.listener.ClientPlayPacketListener;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.s2c.play.BundleS2CPacket;
import net.minecraft.network.packet.s2c.play.EntityEquipmentUpdateS2CPacket;
import net.minecraft.network.packet.s2c.play.FeaturesS2CPacket;
import net.minecraft.network.packet.s2c.play.PlaySoundS2CPacket;
import net.minecraft.network.packet.s2c.play.*;
import net.minecraft.resource.featuretoggle.FeatureFlag;
import net.minecraft.resource.featuretoggle.FeatureFlags;
import net.minecraft.resource.featuretoggle.FeatureManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

import net.minecraft.world.chunk.PalettedContainer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.gen.Accessor;
import org.spongepowered.asm.mixin.gen.Invoker;

@Mixin(PalettedContainer.class)
public interface PalettedContainerAccessor {
public interface PalettedContainerAccessor<T> {
@Accessor
PalettedContainer.Data<?> getData();
PalettedContainer.Data<T> getData();

@Accessor
PalettedContainer.PaletteProvider getPaletteProvider();

@Invoker
void callSet(int index, T value);

@Invoker
T callGet(int index);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package eu.pb4.polymer.core.mixin.block.packet;

import eu.pb4.polymer.core.api.block.PolymerBlockUtils;
import net.minecraft.network.packet.s2c.play.ExplosionS2CPacket;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;

import java.util.List;

@Mixin(ExplosionS2CPacket.class)
public class ExplosionS2CPacketMixin {
@ModifyArg(method = "<init>(DDDFLjava/util/List;Lnet/minecraft/util/math/Vec3d;)V", at = @At(value = "INVOKE", target = "Lcom/google/common/collect/Lists;newArrayList(Ljava/lang/Iterable;)Ljava/util/ArrayList;"))
private Iterable<?> polymerCore$removeBlocks(Iterable<?> elements) {
return PolymerBlockUtils.isStrictBlockUpdateRequired() ? List.of() : elements;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public abstract class ServerPlayNetworkHandlerMixin implements PolymerNetworkHan
private ArrayList<ScheduledPacket> polymer$scheduledPackets = new ArrayList<>();
@Unique
private BlockMapper polymer$blockMapper;
private List<Runnable> polymer$afterSequence = new ArrayList<>();
private final List<Runnable> polymer$afterSequence = new ArrayList<>();

@Shadow
public abstract void sendPacket(Packet<?> packet);
Expand All @@ -60,8 +60,6 @@ public abstract class ServerPlayNetworkHandlerMixin implements PolymerNetworkHan

@Shadow private int sequence;

@Shadow protected abstract ParseResults<ServerCommandSource> parse(String command);

@Inject(method = "<init>", at = @At("TAIL"))
private void polymer$setupInitial(MinecraftServer server, ClientConnection connection, ServerPlayerEntity player, CallbackInfo ci) {
this.polymer$blockMapper = BlockMapper.getDefault(player);
Expand Down
1 change: 1 addition & 0 deletions polymer-core/src/main/resources/polymer-core.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"block.packet.ChunkDataS2CPacketMixin",
"block.packet.ChunkDeltaUpdateS2CPacketAccessor",
"block.packet.ChunkDeltaUpdateS2CPacketMixin",
"block.packet.ExplosionS2CPacketMixin",
"block.packet.PalettedContainerDataMixin",
"block.packet.ParticleS2CPacketMixin",
"block.packet.ThreadedAnvilChunkStorageAccessor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.ArrayList;
import java.util.List;

public final class SimpleDataTracker implements DataTrackerLike {
public class SimpleDataTracker implements DataTrackerLike {
private final Entry<?>[] entries;
private boolean dirty;

Expand Down

0 comments on commit 76247cf

Please sign in to comment.