Advertisement
Guest User

Untitled

a guest
Jan 11th, 2022
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.34 KB | None | 0 0
  1. package me.hasunemiku2015.signpic.TileEntityHandler;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6.  
  7. import com.mojang.blaze3d.matrix.MatrixStack;
  8. import com.mojang.blaze3d.platform.GlStateManager;
  9.  
  10. import org.lwjgl.opengl.GL11;
  11.  
  12. import net.minecraft.block.AbstractSignBlock;
  13. import net.minecraft.client.Minecraft;
  14. import net.minecraft.client.renderer.BufferBuilder;
  15. import net.minecraft.client.renderer.Tessellator;
  16. import net.minecraft.client.renderer.texture.DynamicTexture;
  17. import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
  18. import net.minecraft.util.math.BlockPos;
  19. import net.minecraft.util.math.vector.Matrix4f;
  20. import net.minecraft.util.math.vector.Vector3d;
  21. import net.minecraftforge.api.distmarker.Dist;
  22. import net.minecraftforge.api.distmarker.OnlyIn;
  23. import net.minecraftforge.client.event.RenderWorldLastEvent;
  24. import net.minecraftforge.eventbus.api.SubscribeEvent;
  25.  
  26. @OnlyIn(Dist.CLIENT)
  27. public class RenderEvent {
  28.   static Minecraft mc = Minecraft.getInstance();
  29.   protected static HashMap<BlockPos, RenderInfo> renderInfoMap = new HashMap<>();
  30.   protected static HashMap<String, DynamicTexture> textureMap = new HashMap<>();
  31.  
  32.   /**
  33.    * This event draws the image itself
  34.    */
  35.   @SuppressWarnings("deprecation")
  36.   @SubscribeEvent
  37.   public void renderImage(RenderWorldLastEvent event) {
  38.     if (!ModController.isEnabled) return;
  39.  
  40.     int renderDistance = mc.gameSettings.renderDistanceChunks;
  41.     Vector3d projView = mc.gameRenderer.getActiveRenderInfo().getProjectedView();
  42.     MatrixStack stack = event.getMatrixStack();
  43.  
  44.     List<String> deadKeys = new ArrayList<>();
  45.     for (RenderInfo info : renderInfoMap.values()) {
  46.       if(!textureMap.containsKey(info.texture)) continue;
  47.  
  48.       if (Math
  49.           .sqrt(Math.pow((info.x - mc.player.getPosX()), 2)
  50.               + Math.pow((info.z - mc.player.getPosZ()), 2)) > renderDistance * 16
  51.           || !(mc.world.getBlockState(new BlockPos(info.x, info.y, info.z)).getBlock() instanceof AbstractSignBlock)) {
  52.  
  53.         renderInfoMap.values().remove(info);
  54.         deadKeys.add(info.texture);
  55.         return;
  56.       }
  57.  
  58.       if(deadKeys.contains(info.texture)){
  59.         deadKeys.remove(info.texture);
  60.       }
  61.  
  62.       stack.push();
  63.       stack.translate(-projView.x, -projView.y, -projView.z);
  64.       Matrix4f matrix = stack.getLast().getMatrix();
  65.       GlStateManager.multMatrix(matrix);
  66.  
  67.       textureMap.get(info.texture).bindTexture();
  68.  
  69.       BufferBuilder buffer = Tessellator.getInstance().getBuffer();
  70.       buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX);
  71.       renderWithState(buffer, info);
  72.       Tessellator.getInstance().draw();
  73.  
  74.       // Fixes the hand problem (So only the tessellator got multiplied)
  75.       matrix.invert();
  76.       GlStateManager.multMatrix(matrix);
  77.       stack.pop();
  78.     }
  79.  
  80.     for(String key: deadKeys)
  81.       textureMap.remove(key);
  82.   }
  83.  
  84.   /**
  85.    *  Draws the image itself into the world, based on oritation code
  86.    *  @param buffer: The buffer build to draw the image
  87.    *  @param info: The data packet to be rendered
  88.    */
  89.   private void renderWithState(BufferBuilder buffer, RenderInfo info) {
  90.     switch (info.state) {
  91.       //0.45 not 0.5 to avoid culling
  92.       default:
  93.       case 0: {
  94.         double wallSignOffset = info.isWallSign ? 0.45 : 0;
  95.  
  96.         // South, Vertical
  97.         buffer.pos(-info.width / 2.0 + (info.x + 0.5) + info.offsetW, -info.height / 2.0 + (info.y + 0.5) + info.offsetH, (info.z + 0.5 + wallSignOffset)).tex(1.0F, 1.0F).endVertex();
  98.         buffer.pos(-info.width / 2.0 + (info.x + 0.5) + info.offsetW,  info.height / 2.0 + (info.y + 0.5) + info.offsetH, (info.z + 0.5 + wallSignOffset)).tex(1.0F, 0.0F).endVertex();
  99.         buffer.pos( info.width / 2.0 + (info.x + 0.5) + info.offsetW,  info.height / 2.0 + (info.y + 0.5) + info.offsetH, (info.z + 0.5 + wallSignOffset)).tex(0.0F, 0.0F).endVertex();
  100.         buffer.pos( info.width / 2.0 + (info.x + 0.5) + info.offsetW, -info.height / 2.0 + (info.y + 0.5) + info.offsetH, (info.z + 0.5 + wallSignOffset)).tex(0.0F, 1.0F).endVertex();
  101.         return;
  102.       }
  103.  
  104.       case 1: {
  105.         double wallSignOffset = info.isWallSign ? -0.45 : 0;
  106.  
  107.         // North, Vertical
  108.         buffer.pos( info.width / 2.0 + (info.x + 0.5) + info.offsetW, -info.height / 2.0 + (info.y + 0.5) + info.offsetH, (info.z + 0.5 + wallSignOffset)).tex(1.0F, 1.0F).endVertex();
  109.         buffer.pos( info.width / 2.0 + (info.x + 0.5) + info.offsetW,  info.height / 2.0 + (info.y + 0.5) + info.offsetH, (info.z + 0.5 + wallSignOffset)).tex(1.0F, 0.0F).endVertex();
  110.         buffer.pos(-info.width / 2.0 + (info.x + 0.5) + info.offsetW,  info.height / 2.0 + (info.y + 0.5) + info.offsetH, (info.z + 0.5 + wallSignOffset)).tex(0.0F, 0.0F).endVertex();
  111.         buffer.pos(-info.width / 2.0 + (info.x + 0.5) + info.offsetW, -info.height / 2.0 + (info.y + 0.5) + info.offsetH, (info.z + 0.5 + wallSignOffset)).tex(0.0F, 1.0F).endVertex();
  112.         return;
  113.       }
  114.  
  115.       case 2: {
  116.         double wallSignOffset = info.isWallSign ? 0.45 : 0;
  117.  
  118.         // East, Verical
  119.         buffer.pos((info.x + 0.5 + wallSignOffset), -info.height / 2.0 + (info.y + 0.5) + info.offsetH,  info.width / 2.0 + (info.z + 0.5) + info.offsetW).tex(1.0F, 1.0F).endVertex();
  120.         buffer.pos((info.x + 0.5 + wallSignOffset),  info.height / 2.0 + (info.y + 0.5) + info.offsetH,  info.width / 2.0 + (info.z + 0.5) + info.offsetW).tex(1.0F, 0.0F).endVertex();
  121.         buffer.pos((info.x + 0.5 + wallSignOffset),  info.height / 2.0 + (info.y + 0.5) + info.offsetH, -info.width / 2.0 + (info.z + 0.5) + info.offsetW).tex(0.0F, 0.0F).endVertex();
  122.         buffer.pos((info.x + 0.5 + wallSignOffset), -info.height / 2.0 + (info.y + 0.5) + info.offsetH, -info.width / 2.0 + (info.z + 0.5) + info.offsetW).tex(0.0F, 1.0F).endVertex();
  123.         return;
  124.       }
  125.  
  126.       case 3: {
  127.         double wallSignOffset = info.isWallSign ? -0.45 : 0;
  128.  
  129.         // West, Vertical
  130.         buffer.pos((info.x + 0.5 + wallSignOffset), -info.height / 2.0 + (info.y + 0.5) + info.offsetH, -info.width / 2.0 + (info.z + 0.5) + info.offsetW).tex(1.0F, 1.0F).endVertex();
  131.         buffer.pos((info.x + 0.5 + wallSignOffset),  info.height / 2.0 + (info.y + 0.5) + info.offsetH, -info.width / 2.0 + (info.z + 0.5) + info.offsetW).tex(1.0F, 0.0F).endVertex();
  132.         buffer.pos((info.x + 0.5 + wallSignOffset),  info.height / 2.0 + (info.y + 0.5) + info.offsetH,  info.width / 2.0 + (info.z + 0.5) + info.offsetW).tex(0.0F, 0.0F).endVertex();
  133.         buffer.pos((info.x + 0.5 + wallSignOffset), -info.height / 2.0 + (info.y + 0.5) + info.offsetH,  info.width / 2.0 + (info.z + 0.5) + info.offsetW).tex(0.0F, 1.0F).endVertex();
  134.         return;
  135.       }
  136.  
  137.       //Added 0.01 to y (arbitary minimal amount) to avoid culling.
  138.       case 4: {
  139.         // North, Horizontal
  140.         buffer.pos(-info.width / 2.0 + (info.x + 0.5) + info.offsetW, (info.y + 0.1),-info.height / 2.0 + (info.z + 0.5) + info.offsetH).tex(0.0F, 0.0F).endVertex();
  141.         buffer.pos(-info.width / 2.0 + (info.x + 0.5) + info.offsetW, (info.y + 0.1), info.height / 2.0 + (info.z + 0.5) + info.offsetH).tex(0.0F, 1.0F).endVertex();
  142.         buffer.pos( info.width / 2.0 + (info.x + 0.5) + info.offsetW, (info.y + 0.1), info.height / 2.0 + (info.z + 0.5) + info.offsetH).tex(1.0F, 1.0F).endVertex();
  143.         buffer.pos( info.width / 2.0 + (info.x + 0.5) + info.offsetW, (info.y + 0.1),-info.height / 2.0 + (info.z + 0.5) + info.offsetH).tex(1.0F, 0.0F).endVertex();
  144.         return;
  145.       }
  146.  
  147.       case 5: {
  148.         // East, Horizontal
  149.         buffer.pos( info.height / 2.0 + (info.x + 0.5) + info.offsetH, (info.y + 0.1), info.width / 2.0 + (info.z + 0.5) + info.offsetW).tex(1.0F, 0.0F).endVertex();
  150.         buffer.pos( info.height / 2.0 + (info.x + 0.5) + info.offsetH, (info.y + 0.1),-info.width / 2.0 + (info.z + 0.5) + info.offsetW).tex(0.0F, 0.0F).endVertex();
  151.         buffer.pos(-info.height / 2.0 + (info.x + 0.5) + info.offsetH, (info.y + 0.1),-info.width / 2.0 + (info.z + 0.5) + info.offsetW).tex(0.0F, 1.0F).endVertex();
  152.         buffer.pos(-info.height / 2.0 + (info.x + 0.5) + info.offsetH, (info.y + 0.1), info.width / 2.0 + (info.z + 0.5) + info.offsetW).tex(1.0F, 1.0F).endVertex();
  153.         return;
  154.       }
  155.  
  156.       case 6: {
  157.         // South, Horizontal
  158.         buffer.pos(-info.width / 2.0 + (info.x + 0.5) + info.offsetW, (info.y + 0.1),-info.height / 2.0 + (info.z + 0.5) + info.offsetH).tex(1.0F, 1.0F).endVertex();
  159.         buffer.pos(-info.width / 2.0 + (info.x + 0.5) + info.offsetW, (info.y + 0.1), info.height / 2.0 + (info.z + 0.5) + info.offsetH).tex(1.0F, 0.0F).endVertex();
  160.         buffer.pos( info.width / 2.0 + (info.x + 0.5) + info.offsetW, (info.y + 0.1), info.height / 2.0 + (info.z + 0.5) + info.offsetH).tex(0.0F, 0.0F).endVertex();
  161.         buffer.pos( info.width / 2.0 + (info.x + 0.5) + info.offsetW, (info.y + 0.1),-info.height / 2.0 + (info.z + 0.5) + info.offsetH).tex(0.0F, 1.0F).endVertex();
  162.         return;
  163.       }
  164.  
  165.       case 7: {
  166.         // West, Horizontal
  167.         buffer.pos( info.height / 2.0 + (info.x + 0.5) + info.offsetH, (info.y + 0.1), info.width / 2.0 + (info.z + 0.5) + info.offsetW).tex(0.0F, 1.0F).endVertex();
  168.         buffer.pos( info.height / 2.0 + (info.x + 0.5) + info.offsetH, (info.y + 0.1),-info.width / 2.0 + (info.z + 0.5) + info.offsetW).tex(1.0F, 1.0F).endVertex();
  169.         buffer.pos(-info.height / 2.0 + (info.x + 0.5) + info.offsetH, (info.y + 0.1),-info.width / 2.0 + (info.z + 0.5) + info.offsetW).tex(1.0F, 0.0F).endVertex();
  170.         buffer.pos(-info.height / 2.0 + (info.x + 0.5) + info.offsetH, (info.y + 0.1), info.width / 2.0 + (info.z + 0.5) + info.offsetW).tex(0.0F, 0.0F).endVertex();
  171.         return;
  172.       }
  173.     }
  174.   }
  175. }
  176.  
  177. class RenderInfo {
  178.   int x, y, z, state;
  179.   double height, width, offsetW, offsetH;
  180.   String texture;
  181.   boolean isWallSign;
  182.  
  183.   public RenderInfo(int x, int y, int z, double width, double height, double offSetW, double offSetH, int state, String texture,
  184.       boolean isWallSign) {
  185.     this.x = x;
  186.     this.y = y;
  187.     this.z = z;
  188.     this.width = width;
  189.     this.height = height;
  190.     this.offsetW= offSetW;
  191.     this.offsetH = offSetH;
  192.     this.state = state;
  193.     this.texture = texture;
  194.  
  195.     this.isWallSign = isWallSign;
  196.   }
  197.  
  198.   /**
  199.    * Register the RenderInfo so it gets rendered.
  200.    */
  201.   public void register() {
  202.     RenderEvent.renderInfoMap.put(new BlockPos(x, y, z), this);
  203.   }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement