Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.famzangl.minecraft.aimbow;
- import java.nio.*;
- import net.minecraft.client.*;
- import org.lwjgl.*;
- import net.minecraft.client.gui.*;
- import net.minecraft.client.entity.*;
- import net.minecraft.item.*;
- import java.lang.reflect.*;
- import net.minecraft.init.*;
- import net.minecraft.client.renderer.*;
- import org.lwjgl.util.glu.*;
- import net.minecraft.entity.*;
- import java.util.*;
- import net.famzangl.minecraft.aimbow.aiming.*;
- import org.lwjgl.opengl.*;
- import net.minecraft.client.renderer.chunk.*;
- import net.minecraft.util.*;
- public class AimbowGui extends GuiIngame
- {
- FloatBuffer modelBuffer;
- FloatBuffer projectionBuffer;
- IntBuffer viewPort;
- private final FloatBuffer win_pos;
- private final double zLevel = 0.0;
- Minecraft mc;
- private final ZoomController zc;
- private float partialTicks;
- public boolean autoAim;
- private MatrixCatcher catcher;
- public AimbowGui(final Minecraft mcIn) {
- super(mcIn);
- this.modelBuffer = BufferUtils.createFloatBuffer(16);
- this.projectionBuffer = BufferUtils.createFloatBuffer(16);
- this.viewPort = BufferUtils.createIntBuffer(4);
- this.win_pos = BufferUtils.createFloatBuffer(3);
- this.mc = Minecraft.getMinecraft();
- this.zc = new ZoomController();
- }
- public void renderGameOverlay(final float partialTicks) {
- super.renderGameOverlay(this.partialTicks = partialTicks);
- }
- protected boolean showCrosshair() {
- final EntityPlayerSP player = this.mc.thePlayer;
- final ItemStack heldItem = player.getHeldItem();
- final ColissionSolver colissionSolver = ColissionSolver.forItem(heldItem, this.mc);
- if (colissionSolver != null) {
- this.checkForMatrixStealing();
- final ScaledResolution resolution = new ScaledResolution(this.mc);
- boolean colissionDrawn = false;
- final ArrayList<ColissionData> colissionPoints = colissionSolver.computeCurrentColissionPoints();
- for (final ColissionData p : colissionPoints) {
- final Pos2 pos = this.getPositionOnScreen(this.mc, p.x, p.y + player.getEyeHeight(), p.z, resolution);
- final boolean hit = p.hitEntity != null;
- this.drawCrosshairAt(this.mc, pos.x, pos.y, hit ? 0.0f : 1.0f, hit ? 1.0f : 0.0f, 0.0f);
- this.zc.zoomTowards(new Vec3(p.x, p.y, p.z));
- if (!colissionDrawn && !hit && this.autoAim && this.shouldAutoaim(heldItem)) {
- this.aimAtCloseEntity(pos, resolution, colissionSolver);
- }
- colissionDrawn = true;
- }
- if (!colissionDrawn) {
- final int x = resolution.getScaledWidth() / 2;
- final int y = resolution.getScaledHeight() / 2;
- this.drawCrosshairAt(this.mc, x, y, 0.6f, 0.6f, 0.6f);
- }
- return false;
- }
- return super.showCrosshair();
- }
- private void checkForMatrixStealing() {
- try {
- for (final Field field : RenderGlobal.class.getDeclaredFields()) {
- if (field.getType() == ChunkRenderContainer.class) {
- field.setAccessible(true);
- final Object current = field.get(this.mc.renderGlobal);
- if (!(current instanceof MatrixCatcher)) {
- this.catcher = new MatrixCatcher((ChunkRenderContainer)current);
- field.set(this.mc.renderGlobal, this.catcher);
- }
- }
- }
- }
- catch (Throwable t) {
- t.printStackTrace();
- }
- }
- private boolean shouldAutoaim(final ItemStack heldItem) {
- final int count = this.mc.thePlayer.getItemInUseCount();
- return heldItem.getItem() != Items.bow || count > 0;
- }
- private void drawCrosshairAt(final Minecraft mc, final int x, final int y, final float r, final float g, final float b) {
- GlStateManager.enableBlend();
- GlStateManager.enableAlpha();
- this.drawTexturedModalRect(x - 7, y - 7, 0, 0, 16, 16, r, g, b);
- }
- public void drawTexturedModalRect(final int par1, final int par2, final int par3, final int par4, final int par5, final int par6, final float r, final float g, final float b) {
- final float f = 0.00390625f;
- final float f2 = 0.00390625f;
- final WorldRenderer tessellator = Tessellator.getInstance().getWorldRenderer();
- tessellator.startDrawingQuads();
- tessellator.setColorRGBA_F(r, g, b, 0.5f);
- final WorldRenderer worldRenderer = tessellator;
- final double n = par1 + 0;
- final double n2 = par2 + par6;
- this.getClass();
- worldRenderer.addVertexWithUV(n, n2, 0.0, (double)((par3 + 0) * f), (double)((par4 + par6) * f2));
- final WorldRenderer worldRenderer2 = tessellator;
- final double n3 = par1 + par5;
- final double n4 = par2 + par6;
- this.getClass();
- worldRenderer2.addVertexWithUV(n3, n4, 0.0, (double)((par3 + par5) * f), (double)((par4 + par6) * f2));
- final WorldRenderer worldRenderer3 = tessellator;
- final double n5 = par1 + par5;
- final double n6 = par2 + 0;
- this.getClass();
- worldRenderer3.addVertexWithUV(n5, n6, 0.0, (double)((par3 + par5) * f), (double)((par4 + 0) * f2));
- final WorldRenderer worldRenderer4 = tessellator;
- final double n7 = par1 + 0;
- final double n8 = par2 + 0;
- this.getClass();
- worldRenderer4.addVertexWithUV(n7, n8, 0.0, (double)((par3 + 0) * f), (double)((par4 + 0) * f2));
- Tessellator.getInstance().draw();
- }
- public Pos2 getPositionOnScreen(final Minecraft mc, final double x, final double y, final double z, final ScaledResolution resolution) {
- final Vec3 player = mc.getRenderViewEntity().getPositionEyes(this.partialTicks);
- this.viewPort.rewind();
- this.viewPort.put(0);
- this.viewPort.put(0);
- this.viewPort.put(resolution.getScaledWidth());
- this.viewPort.put(resolution.getScaledHeight());
- this.viewPort.rewind();
- this.win_pos.rewind();
- this.modelBuffer.rewind();
- this.projectionBuffer.rewind();
- GLU.gluProject((float)(x - player.xCoord), (float)(y - player.yCoord), (float)(z - player.zCoord), this.modelBuffer, this.projectionBuffer, this.viewPort, this.win_pos);
- this.win_pos.rewind();
- final int sx = (int)this.win_pos.get();
- final int sy = resolution.getScaledHeight() - (int)this.win_pos.get();
- if (this.win_pos.get() < 1.0f) {
- return new Pos2(sx, sy);
- }
- return new Pos2(-100, -100);
- }
- public void aimAtCloseEntity(final Pos2 toScreenPos, final ScaledResolution resolution, final ColissionSolver colissionSolver) {
- final AxisAlignedBB bbox = this.mc.thePlayer.getEntityBoundingBox();
- final List<Entity> entities = (List<Entity>)this.mc.theWorld.getEntitiesWithinAABB((Class)Entity.class, bbox.expand(200.0, 100.0, 200.0));
- final ArrayList<CloseEntity> nearEntities = new ArrayList<CloseEntity>();
- for (final Entity e : entities) {
- if (e.canBeCollidedWith() && e != Minecraft.getMinecraft().thePlayer) {
- final Pos2 onScreen = this.getPositionOnScreen(this.mc, e.posX, e.posY, e.posZ, resolution);
- final double d = toScreenPos.distanceTo(onScreen);
- if (d >= 100.0) {
- continue;
- }
- nearEntities.add(new CloseEntity(e, d));
- }
- }
- Collections.sort(nearEntities);
- final ReverseBowSolver rbs = new ReverseBowSolver(colissionSolver.getGravity(), colissionSolver.getVelocity());
- for (final CloseEntity e2 : nearEntities) {
- final Vec3 look = rbs.getLookForTarget(e2.entity);
- final ArrayList<ColissionData> foundColissions = colissionSolver.computeColissionWithLook(look);
- if (foundColissions.size() > 0 && foundColissions.get(0).hitEntity == e2.entity) {
- this.lookAt(look);
- break;
- }
- }
- }
- private void lookAt(final Vec3 look) {
- final double d0 = look.xCoord;
- final double d2 = look.zCoord;
- final double d3 = look.yCoord;
- final double d4 = d0 * d0 + d3 * d3 + d2 * d2;
- if (d4 >= 2.500000277905201E-7) {
- final float rotationYaw = this.mc.thePlayer.rotationYaw;
- final float rotationPitch = this.mc.thePlayer.rotationPitch;
- final float yaw = (float)(Math.atan2(d2, d0) * 180.0 / 3.141592653589793) - 90.0f;
- final float pitch = (float)(-(Math.atan2(d3, Math.sqrt(d0 * d0 + d2 * d2)) * 180.0 / 3.141592653589793));
- this.mc.thePlayer.setAngles((yaw - rotationYaw) / 0.15f, -(pitch - rotationPitch) / 0.15f);
- }
- }
- public void stealProjectionMatrix() {
- this.modelBuffer.rewind();
- this.projectionBuffer.rewind();
- GL11.glGetFloat(2982, this.modelBuffer);
- GL11.glGetFloat(2983, this.projectionBuffer);
- }
- private static class CloseEntity implements Comparable<CloseEntity>
- {
- private final Entity entity;
- private final double distance;
- public CloseEntity(final Entity entity, final double distance) {
- this.entity = entity;
- this.distance = distance;
- }
- @Override
- public int compareTo(final CloseEntity o) {
- return Double.compare(this.distance, o.distance);
- }
- @Override
- public String toString() {
- return "CloseEntity [entity=" + this.entity + ", distance=" + this.distance + "]";
- }
- }
- public class MatrixCatcher extends ChunkRenderContainer
- {
- ChunkRenderContainer base;
- public void addRenderChunk(final RenderChunk p_178002_1_, final EnumWorldBlockLayer p_178002_2_) {
- this.base.addRenderChunk(p_178002_1_, p_178002_2_);
- }
- public boolean equals(final Object obj) {
- return this.base.equals(obj);
- }
- public int hashCode() {
- return this.base.hashCode();
- }
- public void initialize(final double p_178004_1_, final double p_178004_3_, final double p_178004_5_) {
- this.base.initialize(p_178004_1_, p_178004_3_, p_178004_5_);
- }
- public void preRenderChunk(final RenderChunk p_178003_1_) {
- this.base.preRenderChunk(p_178003_1_);
- }
- public void renderChunkLayer(final EnumWorldBlockLayer p_178001_1_) {
- if (AimbowGui.this.catcher == this) {
- AimbowGui.this.stealProjectionMatrix();
- }
- this.base.renderChunkLayer(p_178001_1_);
- }
- public String toString() {
- return this.base.toString();
- }
- public MatrixCatcher(final ChunkRenderContainer base) {
- this.base = base;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement