Skip to content

Commit

Permalink
Move block collisions helper method to BlockUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander01998 committed Oct 31, 2023
1 parent b20fd68 commit 8b2d762
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 70 deletions.
9 changes: 3 additions & 6 deletions src/main/java/net/wurstclient/commands/VClipCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.wurstclient.command.CmdError;
import net.wurstclient.command.CmdException;
import net.wurstclient.command.CmdSyntaxError;
import net.wurstclient.command.Command;
import net.wurstclient.util.BlockUtils;
import net.wurstclient.util.MathUtils;

public final class VClipCmd extends Command
Expand Down Expand Up @@ -92,15 +92,12 @@ private double calculateHeight(Direction direction) throws CmdError

private boolean hasCollisions(Box box)
{
Iterable<VoxelShape> collisions =
MC.world.getBlockCollisions(MC.player, box);

return collisions.iterator().hasNext();
return BlockUtils.getBlockCollisions(box).findAny().isPresent();
}

private double getSubBlockOffset(Box offsetBox)
{
return IMC.getWorld().getCollidingBoxes(MC.player, offsetBox)
return BlockUtils.getBlockCollisions(offsetBox)
.mapToDouble(box -> box.maxY).max().getAsDouble() - offsetBox.minY;
}

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/net/wurstclient/hacks/JesusHack.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,10 @@ public boolean isOverLiquid()
{
boolean foundLiquid = false;
boolean foundSolid = false;
ClientPlayerEntity player = MC.player;
Box box = player.getBoundingBox().offset(0, -0.5, 0);
Box box = MC.player.getBoundingBox().offset(0, -0.5, 0);

// check collision boxes below player
ArrayList<Block> blockCollisions = IMC.getWorld()
.getCollidingBoxes(player, box)
ArrayList<Block> blockCollisions = BlockUtils.getBlockCollisions(box)
.map(bb -> BlockUtils.getBlock(BlockPos.ofFloored(bb.getCenter())))
.collect(Collectors.toCollection(ArrayList::new));

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/wurstclient/hacks/StepHack.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.wurstclient.settings.EnumSetting;
import net.wurstclient.settings.SliderSetting;
import net.wurstclient.settings.SliderSetting.ValueDisplay;
import net.wurstclient.util.BlockUtils;

public final class StepHack extends Hack implements UpdateListener
{
Expand Down Expand Up @@ -83,7 +84,7 @@ public void onUpdate()
if(!MC.world.isSpaceEmpty(player, box.offset(0, 1, 0)))
return;

double stepHeight = IMC.getWorld().getCollidingBoxes(player, box)
double stepHeight = BlockUtils.getBlockCollisions(box)
.mapToDouble(bb -> bb.maxY).max().orElse(Double.NEGATIVE_INFINITY);

stepHeight -= player.getY();
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/net/wurstclient/mixin/MinecraftClientMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import net.minecraft.client.session.ProfileKeys;
import net.minecraft.client.session.ProfileKeysImpl;
import net.minecraft.client.session.Session;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.thread.ReentrantThreadExecutor;
Expand All @@ -41,7 +40,6 @@
import net.wurstclient.mixinterface.IClientPlayerEntity;
import net.wurstclient.mixinterface.IClientPlayerInteractionManager;
import net.wurstclient.mixinterface.IMinecraftClient;
import net.wurstclient.mixinterface.IWorld;

@Mixin(MinecraftClient.class)
public abstract class MinecraftClientMixin
Expand All @@ -56,8 +54,6 @@ public abstract class MinecraftClientMixin
@Shadow
public ClientPlayerEntity player;
@Shadow
public ClientWorld world;
@Shadow
@Final
private YggdrasilAuthenticationService authenticationService;

Expand Down Expand Up @@ -172,12 +168,6 @@ public IClientPlayerEntity getPlayer()
return (IClientPlayerEntity)player;
}

@Override
public IWorld getWorld()
{
return (IWorld)world;
}

@Override
public IClientPlayerInteractionManager getInteractionManager()
{
Expand Down
18 changes: 1 addition & 17 deletions src/main/java/net/wurstclient/mixin/WorldMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,18 @@
*/
package net.wurstclient.mixin;

import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.jetbrains.annotations.Nullable;
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;

import net.minecraft.entity.Entity;
import net.minecraft.util.math.Box;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
import net.wurstclient.WurstClient;
import net.wurstclient.hacks.NoWeatherHack;
import net.wurstclient.mixinterface.IWorld;

@Mixin(World.class)
public abstract class WorldMixin implements WorldAccess, AutoCloseable, IWorld
public abstract class WorldMixin implements WorldAccess, AutoCloseable
{
@Inject(at = @At("HEAD"),
method = "getRainGradient(F)F",
Expand Down Expand Up @@ -59,12 +51,4 @@ public int getMoonPhase()

return getDimension().getMoonPhase(getLunarTime());
}

@Override
public Stream<VoxelShape> getBlockCollisionsStream(@Nullable Entity entity,
Box box)
{
return StreamSupport
.stream(getBlockCollisions(entity, box).spliterator(), false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,5 @@ public interface IMinecraftClient

public IClientPlayerEntity getPlayer();

public IWorld getWorld();

public void setSession(Session session);
}
30 changes: 0 additions & 30 deletions src/main/java/net/wurstclient/mixinterface/IWorld.java

This file was deleted.

22 changes: 22 additions & 0 deletions src/main/java/net/wurstclient/util/BlockUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

import java.util.ArrayList;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.Entity;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;
import net.minecraft.util.InvalidIdentifierException;
Expand All @@ -24,6 +26,7 @@
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.CollisionView;
import net.minecraft.world.RaycastContext;
import net.wurstclient.WurstClient;

Expand Down Expand Up @@ -149,6 +152,25 @@ public static boolean hasLineOfSight(Vec3d to)
.getType() == HitResult.Type.MISS;
}

/**
* Returns a stream of all blocks that collide with the given box.
*
* <p>
* Unlike {@link CollisionView#getBlockCollisions(Entity, Box)}, this method
* breaks the voxel shapes down into their bounding boxes and only returns
* those that actually intersect with the given box. It also assumes that
* the entity is the player.
*/
public static Stream<Box> getBlockCollisions(Box box)
{
Iterable<VoxelShape> blockCollisions =
MC.world.getBlockCollisions(MC.player, box);

return StreamSupport.stream(blockCollisions.spliterator(), false)
.flatMap(shape -> shape.getBoundingBoxes().stream())
.filter(shapeBox -> shapeBox.intersects(box));
}

public static ArrayList<BlockPos> getAllInBox(BlockPos from, BlockPos to)
{
ArrayList<BlockPos> blocks = new ArrayList<>();
Expand Down

0 comments on commit 8b2d762

Please sign in to comment.