Tile Entity render function

Started by AlexTron on Fri, 05/15/2020 - 19:42

Topic category: Advanced modding

Last seen on 03:38, 7. Aug 2020
Joined Sep 2015
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Tile Entity render function
Fri, 05/15/2020 - 19:42

I have added a class to the block class for a tile entity to provide custom rendering, however, the render function in it is not called. Is there anything wrong with the code I have wrote:


package net.alextronstudios.generationofthevoid.block;

import net.minecraftforge.registries.ObjectHolder;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.common.ToolType;

import net.minecraft.world.storage.loot.LootContext;
import net.minecraft.world.World;
import net.minecraft.world.IBlockReader;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.NonNullList;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.LockableLootTileEntity;
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
import net.minecraft.network.NetworkManager;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Item;
import net.minecraft.item.BlockItem;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.ChestContainer;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.inventory.InventoryHelper;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
import net.minecraft.block.material.Material;
import net.minecraft.block.SoundType;
import net.minecraft.block.BlockState;
import net.minecraft.block.Block;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.client.renderer.entity.model.EnderCrystalModel;

import net.alextronstudios.generationofthevoid.itemgroup.VoidtabItemGroup;
import net.alextronstudios.generationofthevoid.GenerationoftheVoidElements;

import java.util.List;
import java.util.Collections;

import com.mojang.blaze3d.platform.GlStateManager;

@GenerationoftheVoidElements.ModElement.Tag
public class VoidAlterBlock extends GenerationoftheVoidElements.ModElement {
	@ObjectHolder("generationofthevoid:voidalter")
	public static final Block block = null;
	@ObjectHolder("generationofthevoid:voidalter")
	public static final TileEntityType<VoidAlterTileEntity> tileEntityType = null;
	public static final ResourceLocation ENDER_CRYSTAL_TEXTURES = new ResourceLocation("generationofthevoid:textures/voidcrystal.png");
	public VoidAlterBlock(GenerationoftheVoidElements instance) {
		super(instance, 62);
		FMLJavaModLoadingContext.get().getModEventBus().register(this);
	}

	@Override
	public void initElements() {
		elements.blocks.add(() -> new CustomBlock());
		elements.items.add(() -> new BlockItem(block, new Item.Properties().group(VoidtabItemGroup.tab)).setRegistryName(block.getRegistryName()));
	}

	@SubscribeEvent
	public void registerTileEntity(RegistryEvent.Register<TileEntityType<?>> event) {
		event.getRegistry().register(TileEntityType.Builder.create(VoidAlterTileEntity::new, block).build(null).setRegistryName("voidalter"));
	}
	public static class CustomBlock extends Block {
		public CustomBlock() {
			super(Block.Properties.create(Material.IRON).sound(SoundType.METAL).hardnessAndResistance(5f, 25f).lightValue(0).harvestLevel(5)
					.harvestTool(ToolType.PICKAXE));
			setRegistryName("voidalter");
		}

		@Override
		public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
			List<ItemStack> dropsOriginal = super.getDrops(state, builder);
			if (!dropsOriginal.isEmpty())
				return dropsOriginal;
			return Collections.singletonList(new ItemStack(this, 1));
		}

		@Override
		public INamedContainerProvider getContainer(BlockState state, World worldIn, BlockPos pos) {
			TileEntity tileEntity = worldIn.getTileEntity(pos);
			return tileEntity instanceof INamedContainerProvider ? (INamedContainerProvider) tileEntity : null;
		}

		@Override
		public boolean hasTileEntity(BlockState state) {
			return true;
		}

		@Override
		public BlockRenderLayer getRenderLayer() {
			return BlockRenderLayer.TRANSLUCENT;
		}

		@Override
		public boolean isNormalCube(BlockState state, IBlockReader worldIn, BlockPos pos) {
			return false;
		}

		@Override
		public boolean propagatesSkylightDown(BlockState state, IBlockReader reader, BlockPos pos) {
			return true;
		}
		@Override
		public TileEntity createTileEntity(BlockState state, IBlockReader world) {
			return new VoidAlterTileEntity();
		}

		@Override
		public boolean eventReceived(BlockState state, World world, BlockPos pos, int eventID, int eventParam) {
			super.eventReceived(state, world, pos, eventID, eventParam);
			TileEntity tileentity = world.getTileEntity(pos);
			return tileentity == null ? false : tileentity.receiveClientEvent(eventID, eventParam);
		}

		@Override
		public void onReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean isMoving) {
			if (state.getBlock() != newState.getBlock()) {
				TileEntity tileentity = world.getTileEntity(pos);
				if (tileentity instanceof VoidAlterTileEntity) {
					InventoryHelper.dropInventoryItems(world, pos, (VoidAlterTileEntity) tileentity);
					world.updateComparatorOutputLevel(pos, this);
				}
				super.onReplaced(state, world, pos, newState, isMoving);
			}
		}

		@Override
		public boolean hasComparatorInputOverride(BlockState state) {
			return true;
		}

		@Override
		public int getComparatorInputOverride(BlockState blockState, World world, BlockPos pos) {
			TileEntity tileentity = world.getTileEntity(pos);
			if (tileentity instanceof VoidAlterTileEntity)
				return Container.calcRedstoneFromInventory((VoidAlterTileEntity) tileentity);
			else
				return 0;
		}
	}

	public static class VoidAlterTileEntity extends LockableLootTileEntity {
		private NonNullList<ItemStack> stacks = NonNullList.<ItemStack>withSize(9, ItemStack.EMPTY);
		protected VoidAlterTileEntity() {
			super(tileEntityType);
		}

		@Override
		public void read(CompoundNBT compound) {
			super.read(compound);
			this.stacks = NonNullList.withSize(this.getSizeInventory(), ItemStack.EMPTY);
			ItemStackHelper.loadAllItems(compound, this.stacks);
		}

		@Override
		public CompoundNBT write(CompoundNBT compound) {
			super.write(compound);
			ItemStackHelper.saveAllItems(compound, this.stacks);
			return compound;
		}

		@Override
		public SUpdateTileEntityPacket getUpdatePacket() {
			return new SUpdateTileEntityPacket(this.pos, 0, this.getUpdateTag());
		}

		@Override
		public CompoundNBT getUpdateTag() {
			return this.write(new CompoundNBT());
		}

		@Override
		public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
			this.read(pkt.getNbtCompound());
		}

		@Override
		public int getSizeInventory() {
			return 9;
		}

		@Override
		public boolean isEmpty() {
			for (ItemStack itemstack : this.stacks)
				if (!itemstack.isEmpty())
					return false;
			return true;
		}

		@Override
		public boolean isItemValidForSlot(int index, ItemStack stack) {
			return true;
		}

		@Override
		public ItemStack getStackInSlot(int slot) {
			return stacks.get(slot);
		}

		@Override
		public ITextComponent getDefaultName() {
			return new StringTextComponent("voidalter");
		}

		@Override
		public int getInventoryStackLimit() {
			return 64;
		}

		@Override
		public Container createMenu(int id, PlayerInventory player) {
			return ChestContainer.createGeneric9X3(id, player, this);
		}

		@Override
		public ITextComponent getDisplayName() {
			return new StringTextComponent("Void Alter");
		}

		@Override
		protected NonNullList<ItemStack> getItems() {
			return this.stacks;
		}

		@Override
		protected void setItems(NonNullList<ItemStack> stacks) {
			this.stacks = stacks;
		}
	}

	public class VoidAlterRenderer extends TileEntityRenderer<VoidAlterTileEntity> {
		private final EnderCrystalModel EnderCrystalModel = new EnderCrystalModel(0.8F, false);
		private int rotation = (int) (Math.random() * 100000.0D);
		public void render(VoidAlterTileEntity te, double x, double y, double z, float partialTicks, int destroyStage) {
			double totalWorldTime = (double) te.getWorld().getGameTime();
			this.renderNetherStar(x, y, z, partialTicks, totalWorldTime);
		}

		public void renderNetherStar(double x, double y, double z, float partialTicks, double totalWorldTime) {
			int rotation = (int) (this.rotation + totalWorldTime);
			float f = rotation + partialTicks;
			GlStateManager.pushMatrix();
			GlStateManager.translatef((float) x + 0.0F, (float) y + -0.7F, (float) z + 0.0F);
			this.bindTexture(ENDER_CRYSTAL_TEXTURES);
			GlStateManager.translatef(0.5F, 0.5F, 0.5F);
			this.EnderCrystalModel.render(null, 0.0F, f * 3.0F, 0.1F, 0.0F, 0.0F, 0.0625F);
			GlStateManager.popMatrix();
		}
	}
}

 

You have added…
Sat, 05/16/2020 - 09:18

You have added VoidAlterRenderer class but you never actually construct it or use it otherwise so logically nothing will happen.