Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gamebuster19901.dungeonforge.client.render.tileentity;
- import org.apache.logging.log4j.Level;
- import org.lwjgl.opengl.GL11;
- import com.gamebuster19901.dungeonforge.Main;
- import com.gamebuster19901.dungeonforge.block.BlockWrapper;
- import com.gamebuster19901.dungeonforge.tileentity.TileEntityChangeableBlock;
- import net.minecraft.block.Block;
- import net.minecraft.block.material.Material;
- import net.minecraft.block.state.IBlockState;
- import net.minecraft.client.Minecraft;
- import net.minecraft.client.entity.EntityPlayerSP;
- import net.minecraft.client.renderer.BlockRendererDispatcher;
- import net.minecraft.client.renderer.BufferBuilder;
- import net.minecraft.client.renderer.GlStateManager;
- import net.minecraft.client.renderer.RenderHelper;
- import net.minecraft.client.renderer.Tessellator;
- import net.minecraft.client.renderer.texture.TextureMap;
- import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
- import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
- import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.math.MathHelper;
- import net.minecraft.util.text.ITextComponent;
- public class TileEntityChangeableBlockRenderer<T extends TileEntityChangeableBlock> extends TileEntitySpecialRenderer<TileEntityChangeableBlock> {
- protected BlockRendererDispatcher renderer;
- private boolean canRenderTE = true;
- @Override
- public void render(TileEntityChangeableBlock te, double x, double y, double z, float partialTicks, int destroyStage, float alpha){
- EntityPlayerSP player = Minecraft.getMinecraft().player;
- BlockWrapper wrapper = te.getDisplayingWrapper(player);
- renderNameplate(te, x, y, z, player);
- renderer = Minecraft.getMinecraft().getBlockRendererDispatcher();
- boolean renderedTileEntity = renderWrappedTileEntity(wrapper, x, y, z, partialTicks, destroyStage, alpha);
- if (shouldRenderBlock(wrapper, te.getPos(), renderedTileEntity)) {
- renderWrappedBlock(te, wrapper, x, y, z);
- }
- }
- private void renderNameplate(TileEntityChangeableBlock te, double x, double y, double z, EntityPlayer player) {
- this.setLightmapDisabled(true);
- ITextComponent itextcomponent = ((TileEntityChangeableBlock) te).getHoverText(player);
- if (itextcomponent != null && this.rendererDispatcher.cameraHitResult != null && te.getPos().equals(this.rendererDispatcher.cameraHitResult.getBlockPos()))
- {
- if(player.isSneaking()) {
- this.drawNameplate(te, "When connected:", x, y + 0.3, z, 12);
- }
- else {
- this.drawNameplate(te, "When disconnected:", x, y + 0.3, z, 12);
- }
- this.drawNameplate(te, itextcomponent.getFormattedText(), x, y, z, 12);
- }
- this.setLightmapDisabled(false);
- }
- /**
- *
- * @param wrapper the wrapper of the tile entity to be rendered
- * @param x
- * @param y
- * @param z
- * @param partialTicks
- * @param destroyStage
- * @param alpha
- * @return true if the tile entity rendered, false otherwise
- */
- private boolean renderWrappedTileEntity(BlockWrapper wrapper, double x, double y, double z, float partialTicks, int destroyStage, float alpha) {
- Block block = wrapper.getBlock();
- BlockPos blockpos = new BlockPos(0,0,0);
- IBlockState blockState = block.getActualState(block.getStateFromMeta(wrapper.getMeta()), getWorld(), blockpos);
- TileEntitySpecialRenderer wrappedRenderer = null;
- if(block.hasTileEntity() && canRenderTE){
- TileEntity wrappedTileEntity = block.createTileEntity(getWorld(), block.getActualState(block.getStateFromMeta(wrapper.getMeta()), getWorld(), new BlockPos(x,y,z)));
- Class<TileEntity> wrappedTileEntityClass = (Class<TileEntity>) wrappedTileEntity.getClass();
- if (wrappedTileEntity != null) {
- wrappedRenderer = TileEntityRendererDispatcher.instance.renderers.get(wrappedTileEntityClass);
- if(wrappedRenderer != null) {
- try {
- RenderHelper.disableStandardItemLighting();
- GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
- GlStateManager.enableBlend();
- GlStateManager.disableLighting();
- if (Minecraft.isAmbientOcclusionEnabled())
- {
- GlStateManager.shadeModel(GL11.GL_SMOOTH);
- }
- else
- {
- GlStateManager.shadeModel(GL11.GL_FLAT);
- }
- wrappedTileEntity.setWorld(getWorld());
- wrappedRenderer.render(wrappedTileEntity, x, y, z, partialTicks, destroyStage, alpha);
- return true;
- }
- catch(Exception | AssertionError e) {
- UnsupportedOperationException ex = new UnsupportedOperationException("Could not render TileEntityDoorwayMarkerRenderer with nbt " + wrapper.getNBT(), e);
- Main.LOGGER.catching(Level.ERROR, ex);
- canRenderTE = false;
- }
- }
- }
- }
- return false;
- }
- private boolean shouldRenderBlock(BlockWrapper wrapper, BlockPos tepos, boolean didTileEntityRender) {
- Block block = wrapper.getBlock();
- IBlockState blockState = block.getActualState(block.getStateFromMeta(wrapper.getMeta()), getWorld(), tepos);
- return block.getMaterial(blockState) != Material.AIR && !didTileEntityRender || (didTileEntityRender && block.getBoundingBox(blockState, getWorld(), tepos).equals(Block.FULL_BLOCK_AABB));
- }
- private void renderWrappedBlock(TileEntityChangeableBlock te, BlockWrapper wrapper, double x, double y, double z) {
- Tessellator tessellator = Tessellator.getInstance();
- BufferBuilder bufferbuilder = tessellator.getBuffer();
- Block block = wrapper.getBlock();
- BlockPos tepos = te.getPos();
- IBlockState blockState = block.getActualState(block.getStateFromMeta(wrapper.getMeta()), getWorld(), tepos);
- this.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
- RenderHelper.disableStandardItemLighting();
- GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
- GlStateManager.enableBlend();
- GlStateManager.disableLighting();
- if (Minecraft.isAmbientOcclusionEnabled())
- {
- GlStateManager.shadeModel(GL11.GL_SMOOTH);
- }
- else
- {
- GlStateManager.shadeModel(GL11.GL_FLAT);
- }
- bufferbuilder.begin(GL11.GL_QUADS, DefaultVertexFormats.BLOCK);
- bufferbuilder.setTranslation(x - tepos.getX(), y - tepos.getY(), z - tepos.getZ());
- renderer.getBlockModelRenderer().renderModel(getWorld(), renderer.getModelForState(blockState), blockState, tepos, bufferbuilder, false, MathHelper.getPositionRandom(BlockPos.ORIGIN));
- tessellator.draw();
- bufferbuilder.setTranslation(0.0D, 0.0D, 0.0D);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement