Advertisement
Gamebuster

Untitled

Jul 10th, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.56 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 renderedTileEntity = renderWrappedTileEntity(wrapper, x, y, z, partialTicks, destroyStage, alpha);
  49.        
  50.         if (shouldRenderBlock(wrapper, te.getPos(), renderedTileEntity)) {
  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.         if(block.hasTileEntity() && canRenderTE){
  91.             TileEntity wrappedTileEntity = block.createTileEntity(getWorld(), block.getActualState(block.getStateFromMeta(wrapper.getMeta()), getWorld(), new BlockPos(x,y,z)));
  92.             Class<TileEntity> wrappedTileEntityClass = (Class<TileEntity>) wrappedTileEntity.getClass();
  93.             if (wrappedTileEntity != null) {
  94.                 wrappedRenderer = TileEntityRendererDispatcher.instance.renderers.get(wrappedTileEntityClass);
  95.                 if(wrappedRenderer != null) {
  96.                     try {
  97.                         RenderHelper.disableStandardItemLighting();
  98.                         GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
  99.                         GlStateManager.enableBlend();
  100.                         GlStateManager.disableLighting();
  101.                         if (Minecraft.isAmbientOcclusionEnabled())
  102.                         {
  103.                             GlStateManager.shadeModel(GL11.GL_SMOOTH);
  104.                         }
  105.                         else
  106.                         {
  107.                             GlStateManager.shadeModel(GL11.GL_FLAT);
  108.                         }
  109.                         wrappedTileEntity.setWorld(getWorld());
  110.                         wrappedRenderer.render(wrappedTileEntity, x, y, z, partialTicks, destroyStage, alpha);
  111.  
  112.                         return true;
  113.                     }
  114.                     catch(Exception | AssertionError e) {
  115.                         UnsupportedOperationException ex = new UnsupportedOperationException("Could not render TileEntityDoorwayMarkerRenderer with nbt " + wrapper.getNBT(), e);
  116.                         Main.LOGGER.catching(Level.ERROR, ex);
  117.                         canRenderTE = false;
  118.                     }
  119.                 }
  120.             }
  121.         }
  122.         return false;
  123.     }
  124.    
  125.     private boolean shouldRenderBlock(BlockWrapper wrapper, BlockPos tepos, boolean didTileEntityRender) {
  126.         Block block = wrapper.getBlock();
  127.         IBlockState blockState = block.getActualState(block.getStateFromMeta(wrapper.getMeta()), getWorld(), tepos);
  128.         return block.getMaterial(blockState) != Material.AIR && !didTileEntityRender || (didTileEntityRender && block.getBoundingBox(blockState, getWorld(), tepos).equals(Block.FULL_BLOCK_AABB));
  129.     }
  130.    
  131.     private void renderWrappedBlock(TileEntityChangeableBlock te, BlockWrapper wrapper, double x, double y, double z) {
  132.        
  133.         Tessellator tessellator = Tessellator.getInstance();
  134.        
  135.         BufferBuilder bufferbuilder = tessellator.getBuffer();
  136.         Block block = wrapper.getBlock();
  137.         BlockPos tepos = te.getPos();
  138.         IBlockState blockState = block.getActualState(block.getStateFromMeta(wrapper.getMeta()), getWorld(), tepos);
  139.        
  140.         this.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
  141.         RenderHelper.disableStandardItemLighting();
  142.         GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
  143.         GlStateManager.enableBlend();
  144.         GlStateManager.disableLighting();
  145.  
  146.         if (Minecraft.isAmbientOcclusionEnabled())
  147.         {
  148.             GlStateManager.shadeModel(GL11.GL_SMOOTH);
  149.         }
  150.         else
  151.         {
  152.             GlStateManager.shadeModel(GL11.GL_FLAT);
  153.         }
  154.        
  155.         bufferbuilder.begin(GL11.GL_QUADS, DefaultVertexFormats.BLOCK);
  156.         bufferbuilder.setTranslation(x - tepos.getX(), y - tepos.getY(), z - tepos.getZ());
  157.         renderer.getBlockModelRenderer().renderModel(getWorld(), renderer.getModelForState(blockState), blockState, tepos, bufferbuilder, false, MathHelper.getPositionRandom(BlockPos.ORIGIN));
  158.         tessellator.draw();
  159.         bufferbuilder.setTranslation(0.0D, 0.0D, 0.0D);
  160.     }
  161.    
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement