Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 165
src/client/java/com/kodari/fastteleportmacro/FastTeleportMacroClient.java
package com.kodari.fastteleportmacro;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
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.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraft.util.hit.EntityHitResult;
import org.lwjgl.glfw.GLFW;
public final class FastTeleportMacroClient implements ClientModInitializer {
private final ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor(task -> {
Thread thread = new Thread(task, "fast-teleport-macro");
thread.setDaemon(true);
return thread;
});
private boolean active;
@Override
public void onInitializeClient() {
KeyBinding.Category category = KeyBinding.Category.create(Identifier.of("fastteleportmacro", "controls"));
KeyBinding key = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.fastteleportmacro.activate", GLFW.GLFW_KEY_X, category));
ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (key.wasPressed()) {
if (client.currentScreen == null) {
activate(client);
}
}
});
}
private void activate(MinecraftClient client) {
ClientPlayerEntity player = client.player;
ClientPlayNetworkHandler handler = client.getNetworkHandler();
if (active || player == null || handler == null
|| !(client.crosshairTarget instanceof EntityHitResult hit)
|| !(hit.getEntity() instanceof PlayerEntity target)) {
return;
}
String uuid = target.getUuid().toString();
String returnCommand = "tppos " + coordinate(player.getX()) + " "
+ coordinate(player.getY()) + " " + coordinate(player.getZ());
active = true;
handler.sendChatCommand("tppos 1744.42 3.02 3530.5");
schedule(client, player, handler, "fly " + uuid + " false", 200, false);
schedule(client, player, handler, "tphere " + uuid, 400, false);
schedule(client, player, handler, returnCommand, 600, true);
}
private void schedule(MinecraftClient client, ClientPlayerEntity player,
ClientPlayNetworkHandler handler, String command, long delay, boolean last) {
timer.schedule(() -> client.execute(() -> {
try {
if (client.player == player && client.getNetworkHandler() == handler) {
handler.sendChatCommand(command);
}
} finally {
if (last) {
active = false;
}
}
}), delay, TimeUnit.MILLISECONDS);
}
private static String coordinate(double value) {
return BigDecimal.valueOf(value).setScale(2, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
}
}