1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-23 07:26:58 +00:00

Warn when Optifine is installed

We keep getting bug reports on 1.20.1 about an Optifine bug that causes
Forge's capabilities to not work (#1458). The cause of this bug is not
immediately visible to users, and can be very confusing when hit.

Optifine have not released a fix for this bug (despite it being reported
a year ago), and we continue to receive bug reports about it.

Nobody likes it when mods complain about other mods. So much Minecraft
drama can be traced back to this, and it's a slippery slope to go down.
I've tried to keep this as unobtrusive as possible — it's just a chat
message at world join, and it'll turn off if the bug is fixed.
This commit is contained in:
Jonathan Coates 2024-05-28 18:10:50 +01:00
parent 862d92785e
commit 0c9f9a8652
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
2 changed files with 91 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import dan200.computercraft.api.ComputerCraftAPI;
import dan200.computercraft.shared.command.CommandComputerCraft;
import dan200.computercraft.shared.computer.blocks.ComputerBlockEntity;
import dan200.computercraft.shared.config.Config;
import dan200.computercraft.shared.integration.Optifine;
import dan200.computercraft.shared.network.client.UpgradesLoadedMessage;
import dan200.computercraft.shared.network.server.ServerNetworking;
import dan200.computercraft.shared.peripheral.commandblock.CommandBlockPeripheral;
@ -29,6 +30,7 @@ import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraftforge.event.*;
import net.minecraftforge.event.entity.EntityJoinLevelEvent;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.level.ChunkEvent;
import net.minecraftforge.event.level.ChunkTicketLevelUpdatedEvent;
import net.minecraftforge.event.level.ChunkWatchEvent;
@ -61,6 +63,11 @@ public class ForgeCommonHooks {
CommonHooks.onServerStarting(event.getServer());
}
@SubscribeEvent
public static void onPlayerJoin(PlayerEvent.PlayerLoggedInEvent event) {
Optifine.warnAboutOptifine(event.getEntity()::sendSystemMessage);
}
@SubscribeEvent
public static void onServerStopped(ServerStoppedEvent event) {
CommonHooks.onServerStopped();

View File

@ -0,0 +1,84 @@
// SPDX-FileCopyrightText: 2024 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.shared.integration;
import dan200.computercraft.core.util.Nullability;
import dan200.computercraft.shared.Capabilities;
import dan200.computercraft.shared.ModRegistry;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.block.entity.BlockEntity;
import java.util.function.Consumer;
/**
* Detect whether Optifine is installed.
*/
public final class Optifine {
private static final boolean LOADED;
static {
boolean loaded;
try {
Class.forName("optifine.Installer", false, Optifine.class.getClassLoader());
loaded = true;
} catch (ReflectiveOperationException | LinkageError ignore) {
loaded = false;
}
LOADED = loaded;
}
private Optifine() {
}
public static boolean isLoaded() {
return LOADED;
}
/**
* This rant probably should be in a commit message, but sometimes I want it visible in the code.
* <p>
* There is a long-standing Optifine issue (<a href="https://github.com/sp614x/optifine/issues/7549">#7549</a>,
* <a href="https://github.com/sp614x/optifine/issues/7395">#7395</a>) that causes a block entity's
* {@link BlockEntity#gatherCapabilities()} not to be called. This causes peripherals to not show up for block
* entities.
* <p>
* While the bug is marked as fixed, there has not been a release since, and it continues to affect users a year
* after being reported. This is a frequent problem with Optifine (see also <a href="https://github.com/sp614x/optifine/issues/7127">#7127</a>
* and <a href="https://github.com/sp614x/optifine/issues/7261">#7261</a> which I <em>still</em> get bug reports
* about), and this doesn't seem likely to change any time soon.
* <p>
* The ideal situation would be that any time the game starts with Optifine, we delete it from the mods folder.
* That's probably a little uncouth (and a pain to get working on Windows), so instead we try to detect the bug in
* question and print a message in chat whenever a player joins.
*
* @param send The object to send a message too.
*/
public static void warnAboutOptifine(Consumer<Component> send) {
if (!Optifine.isLoaded()) return;
var computer = Nullability.assertNonNull(ModRegistry.BlockEntities.COMPUTER_NORMAL.get().create(
BlockPos.ZERO, ModRegistry.Blocks.COMPUTER_NORMAL.get().defaultBlockState()
));
if (computer.getCapability(Capabilities.CAPABILITY_PERIPHERAL).isPresent()) return;
send.accept(
Component.literal("")
.append(Component.literal("(CC: Tweaked) ").withStyle(ChatFormatting.GRAY))
.append(Component.literal("[WARNING] ").withStyle(ChatFormatting.RED))
.append("It looks like you're running Optifine. This has an ")
.append(Component.literal("unfixed issue").withStyle(s -> s
.withColor(ChatFormatting.BLUE).withUnderlined(true)
.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://github.com/sp614x/optifine/issues/7549"))
))
.append(" which causes issues with many modded block entities, including those added by CC: Tweaked. " +
"Please replace Optifine with an alternative shader mod such as Oculus.")
);
}
}