Skip to content

Commit

Permalink
Fixed support for ViaFabricPlus 3.1.0 and newer versions
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianMichael committed Mar 15, 2024
1 parent f20575f commit 7a1b843
Showing 1 changed file with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.Version;
import net.fabricmc.loader.api.VersionParsingException;
import net.minecraft.SharedConstants;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -40,10 +43,11 @@ public boolean doesItemExist(Item item) {
public static final MultiVersionCompat INSTANCE = Util.make(() -> {
try {
FabricLoader loader = FabricLoader.getInstance();
if (loader.isModLoaded("viafabric")) {
ModContainer modContainer;
if (loader.getModContainer("viafabric").orElse(null) != null) {
return new ViaFabric();
} else if (loader.isModLoaded("viafabricplus")) {
return new ViaFabricPlus();
} else if ((modContainer = loader.getModContainer("viafabricplus").orElse(null)) != null) {
return new ViaFabricPlus(modContainer);
} else {
return new None();
}
Expand Down Expand Up @@ -174,20 +178,41 @@ private int doGetProtocolVersion() throws ReflectiveOperationException {

private static final class ViaFabricPlus extends AbstractViaVersion {
private final Method getTargetVersion;
private final Method versionEnumGetProtocol;
private final Method itemRegistryDiffKeepItem;
private Method versionEnumGetProtocol;

private ViaFabricPlus() throws ReflectiveOperationException {
Class<?> protocolHack = Class.forName("de.florianmichael.viafabricplus.protocolhack.ProtocolHack");
private static final Version V3_0_6 = Util.make(() -> {
try {
return Version.parse("3.0.6");
} catch (VersionParsingException e) {
throw new AssertionError(e);
}
});

private ViaFabricPlus(ModContainer modContainer) throws ReflectiveOperationException {
final boolean newerThan3_0_6 = modContainer.getMetadata().getVersion().compareTo(V3_0_6) > 0;
String protocolTranslatorName;
if (newerThan3_0_6) {
protocolTranslatorName = "protocoltranslator.ProtocolTranslator";
} else {
protocolTranslatorName = "protocolhack.ProtocolHack";
}
Class<?> protocolTranslator = Class.forName("de.florianmichael.viafabricplus." + protocolTranslatorName);
Class<?> itemRegistryDiff = Class.forName("de.florianmichael.viafabricplus.fixes.data.ItemRegistryDiff");
getTargetVersion = protocolHack.getMethod("getTargetVersion");
versionEnumGetProtocol = getTargetVersion.getReturnType().getMethod("getProtocol");
getTargetVersion = protocolTranslator.getMethod("getTargetVersion");
itemRegistryDiffKeepItem = itemRegistryDiff.getMethod("keepItem", Item.class);
if (!newerThan3_0_6) {
versionEnumGetProtocol = getTargetVersion.getReturnType().getMethod("getProtocol");
}
}

@Override
protected Object getCurrentVersion() throws ReflectiveOperationException {
return versionEnumGetProtocol.invoke(getTargetVersion.invoke(null));
if (versionEnumGetProtocol != null) {
return versionEnumGetProtocol.invoke(getTargetVersion.invoke(null));
} else {
return getTargetVersion.invoke(null);
}
}

@Override
Expand Down

0 comments on commit 7a1b843

Please sign in to comment.