|
| 1 | +package de.teamlapen.werewolves.modcompat.playeranimator; |
| 2 | + |
| 3 | +import net.minecraft.client.Minecraft; |
| 4 | +import net.minecraft.world.entity.player.Player; |
| 5 | +import net.minecraftforge.fml.ModList; |
| 6 | + |
| 7 | +import java.lang.reflect.InvocationTargetException; |
| 8 | +import java.lang.reflect.Method; |
| 9 | + |
| 10 | +public class PlayerAnimatorCompat { |
| 11 | + |
| 12 | + private static boolean modChecked = false; |
| 13 | + private static boolean skipCheck; |
| 14 | + |
| 15 | + public static boolean checkAnimation(Player player) { |
| 16 | + if (!checkMod()) { |
| 17 | + return false; |
| 18 | + } |
| 19 | + if (Minecraft.getInstance().player != player) return false; |
| 20 | + return checkFirstPersonAnimation(player); |
| 21 | + } |
| 22 | + |
| 23 | + private static boolean checkMod() { |
| 24 | + if (!modChecked) { |
| 25 | + skipCheck = !ModList.get().isLoaded("playeranimator"); |
| 26 | + modChecked = false; |
| 27 | + } |
| 28 | + return !skipCheck; |
| 29 | + } |
| 30 | + |
| 31 | + private static boolean checkFirstPersonAnimation(Player player) { |
| 32 | + try { |
| 33 | + if (!isFirstPersonPass()) return false; |
| 34 | + return isAnimationActive(player); |
| 35 | + } catch (Exception e) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private static Method isFirstPersonPassMethod; |
| 41 | + |
| 42 | + private static boolean isFirstPersonPass() throws ReflectiveOperationException { |
| 43 | + if (isFirstPersonPassMethod == null) { |
| 44 | + isFirstPersonPassMethod = Class.forName("dev.kosmx.playerAnim.api.firstPerson.FirstPersonMode").getMethod("isFirstPersonPass"); |
| 45 | + } |
| 46 | + |
| 47 | + Object isFirstPersonPass = isFirstPersonPassMethod.invoke(null); |
| 48 | + return isFirstPersonPass != null && (boolean) isFirstPersonPass; |
| 49 | + } |
| 50 | + |
| 51 | + private static Class<?> iAnimatedPlayerClass; |
| 52 | + private static Method getAnimationApplierMethod; |
| 53 | + |
| 54 | + private static boolean isAnimationActive(Player player) throws ReflectiveOperationException { |
| 55 | + if (iAnimatedPlayerClass == null || getAnimationApplierMethod == null) { |
| 56 | + iAnimatedPlayerClass = Class.forName("dev.kosmx.playerAnim.impl.IAnimatedPlayer"); |
| 57 | + getAnimationApplierMethod = iAnimatedPlayerClass.getMethod("playerAnimator_getAnimation"); |
| 58 | + } |
| 59 | + if (!iAnimatedPlayerClass.isInstance(player)) return false; |
| 60 | + Object animationApplier = getAnimationApplierMethod.invoke(player); |
| 61 | + return (boolean) animationApplier.getClass().getMethod("isActive").invoke(animationApplier); |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments