This code implements a Minecraft client-side feature commonly referred to...

July 2, 2025 at 07:30 PM

package rosegoldclient.features; import net.minecraft.block.Block; import net.minecraft.client.settings.KeyBinding; import net.minecraft.item.ItemStack; import net.minecraft.network.play.client.CPacketPlayerTryUseItem; import net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; import net.minecraftforge.client.event.RenderWorldLastEvent; import net.minecraftforge.event.entity.player.PlayerInteractEvent; import net.minecraftforge.fml.common.eventhandler.EventPriority; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import rosegoldclient.Main; import rosegoldclient.events.PacketSentEvent; import rosegoldclient.events.TickEndEvent; import rosegoldclient.utils.RenderUtils; import rosegoldclient.utils.Utils; import rosegoldclient.utils.VecUtils; import java.awt.*; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; public class CursorTP { private static BlockPos blockToTeleport; private static ArrayList<BlockPos> blocks = new ArrayList<>(); private static Block b; private static boolean onto = false; public static boolean disable = false; private static final KeyBinding sneak = Main.mc.gameSettings.keyBindSneak; @SubscribeEvent public void onPacketSent(PacketSentEvent event) { if(event.packet instanceof CPacketPlayerTryUseItem || event.packet instanceof CPacketPlayerTryUseItemOnBlock) { if (!Main.configFile.cursorTeleport) return; if (cantUseItem()) return; if (disable) return; if (blockToTeleport != null) { if(Main.configFile.cursorTeleportCancelClick) { ItemStack itemStack = Main.mc.player.getHeldItemMainhand(); if (itemStack.getTagCompound() != null && !(itemStack.getTagCompound().hasNoTags())) { String nbt = itemStack.getTagCompound().toString(); if (nbt.contains("§a✔§7 Class Req:")) { event.setCanceled(true); } } } if(onto) { tpToBlock(blockToTeleport); } else { tpToBlock(blockToTeleport.add(0, -1, 0)); } blockToTeleport = null; } } } @SubscribeEvent(priority = EventPriority.HIGHEST) public void onInteract(PlayerInteractEvent event) { if (!Main.configFile.cursorTeleport) return; if (cantUseItem()) return; if (disable) return; if (event instanceof PlayerInteractEvent.RightClickEmpty) { if (blockToTeleport != null) { if(onto) { tpToBlock(blockToTeleport); } else { tpToBlock(blockToTeleport.add(0, -1, 0)); } blockToTeleport = null; } } } @SubscribeEvent public void onTick(TickEndEvent event) { if (Main.mc.player == null || Main.mc.world == null) return; if (!Main.configFile.cursorTeleport) return; blockToTeleport = null; b = null; blocks = rayTrace(Main.configFile.cursorTeleportRange, 1); Collections.reverse(blocks); for(BlockPos blockPos : blocks) { BlockPos toTp = getBlockToTp(blockPos); if(toTp != null) { blockToTeleport = toTp; b = Main.mc.world.getBlockState(blockToTeleport).getBlock(); break; } onto = false; } } @SubscribeEvent public void renderWorld(RenderWorldLastEvent event) { if (Main.mc.player == null || Main.mc.world == null) return; if (!Main.configFile.cursorTeleport) return; if (blockToTeleport != null) { RenderUtils.drawBlockESP(blockToTeleport, Color.RED, event.getPartialTicks()); if (b != null && Main.configFile.cursorTeleportShowDistance) { RenderUtils.renderWaypointText(b.getLocalizedName(), blockToTeleport.getX() + 0.5, blockToTeleport.getY() + 0.5, blockToTeleport.getZ() + 0.5, event.getPartialTicks()); } } } private static void tpToBlock(BlockPos blockPos) { onto = false; Main.mc.player.setPosition(blockPos.getX() + 0.5, blockPos.getY() + 1, blockPos.getZ() + 0.5); switch (Main.configFile.cursorTeleportResetVelocity) { case 1: Main.mc.player.setVelocity(0, Main.mc.player.motionY, 0); case 2: Main.mc.player.setVelocity(Main.mc.player.motionX, 0, Main.mc.player.motionZ); case 3: Main.mc.player.setVelocity(Main.mc.player.motionX, Main.mc.player.motionY, Main.mc.player.motionZ); } } private static BlockPos getBlockToTp(BlockPos blockPos) { BlockPos above = blockPos.add(0, 1, 0); BlockPos below = blockPos.add(0, -1, 0); if(isAir(above)) return blockPos; if(isAir(below)) return below; return null; } private static boolean cantUseItem() { switch (Main.configFile.cursorTeleportEmptyHand) { case 0: ItemStack itemStack = Main.mc.player.getHeldItemMainhand(); if (itemStack.getTagCompound() == null || itemStack.getTagCompound().hasNoTags()) return false; String nbt = itemStack.getTagCompound().toString(); return nbt.contains("§a✔§7 Class Req:"); case 1: return !Main.mc.player.getHeldItemMainhand().isEmpty(); default: return false; } } private static boolean isAir(BlockPos blockPos) { return Main.mc.world.getBlockState(blockPos).getCollisionBoundingBox( Main.mc.world, blockPos ) == Block.NULL_AABB; } public ArrayList<BlockPos> rayTrace(double blockReachDistance, float partialTicks) { ArrayList<BlockPos> blockPositions = new ArrayList<>(); Vec3d pos = Main.mc.player.getPositionEyes(partialTicks); Vec3d direction = Main.mc.player.getLook(partialTicks); direction = VecUtils.scaleVec(direction, 0.1f); boolean flag = false; blocks.clear(); for (int i = 0; i < blockReachDistance * 10; i++) { BlockPos blockPos = new BlockPos(pos); if(!isAir(blockPos)) { if(i == 0) flag = true; if(sneak.isKeyDown()) { blockPositions.add(blockPos); onto = true; } if(!flag) break; } blockPositions.add(blockPos); pos = pos.add(direction); } LinkedHashSet<BlockPos> set = new LinkedHashSet<>(blockPositions); blockPositions.clear(); blockPositions.addAll(set); return blockPositions; } }

