From 68e7572139c2211ee4ce586fa37c0a6ad11bbc1a Mon Sep 17 00:00:00 2001 From: MeexReay <127148610+MeexReay@users.noreply.github.com> Date: Tue, 30 May 2023 19:45:23 +0300 Subject: [PATCH] MOVEMENT move -> input --- build.gradle | 3 +- gradle.properties | 2 +- .../themixray/repeating/mod/EasyConfig.java | 37 +++-- .../themixray/repeating/mod/RepeatingMod.java | 127 ++++++++++++++---- .../repeating/mod/RepeatingScreen.java | 20 +-- .../repeating/mod/mixin/InputMixin.java | 16 +-- .../repeating/mod/mixin/MovementMixin.java | 88 ++++++------ src/main/resources/fabric.mod.json | 4 +- 8 files changed, 190 insertions(+), 107 deletions(-) diff --git a/build.gradle b/build.gradle index 128d269..0efd2d3 100644 --- a/build.gradle +++ b/build.gradle @@ -37,8 +37,7 @@ dependencies { // include this if you don't want force your users to install owo // sentinel will warn them and give the option to download it automatically include "io.wispforest:owo-sentinel:${project.owo_version}" - - implementation 'org.yaml:snakeyaml:2.0' + modImplementation 'org.yaml:snakeyaml:2.0' } base { diff --git a/gradle.properties b/gradle.properties index 2bf89a5..463332f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ yarn_mappings=1.19.3+build.1 loader_version=0.14.17 # Mod Properties -mod_version = 1.0.0 +mod_version = 1.0.1 maven_group = themixray.repeating.mod archives_base_name = repeating-mod diff --git a/src/main/java/themixray/repeating/mod/EasyConfig.java b/src/main/java/themixray/repeating/mod/EasyConfig.java index 6701932..a2c6ae1 100644 --- a/src/main/java/themixray/repeating/mod/EasyConfig.java +++ b/src/main/java/themixray/repeating/mod/EasyConfig.java @@ -15,9 +15,9 @@ public class EasyConfig { public Map data; private Yaml yaml; - public EasyConfig(Path path, Map def) { - this.path = path; - this.file = path.toFile(); + public EasyConfig(File f, Map def) { + this.path = f.toPath(); + this.file = f; this.data = new HashMap<>(); this.yaml = new Yaml(); @@ -31,15 +31,38 @@ public class EasyConfig { } reload(); } + public EasyConfig(Path f, Map def) { + this(f.toFile(),def); + } + public EasyConfig(String parent,String child,Map def) { + this(new File(parent,child),def); + } + public EasyConfig(File parent,String child,Map def) { + this(new File(parent,child),def); + } + public EasyConfig(Path parent,String child,Map def) { + this(new File(parent.toFile(),child),def); + } + public EasyConfig(File f) { + this(f,new HashMap<>()); + } public EasyConfig(Path path) { - this(path,new HashMap<>()); + this(path.toFile(),new HashMap<>()); + } + public EasyConfig(String parent,String child) { + this(new File(parent,child),new HashMap<>()); + } + public EasyConfig(File parent,String child) { + this(new File(parent,child),new HashMap<>()); + } + public EasyConfig(Path parent,String child) { + this(new File(parent.toFile(),child),new HashMap<>()); } public void reload() { data = read(); } - public void save() { write(data); } @@ -47,9 +70,8 @@ public class EasyConfig { private String toYaml(Map p) { return yaml.dump(p); } - private Map toMap(String j) { - return yaml.load(j); + return (Map) yaml.load(j); } private Map read() { @@ -60,7 +82,6 @@ public class EasyConfig { } return new HashMap<>(); } - private void write(Map p) { try { Files.write(path, toYaml(p).getBytes()); diff --git a/src/main/java/themixray/repeating/mod/RepeatingMod.java b/src/main/java/themixray/repeating/mod/RepeatingMod.java index 54dbc97..71652d1 100644 --- a/src/main/java/themixray/repeating/mod/RepeatingMod.java +++ b/src/main/java/themixray/repeating/mod/RepeatingMod.java @@ -4,22 +4,13 @@ import com.google.common.collect.Lists; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; -import net.fabricmc.fabric.api.renderer.v1.RendererAccess; -import net.fabricmc.fabric.api.resource.ResourceManagerHelper; -import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener; import net.fabricmc.loader.api.FabricLoader; import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.KeyBinding; import net.minecraft.client.util.InputUtil; import net.minecraft.entity.MovementType; -import net.minecraft.registry.Registries; -import net.minecraft.registry.Registry; -import net.minecraft.resource.Resource; -import net.minecraft.resource.ResourceManager; -import net.minecraft.resource.ResourceType; import net.minecraft.text.Text; import net.minecraft.util.Hand; -import net.minecraft.util.Identifier; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; @@ -29,7 +20,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.InputStream; import java.util.*; public class RepeatingMod implements ClientModInitializer { @@ -45,7 +35,7 @@ public class RepeatingMod implements ClientModInitializer { public Thread replay = null; public boolean is_replaying = false; public boolean loop_replay = false; - public static boolean replay_sneaking = false; + public static RecordInputEvent input_replay = null; public static RepeatingScreen menu; private static KeyBinding menu_key; @@ -118,6 +108,7 @@ public class RepeatingMod implements ClientModInitializer { is_recording = true; menu.update_btns(); record.clear(); + record.add(new RecordMoveEvent(client.player.getPos(),client.player.getHeadYaw(),client.player.getPitch())); sendMessage(Text.translatable("message.repeating-mod.record_start")); } @@ -143,12 +134,13 @@ public class RepeatingMod implements ClientModInitializer { is_recording = false; is_replaying = true; menu.update_btns(); - client.player.setNoGravity(true); replay = new Thread(() -> { while (true) { - for (RecordEvent e : record) - if (is_replaying) + for (RecordEvent e : record) { + if (is_replaying) { e.callback(); + } + } if (!loop_replay || !is_replaying) break; } stopReplay(); @@ -161,7 +153,6 @@ public class RepeatingMod implements ClientModInitializer { is_recording = false; is_replaying = false; menu.update_btns(); - client.player.setNoGravity(false); sendMessage(Text.translatable("message.repeating-mod.replay_stop")); } @@ -196,9 +187,20 @@ public class RepeatingMod implements ClientModInitializer { Double.parseDouble(args[2])), Float.parseFloat(args[3]), Float.parseFloat(args[4])); - } else if (type.equals("s")) { - return new RecordSneakEvent( - args[0].equals("1")); + } else if (type.equals("p")) { + return new RecordInputEvent( + (args[0].equals("n")?null:args[0].equals("1")), + (args[1].equals("n")?null:args[1].equals("1")), + (args[2].equals("n")?null:Float.parseFloat(args[2])), + (args[3].equals("n")?null:Float.parseFloat(args[3])), + (args[4].equals("n")?null:args[4].equals("1")), + (args[5].equals("n")?null:args[5].equals("1")), + (args[6].equals("n")?null:args[6].equals("1")), + (args[7].equals("n")?null:args[7].equals("1")), + Float.parseFloat(args[8]),Float.parseFloat(args[9]), + Float.parseFloat(args[10]), + (args[11].equals("n")?null:args[11].equals("1")), + Float.parseFloat(args[12])); } else if (type.equals("b")) { return new RecordBlockBreakEvent(new BlockPos( Integer.parseInt(args[0]), @@ -275,22 +277,99 @@ public class RepeatingMod implements ClientModInitializer { } } - public static class RecordSneakEvent extends RecordEvent { - public boolean sneaking; + public static class RecordInputEvent extends RecordEvent { + public Boolean sneaking; + public Boolean jumping; + public Boolean pressingForward; + public Boolean pressingBack; + public Boolean pressingLeft; + public Boolean pressingRight; + public Boolean sprinting; - public RecordSneakEvent(boolean sneaking) { + public Float movementSideways; + public Float movementForward; + + public float yaw; + public float head_yaw; + public float body_yaw; + public float pitch; + + + public RecordInputEvent(Boolean sneaking, + Boolean jumping, + Float movementSideways, + Float movementForward, + Boolean pressingForward, + Boolean pressingBack, + Boolean pressingLeft, + Boolean pressingRight, + float head_yaw, + float body_yaw, + float head_pitch, + Boolean sprinting, + float yaw) { this.sneaking = sneaking; + this.jumping = jumping; + this.movementSideways = movementSideways; + this.movementForward = movementForward; + this.pressingForward = pressingForward; + this.pressingBack = pressingBack; + this.pressingLeft = pressingLeft; + this.pressingRight = pressingRight; + this.head_yaw = head_yaw; + this.body_yaw = body_yaw; + this.pitch = head_pitch; + this.sprinting = sprinting; + this.yaw = yaw; + } + + public boolean isEmpty() { + return sneaking == null && + jumping == null && + movementSideways == null && + movementForward == null && + pressingForward == null && + pressingBack == null && + pressingLeft == null && + pressingRight == null && + sprinting == null; } public void callback() { - RepeatingMod.replay_sneaking = sneaking; + input_replay = this; + } + + public void inputCallback() { + if (sneaking != null) if (client.player.input.sneaking != sneaking) client.player.input.sneaking = sneaking; + if (jumping != null) if (client.player.input.jumping != jumping) client.player.input.jumping = jumping; + if (movementSideways != null) if (client.player.input.movementSideways != movementSideways) client.player.input.movementSideways = movementSideways; + if (movementForward != null) if (client.player.input.movementForward != movementForward) client.player.input.movementForward = movementForward; + if (pressingForward != null) if (client.player.input.pressingForward != pressingForward) client.player.input.pressingForward = pressingForward; + if (pressingBack != null) if (client.player.input.pressingBack != pressingBack) client.player.input.pressingBack = pressingBack; + if (pressingLeft != null) if (client.player.input.pressingLeft != pressingLeft) client.player.input.pressingLeft = pressingLeft; + if (pressingRight != null) if (client.player.input.pressingRight != pressingRight) client.player.input.pressingRight = pressingRight; + if (sprinting != null) if (client.player.isSprinting() != sprinting) client.player.setSprinting(sprinting); + if (client.player.getYaw() != yaw) client.player.setYaw(yaw); + if (client.player.getHeadYaw() != head_yaw) client.player.setHeadYaw(head_yaw); + if (client.player.getBodyYaw() != body_yaw) client.player.setBodyYaw(body_yaw); + if (client.player.getPitch() != pitch) client.player.setPitch(pitch); } public String toText() { - return "s="+(sneaking?"1":"0"); + return "p="+ + ((sneaking==null)?"n":(sneaking?"1":"0"))+"&"+ + ((jumping==null)?"n":(jumping?"1":"0"))+"&"+ + ((movementSideways==null)?"n":movementSideways)+"&"+ + ((movementForward==null)?"n":movementForward)+"&"+ + ((pressingForward==null)?"n":(pressingForward?"1":"0"))+"&"+ + ((pressingBack==null)?"n":(pressingBack?"1":"0"))+"&"+ + ((pressingLeft==null)?"n":(pressingLeft?"1":"0"))+"&"+ + ((pressingRight==null)?"n":(pressingRight?"1":"0"))+"&"+ + head_yaw+"&"+body_yaw+"&"+ pitch +"&"+ + ((sprinting==null)?"n":(sprinting?"1":"0")); } public String getType() { - return "sneak"; + return "input"; } } diff --git a/src/main/java/themixray/repeating/mod/RepeatingScreen.java b/src/main/java/themixray/repeating/mod/RepeatingScreen.java index b59022b..3185d4e 100644 --- a/src/main/java/themixray/repeating/mod/RepeatingScreen.java +++ b/src/main/java/themixray/repeating/mod/RepeatingScreen.java @@ -6,26 +6,12 @@ import io.wispforest.owo.ui.container.*; import io.wispforest.owo.ui.container.FlowLayout; import io.wispforest.owo.ui.core.*; import io.wispforest.owo.ui.core.Insets; -import io.wispforest.owo.util.EventSource; -import net.fabricmc.fabric.api.resource.ResourceManagerHelper; import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.resource.ResourceManager; -import net.minecraft.resource.ResourceType; import net.minecraft.text.Text; -import net.minecraft.util.Identifier; import org.jetbrains.annotations.NotNull; -import javax.swing.*; -import java.awt.*; -import java.awt.datatransfer.Clipboard; -import java.awt.datatransfer.DataFlavor; -import java.awt.datatransfer.StringSelection; -import java.awt.datatransfer.UnsupportedFlavorException; import java.io.*; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.OpenOption; -import java.nio.file.Path; public class RepeatingScreen extends BaseOwoScreen { public RepeatingMod mod; @@ -49,7 +35,7 @@ public class RepeatingScreen extends BaseOwoScreen { record_btn.setMessage(Text.translatable("text.repeating-mod." + ((mod.is_recording) ? "stop" : "start")).append(" ") .append(Text.translatable("text.repeating-mod.record"))); - loop_btn.setMessage(Text.of(((mod.loop_replay) ? "\uefff" : "\ueffe"))); + loop_btn.setMessage(Text.of(((mod.loop_replay) ? "\uefff " : "\ueffe "))); } @Override @@ -167,7 +153,7 @@ public class RepeatingScreen extends BaseOwoScreen { .surface(Surface.DARK_PANEL) .verticalAlignment(VerticalAlignment.CENTER) .horizontalAlignment(HorizontalAlignment.CENTER) - .margins(Insets.of(1)))*/ + .margins(Insets.of(1))) ).child( Containers.verticalFlow(Sizing.content(), Sizing.content()) .child(Containers.verticalFlow(Sizing.content(), Sizing.content()) @@ -204,7 +190,7 @@ public class RepeatingScreen extends BaseOwoScreen { .surface(Surface.DARK_PANEL) .verticalAlignment(VerticalAlignment.CENTER) .horizontalAlignment(HorizontalAlignment.CENTER) - .margins(Insets.of(1))) + .margins(Insets.of(1)))*/ ); update_btns(); } diff --git a/src/main/java/themixray/repeating/mod/mixin/InputMixin.java b/src/main/java/themixray/repeating/mod/mixin/InputMixin.java index 5f3874e..bcaf1e0 100644 --- a/src/main/java/themixray/repeating/mod/mixin/InputMixin.java +++ b/src/main/java/themixray/repeating/mod/mixin/InputMixin.java @@ -12,19 +12,9 @@ public abstract class InputMixin { @Inject(at = @At(value = "TAIL"), method = "tick") private void onTickTail(boolean slowDown, float f, CallbackInfo ci) { if (RepeatingMod.me.is_replaying) { - RepeatingMod.client.player.input.sneaking = RepeatingMod.replay_sneaking; - } - } - - @Inject(at = @At(value = "HEAD"), method = "tick") - private void onTickHead(boolean slowDown, float f, CallbackInfo ci) { - if (RepeatingMod.me.is_recording) { - RepeatingMod.RecordSneakEvent e = new RepeatingMod. - RecordSneakEvent(RepeatingMod.client.player.input.sneaking); - RepeatingMod.RecordSneakEvent l = ((RepeatingMod.RecordSneakEvent) - RepeatingMod.me.getLastRecord("sneak")); - if (l == null || l.sneaking != e.sneaking) - RepeatingMod.me.recordTick(e); + if (RepeatingMod.input_replay != null) { + RepeatingMod.input_replay.inputCallback(); + } } } } diff --git a/src/main/java/themixray/repeating/mod/mixin/MovementMixin.java b/src/main/java/themixray/repeating/mod/mixin/MovementMixin.java index 83a18e5..172ab0b 100644 --- a/src/main/java/themixray/repeating/mod/mixin/MovementMixin.java +++ b/src/main/java/themixray/repeating/mod/mixin/MovementMixin.java @@ -43,50 +43,58 @@ public abstract class MovementMixin { }); } - @Inject(at = @At(value = "HEAD"), method = "move") - private void onMove(MovementType movementType, Vec3d vec, CallbackInfo ci) { + @Inject(at = @At(value = "HEAD"), method = "tickMovement") + private void onMove(CallbackInfo ci) { if (RepeatingMod.me.is_recording) { - if (vec != lastVec) { - double dist = 0; - if (lastVec != null) - dist = vec.distanceTo(lastVec); - if (dist > 0.0) { - Vec3d c = client.player.getPos(); + RepeatingMod.RecordInputEvent l = ((RepeatingMod.RecordInputEvent)RepeatingMod.me.getLastRecord("input")); + if (l == null) { + RepeatingMod.RecordInputEvent e = new RepeatingMod.RecordInputEvent( + RepeatingMod.client.player.input.sneaking, + RepeatingMod.client.player.input.jumping, + RepeatingMod.client.player.input.movementSideways, + RepeatingMod.client.player.input.movementForward, + RepeatingMod.client.player.input.pressingForward, + RepeatingMod.client.player.input.pressingBack, + RepeatingMod.client.player.input.pressingLeft, + RepeatingMod.client.player.input.pressingRight, + RepeatingMod.client.player.getHeadYaw(), + RepeatingMod.client.player.getBodyYaw(), + RepeatingMod.client.player.getPitch(), + RepeatingMod.client.player.isSprinting(), + RepeatingMod.client.player.getYaw()); + RepeatingMod.me.recordTick(e); + } else { + RepeatingMod.RecordInputEvent e = new RepeatingMod.RecordInputEvent( + ((Boolean) RepeatingMod.client.player.input.sneaking == l.sneaking) ? null : RepeatingMod.client.player.input.sneaking, + ((Boolean) RepeatingMod.client.player.input.jumping == l.jumping) ? null : RepeatingMod.client.player.input.jumping, + (((Float) RepeatingMod.client.player.input.movementSideways).equals(l.movementSideways)) ? null : RepeatingMod.client.player.input.movementSideways, + (((Float) RepeatingMod.client.player.input.movementForward).equals(l.movementForward)) ? null : RepeatingMod.client.player.input.movementForward, + ((Boolean) RepeatingMod.client.player.input.pressingForward == l.pressingForward) ? null : RepeatingMod.client.player.input.pressingForward, + ((Boolean) RepeatingMod.client.player.input.pressingBack == l.pressingBack) ? null : RepeatingMod.client.player.input.pressingBack, + ((Boolean) RepeatingMod.client.player.input.pressingLeft == l.pressingLeft) ? null : RepeatingMod.client.player.input.pressingLeft, + ((Boolean) RepeatingMod.client.player.input.pressingRight == l.pressingRight) ? null : RepeatingMod.client.player.input.pressingRight, + RepeatingMod.client.player.getHeadYaw(),RepeatingMod.client.player.getBodyYaw(),RepeatingMod.client.player.getPitch(), + ((Boolean) RepeatingMod.client.player.isSprinting() == l.sprinting) ? null : RepeatingMod.client.player.isSprinting(), + RepeatingMod.client.player.getYaw()); - RepeatingMod.RecordMoveEvent ev = new RepeatingMod.RecordMoveEvent( - new Vec3d(c.getX() + vec.getX(), - c.getY() + vec.getY(), - c.getZ() + vec.getZ()), - lastYaw, lastPitch); - - boolean just_add = true; - Date now = new Date(); - if (RepeatingMod.me.last_record != null) { - long diff = now.getTime() - RepeatingMod.me.last_record.getTime(); - boolean add_delay = true; - if (diff > 0) { - RepeatingMod.RecordEvent last_ev = RepeatingMod.me.record.get(RepeatingMod.me.record.size()-1); - if (last_ev instanceof RepeatingMod.RecordMoveEvent) { - RepeatingMod.RecordMoveEvent last_ev1 = (RepeatingMod.RecordMoveEvent) last_ev; - if (last_ev1.vec.distanceTo(ev.vec) < RepeatingMod.me.record_blocks_limit && - diff < RepeatingMod.me.record_time_limit) { - just_add = false; - add_delay = false; - last_ev1.vec = ev.vec; - } - } - } - if (add_delay) { - RepeatingMod.me.record.add(new RepeatingMod.RecordDelayEvent(diff)); - } - } - if (just_add) { - RepeatingMod.me.record.add(ev); - RepeatingMod.me.last_record = now; - } + if (!(e.isEmpty() && + e.yaw == l.yaw && + e.head_yaw == l.head_yaw && + e.pitch == l.pitch && + e.body_yaw == l.body_yaw)) { + RepeatingMod.me.recordTick(e); } } - lastVec = vec; + } + } + + @Inject(at = @At(value = "INVOKE"), method = "setSprinting", cancellable = true) + private void onSprint(boolean sprinting,CallbackInfo ci) { + if (RepeatingMod.me.is_replaying) { + if (RepeatingMod.input_replay != null && + RepeatingMod.input_replay.sprinting != sprinting) { + ci.cancel(); + } } } } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 5328b3f..cfdd905 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -9,8 +9,8 @@ "TheMixRay" ], "contact": { - "homepage": "https://fabricmc.net/", - "sources": "https://github.com/FabricMC/fabric-example-mod" + "homepage": "https://modrinth.com/mod/repeating-mod", + "sources": "https://github.com/MeexReay/repeating-mod" }, "license": "CC0-1.0",