This code implements a Minecraft client-side feature commonly referred to...
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.
- Performs ray tracing from the player's cursor to determine blocks at intersected positions up to the configured
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.