diff --git a/src/main/java/com/jozufozu/flywheel/backend/Backend.java b/src/main/java/com/jozufozu/flywheel/backend/Backend.java index 51853e0ba..770e0734c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/Backend.java +++ b/src/main/java/com/jozufozu/flywheel/backend/Backend.java @@ -43,7 +43,6 @@ public class Backend { private Matrix4f projectionMatrix = new Matrix4f(); private boolean instancedArrays; private boolean enabled; - public boolean chunkCachingEnabled; private final List> contexts = new ArrayList<>(); private final Map> materialRegistry = new HashMap<>(); @@ -154,8 +153,6 @@ public class Backend { enabled = FlwConfig.get() .enabled() && !OptifineHandler.usingShaders(); - chunkCachingEnabled = FlwConfig.get() - .chunkCaching(); } public boolean canUseInstancing(@Nullable World world) { diff --git a/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java b/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java index 98f8701f2..d8ac42a72 100644 --- a/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java +++ b/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java @@ -8,6 +8,7 @@ import com.jozufozu.flywheel.backend.OptifineHandler; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.player.ClientPlayerEntity; +import net.minecraft.network.PacketBuffer; import net.minecraft.util.text.IFormattableTextComponent; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.StringTextComponent; @@ -18,7 +19,6 @@ import net.minecraftforge.api.distmarker.OnlyIn; public enum BooleanConfig { ENGINE(() -> BooleanConfig::enabled), NORMAL_OVERLAY(() -> BooleanConfig::normalOverlay), - CHUNK_CACHING(() -> BooleanConfig::chunkCaching), ; final Supplier> receiver; @@ -31,6 +31,27 @@ public enum BooleanConfig { return new SConfigureBooleanPacket(this, directive); } + /** + * Encode a variant of BooleanConfig. Symmetrical function to {@link #decode} + */ + public void encode(PacketBuffer buffer) { + buffer.writeByte(this.ordinal()); + } + + /** + * Safely decode a variant of BooleanConfig. Symmetrical function to {@link #encode} + */ + public static BooleanConfig decode(PacketBuffer buffer) { + byte t = buffer.readByte(); + BooleanConfig[] values = values(); + // Protects against version differences. + // Shouldn't ever happen but do a sanity check for safety. + if (t >= 0 && t < values.length) + return values[t]; + else + return null; + } + @OnlyIn(Dist.CLIENT) private static void enabled(BooleanDirective state) { ClientPlayerEntity player = Minecraft.getInstance().player; @@ -72,25 +93,6 @@ public enum BooleanConfig { player.displayClientMessage(text, false); } - @OnlyIn(Dist.CLIENT) - private static void chunkCaching(BooleanDirective state) { - ClientPlayerEntity player = Minecraft.getInstance().player; - if (player == null || state == null) return; - - if (state == BooleanDirective.DISPLAY) { - ITextComponent text = new StringTextComponent("Chunk caching is currently: ").append(boolToText(FlwConfig.get().client.chunkCaching.get())); - player.displayClientMessage(text, false); - return; - } - - FlwConfig.get().client.chunkCaching.set(state.get()); - - ITextComponent text = boolToText(FlwConfig.get().client.chunkCaching.get()).append(new StringTextComponent(" chunk caching").withStyle(TextFormatting.WHITE)); - - player.displayClientMessage(text, false); - Backend.reloadWorldRenderers(); - } - private static IFormattableTextComponent boolToText(boolean b) { return b ? new StringTextComponent("enabled").withStyle(TextFormatting.DARK_GREEN) : new StringTextComponent("disabled").withStyle(TextFormatting.RED); } diff --git a/src/main/java/com/jozufozu/flywheel/config/BooleanDirective.java b/src/main/java/com/jozufozu/flywheel/config/BooleanDirective.java index d0ff5e62d..ec0d18bee 100644 --- a/src/main/java/com/jozufozu/flywheel/config/BooleanDirective.java +++ b/src/main/java/com/jozufozu/flywheel/config/BooleanDirective.java @@ -1,5 +1,7 @@ package com.jozufozu.flywheel.config; +import net.minecraft.network.PacketBuffer; + public enum BooleanDirective { TRUE(true), FALSE(false), @@ -16,7 +18,21 @@ public enum BooleanDirective { } public boolean get() { - if (this == DISPLAY) throw new IllegalStateException("Cannot get value from DISPLAY directive"); + if (this == DISPLAY) throw new IllegalStateException("DISPLAY directive has no value"); return b; } + + /** + * Encode a variant of BooleanDirective. Symmetrical function to {@link #decode} + */ + public void encode(PacketBuffer buffer) { + buffer.writeByte(this.ordinal()); + } + + /** + * Safely decode a variant of BooleanDirective. Symmetrical function to {@link #encode} + */ + public static BooleanDirective decode(PacketBuffer buffer) { + return values()[buffer.readByte()]; + } } diff --git a/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java b/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java index a601971cb..7775827db 100644 --- a/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java +++ b/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java @@ -17,7 +17,6 @@ public class FlwCommands { dispatcher.register(Commands.literal("flywheel") .then(new BooleanConfigCommand("backend", BooleanConfig.ENGINE).register()) .then(new BooleanConfigCommand("debugNormals", BooleanConfig.NORMAL_OVERLAY).register()) - .then(new BooleanConfigCommand("chunkCaching", BooleanConfig.CHUNK_CACHING).register()) ); } } diff --git a/src/main/java/com/jozufozu/flywheel/config/FlwConfig.java b/src/main/java/com/jozufozu/flywheel/config/FlwConfig.java index 359d7111e..0fd5a2673 100644 --- a/src/main/java/com/jozufozu/flywheel/config/FlwConfig.java +++ b/src/main/java/com/jozufozu/flywheel/config/FlwConfig.java @@ -34,17 +34,12 @@ public class FlwConfig { return client.debugNormals.get(); } - public boolean chunkCaching() { - return client.chunkCaching.get(); - } - public static void init() { } public static class ClientConfig { public final BooleanValue enabled; public final BooleanValue debugNormals; - public final BooleanValue chunkCaching; public ClientConfig(ForgeConfigSpec.Builder builder) { @@ -53,9 +48,6 @@ public class FlwConfig { debugNormals = builder.comment("Enable or disable a debug overlay that colors pixels by their normal") .define("debugNormals", false); - - chunkCaching = builder.comment("Cache chunk lookups to improve performance.") - .define("chunkCaching", true); } } } diff --git a/src/main/java/com/jozufozu/flywheel/config/SConfigureBooleanPacket.java b/src/main/java/com/jozufozu/flywheel/config/SConfigureBooleanPacket.java index 6d2c97394..cc6d6c429 100644 --- a/src/main/java/com/jozufozu/flywheel/config/SConfigureBooleanPacket.java +++ b/src/main/java/com/jozufozu/flywheel/config/SConfigureBooleanPacket.java @@ -19,18 +19,20 @@ public class SConfigureBooleanPacket { } public SConfigureBooleanPacket(PacketBuffer buffer) { - target = BooleanConfig.values()[buffer.readByte()]; - directive = BooleanDirective.values()[buffer.readByte()]; + target = BooleanConfig.decode(buffer); + directive = BooleanDirective.decode(buffer); } public void encode(PacketBuffer buffer) { - buffer.writeByte(target.ordinal()); - buffer.writeByte(directive.ordinal()); + target.encode(buffer); + directive.encode(buffer); } public void execute(Supplier ctx) { - target.receiver.get() - .accept(directive); + if (directive != null) { + target.receiver.get() + .accept(directive); + } ctx.get() .setPacketHandled(true); } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/FastChunkProviderMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/FastChunkProviderMixin.java deleted file mode 100644 index 28301ea18..000000000 --- a/src/main/java/com/jozufozu/flywheel/mixin/FastChunkProviderMixin.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.jozufozu.flywheel.mixin; - -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.Redirect; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import com.jozufozu.flywheel.backend.Backend; - -import net.minecraft.client.multiplayer.ClientChunkProvider; -import net.minecraft.client.world.ClientWorld; -import net.minecraft.nbt.CompoundNBT; -import net.minecraft.network.PacketBuffer; -import net.minecraft.world.biome.BiomeContainer; -import net.minecraft.world.chunk.AbstractChunkProvider; -import net.minecraft.world.chunk.Chunk; -import net.minecraft.world.chunk.ChunkStatus; -import net.minecraft.world.chunk.IChunk; - -@Mixin(ClientChunkProvider.class) -public abstract class FastChunkProviderMixin extends AbstractChunkProvider { - - @Shadow - @Final - private ClientWorld level; - @Unique - private int lastX; - @Unique - private int lastZ; - - @Unique - private IChunk lastChunk; - - @Inject(method = "getChunk", - at = @At("HEAD"), - cancellable = true) - public void returnCachedChunk(int x, int z, ChunkStatus status, boolean create, CallbackInfoReturnable cir) { - if (Backend.getInstance().chunkCachingEnabled && status.isOrAfter(ChunkStatus.FULL)) { - synchronized (level) { - if (lastChunk != null && x == lastX && z == lastZ) { - cir.setReturnValue(lastChunk); - } - } - } - } - - @Inject(method = "getChunk", - at = @At("RETURN")) - public void cacheChunk(int x, int z, ChunkStatus status, boolean create, CallbackInfoReturnable cir) { - if (Backend.getInstance().chunkCachingEnabled && status.isOrAfter(ChunkStatus.FULL)) { - synchronized (level) { - lastChunk = cir.getReturnValue(); - lastX = x; - lastZ = z; - } - } - } - - @Inject(method = "drop", at = @At("HEAD")) - public void invalidateOnDrop(int x, int z, CallbackInfo ci) { - if (Backend.getInstance().chunkCachingEnabled) { - synchronized (level) { - if (x == lastX && z == lastZ) lastChunk = null; - } - } - } - - @Inject(method = "replaceWithPacketData", at = @At("HEAD")) - public void invalidateOnPacket(int x, int z, BiomeContainer p_228313_3_, PacketBuffer p_228313_4_, CompoundNBT p_228313_5_, int p_228313_6_, boolean p_228313_7_, CallbackInfoReturnable cir) { - if (Backend.getInstance().chunkCachingEnabled) { - synchronized (level) { - if (x == lastX && z == lastZ) lastChunk = null; - } - } - } - - @Redirect(method = "isTickingChunk", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/multiplayer/ClientChunkProvider;hasChunk(II)Z")) - public boolean redirectTicking(ClientChunkProvider clientChunkProvider, int x, int z) { - if (Backend.getInstance().chunkCachingEnabled) { - synchronized (level) { - if (lastChunk != null && x == lastX && z == lastZ) return true; - } - } - - return clientChunkProvider.hasChunk(x, z); - } -} diff --git a/src/main/resources/flywheel.mixins.json b/src/main/resources/flywheel.mixins.json index 13b737f6f..ac826f953 100644 --- a/src/main/resources/flywheel.mixins.json +++ b/src/main/resources/flywheel.mixins.json @@ -4,7 +4,7 @@ "package": "com.jozufozu.flywheel.mixin", "compatibilityLevel": "JAVA_8", "refmap": "flywheel.refmap.json", - "client": ["BeginFrameMixin", "CancelEntityRenderMixin", "CancelTileEntityRenderMixin", "FastChunkProviderMixin", "FixFabulousDepthMixin", "FogColorTrackerMixin", "RenderHooksMixin", "ShaderCloseMixin", "StoreProjectionMatrixMixin", "TileRemoveMixin", "TileWorldHookMixin", "atlas.AtlasDataMixin", "atlas.SheetDataAccessor", "light.LightUpdateMixin", "light.NetworkLightUpdateMixin"], + "client": ["BeginFrameMixin", "CancelEntityRenderMixin", "CancelTileEntityRenderMixin", "FixFabulousDepthMixin", "FogColorTrackerMixin", "RenderHooksMixin", "ShaderCloseMixin", "StoreProjectionMatrixMixin", "TileRemoveMixin", "TileWorldHookMixin", "atlas.AtlasDataMixin", "atlas.SheetDataAccessor", "light.LightUpdateMixin", "light.NetworkLightUpdateMixin"], "injectors": { "defaultRequire": 0 }