Jump to content

[1.14.4] TileEntityRender renders block transparent


Recommended Posts

Posted

Hello!

I am making an animated block with TER. 

My achieve is to make a mill, behind the block, rotates.

I couldn't do it, so I tried to make a pillar with a plate about it (just to lern how TER works with a block simpler) that rotates clockwise.

When this block has been placed, it is renderd transparent.

 

p.s. I have taken a look at Chest, Beacon and Enchantment Table, yet.

 

Main(I registered here the TER)

package com.olivemod;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.olivemod.blocks.machine.active.flour_mill.FlourMillTE;
import com.olivemod.blocks.machine.active.flour_mill.FlourMillTER;
import com.olivemod.event.generation.OreGeneration;
import com.olivemod.event.generation.tree.TreeGeneration;
import com.olivemod.fluid.Fluids;
import com.olivemod.init.BlockInit;
import com.olivemod.init.ItemInit;
import com.olivemod.utils.ModContainerTypes;
import com.olivemod.utils.ModTileEntityTypes;
import com.olivemod.utils.Reference.Reference;

import net.minecraft.item.crafting.ShapedRecipe;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;

@Mod(Reference.MOD_ID)
@Mod.EventBusSubscriber(modid = Reference.MOD_ID, bus = EventBusSubscriber.Bus.MOD)
public class Main
{
	
	public static Main instance;
	
	// Directly reference a log4j logger.
    public static final Logger LOGGER = LogManager.getLogger();
    
   public Main() {
	
	   	LOGGER.debug("Welcome from OliveMod");	   
	   	@SuppressWarnings("unused")
		final ModLoadingContext modLoadingContext = ModLoadingContext.get();
	   
	   	/*
	   	 * @modEventBus register the setup method for modLoading
	   	 * @modEventBus register the initClient method for modLoading
	   	 */
	   	final IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
		modEventBus.addListener(this::setup);
		modEventBus.addListener(this::initClient);

		ItemInit.ITEM.register(modEventBus);
		BlockInit.BLOCK.register(modEventBus);
		ModContainerTypes.CONTAINER_TYPE.register(modEventBus);
		ModTileEntityTypes.TILE_ENTITY_TYPE.register(modEventBus);
		Fluids.FLUIDS.register(modEventBus);

		instance = this;
		MinecraftForge.EVENT_BUS.register(this);
   }
   

	public  void setup(final FMLCommonSetupEvent event) {// K9#8016
		
		OreGeneration.oreGeneration();
		TreeGeneration.init();
		//CapabilityManager.INSTANCE.register(IThirsty.class, new ThirstyStorage(), Thirsty::new);
		ShapedRecipe.setCraftingSize(5, 5);
	}
	
   private void initClient(final FMLClientSetupEvent event)
   {
	   ClientRegistry.bindTileEntitySpecialRenderer(FlourMillTE.class, new FlourMillTER());
   }
}

TER:

package com.olivemod.blocks.machine.active.flour_mill;

import java.util.Random;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lwjgl.opengl.GL11;

import com.mojang.blaze3d.platform.GlStateManager;
import com.olivemod.utils.Reference.Reference;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BlockRendererDispatcher;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.model.data.EmptyModelData;
import net.minecraftforge.client.model.data.IModelData;

@OnlyIn(Dist.CLIENT)
public class FlourMillTER extends TileEntityRenderer<FlourMillTE> 
{
	
	// Directly reference a log4j logger.
    public static final Logger LOGGER = LogManager.getLogger();

	public static final FlourMillTER INSTANCE = new FlourMillTER();
	
	private final ResourceLocation TEXTURE_LOCATION;
	private FlourMillModel model = new FlourMillModel();
	
	public FlourMillTER() 
	{

		TEXTURE_LOCATION = new ResourceLocation(Reference.MOD_ID, "textures/entity/x.png");
		
	}
	
	@Override
	public void render(FlourMillTE tileEntityIn, double x, double y, double z, float partialTicks, int destroyStage) {

		GlStateManager.pushMatrix();
        GlStateManager.translatef(0.5F, 0.5F, 0.5F);
        GlStateManager.rotatef(0.0f, 0.0F, 1.0F, 0.0F);
        this.bindTexture(TEXTURE_LOCATION);
        this.model.renderAll();
        GlStateManager.popMatrix();
	}
	


}

TE:

package com.olivemod.blocks.machine.active.flour_mill;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.olivemod.utils.ModTileEntityTypes;
import com.olivemod.utils.Reference.NBTKeys;

import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.HorizontalBlock;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.fluid.Fluids;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.LockableTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.Direction;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.model.animation.IAnimationStateMachine;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.items.wrapper.RangedWrapper;

public class FlourMillTE extends TileEntity implements ITickableTileEntity{

	private final int IN_SLOT = 0;
	
	public final ItemStackHandler inventory = new ItemStackHandler(IN_SLOT + 1)
	{
		public boolean isItemValid(int slot, ItemStack stack) {
			
			return slot == IN_SLOT && stack.getItem() == Items.WHEAT;
		};
		
		protected void onContentsChanged(int slot) {
			
			FlourMillTE.this.markDirty();
		};
	};
	
	private final LazyOptional<ItemStackHandler> LOInventory = LazyOptional.of( () -> this.inventory);
	
	@Override
	public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {

		return side == Direction.SOUTH ? this.LOInventory.cast() : LazyOptional.empty();
	}
	
	protected FlourMillTE(TileEntityType<?> typeIn) {
		super(typeIn);
	}
	
	public FlourMillTE() {
		super(ModTileEntityTypes.FLOUR_MILL_TE.get());
	}

	@Override
	public void read(CompoundNBT compound) {
		// TODO Auto-generated method stub
		super.read(compound);
		this.inventory.deserializeNBT(compound);
	}
	
	@Override
	public CompoundNBT write(CompoundNBT compound) {

		compound = super.write(compound);
		compound.put("inventory", this.inventory.serializeNBT());
		return compound;
	}
	
	/*
	 * Retrieves packet to send to the client whenever this Tile Entity is re-sinced via World#notifyBlockUpdate. 
	 * This packet comes back client-side via (@link #onDataPacket)
	 */
	@Nullable
	public SUpdateTileEntityPacket getUpdatePacket() {

		final CompoundNBT tag = new CompoundNBT();
		//We pass 0 for TileEntityTypesIn because we have a modded TE.See ClientPlayNetHandler#handlerUpdateTileEntity(SUpdateTileEntityPacket)
		return new SUpdateTileEntityPacket(this.pos, 0, tag);
	}
	
	/*
	 * Get an NBT compount to sync to the client with SPacketChunkData, used to initial loading of the chunk or when many blocks change at once
	 * This compound comes back to the client-side in (@link #handleUpdateTag)
	 * The default implementation ({@link TileEntity#handleUpdateTag}) calls {@link #writeInternal)}
	 * wich doesn't save any of our extra data so we override it to call {@link #write} instead 
	 */
	@Nonnull
	public CompoundNBT getUpdateTag() {

		return this.write(new CompoundNBT());
	}	
	
	/*
	 * Invalidates our Tile Entity 
	 */
	@Override
	public void remove() {

		super.remove();
		//We need to invalidate our capability references so that any cached references (by other mod) don't continue to reference our capablities
		//and try to use them and/or prevent them from being garbage collected
		LOInventory.invalidate();
	}

	/*
	 * Handle a packet created in (@link #getUpdatePacket())
	 */
	@Override
	public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {

		super.onDataPacket(net, pkt);
	}
	
	
	@Override
	public void onLoad() {
		
		super.onLoad();
	}

	@Override
	public void tick() {

		if (!this.inventory.getStackInSlot(IN_SLOT).isEmpty())
		{
		}
	}

	@Override
	public AxisAlignedBB getRenderBoundingBox() {
		// TODO Auto-generated method stub
		return super.getRenderBoundingBox();
	}
	
}

Model:

package com.olivemod.blocks.machine.active.flour_mill;

import net.minecraft.client.renderer.entity.model.RendererModel;
import net.minecraft.client.renderer.model.Model;

public class FlourMillModel extends Model {
	private final RendererModel bb_main;// = (new RendererModel(this, 0, 0)).setTextureSize(64, 64);

	public FlourMillModel() {
		textureWidth = 16;
		textureHeight = 16;

		bb_main = new RendererModel(this);
		bb_main.setRotationPoint(0.0F, 1.0F, 0.0F);
		bb_main.addBox(0f, 0f, 0f, 16, 16, 16);
	}

	void renderAll() {
		
		this.bb_main.render(0.0625f);
	}
	
	
	public void setRotationAngle(RendererModel modelRenderer, float x, float y, float z) {
		modelRenderer.rotateAngleX = x;
		modelRenderer.rotateAngleY = y;
		modelRenderer.rotateAngleZ = z;
	}
}

Block:

package com.olivemod.blocks.machine.active.flour_mill;

import com.olivemod.blocks.machine.active.fluid_transporter.TileEntityFluidTransporter;
import com.olivemod.utils.ModTileEntityTypes;
import com.olivemod.utils.Reference.Reference;

import net.minecraft.block.Block;
import net.minecraft.block.BlockRenderType;
import net.minecraft.block.BlockState;
import net.minecraft.block.HorizontalBlock;
import net.minecraft.block.material.Material;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.state.StateContainer.Builder;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.Direction;
import net.minecraft.util.Mirror;
import net.minecraft.util.Rotation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.common.ToolType;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.ItemStackHandler;

@EventBusSubscriber(modid = Reference.MOD_ID, bus = EventBusSubscriber.Bus.MOD)
public class FlourMill extends HorizontalBlock{

	public FlourMill() {
		
		super(Block.Properties.create(Material.IRON).hardnessAndResistance(3.0f).harvestLevel(2).harvestTool(ToolType.PICKAXE));
		this.setDefaultState(this.getDefaultState().with(HORIZONTAL_FACING, Direction.NORTH));
	}
	
	@Override
	public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {

		if (state != newState)
		{
			TileEntity tileEntity = worldIn.getTileEntity(pos);
			if(tileEntity instanceof FlourMillTE)
			{
				final ItemStackHandler inventory = ((FlourMillTE)tileEntity).inventory;
				for (int i = 0; i < inventory.getSlots(); i++)
				{
					InventoryHelper.spawnItemStack(worldIn, pos.getX(), pos.getY(), pos.getZ(), inventory.getStackInSlot(i));
				}
			}
		}
		super.onReplaced(state, worldIn, pos, newState, isMoving);
	}

	@Override
	public BlockState getStateForPlacement(BlockItemUseContext context) {

		return this.getDefaultState().with(HORIZONTAL_FACING, context.getPlacementHorizontalFacing().getOpposite());
	}

	@Override
	public int getComparatorInputOverride(BlockState blockState, World worldIn, BlockPos pos) {

		final TileEntity tileEntity = worldIn.getTileEntity(pos);
		if(tileEntity instanceof FlourMillTE)
		{
			return ItemHandlerHelper.calcRedstoneFromInventory(((TileEntityFluidTransporter)tileEntity).inventory);
		}
		return super.getComparatorInputOverride(blockState, worldIn, pos);
	}
	
	@Override
	protected void fillStateContainer(Builder<Block, BlockState> builder) {

		
		builder.add(HORIZONTAL_FACING);
	}
	
	@Override
	public BlockState rotate(BlockState state, Rotation rot) {

		return state.with(HORIZONTAL_FACING, state.get(HORIZONTAL_FACING));
	}
	
	@Override
	public BlockState mirror(BlockState state, Mirror mirrorIn) {

		return state.rotate(mirrorIn.toRotation(state.get(HORIZONTAL_FACING)));
	}
	
	@Override
	public BlockRenderLayer getRenderLayer() {
		// TODO Auto-generated method stub
		return BlockRenderLayer.CUTOUT;
	}
	
	@Override
	public BlockRenderType getRenderType(BlockState state) {

		return BlockRenderType.MODEL;
	}
	
	@Override
	public boolean hasTileEntity() {

		return true;
	}
	
	@Override
	public TileEntity createTileEntity(BlockState state, IBlockReader world) {

		return ModTileEntityTypes.FLOUR_MILL_TE.get().create();
	}


}

 

Cattura.PNG

2020-08-31_10.28.49.png

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.