Skip to content

Commit

Permalink
compat with fabric loader 0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
plusls committed Oct 12, 2021
1 parent 95af0a2 commit 81b7b6a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx1G
# check these on https://fabricmc.net/use
minecraft_version=1.17.1
yarn_mappings=1.17.1+build.10
loader_version=0.11.6
loader_version=0.12.1
# Mod Properties
mod_version=0.3.9-alpha.114514
maven_group=com.plusls
Expand Down
42 changes: 31 additions & 11 deletions src/main/java/com/plusls/ommc/OmmcCompatMixinPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import com.plusls.ommc.util.YarnUtil;
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.fabricmc.loader.util.version.VersionPredicateParser;
import net.fabricmc.loader.impl.util.version.VersionPredicateParser;
import org.apache.commons.io.FileUtils;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Type;
Expand All @@ -33,6 +34,8 @@ public class OmmcCompatMixinPlugin extends OmmcMixinPlugin {
private final List<String> obfuscatedMixinList = new ArrayList<>();
static private Path tempDirectory;

static private Method oldMatchesMethod;

static {
if (!FabricLoader.getInstance().isDevelopmentEnvironment()) {
try {
Expand All @@ -42,6 +45,11 @@ public class OmmcCompatMixinPlugin extends OmmcMixinPlugin {
throw new IllegalStateException("Cannot create temp directory.");
}
}
try {
oldMatchesMethod = Class.forName("net.fabricmc.loader.util.version.VersionPredicateParser").getMethod("matches", Version.class, String.class);
} catch (ClassNotFoundException | NoSuchMethodException ignored) {
}

}
// private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

Expand Down Expand Up @@ -139,7 +147,12 @@ public void onLoad(String mixinPackage) {
Object urlLoader = Thread.currentThread().getContextClassLoader();
Class<?> knotClassLoader;
try {
knotClassLoader = Class.forName("net.fabricmc.loader.launch.knot.KnotClassLoader");
if (oldMatchesMethod == null) {
knotClassLoader = Class.forName("net.fabricmc.loader.impl.launch.knot.KnotClassLoader");
} else {
knotClassLoader = Class.forName("net.fabricmc.loader.launch.knot.KnotClassLoader");
}

} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new IllegalStateException("Cannot load class: net.fabricmc.loader.launch.knot.KnotClassLoader");
Expand Down Expand Up @@ -169,22 +182,29 @@ private void obfuscateClass(String classFullName) {
}
}


private boolean myMatches(Version version, String s) {
try {
if (oldMatchesMethod != null) {
return (boolean) oldMatchesMethod.invoke(null, version, s);
}
return VersionPredicateParser.parse(s).test(version);
} catch (VersionParsingException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
return false;
}
}

private boolean checkDependency(String targerClassName, AnnotationNode dependency) {
String modId = Annotations.getValue(dependency, "modId");
Optional<ModContainer> modContainerOptional = FabricLoader.getInstance().getModContainer(modId);
if (modContainerOptional.isPresent()) {
ModContainer modContainer = modContainerOptional.get();
List<String> versionList = Annotations.getValue(dependency, "version");
try {
for (String version : versionList) {
// not work in fabric-loader 0.12
if (!VersionPredicateParser.matches(modContainer.getMetadata().getVersion(), version)) {
return false;
}
for (String version : versionList) {
if (!myMatches(modContainer.getMetadata().getVersion(), version)) {
return false;
}
} catch (VersionParsingException e) {
e.printStackTrace();
throw new IllegalStateException(String.format("VersionParsingException, modid=%s, version=%s", modId, versionList));
}
ClassNode targetClassNode;
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.plusls.ommc.mixin.feature.betterSneaking;

import com.plusls.ommc.config.Configs;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.MovementType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.Vec3d;
Expand All @@ -12,13 +11,13 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(PlayerEntity.class)
public class MixinPlayerEntity {
public class MixinPlayerEntity {

final private static float MAX_STEP_HEIGHT = 1.25f;
final private static float DEFAULT_STEP_HEIGHT = 114514;
private float prevStepHeight = DEFAULT_STEP_HEIGHT;

@Inject(method = "adjustMovementForSneaking", at=@At(value = "FIELD", target = "Lnet/minecraft/util/math/Vec3d;x:D", opcode = Opcodes.GETFIELD, ordinal = 0))
@Inject(method = "adjustMovementForSneaking", at = @At(value = "FIELD", target = "Lnet/minecraft/util/math/Vec3d;x:D", opcode = Opcodes.GETFIELD, ordinal = 0))
private void setStepHeight(Vec3d movement, MovementType type, CallbackInfoReturnable<Vec3d> cir) {
if (!Configs.FeatureToggle.BETTER_SNEAKING.getBooleanValue()) {
return;
Expand All @@ -28,7 +27,7 @@ private void setStepHeight(Vec3d movement, MovementType type, CallbackInfoReturn
playerEntity.stepHeight = MAX_STEP_HEIGHT;
}

@Inject(method = "adjustMovementForSneaking", at=@At(value = "INVOKE", target = "Lnet/minecraft/util/math/Vec3d;<init>(DDD)V", ordinal = 0))
@Inject(method = "adjustMovementForSneaking", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/math/Vec3d;<init>(DDD)V", ordinal = 0))
private void restoreStepHeight(Vec3d movement, MovementType type, CallbackInfoReturnable<Vec3d> cir) {
if (!Configs.FeatureToggle.BETTER_SNEAKING.getBooleanValue() || Math.abs(prevStepHeight - DEFAULT_STEP_HEIGHT) <= 0.001) {
return;
Expand Down

0 comments on commit 81b7b6a

Please sign in to comment.