Advertisement
Gamebuster

Untitled

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