Advertisement
Gamebuster

Untitled

Jul 9th, 2018
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.78 KB | None | 0 0
  1. package com.gamebuster19901.dungeonforge.client.render.tileentity;
  2.  
  3. import org.apache.logging.log4j.Level;
  4. import org.lwjgl.opengl.GL11;
  5.  
  6. import com.gamebuster19901.dungeonforge.Main;
  7. import com.gamebuster19901.dungeonforge.block.BlockWrapper;
  8. import com.gamebuster19901.dungeonforge.tileentity.TileEntityChangeableBlock;
  9.  
  10. import net.minecraft.block.Block;
  11. import net.minecraft.block.material.Material;
  12. import net.minecraft.block.state.IBlockState;
  13.  
  14. import net.minecraft.client.Minecraft;
  15. import net.minecraft.client.entity.EntityPlayerSP;
  16. import net.minecraft.client.renderer.BlockRendererDispatcher;
  17. import net.minecraft.client.renderer.BufferBuilder;
  18. import net.minecraft.client.renderer.GlStateManager;
  19. import net.minecraft.client.renderer.RenderHelper;
  20. import net.minecraft.client.renderer.Tessellator;
  21. import net.minecraft.client.renderer.texture.TextureMap;
  22. import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
  23. import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
  24. import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
  25.  
  26. import net.minecraft.entity.player.EntityPlayer;
  27.  
  28. import net.minecraft.tileentity.TileEntity;
  29.  
  30. import net.minecraft.util.math.BlockPos;
  31. import net.minecraft.util.math.MathHelper;
  32. import net.minecraft.util.text.ITextComponent;
  33.  
  34. public class TileEntityChangeableBlockRenderer<T extends TileEntityChangeableBlock> extends TileEntitySpecialRenderer<TileEntityChangeableBlock> {
  35.     protected BlockRendererDispatcher renderer;
  36.     private boolean canRenderTE = true;
  37.    
  38.     @Override
  39.     public void render(TileEntityChangeableBlock te, double x, double y, double z, float partialTicks, int destroyStage, float alpha){
  40.         EntityPlayerSP player = Minecraft.getMinecraft().player;
  41.        
  42.         BlockWrapper wrapper = te.getDisplayingWrapper(player);
  43.        
  44.         renderNameplate(te, x, y, z, player);
  45.        
  46.         renderer = Minecraft.getMinecraft().getBlockRendererDispatcher();
  47.        
  48.         boolean shouldRenderBlock = renderWrappedTileEntity(wrapper, x, y, z, partialTicks, destroyStage, alpha);
  49.        
  50.         if (shouldRenderBlock(wrapper, te.getPos(), shouldRenderBlock)) {
  51.             renderWrappedBlock(te, wrapper, x, y, z);
  52.         }
  53.  
  54.     }
  55.    
  56.     private void renderNameplate(TileEntityChangeableBlock te, double x, double y, double z, EntityPlayer player) {
  57.         this.setLightmapDisabled(true);
  58.         ITextComponent itextcomponent = ((TileEntityChangeableBlock) te).getHoverText(player);
  59.         if (itextcomponent != null && this.rendererDispatcher.cameraHitResult != null && te.getPos().equals(this.rendererDispatcher.cameraHitResult.getBlockPos()))
  60.         {
  61.             if(player.isSneaking()) {
  62.                 this.drawNameplate(te, "When connected:", x, y + 0.3, z, 12);
  63.             }
  64.             else {
  65.                 this.drawNameplate(te, "When disconnected:", x, y + 0.3, z, 12);
  66.             }
  67.             this.drawNameplate(te, itextcomponent.getFormattedText(), x, y, z, 12);
  68.         }
  69.         this.setLightmapDisabled(false);
  70.     }
  71.    
  72.     /**
  73.      *
  74.      * @param wrapper the wrapper of the tile entity to be rendered
  75.      * @param x
  76.      * @param y
  77.      * @param z
  78.      * @param partialTicks
  79.      * @param destroyStage
  80.      * @param alpha
  81.      * @return true if the tile entity rendered, false otherwise
  82.      */
  83.     private boolean renderWrappedTileEntity(BlockWrapper wrapper, double x, double y, double z, float partialTicks, int destroyStage, float alpha) {
  84.        
  85.         Block block = wrapper.getBlock();
  86.         BlockPos blockpos = new BlockPos(0,0,0);
  87.         IBlockState blockState = block.getActualState(block.getStateFromMeta(wrapper.getMeta()), getWorld(), blockpos);
  88.        
  89.         TileEntitySpecialRenderer wrappedRenderer = null;
  90.        
  91.         if(block.hasTileEntity() && canRenderTE){
  92.             TileEntity wrappedTileEntity = block.createTileEntity(getWorld(), block.getActualState(block.getStateFromMeta(wrapper.getMeta()), getWorld(), new BlockPos(x,y,z)));
  93.             Class<TileEntity> wrappedTileEntityClass = (Class<TileEntity>) wrappedTileEntity.getClass();
  94.             if (wrappedTileEntity != null) {
  95.                 wrappedRenderer = TileEntityRendererDispatcher.instance.renderers.get(wrappedTileEntityClass);
  96.                 if(wrappedRenderer != null) {
  97.                     try {
  98.                        
  99.                         RenderHelper.disableStandardItemLighting();
  100.                         GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
  101.                         GlStateManager.enableBlend();
  102.                         GlStateManager.enableLighting();
  103.  
  104.                         if (Minecraft.isAmbientOcclusionEnabled())
  105.                         {
  106.                             GlStateManager.shadeModel(GL11.GL_SMOOTH);
  107.                         }
  108.                         else
  109.                         {
  110.                             GlStateManager.shadeModel(GL11.GL_FLAT);
  111.                         }
  112.                        
  113.                         wrappedTileEntity.setWorld(getWorld());
  114.                         wrappedRenderer.render(wrappedTileEntity, x, y, z, partialTicks, destroyStage, alpha);
  115.  
  116.                         return true;
  117.                     }
  118.                     catch(Exception | AssertionError e) {
  119.                         UnsupportedOperationException ex = new UnsupportedOperationException("Could not render TileEntityDoorwayMarkerRenderer with nbt " + wrapper.getNBT(), e);
  120.                         Main.LOGGER.catching(Level.ERROR, ex);
  121.                         canRenderTE = false;
  122.                     }
  123.                 }
  124.             }
  125.         }
  126.        
  127.         return false;
  128.     }
  129.    
  130.     private boolean shouldRenderBlock(BlockWrapper wrapper, BlockPos tepos, boolean didTileEntityRender) {
  131.         Block block = wrapper.getBlock();
  132.         IBlockState blockState = block.getActualState(block.getStateFromMeta(wrapper.getMeta()), getWorld(), tepos);
  133.         return block.getMaterial(blockState) != Material.AIR && !didTileEntityRender || (didTileEntityRender && block.getBoundingBox(blockState, getWorld(), tepos).equals(Block.FULL_BLOCK_AABB));
  134.     }
  135.    
  136.     private void renderWrappedBlock(TileEntityChangeableBlock te, BlockWrapper wrapper, double x, double y, double z) {
  137.        
  138.         Tessellator tessellator = Tessellator.getInstance();
  139.        
  140.         BufferBuilder bufferbuilder = tessellator.getBuffer();
  141.         Block block = wrapper.getBlock();
  142.         BlockPos tepos = te.getPos();
  143.         IBlockState blockState = block.getActualState(block.getStateFromMeta(wrapper.getMeta()), getWorld(), tepos);
  144.        
  145.        
  146.        
  147.         this.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
  148.         RenderHelper.disableStandardItemLighting();
  149.         GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
  150.         GlStateManager.enableBlend();
  151.         GlStateManager.enableLighting();
  152.  
  153.         if (Minecraft.isAmbientOcclusionEnabled())
  154.         {
  155.             GlStateManager.shadeModel(GL11.GL_SMOOTH);
  156.         }
  157.         else
  158.         {
  159.             GlStateManager.shadeModel(GL11.GL_FLAT);
  160.         }
  161.        
  162.         bufferbuilder.begin(GL11.GL_QUADS, DefaultVertexFormats.BLOCK);
  163.         bufferbuilder.setTranslation(x - tepos.getX(), y - tepos.getY(), z - tepos.getZ());
  164.         renderer.getBlockModelRenderer().renderModel(getWorld(), renderer.getModelForState(blockState), blockState, tepos, bufferbuilder, false, MathHelper.getPositionRandom(BlockPos.ORIGIN));
  165.         tessellator.draw();
  166.         bufferbuilder.setTranslation(0.0D, 0.0D, 0.0D);
  167.     }
  168.    
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement