Skip to content

Commit

Permalink
Added other projectiles to Trajectory
Browse files Browse the repository at this point in the history
  • Loading branch information
coltonk9043 committed Jun 28, 2024
1 parent 65396d6 commit 480bf35
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 95 deletions.
16 changes: 1 addition & 15 deletions src/main/java/net/aoba/misc/ModuleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,11 @@ public class ModuleUtils {

public static boolean isThrowable(ItemStack stack) {
Item item = stack.getItem();
return item instanceof BowItem || item instanceof SnowballItem || item instanceof EggItem
return item == Items.BOW || item == Items.SNOWBALL || item == Items.EGG
|| item instanceof EnderPearlItem || item instanceof SplashPotionItem
|| item instanceof LingeringPotionItem || item instanceof FishingRodItem;
}

public static double throwableGravity(Item item) {
if(item instanceof RangedWeaponItem) {
return 0.05;
}else if(item instanceof ThrowablePotionItem) {
return 0.4;
}else if(item instanceof FishingRodItem) {
return 0.15;
}else if(item instanceof TridentItem) {
return 0.015;
}else {
return 0.03;
}
}

public static boolean isPlantable(ItemStack stack) {
Item item = stack.getItem();
return item == Items.WHEAT_SEEDS || item == Items.CARROT || item == Items.POTATO;
Expand Down
182 changes: 102 additions & 80 deletions src/main/java/net/aoba/module/modules/render/Trajectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@
import net.minecraft.entity.Entity;
import net.minecraft.entity.projectile.ProjectileUtil;
import net.minecraft.item.BowItem;
import net.minecraft.item.FishingRodItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.RangedWeaponItem;
import net.minecraft.item.ThrowablePotionItem;
import net.minecraft.item.TridentItem;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.hit.HitResult;
Expand All @@ -60,15 +66,16 @@
public class Trajectory extends Module implements RenderListener {

private ColorSetting color = new ColorSetting("trajectory_color", "Color", "Color", new Color(0, 1f, 1f));
private FloatSetting blipSize = new FloatSetting("trajectory_blipsize", "Blip Size", "Blip Size", 0.15f, 0.05f, 1.0f, 0.05f);

private FloatSetting blipSize = new FloatSetting("trajectory_blipsize", "Blip Size", "Blip Size", 0.15f, 0.05f,
1.0f, 0.05f);

public Trajectory() {
super(new KeybindSetting("key.trajectory", "Trajectory Key", InputUtil.fromKeyCode(GLFW.GLFW_KEY_UNKNOWN, 0)));

this.setName("Trajectory");
this.setCategory(Category.Render);
this.setDescription("Allows the player to see where they are aiming. (DISABLED)");

this.addSetting(color);
this.addSetting(blipSize);
}
Expand All @@ -87,88 +94,103 @@ public void onEnable() {
public void onToggle() {

}

@Override
public void OnRender(RenderEvent event) {
if(MC.player.isUsingItem()) {
Color renderColor = color.getValue();
Matrix4f matrix = event.GetMatrix().peek().getPositionMatrix();

ItemStack itemStack = MC.player.getActiveItem();
if(ModuleUtils.isThrowable(itemStack)) {
float initialVelocity = (52f * BowItem.getPullProgress(MC.player.getItemUseTime()));

Camera camera = MC.gameRenderer.getCamera();
Vec3d offset = RenderUtils.getEntityPositionOffsetInterpolated(MC.cameraEntity, event.GetPartialTicks());
Vec3d eyePos = MC.cameraEntity.getEyePos();

// Calculate look direction.
Vec3d right = Vec3d.fromPolar(0, camera.getYaw() + 90).multiply(0.14f);
Vec3d lookDirection = Vec3d.fromPolar(camera.getPitch(), camera.getYaw());
Vec3d velocity = lookDirection.multiply(initialVelocity).multiply(0.2f);

// Calculate starting point.
Vec3d prevPoint = new Vec3d(0, 0, 0).add(eyePos).subtract(offset).add(right);
Vec3d landPosition = null;

RenderSystem.setShaderColor(renderColor.getRedFloat(), renderColor.getGreenFloat(), renderColor.getBlueFloat(), renderColor.getAlphaFloat());

GL11.glEnable(GL11.GL_BLEND);
GL11.glDisable(GL11.GL_DEPTH_TEST);

Tessellator tessellator = RenderSystem.renderThreadTesselator();
RenderSystem.setShader(GameRenderer::getPositionProgram);
BufferBuilder bufferBuilder = tessellator.begin(VertexFormat.DrawMode.DEBUG_LINES, VertexFormats.POSITION);

for(int iteration = 0; iteration < 150; iteration++){
Vec3d nextPoint = prevPoint.add(velocity.multiply(0.1));
bufferBuilder.vertex(matrix, (float) prevPoint.x, (float) prevPoint.y, (float) prevPoint.z);

// Check to see if we have collided with a block.
RaycastContext context = new RaycastContext(prevPoint, nextPoint, RaycastContext.ShapeType.COLLIDER, FluidHandling.NONE, MC.player);
BlockHitResult result = MC.world.raycast(context);
if(result.getType() != HitResult.Type.MISS) {
// Arrow is collided with a block, draw one last vertice and set land position to the raycast result position.
landPosition = result.getPos();
bufferBuilder.vertex(matrix, (float) landPosition.x, (float) landPosition.y, (float) landPosition.z);
Color renderColor = color.getValue();
Matrix4f matrix = event.GetMatrix().peek().getPositionMatrix();

ItemStack itemStack = MC.player.getMainHandStack();
if (ModuleUtils.isThrowable(itemStack)) {

// Get Velocity
float initialVelocity = (52f);
if (itemStack.getItem() == Items.BOW && MC.player.isUsingItem())
initialVelocity *= BowItem.getPullProgress(MC.player.getItemUseTime());

Camera camera = MC.gameRenderer.getCamera();
Vec3d offset = RenderUtils.getEntityPositionOffsetInterpolated(MC.cameraEntity, event.GetPartialTicks());
Vec3d eyePos = MC.cameraEntity.getEyePos();

// Calculate look direction.
Vec3d right = Vec3d.fromPolar(0, camera.getYaw() + 90).multiply(0.14f);
Vec3d lookDirection = Vec3d.fromPolar(camera.getPitch(), camera.getYaw());
Vec3d velocity = lookDirection.multiply(initialVelocity).multiply(0.2f);

// Calculate starting point.
Vec3d prevPoint = new Vec3d(0, 0, 0).add(eyePos).subtract(offset).add(right);
Vec3d landPosition = null;

RenderSystem.setShaderColor(renderColor.getRedFloat(), renderColor.getGreenFloat(),
renderColor.getBlueFloat(), renderColor.getAlphaFloat());

GL11.glEnable(GL11.GL_BLEND);
GL11.glDisable(GL11.GL_DEPTH_TEST);

Tessellator tessellator = RenderSystem.renderThreadTesselator();
RenderSystem.setShader(GameRenderer::getPositionProgram);
BufferBuilder bufferBuilder = tessellator.begin(VertexFormat.DrawMode.DEBUG_LINES, VertexFormats.POSITION);

for (int iteration = 0; iteration < 150; iteration++) {
Vec3d nextPoint = prevPoint.add(velocity.multiply(0.1));
bufferBuilder.vertex(matrix, (float) prevPoint.x, (float) prevPoint.y, (float) prevPoint.z);

// Check to see if we have collided with a block.
RaycastContext context = new RaycastContext(prevPoint, nextPoint, RaycastContext.ShapeType.COLLIDER,
FluidHandling.NONE, MC.player);
BlockHitResult result = MC.world.raycast(context);
if (result.getType() != HitResult.Type.MISS) {
// Arrow is collided with a block, draw one last vertice and set land position
// to the raycast result position.
landPosition = result.getPos();
bufferBuilder.vertex(matrix, (float) landPosition.x, (float) landPosition.y,
(float) landPosition.z);
break;
} else {
// We did NOT find a collision with a block, check entities.
Box box = new Box(prevPoint, nextPoint);
Predicate<Entity> predicate = e -> !e.isSpectator() && e.canHit();
EntityHitResult entityResult = ProjectileUtil.raycast(MC.player, prevPoint, nextPoint, box,
predicate, 4096);

if (entityResult != null && entityResult.getType() != HitResult.Type.MISS) {
// Arrow is collided with an entity, draw one last vertice and set land position
// to the raycast result position.
landPosition = entityResult.getPos();
bufferBuilder.vertex(matrix, (float) landPosition.x, (float) landPosition.y,
(float) landPosition.z);
break;
}else {
// We did NOT find a collision with a block, check entities.
Box box = new Box(prevPoint, nextPoint);
Predicate<Entity> predicate = e -> !e.isSpectator() && e.canHit();
EntityHitResult entityResult = ProjectileUtil.raycast(MC.player, prevPoint, nextPoint, box, predicate, 4096);

if(entityResult != null && entityResult.getType() != HitResult.Type.MISS) {
// Arrow is collided with an entity, draw one last vertice and set land position to the raycast result position.
landPosition = entityResult.getPos();
bufferBuilder.vertex(matrix, (float) landPosition.x, (float) landPosition.y, (float) landPosition.z);
break;
}else {
// No collisions from raycast, draw next vertice.
bufferBuilder.vertex(matrix, (float) nextPoint.x, (float) nextPoint.y, (float) nextPoint.z);
}
} else {
// No collisions from raycast, draw next vertice.
bufferBuilder.vertex(matrix, (float) nextPoint.x, (float) nextPoint.y, (float) nextPoint.z);
}

prevPoint = nextPoint;
velocity = velocity.multiply(0.99).add(0, -0.045f, 0);
}


BufferRenderer.drawWithGlobalProgram(bufferBuilder.end());
RenderSystem.setShaderColor(1, 1, 1, 1);

GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_BLEND);

// Draw Cube if a landing position exists.
if(landPosition != null) {
float size = blipSize.getValue();
Vec3d pos1 = landPosition.add(-size, -size, -size);
Vec3d pos2 = landPosition.add(size, size, size);
Box box = new Box(pos1.x, pos1.y, pos1.z, pos2.x, pos2.y, pos2.z);
RenderUtils.draw3DBox(matrix, box, renderColor);
}

prevPoint = nextPoint;
velocity = velocity.multiply(0.99).add(0, throwableGravity(itemStack.getItem()), 0);
}

BufferRenderer.drawWithGlobalProgram(bufferBuilder.end());
RenderSystem.setShaderColor(1, 1, 1, 1);

GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_BLEND);

// Draw Cube if a landing position exists.
if (landPosition != null) {
float size = blipSize.getValue();
Vec3d pos1 = landPosition.add(-size, -size, -size);
Vec3d pos2 = landPosition.add(size, size, size);
Box box = new Box(pos1.x, pos1.y, pos1.z, pos2.x, pos2.y, pos2.z);
RenderUtils.draw3DBox(matrix, box, renderColor);
}
}
}

public double throwableGravity(Item item) {
if(item == Items.BOW)
return -0.045f;
else
return -0.13f;
}
}

0 comments on commit 480bf35

Please sign in to comment.