Adrenix 4 سال پیش
والد
کامیت
32718fde27

+ 6 - 0
README.md

@@ -9,6 +9,12 @@
 
 This mod brings back the old swinging animations before Minecraft Beta 1.8. This is an "eye candy" mod targeted towards nostalgic enthusiasts. As a bonus, this mod is also very configurable. All animation changes can be toggled. Moreover, you can customize the swing speeds of swords, tools, items, and everything in-between!
 
+## Version 2.0.1
+Minor update that fixes a crash and couple bugs.
+- The mod is now compatible with Optifine/Optiforge
+- Changed the custom swing "items" command parameter to "else" to make it more clear what the command is about to do.
+- Fixed a bug that would allow an out of range config value to be saved even after command rejection.
+
 ## Version 2.0.0
 Old swing has been updated to 1.16! Hooray!
 - Every change in the game is now configurable.

+ 18 - 5
build.gradle

@@ -1,25 +1,34 @@
 buildscript {
     repositories {
         maven { url = 'https://files.minecraftforge.net/maven' }
+        maven { url = 'https://repo.spongepowered.org/maven' }
         jcenter()
         mavenCentral()
     }
     dependencies {
         classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '3.+', changing: true
+        classpath 'org.spongepowered:mixingradle:0.7-SNAPSHOT'
     }
 }
+
 apply plugin: 'net.minecraftforge.gradle'
-// Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.
 apply plugin: 'eclipse'
 apply plugin: 'maven-publish'
+apply plugin: 'org.spongepowered.mixin'
+
 
-version = '2.0.0'
+version = '2.0.1'
 group = 'mod.adrenix.oldswing' // http://maven.apache.org/guides/mini/guide-naming-conventions.html
 archivesBaseName = 'oldswing'
 
 sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8' // Need this here so eclipse task generates correctly.
 
 println('Java: ' + System.getProperty('java.version') + ' JVM: ' + System.getProperty('java.vm.version') + '(' + System.getProperty('java.vendor') + ') Arch: ' + System.getProperty('os.arch'))
+
+mixin {
+    add sourceSets.main, "oldswing.refmap.json"
+}
+
 minecraft {
     // The mappings can be changed at any time, and must be in the following format.
     // snapshot_YYYYMMDD   Snapshot are built nightly.
@@ -36,6 +45,7 @@ minecraft {
     runs {
         client {
             workingDirectory project.file('run')
+            arg '-mixin.config=oldswing.mixins.json'
 
             // Recommended logging data for a userdev environment
             property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
@@ -118,18 +128,21 @@ jar {
         attributes([
             "Specification-Title": "oldswing",
             "Specification-Vendor": "mod.adrenix.oldswing",
-            "Specification-Version": "2.0.0", // We are version 1 of ourselves
+            "Specification-Version": "2.0.0",
             "Implementation-Title": project.name,
             "Implementation-Version": "${version}",
             "Implementation-Vendor" :"mod.adrenix.oldswing",
-            "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")
+            "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"),
+            "TweakClass": "org.spongepowered.asm.launch.MixinTweaker",
+            "MixinConfigs": "oldswing.mixins.json",
+            "FMLCorePluginContainsFMLMod": "true",
         ])
     }
 }
 
 // Example configuration to allow publishing using the maven-publish task
 // This is the preferred method to reobfuscate your jar file
-jar.finalizedBy('reobfJar') 
+jar.finalizedBy('reobfJar')
 // However if you are in a multi-project build, dev time needs unobfed jar files, so you can delay the obfuscation until publishing by doing
 //publish.dependsOn('reobfJar')
 

+ 9 - 7
src/main/java/mod/adrenix/oldswing/command/executors/Swings.java

@@ -26,9 +26,8 @@ public class Swings {
     private static final int MAX = ConfigHandler.MAX;
 
     static {
-        for (int i = MIN; i <= MAX; i++) {
+        for (int i = MIN; i <= MAX; i++)
             VALID_SPEEDS.add(Integer.toString(i));
-        }
     }
 
     private static final SuggestionProvider<CommandSource> SPEED_SUGGESTION = (context, builder) ->
@@ -36,11 +35,11 @@ public class Swings {
 
     public static LiteralArgumentBuilder<CommandSource> register() {
         return Commands.literal("swing")
-                .then(Commands.literal("items")
+                .then(Commands.literal("else")
                         .then(Commands.argument("speed", IntegerArgumentType.integer())
                                 .suggests(SPEED_SUGGESTION)
                                 .executes(context -> changeSwingSpeed(
-                                        context.getSource(), IntegerArgumentType.getInteger(context, "speed"), "items"
+                                        context.getSource(), IntegerArgumentType.getInteger(context, "speed"), "else"
                                 ))
                         )
                 )
@@ -83,12 +82,11 @@ public class Swings {
     }
 
     private static int changeSwingSpeed(CommandSource source, int speed, String on) {
-        if (speed < MIN || speed > MAX) {
+        if (speed < MIN || speed > MAX)
             return rangeError(source);
-        }
 
         switch (on) {
-            case "items":
+            case "else":
                 ClientConfig.swing_speed.set(speed);
                 break;
             case "swords":
@@ -111,6 +109,10 @@ public class Swings {
                 ColorUtil.format("6", TextFormatting.YELLOW)
         );
 
+        if (on.equals("else")) {
+            on = "everything else";
+        }
+
         final String info = String.format("%s\n%s\nSuccessfully changed %s swing speed to: %s.",
                 oldSwing, newSwing, ColorUtil.format(on, TextFormatting.GOLD), ColorUtil.format(Integer.toString(speed), TextFormatting.AQUA));
         source.sendFeedback(ITextComponent.func_244388_a(info), true);

+ 2 - 13
src/main/java/mod/adrenix/oldswing/config/TransformerHelper.java

@@ -2,7 +2,6 @@ package mod.adrenix.oldswing.config;
 
 import com.electronwill.nightconfig.core.Config;
 import com.mojang.blaze3d.matrix.MatrixStack;
-import net.minecraft.client.Minecraft;
 import net.minecraft.client.entity.player.ClientPlayerEntity;
 import net.minecraft.item.Item;
 import net.minecraft.item.ItemStack;
@@ -16,19 +15,11 @@ import net.minecraftforge.registries.ForgeRegistries;
 
 import javax.annotation.Nonnull;
 
-/* Transformer Helpers - Suppressed unused since these are called with ASM */
-@SuppressWarnings("unused")
 public class TransformerHelper {
-    @SuppressWarnings("unused")
-    public static int swingSpeed() {
+    public static int swingSpeed(ClientPlayerEntity player) {
         if (FMLLoader.getDist().isDedicatedServer() || !ConfigHandler.mod_enabled)
             return ConfigHandler.NEW_SPEED;
 
-        ClientPlayerEntity player = Minecraft.getInstance().player;
-        if (player == null) {
-            return ConfigHandler.swing_speed;
-        }
-
         Item item = player.getHeldItemMainhand().getItem();
         ResourceLocation source = ForgeRegistries.ITEMS.getKey(item);
 
@@ -47,12 +38,10 @@ public class TransformerHelper {
         return ConfigHandler.swing_speed;
     }
 
-    @SuppressWarnings("unused")
     public static float getCooldownAnimationFloat(ClientPlayerEntity player, float adjustTicks) {
         return ConfigHandler.prevent_cooldown && ConfigHandler.mod_enabled ? 1.0F : player.getCooledAttackStrength(adjustTicks);
     }
 
-    @SuppressWarnings("unused")
     public static boolean shouldCauseReequipAnimation(@Nonnull ItemStack from, @Nonnull ItemStack to, int slot) {
         if (!ConfigHandler.prevent_reequip || !ConfigHandler.mod_enabled) {
             return ForgeHooksClient.shouldCauseReequipAnimation(from, to, slot);
@@ -67,7 +56,7 @@ public class TransformerHelper {
         return !ItemStack.areItemsEqualIgnoreDurability(from, to);
     }
 
-    @SuppressWarnings("unused")
+    @SuppressWarnings("unused") /* This method is used by ASM */
     public static void armSway(MatrixStack matrixStackIn, Quaternion quaternion) {
         if (!ConfigHandler.prevent_sway || !ConfigHandler.mod_enabled) {
             matrixStackIn.rotate(quaternion);

+ 70 - 0
src/main/java/mod/adrenix/oldswing/mixin/FirstPersonRendererMixin.java

@@ -0,0 +1,70 @@
+package mod.adrenix.oldswing.mixin;
+
+import mod.adrenix.oldswing.config.TransformerHelper;
+import net.minecraft.client.Minecraft;
+import net.minecraft.client.entity.player.ClientPlayerEntity;
+import net.minecraft.client.renderer.FirstPersonRenderer;
+import net.minecraft.item.ItemStack;
+import net.minecraft.util.math.MathHelper;
+import org.spongepowered.asm.mixin.Final;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.Overwrite;
+import org.spongepowered.asm.mixin.Shadow;
+
+@Mixin(FirstPersonRenderer.class)
+public abstract class FirstPersonRendererMixin {
+    @Shadow private float prevEquippedProgressMainHand;
+    @Shadow private float equippedProgressMainHand;
+    @Shadow private float prevEquippedProgressOffHand;
+    @Shadow private float equippedProgressOffHand;
+    @Shadow private ItemStack itemStackMainHand;
+    @Shadow private ItemStack itemStackOffHand;
+    @Shadow @Final private Minecraft mc;
+
+    /**
+     * @author Adrenix
+     * @reason Allows for manipulation of equipment progress animations.
+     */
+    @Overwrite
+    public void tick() {
+        this.prevEquippedProgressMainHand = this.equippedProgressMainHand;
+        this.prevEquippedProgressOffHand = this.equippedProgressOffHand;
+
+        ClientPlayerEntity player = this.mc.player;
+        ItemStack itemStackMain = player.getHeldItemMainhand();
+        ItemStack itemStackOff = player.getHeldItemOffhand();
+
+        if (ItemStack.areItemStacksEqual(this.itemStackMainHand, itemStackMain)) {
+            this.itemStackMainHand = itemStackMain;
+        }
+
+        if (ItemStack.areItemStacksEqual(this.itemStackOffHand, itemStackOff)) {
+            this.itemStackOffHand = itemStackOff;
+        }
+
+        if (player.isRowingBoat()) {
+            this.equippedProgressMainHand = MathHelper.clamp(this.equippedProgressMainHand - 0.4F, 0.0F, 1.0F);
+            this.equippedProgressOffHand = MathHelper.clamp(this.equippedProgressOffHand - 0.4F, 0.0F, 1.0F);
+        } else {
+            float f = TransformerHelper.getCooldownAnimationFloat(player, 1.0F);
+            boolean reequipM = TransformerHelper.shouldCauseReequipAnimation(this.itemStackMainHand, itemStackMain, player.inventory.currentItem);
+            boolean reequipO = TransformerHelper.shouldCauseReequipAnimation(this.itemStackOffHand, itemStackOff, -1);
+
+            if (!reequipM && this.itemStackMainHand != itemStackMain)
+                this.itemStackMainHand = itemStackMain;
+            if (!reequipO && this.itemStackOffHand != itemStackOff)
+                this.itemStackOffHand = itemStackOff;
+
+            this.equippedProgressMainHand += MathHelper.clamp((!reequipM ? f * f * f : 0.0F) - this.equippedProgressMainHand, -0.4F, 0.4F);
+            this.equippedProgressOffHand += MathHelper.clamp((float)(!reequipO ? 1 : 0) - this.equippedProgressOffHand, -0.4F, 0.4F);
+        }
+
+        if (this.equippedProgressMainHand < 0.1F) {
+            this.itemStackMainHand = itemStackMain;
+        }
+
+        if (this.equippedProgressOffHand < 0.1F) {
+            this.itemStackOffHand = itemStackOff;
+        }
+    }
+}

+ 47 - 0
src/main/java/mod/adrenix/oldswing/mixin/LivingEntityMixin.java

@@ -0,0 +1,47 @@
+package mod.adrenix.oldswing.mixin;
+
+import mod.adrenix.oldswing.config.TransformerHelper;
+import net.minecraft.client.Minecraft;
+import net.minecraft.client.entity.player.ClientPlayerEntity;
+import net.minecraft.entity.Entity;
+import net.minecraft.entity.EntityType;
+import net.minecraft.entity.LivingEntity;
+import net.minecraft.potion.Effect;
+import net.minecraft.potion.EffectInstance;
+import net.minecraft.potion.EffectUtils;
+import net.minecraft.potion.Effects;
+import net.minecraft.world.World;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.Overwrite;
+import org.spongepowered.asm.mixin.Shadow;
+
+@Mixin(LivingEntity.class)
+public abstract class LivingEntityMixin extends Entity {
+    public LivingEntityMixin(EntityType<? extends LivingEntity> entityTypeIn, World worldIn) {
+        super(entityTypeIn, worldIn);
+    }
+
+    @Shadow public abstract boolean isPotionActive(Effect potionIn);
+    @Shadow public abstract EffectInstance getActivePotionEffect(Effect potionIn);
+
+    /**
+     * @author Adrenix
+     * @reason Allows for the manipulation of the speed during swinging animation.
+     */
+    @Overwrite
+    private int getArmSwingAnimationEnd() {
+        ClientPlayerEntity player = Minecraft.getInstance().player;
+        if (player == null)
+            return 0;
+
+        int mod = TransformerHelper.swingSpeed(player);
+
+        if (EffectUtils.hasMiningSpeedup(player)) {
+            return mod - (1 + EffectUtils.getMiningSpeedup(player));
+        } else {
+            return this.isPotionActive(Effects.MINING_FATIGUE) ?
+                    mod + (1 + this.getActivePotionEffect(Effects.MINING_FATIGUE).getAmplifier()) * 2 :
+                    mod;
+        }
+    }
+}

+ 1 - 1
src/main/resources/META-INF/mods.toml

@@ -27,5 +27,5 @@ logoFile="oldswing-logo.png" #optional
 authors="Adrenix" #optional
 # The description text for the mod (multi line!) (#mandatory)
 description='''
-Brings back the old swinging animations before Minecraft Beta 1.8.
+Brings back the old swinging animations before Minecraft Beta 1.8
 '''

+ 0 - 90
src/main/resources/oldswing-transformer.js

@@ -8,37 +8,6 @@ function initializeCoreMod() {
     /*Class*/ MethodInsnNode = Java.type("org.objectweb.asm.tree.MethodInsnNode");
 
     return {
-        "LivingEntity#getArmSwingAnimationEnd": {
-            "target": {
-                "type": "METHOD",
-                "class": "net.minecraft.entity.LivingEntity",
-                "methodName": "func_82166_i",
-                "methodDesc": "()I"
-            },
-            "transformer": function(method) {
-                debug("Running LivingEntity#getArmSwingAnimationEnd transformer...");
-
-                for (var i = 0; i < method.instructions.size(); i++) {
-                    var instruction = method.instructions.get(i);
-
-                    if (instruction.getOpcode() == Opcodes.BIPUSH) {
-                        if (instruction.operand == 6) {
-                            method.instructions.set(instruction, new MethodInsnNode(
-                                Opcodes.INVOKESTATIC,
-                                "mod/adrenix/oldswing/config/TransformerHelper",
-                                "swingSpeed",
-                                "()I"
-                            ));
-
-                            debug("Swapped an operand from 6 to TransformerHelper.swingSpeed method");
-                        }
-                    }
-                }
-
-                return method;
-            }
-        },
-
         "FirstPersonRenderer#renderItemInFirstPerson": {
             "target": {
                 "type": "METHOD",
@@ -78,65 +47,6 @@ function initializeCoreMod() {
                     }
                 }
 
-                return method;
-            }
-        },
-
-        "FirstPersonRenderer#tick": {
-            "target": {
-                "type": "METHOD",
-                "class": "net.minecraft.client.renderer.FirstPersonRenderer",
-                "methodName": "func_78441_a",
-                "methodDesc": "()V"
-            },
-            "transformer": function(method) {
-                debug("Running FirstPersonRenderer#tick transformer...");
-
-                var isFloatChanged = false;
-                var isReequipChangedMain = false;
-                var isReequipChangedOff = false;
-
-                for (var i = 0; i < method.instructions.size(); i++) {
-                    if (isFloatChanged && isReequipChangedMain && isReequipChangedOff) { break; }
-
-                    var instruction = method.instructions.get(i);
-                    if (!isFloatChanged && instruction.getOpcode() == Opcodes.ALOAD && instruction.var == 1) {
-                        if (instruction.getNext().getOpcode() == Opcodes.FCONST_1) {
-                            var instruction = instruction.getNext().getNext();
-                            if (instruction.getOpcode() == Opcodes.INVOKEVIRTUAL) {
-                                method.instructions.set(instruction, new MethodInsnNode(
-                                    Opcodes.INVOKESTATIC,
-                                    "mod/adrenix/oldswing/config/TransformerHelper",
-                                    "getCooldownAnimationFloat",
-                                    "(Lnet/minecraft/client/entity/player/ClientPlayerEntity;F)F",
-                                    false
-                                ));
-
-                                isFloatChanged = true;
-                                debug("Successfully reassigned getCooledAttackStrength to TransformerHelper getCooldownAnimationFloat");
-                            }
-                        }
-                    } else if (isFloatChanged && (instruction.getOpcode() == Opcodes.GETFIELD || instruction.getOpcode() == Opcodes.ICONST_M1)) {
-                        if (instruction.getNext().getOpcode() == Opcodes.INVOKESTATIC) {
-                            method.instructions.set(instruction.getNext(), new MethodInsnNode(
-                                Opcodes.INVOKESTATIC,
-                                "mod/adrenix/oldswing/config/TransformerHelper",
-                                "shouldCauseReequipAnimation",
-                                "(Lnet/minecraft/item/ItemStack;Lnet/minecraft/item/ItemStack;I)Z",
-                                false
-                            ));
-
-                            if (!isReequipChangedMain && !isReequipChangedOff) {
-                                isReequipChangedMain = true;
-                                debug("Successfully reassigned shouldCauseReequipAnimation to TransformerHelper for Main");
-                            } else if (!isReequipChangedOff) {
-                                isReequipChangedOff = true;
-                                debug("Successfully reassigned shouldCauseReequipAnimation to TransformerHelper for Off");
-                            }
-                        }
-                    }
-                }
-
                 return method;
             }
         }

+ 10 - 0
src/main/resources/oldswing.mixins.json

@@ -0,0 +1,10 @@
+{
+  "required": true,
+  "package": "mod.adrenix.oldswing.mixin",
+  "compatibilityLevel": "JAVA_8",
+  "refmap": "oldswing.refmap.json",
+  "client": [
+    "LivingEntityMixin",
+    "FirstPersonRendererMixin"
+  ]
+}