This code implements a Minecraft client-side feature commonly referred to as "Cursor Teleport." It is a modification (mod) designed for the game Minecraft, which enables a player to teleport to blocks they are targeting with their cursor (crosshair) within a configured range. Here's a breakdown of what the code does:

1. Key Classes and Fields

  • Core Logic is Located in Class CursorTP:
    • Handles ray tracing to find blocks that the player's cursor intersects with.
    • Determines whether teleportation should occur and executes it.
  • Important Fields:
    • blockToTeleport: The block targeted for teleportation.
    • blocks: A list of blocks calculated through ray tracing that are in the teleportation range.
    • disable: A flag to enable or disable the feature.
    • sneak: Key binding for sneaking, used during specific teleportation logic.

2. Packet Interception

  • onPacketSent(PacketSentEvent event):
    • This listens for packets in which the player tries to use an item, e.g., right-clicking.
    • If conditions (like enabled configuration and proper item in hand) are met, it cancels the action and teleports the player to the targeted block.
    • The teleportation target can be adjusted to teleport "onto" the block or "below" the block.

3. Event-Driven Interaction

  • onInteract(PlayerInteractEvent event):

    • Listens to player interactions (e.g., right-clicking empty space).
    • If the teleportation logic enables it, the player will be teleported to the block they are targeted at.
  • onTick(TickEndEvent event):

    • Runs every tick (game logic update).
    • Performs ray tracing to find blocks in range of the cursor.
    • Updates the block to teleport to (blockToTeleport) and ensures teleportation is ready when triggered.
  • renderWorld(RenderWorldLastEvent event):

    • Handles rendering information to the screen.
    • Visually highlights the block that the player is targeting for teleportation.
    • Optionally displays the block's name and distance.

4. Helpers for Teleportation

  • tpToBlock(BlockPos blockPos):

    • Handles the actual teleportation logic.
    • Moves the player to the designated block's position (with slight offsets to center the player on the block).
    • Optionally modifies velocity based on configuration settings.
  • getBlockToTp(BlockPos blockPos):

    • Determines which block (above or below the given position) is a valid teleportation target based on whether it is air or not.
  • cantUseItem():

    • Checks if the player is holding an item that would disallow teleportation, based on specific NBT (data stored in items) or if an empty hand is required.
  • isAir(BlockPos blockPos):

    • Checks if the block at a specified position is air or has no collision box (thus, a valid teleportation target).
  • rayTrace(double blockReachDistance, float partialTicks):

    • Performs ray tracing from the player's cursor to determine blocks at intersected positions up to the configured blockReachDistance.
    • Produces a list of valid blocks, ensuring no duplicates, and supports visual targeting.

5. Behavior Configuration

  • The feature is configurable via Main.configFile:
    • cursorTeleport: Enables or disables the feature.
    • cursorTeleportRange: Maximum teleportation range.
    • cursorTeleportCancelClick: Controls whether the teleport cancels an item click.
    • cursorTeleportResetVelocity: Determines how player velocity is reset after teleportation.
    • cursorTeleportEmptyHand: Determines if teleportation is restricted to an empty hand.
    • cursorTeleportShowDistance: Decides if the block's distance and name are displayed.

6. Rendering and Visual Feedback

  • Highlights teleportation targets in red with a rendered bounding box (via RenderUtils.drawBlockESP).
  • Optionally displays the targeted block's name and distance from the player.

Summary:

This code allows the player to hover their cursor over a block in their Minecraft world and teleport to its position, as long as it is within the configured range and conditions. It uses a combination of ray tracing, event handling, and packet interception to enable this functionality, while providing visual feedback to the player about the teleportation target. The feature is commonly used in mods to enhance player movement or cheat in custom worlds.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node