generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from valoeghese/master
fuck fuck fuck fuck fuck
- Loading branch information
Showing
11 changed files
with
314 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/io/github/fabriccommunity/events/impl/ChickenTurnLeftImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.github.fabriccommunity.events.impl; | ||
|
||
import io.github.fabriccommunity.events.world.ChickenTurnLeftCallback; | ||
import net.minecraft.entity.passive.ChickenEntity; | ||
import net.minecraft.util.ActionResult; | ||
import net.minecraft.util.math.MathHelper; | ||
|
||
/** | ||
* @author leocth | ||
*/ | ||
public final class ChickenTurnLeftImpl { | ||
private ChickenTurnLeftImpl() { | ||
|
||
} | ||
|
||
public static float hackChangeAngle(ChickenEntity entity, float headYaw, float targetYaw, float yawSpeed, ChickenTurnLeftCallback.TurnLeftType type) { | ||
float f = MathHelper.subtractAngles(headYaw, targetYaw); | ||
float g = MathHelper.clamp(f, -yawSpeed, yawSpeed); | ||
float h = headYaw + g; | ||
if (MathHelper.wrapDegrees(g) < 0f) { | ||
ActionResult result = ChickenTurnLeftCallback.EVENT.invoker().onTurnLeft(entity, headYaw, targetYaw, yawSpeed, type); | ||
|
||
if (result == ActionResult.FAIL) { | ||
// trolololololo lololooooo lo lo loo loooo lo lo | ||
h = headYaw - g; | ||
} | ||
} | ||
return MathHelper.wrapDegrees(h); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/io/github/fabriccommunity/events/mixin/client/MixinTextFieldWidget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.github.fabriccommunity.events.mixin.client; | ||
|
||
import io.github.fabriccommunity.events.play.ClientPlayerInteractionEvents; | ||
import net.minecraft.client.gui.widget.TextFieldWidget; | ||
import net.minecraft.util.ActionResult; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
/** | ||
* @author leocth | ||
*/ | ||
@Mixin(TextFieldWidget.class) | ||
public class MixinTextFieldWidget { | ||
@Inject(at = @At("HEAD"), method = "charTyped", cancellable = true) | ||
public void charTyped(char chr, int keyCode, CallbackInfoReturnable<Boolean> cir) { | ||
ActionResult result = ClientPlayerInteractionEvents.TYPE_LETTER.invoker().onType(chr, keyCode); | ||
if (result == ActionResult.FAIL) { | ||
cir.setReturnValue(false); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/io/github/fabriccommunity/events/mixin/world/MixinLookControl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.github.fabriccommunity.events.mixin.world; | ||
|
||
import io.github.fabriccommunity.events.impl.ChickenTurnLeftImpl; | ||
import io.github.fabriccommunity.events.world.ChickenTurnLeftCallback; | ||
import net.minecraft.entity.ai.control.LookControl; | ||
import net.minecraft.entity.mob.MobEntity; | ||
import net.minecraft.entity.passive.ChickenEntity; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Redirect; | ||
|
||
/** | ||
* @author leocth | ||
*/ | ||
@Mixin(LookControl.class) | ||
public abstract class MixinLookControl { | ||
|
||
@Shadow abstract float changeAngle(float from, float to, float max); | ||
|
||
@Redirect( | ||
at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/ai/control/LookControl;changeAngle(FFF)F", ordinal = 0), | ||
method = "tick" | ||
) | ||
private float redirectHeadChangeAngle(LookControl self, float headYaw, float targetYaw, float yawSpeed) { | ||
return (entity instanceof ChickenEntity) | ||
? ChickenTurnLeftImpl.hackChangeAngle((ChickenEntity) entity, headYaw, targetYaw, yawSpeed, ChickenTurnLeftCallback.TurnLeftType.HEAD) | ||
: this.changeAngle(headYaw, targetYaw, yawSpeed); | ||
} | ||
|
||
@Redirect( | ||
at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/ai/control/LookControl;changeAngle(FFF)F", ordinal = 1), | ||
method = "tick" | ||
) | ||
private float redirectBodyChangeAngle(LookControl self, float headYaw, float targetYaw, float yawSpeed) { | ||
return (entity instanceof ChickenEntity) | ||
? ChickenTurnLeftImpl.hackChangeAngle((ChickenEntity) entity, headYaw, targetYaw, yawSpeed, ChickenTurnLeftCallback.TurnLeftType.BODY) | ||
: this.changeAngle(headYaw, targetYaw, yawSpeed); | ||
} | ||
|
||
@Shadow @Final private MobEntity entity; | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/io/github/fabriccommunity/events/mixin/world/MixinMoveControl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.github.fabriccommunity.events.mixin.world; | ||
|
||
import io.github.fabriccommunity.events.impl.ChickenTurnLeftImpl; | ||
import io.github.fabriccommunity.events.world.ChickenTurnLeftCallback; | ||
import net.minecraft.entity.ai.control.MoveControl; | ||
import net.minecraft.entity.mob.MobEntity; | ||
import net.minecraft.entity.passive.ChickenEntity; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Redirect; | ||
|
||
/** | ||
* @author leocth | ||
*/ | ||
@Mixin(MoveControl.class) | ||
public abstract class MixinMoveControl { | ||
|
||
@Shadow abstract float changeAngle(float from, float to, float max); | ||
|
||
@Redirect( | ||
at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/ai/control/MoveControl;changeAngle(FFF)F", ordinal = 0), | ||
method = "tick" | ||
) | ||
private float redirectBodyChangeAngle(MoveControl self, float headYaw, float targetYaw, float yawSpeed) { | ||
return (entity instanceof ChickenEntity) | ||
? ChickenTurnLeftImpl.hackChangeAngle((ChickenEntity) entity, headYaw, targetYaw, yawSpeed, ChickenTurnLeftCallback.TurnLeftType.BODY) | ||
: this.changeAngle(headYaw, targetYaw, yawSpeed); | ||
} | ||
|
||
@Shadow @Final private MobEntity entity; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/io/github/fabriccommunity/events/world/ChickenTurnLeftCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.github.fabriccommunity.events.world; | ||
|
||
import net.fabricmc.fabric.api.event.Event; | ||
import net.fabricmc.fabric.api.event.EventFactory; | ||
import net.minecraft.entity.passive.ChickenEntity; | ||
import net.minecraft.util.ActionResult; | ||
|
||
/** | ||
* You deserved this. | ||
* @author leocth | ||
*/ | ||
@FunctionalInterface | ||
public interface ChickenTurnLeftCallback { | ||
Event<ChickenTurnLeftCallback> EVENT = EventFactory.createArrayBacked(ChickenTurnLeftCallback.class, listeners -> (chicken, oldYaw, targetYaw, yawSpeed, type) -> { | ||
for (ChickenTurnLeftCallback listener : listeners) { | ||
ActionResult result = listener.onTurnLeft(chicken, oldYaw, targetYaw, yawSpeed, type); | ||
if (result != ActionResult.PASS) | ||
return result; | ||
} | ||
return ActionResult.PASS; | ||
}); | ||
|
||
/** | ||
* Called when a chicken ({@code ChickenEntity}) turns left. | ||
* | ||
* @param chicken the chicken in question | ||
* @param oldYaw the chicken's previous head yaw | ||
* @param targetYaw the chicken's target yaw (new yaw) | ||
* @param yawSpeed the speed (limiter, to be exact) of the rotation. | ||
* @param type the type of the turn (head, body) | ||
* | ||
* @return | ||
* {@code SUCCESS} and {@code CONSUME} cancels further processing and allows the chicken to turn. | ||
* {@code PASS} pass event handling on to further processing. If all listeners pass, functions exactly the same as {@code SUCCESS} and {@code CONSUME}. | ||
* {@code FAIL} cancels further processing and forces the chicken to turn right by {@code |oldYaw - targetYaw|} instead. | ||
*/ | ||
ActionResult onTurnLeft(ChickenEntity chicken, float oldYaw, float targetYaw, float yawSpeed, TurnLeftType type); | ||
|
||
enum TurnLeftType { | ||
HEAD, BODY | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/test/java/io/github/fabriccommunity/events/test/ChickenTurnLeftTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.github.fabriccommunity.events.test; | ||
|
||
import io.github.fabriccommunity.events.world.ChickenTurnLeftCallback; | ||
import net.fabricmc.api.ModInitializer; | ||
import net.minecraft.util.ActionResult; | ||
|
||
/** | ||
* @author leocth | ||
*/ | ||
public class ChickenTurnLeftTest implements ModInitializer { | ||
public static final boolean ENABLED = true; | ||
|
||
@Override | ||
public void onInitialize() { | ||
if (ENABLED) { | ||
ChickenTurnLeftCallback.EVENT.register((chicken, oldYaw, targetYaw, yawSpeed, type) -> { | ||
if (type == ChickenTurnLeftCallback.TurnLeftType.HEAD) { | ||
// disable this if you want to | ||
System.out.println(chicken + " turned left by " + Math.abs(oldYaw - targetYaw) + "! Adjusting..."); | ||
return ActionResult.FAIL; | ||
} | ||
return ActionResult.PASS; | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.