Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.17 KB | None | 0 0
  1. package net.famzangl.minecraft.aimbow;
  2.  
  3. import java.nio.*;
  4. import net.minecraft.client.*;
  5. import org.lwjgl.*;
  6. import net.minecraft.client.gui.*;
  7. import net.minecraft.client.entity.*;
  8. import net.minecraft.item.*;
  9. import java.lang.reflect.*;
  10. import net.minecraft.init.*;
  11. import net.minecraft.client.renderer.*;
  12. import org.lwjgl.util.glu.*;
  13. import net.minecraft.entity.*;
  14. import java.util.*;
  15. import net.famzangl.minecraft.aimbow.aiming.*;
  16. import org.lwjgl.opengl.*;
  17. import net.minecraft.client.renderer.chunk.*;
  18. import net.minecraft.util.*;
  19.  
  20. public class AimbowGui extends GuiIngame
  21. {
  22. FloatBuffer modelBuffer;
  23. FloatBuffer projectionBuffer;
  24. IntBuffer viewPort;
  25. private final FloatBuffer win_pos;
  26. private final double zLevel = 0.0;
  27. Minecraft mc;
  28. private final ZoomController zc;
  29. private float partialTicks;
  30. public boolean autoAim;
  31. private MatrixCatcher catcher;
  32.  
  33. public AimbowGui(final Minecraft mcIn) {
  34. super(mcIn);
  35. this.modelBuffer = BufferUtils.createFloatBuffer(16);
  36. this.projectionBuffer = BufferUtils.createFloatBuffer(16);
  37. this.viewPort = BufferUtils.createIntBuffer(4);
  38. this.win_pos = BufferUtils.createFloatBuffer(3);
  39. this.mc = Minecraft.getMinecraft();
  40. this.zc = new ZoomController();
  41. }
  42.  
  43. public void renderGameOverlay(final float partialTicks) {
  44. super.renderGameOverlay(this.partialTicks = partialTicks);
  45. }
  46.  
  47. protected boolean showCrosshair() {
  48. final EntityPlayerSP player = this.mc.thePlayer;
  49. final ItemStack heldItem = player.getHeldItem();
  50. final ColissionSolver colissionSolver = ColissionSolver.forItem(heldItem, this.mc);
  51. if (colissionSolver != null) {
  52. this.checkForMatrixStealing();
  53. final ScaledResolution resolution = new ScaledResolution(this.mc);
  54. boolean colissionDrawn = false;
  55. final ArrayList<ColissionData> colissionPoints = colissionSolver.computeCurrentColissionPoints();
  56. for (final ColissionData p : colissionPoints) {
  57. final Pos2 pos = this.getPositionOnScreen(this.mc, p.x, p.y + player.getEyeHeight(), p.z, resolution);
  58. final boolean hit = p.hitEntity != null;
  59. this.drawCrosshairAt(this.mc, pos.x, pos.y, hit ? 0.0f : 1.0f, hit ? 1.0f : 0.0f, 0.0f);
  60. this.zc.zoomTowards(new Vec3(p.x, p.y, p.z));
  61. if (!colissionDrawn && !hit && this.autoAim && this.shouldAutoaim(heldItem)) {
  62. this.aimAtCloseEntity(pos, resolution, colissionSolver);
  63. }
  64. colissionDrawn = true;
  65. }
  66. if (!colissionDrawn) {
  67. final int x = resolution.getScaledWidth() / 2;
  68. final int y = resolution.getScaledHeight() / 2;
  69. this.drawCrosshairAt(this.mc, x, y, 0.6f, 0.6f, 0.6f);
  70. }
  71. return false;
  72. }
  73. return super.showCrosshair();
  74. }
  75.  
  76. private void checkForMatrixStealing() {
  77. try {
  78. for (final Field field : RenderGlobal.class.getDeclaredFields()) {
  79. if (field.getType() == ChunkRenderContainer.class) {
  80. field.setAccessible(true);
  81. final Object current = field.get(this.mc.renderGlobal);
  82. if (!(current instanceof MatrixCatcher)) {
  83. this.catcher = new MatrixCatcher((ChunkRenderContainer)current);
  84. field.set(this.mc.renderGlobal, this.catcher);
  85. }
  86. }
  87. }
  88. }
  89. catch (Throwable t) {
  90. t.printStackTrace();
  91. }
  92. }
  93.  
  94. private boolean shouldAutoaim(final ItemStack heldItem) {
  95. final int count = this.mc.thePlayer.getItemInUseCount();
  96. return heldItem.getItem() != Items.bow || count > 0;
  97. }
  98.  
  99. private void drawCrosshairAt(final Minecraft mc, final int x, final int y, final float r, final float g, final float b) {
  100. GlStateManager.enableBlend();
  101. GlStateManager.enableAlpha();
  102. this.drawTexturedModalRect(x - 7, y - 7, 0, 0, 16, 16, r, g, b);
  103. }
  104.  
  105. 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) {
  106. final float f = 0.00390625f;
  107. final float f2 = 0.00390625f;
  108. final WorldRenderer tessellator = Tessellator.getInstance().getWorldRenderer();
  109. tessellator.startDrawingQuads();
  110. tessellator.setColorRGBA_F(r, g, b, 0.5f);
  111. final WorldRenderer worldRenderer = tessellator;
  112. final double n = par1 + 0;
  113. final double n2 = par2 + par6;
  114. this.getClass();
  115. worldRenderer.addVertexWithUV(n, n2, 0.0, (double)((par3 + 0) * f), (double)((par4 + par6) * f2));
  116. final WorldRenderer worldRenderer2 = tessellator;
  117. final double n3 = par1 + par5;
  118. final double n4 = par2 + par6;
  119. this.getClass();
  120. worldRenderer2.addVertexWithUV(n3, n4, 0.0, (double)((par3 + par5) * f), (double)((par4 + par6) * f2));
  121. final WorldRenderer worldRenderer3 = tessellator;
  122. final double n5 = par1 + par5;
  123. final double n6 = par2 + 0;
  124. this.getClass();
  125. worldRenderer3.addVertexWithUV(n5, n6, 0.0, (double)((par3 + par5) * f), (double)((par4 + 0) * f2));
  126. final WorldRenderer worldRenderer4 = tessellator;
  127. final double n7 = par1 + 0;
  128. final double n8 = par2 + 0;
  129. this.getClass();
  130. worldRenderer4.addVertexWithUV(n7, n8, 0.0, (double)((par3 + 0) * f), (double)((par4 + 0) * f2));
  131. Tessellator.getInstance().draw();
  132. }
  133.  
  134. public Pos2 getPositionOnScreen(final Minecraft mc, final double x, final double y, final double z, final ScaledResolution resolution) {
  135. final Vec3 player = mc.getRenderViewEntity().getPositionEyes(this.partialTicks);
  136. this.viewPort.rewind();
  137. this.viewPort.put(0);
  138. this.viewPort.put(0);
  139. this.viewPort.put(resolution.getScaledWidth());
  140. this.viewPort.put(resolution.getScaledHeight());
  141. this.viewPort.rewind();
  142. this.win_pos.rewind();
  143. this.modelBuffer.rewind();
  144. this.projectionBuffer.rewind();
  145. GLU.gluProject((float)(x - player.xCoord), (float)(y - player.yCoord), (float)(z - player.zCoord), this.modelBuffer, this.projectionBuffer, this.viewPort, this.win_pos);
  146. this.win_pos.rewind();
  147. final int sx = (int)this.win_pos.get();
  148. final int sy = resolution.getScaledHeight() - (int)this.win_pos.get();
  149. if (this.win_pos.get() < 1.0f) {
  150. return new Pos2(sx, sy);
  151. }
  152. return new Pos2(-100, -100);
  153. }
  154.  
  155. public void aimAtCloseEntity(final Pos2 toScreenPos, final ScaledResolution resolution, final ColissionSolver colissionSolver) {
  156. final AxisAlignedBB bbox = this.mc.thePlayer.getEntityBoundingBox();
  157. final List<Entity> entities = (List<Entity>)this.mc.theWorld.getEntitiesWithinAABB((Class)Entity.class, bbox.expand(200.0, 100.0, 200.0));
  158. final ArrayList<CloseEntity> nearEntities = new ArrayList<CloseEntity>();
  159. for (final Entity e : entities) {
  160. if (e.canBeCollidedWith() && e != Minecraft.getMinecraft().thePlayer) {
  161. final Pos2 onScreen = this.getPositionOnScreen(this.mc, e.posX, e.posY, e.posZ, resolution);
  162. final double d = toScreenPos.distanceTo(onScreen);
  163. if (d >= 100.0) {
  164. continue;
  165. }
  166. nearEntities.add(new CloseEntity(e, d));
  167. }
  168. }
  169. Collections.sort(nearEntities);
  170. final ReverseBowSolver rbs = new ReverseBowSolver(colissionSolver.getGravity(), colissionSolver.getVelocity());
  171. for (final CloseEntity e2 : nearEntities) {
  172. final Vec3 look = rbs.getLookForTarget(e2.entity);
  173. final ArrayList<ColissionData> foundColissions = colissionSolver.computeColissionWithLook(look);
  174. if (foundColissions.size() > 0 && foundColissions.get(0).hitEntity == e2.entity) {
  175. this.lookAt(look);
  176. break;
  177. }
  178. }
  179. }
  180.  
  181. private void lookAt(final Vec3 look) {
  182. final double d0 = look.xCoord;
  183. final double d2 = look.zCoord;
  184. final double d3 = look.yCoord;
  185. final double d4 = d0 * d0 + d3 * d3 + d2 * d2;
  186. if (d4 >= 2.500000277905201E-7) {
  187. final float rotationYaw = this.mc.thePlayer.rotationYaw;
  188. final float rotationPitch = this.mc.thePlayer.rotationPitch;
  189. final float yaw = (float)(Math.atan2(d2, d0) * 180.0 / 3.141592653589793) - 90.0f;
  190. final float pitch = (float)(-(Math.atan2(d3, Math.sqrt(d0 * d0 + d2 * d2)) * 180.0 / 3.141592653589793));
  191. this.mc.thePlayer.setAngles((yaw - rotationYaw) / 0.15f, -(pitch - rotationPitch) / 0.15f);
  192. }
  193. }
  194.  
  195. public void stealProjectionMatrix() {
  196. this.modelBuffer.rewind();
  197. this.projectionBuffer.rewind();
  198. GL11.glGetFloat(2982, this.modelBuffer);
  199. GL11.glGetFloat(2983, this.projectionBuffer);
  200. }
  201.  
  202. private static class CloseEntity implements Comparable<CloseEntity>
  203. {
  204. private final Entity entity;
  205. private final double distance;
  206.  
  207. public CloseEntity(final Entity entity, final double distance) {
  208. this.entity = entity;
  209. this.distance = distance;
  210. }
  211.  
  212. @Override
  213. public int compareTo(final CloseEntity o) {
  214. return Double.compare(this.distance, o.distance);
  215. }
  216.  
  217. @Override
  218. public String toString() {
  219. return "CloseEntity [entity=" + this.entity + ", distance=" + this.distance + "]";
  220. }
  221. }
  222.  
  223. public class MatrixCatcher extends ChunkRenderContainer
  224. {
  225. ChunkRenderContainer base;
  226.  
  227. public void addRenderChunk(final RenderChunk p_178002_1_, final EnumWorldBlockLayer p_178002_2_) {
  228. this.base.addRenderChunk(p_178002_1_, p_178002_2_);
  229. }
  230.  
  231. public boolean equals(final Object obj) {
  232. return this.base.equals(obj);
  233. }
  234.  
  235. public int hashCode() {
  236. return this.base.hashCode();
  237. }
  238.  
  239. public void initialize(final double p_178004_1_, final double p_178004_3_, final double p_178004_5_) {
  240. this.base.initialize(p_178004_1_, p_178004_3_, p_178004_5_);
  241. }
  242.  
  243. public void preRenderChunk(final RenderChunk p_178003_1_) {
  244. this.base.preRenderChunk(p_178003_1_);
  245. }
  246.  
  247. public void renderChunkLayer(final EnumWorldBlockLayer p_178001_1_) {
  248. if (AimbowGui.this.catcher == this) {
  249. AimbowGui.this.stealProjectionMatrix();
  250. }
  251. this.base.renderChunkLayer(p_178001_1_);
  252. }
  253.  
  254. public String toString() {
  255. return this.base.toString();
  256. }
  257.  
  258. public MatrixCatcher(final ChunkRenderContainer base) {
  259. this.base = base;
  260. }
  261. }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement