Jump to content

Problem with Tileentity and Special Renderer


Dustpuppy

Recommended Posts

Hi,

i have a funny problem with my tile entity. It's a jar, like in one of the tutorials from MrCrayfish. It work's ok, but if i place a second one in the world, both jars running synced. If i put an item in one jar, it also spawns in the second jar. If i destroy one of the jars, i got my items back and the other jar will become empty. It's like i have a clone of it, but not a seperate one.

 

Here's my Block class

package thewizardmod.jars;

import java.util.List;

import javax.annotation.Nullable;

import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import thewizardmod.items.StartupCommon;

public class BlockJar extends Block implements ITileEntityProvider
{
	private static final AxisAlignedBB BOUNCING_BOX = new AxisAlignedBB(0.0625 * 3, 0, 0.0625 * 3, 0.0625 * 13, 0.0625 * 15, 0.0625 * 13);
	private static final AxisAlignedBB COLLISION_BOX = new AxisAlignedBB(0.0625 * 4, 0, 0.0625 * 4, 0.0625 * 12, 0.0625 * 14, 0.0625 * 12);
	
  public BlockJar()
  {
    super(Material.GLASS);
    this.setCreativeTab(CreativeTabs.MISC);   // the block will appear on the Blocks tab in creative
    

    setHardness(0.3F);
    
  }

  @SideOnly(Side.CLIENT)
  public BlockRenderLayer getBlockLayer()
  {
    return BlockRenderLayer.TRANSLUCENT;
  }

  @Override
  public boolean isOpaqueCube(IBlockState iBlockState) {
    return false;
  }

  @Override
  public boolean isFullCube(IBlockState iBlockState) {
    return false;
  }

  @Override
  @Deprecated
	public boolean hasTileEntity() {
	return true;
  }
  
  @Override
  public EnumBlockRenderType getRenderType(IBlockState iBlockState) {
    return EnumBlockRenderType.MODEL;
  }

  
  @Override
  @Deprecated
  public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
	  return BOUNCING_BOX;
  }
  
  @Override
  @Deprecated
  public void addCollisionBoxToList(IBlockState state, World worldIn, BlockPos pos, AxisAlignedBB entityBox, List<AxisAlignedBB> collidingBoxes, @Nullable Entity entityIn) {
	  super.addCollisionBoxToList(pos, entityBox, collidingBoxes, COLLISION_BOX);
  }

  @Override
  public TileEntity createNewTileEntity(World worldIn, int meta) {
	return new TileEntityJar();
  }
  
  @Override
  public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
	  if(!worldIn.isRemote)
	  {
		  TileEntity tileEntity = worldIn.getTileEntity(pos);
		  if(tileEntity instanceof TileEntityJar)
		  {
			  TileEntityJar jar = (TileEntityJar) tileEntity;
			  if(heldItem != null)
			  {
				  if(heldItem.getItem() == StartupCommon.magicGem);
				  {
					  if(jar.addItem())
					  {
						  heldItem.stackSize--;
						  return true;
					  }
				  }
			  }
			  jar.removeItem();
		  }
	  }
	  return true;
  }
  
  @Override
  public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
  {
      if (!worldIn.isRemote)
      {
          TileEntity tileEntity = worldIn.getTileEntity(pos);
             
          if (tileEntity instanceof TileEntityJar)
          {
              TileEntityJar jar = (TileEntityJar) tileEntity;
                 
              while (jar.counter > 0)
              {
                  jar.removeItem();
              }
          }
      }
         
      super.breakBlock(worldIn, pos, state);
  }

}

 

My Tile Entity

package thewizardmod.jars;

import javax.annotation.Nullable;

import thewizardmod.items.StartupCommon;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;

public class TileEntityJar extends TileEntity{

	public static int counter = 0;
	private int MAX = 14;
	
	public boolean addItem()
	{
		if(counter < MAX)
		{
			counter++;
			markDirty();
			IBlockState state = worldObj.getBlockState(pos);
			worldObj.notifyBlockUpdate(pos, state, state, 3);
			return true;
		}
		return false;
	}
	
	public void removeItem()
	{
			if(counter > 0)
			{
				worldObj.spawnEntityInWorld(new EntityItem(worldObj, pos.getX() + 0.5F, pos.getY() + 1, pos.getZ() + 0.5F, new ItemStack(StartupCommon.magicGem)));
				counter --;
				markDirty();
				IBlockState state = worldObj.getBlockState(pos);
				worldObj.notifyBlockUpdate(pos, state, state, 3);
			}
	}
	
	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		compound.setInteger("counter", counter);
		return compound;
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		this.counter = compound.getInteger("counter");
	}
	
	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
		NBTTagCompound tag = pkt.getNbtCompound();
		this.counter = tag.getInteger("counter");
	}
	
	@Override
	@Nullable
	public SPacketUpdateTileEntity getUpdatePacket() {
		NBTTagCompound tag = new NBTTagCompound();
		tag.setInteger("counter", counter);
		return new SPacketUpdateTileEntity(pos, getBlockMetadata(), tag);
	}
	
	@Override
	public NBTTagCompound getUpdateTag() {
		NBTTagCompound tag = super.getUpdateTag();
		tag.setInteger("counter", counter);
		return tag;
	}
	
}

 

My Special Renderer

package thewizardmod.jars;

import thewizardmod.items.StartupCommon;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;

public class RendererJar extends TileEntitySpecialRenderer<TileEntityJar>{

	private static final EntityItem ITEM = new EntityItem(Minecraft.getMinecraft().theWorld, 0, 0, 0, new ItemStack(StartupCommon.magicGem));
	
	@Override
	public void renderTileEntityAt(TileEntityJar te, double x, double y, double z, float partialTicks, int destroyStage) {
		super.renderTileEntityAt(te, x, y, z, partialTicks, destroyStage);
	
		// For items that hover around
		ITEM.hoverStart = 0F;
		float pt = 0F;
//		float pt = partialTicks;
		
		GlStateManager.pushMatrix();
		{
			GlStateManager.translate(x, y, z);
			GlStateManager.rotate(90F, 1, 0, 0);
			GlStateManager.translate(0.5F, 0.1F, -0.1F);
			GlStateManager.scale(0.7F, 0.7F, 0.7F);
			for(int i = 0; i < te.counter; i++)
			{
				Minecraft.getMinecraft().getRenderManager().doRenderEntity(ITEM, 0, 0, 0, 0F, pt, false);
				GlStateManager.translate(0, 0, -0.0625F);
			}
		}
		GlStateManager.popMatrix();
	}
}

 

I register the block in my common proxy

package thewizardmod.jars;

import thewizardmod.TheWizardMod;
import net.minecraft.item.ItemBlock;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class StartupCommon
{
  public static BlockJar jar;
  public static ItemBlock itemJar;



  public static void preInitCommon()
  {
    jar = (BlockJar)(new BlockJar().setUnlocalizedName("twm_jar_unlocalised_name"));
    jar.setRegistryName("jar");
    GameRegistry.register(jar);

    itemJar = new ItemBlock(jar);
    itemJar.setRegistryName(jar.getRegistryName());
    GameRegistry.register(itemJar);

    GameRegistry.registerTileEntity(TileEntityJar.class, TheWizardMod.MODID + "TileEntityJar");
  
}

  public static void initCommon()
  {
  }

  public static void postInitCommon()
  {
  }

}

 

and the model and renderer in my Client proxy

package thewizardmod.jars;

import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.fml.client.registry.ClientRegistry;

public class StartupClientOnly
{
  public static void preInitClientOnly()
  {
	    final int DEFAULT_ITEM_SUBTYPE = 0;
    ModelResourceLocation itemModelResourceLocation = new ModelResourceLocation("thewizardmod:jar", "inventory");
    ModelLoader.setCustomModelResourceLocation(StartupCommon.itemJar, DEFAULT_ITEM_SUBTYPE, itemModelResourceLocation);

    ClientRegistry.bindTileEntitySpecialRenderer(TileEntityJar.class, new RendererJar());

}

  public static void initClientOnly()
  {
  }

  public static void postInitClientOnly()
  {
  }
}

 

I've started learning Java just 2 weeks ago. If you see crap in files, please don't blame me. Still learning.

 

Edited by Dustpuppy
Link to comment
Share on other sites

Quote

Excerpt from your Block class:


@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
  {
      if (!worldIn.isRemote)
      {
          TileEntity tileEntity = worldIn.getTileEntity(pos);
             
          if (tileEntity instanceof TileEntityJar)
          {
              TileEntityJar jar = (TileEntityJar) tileEntity;
                 
              while (jar.counter > 0)
              {
                  jar.removeItem();
              }
          }
      }
         
      super.breakBlock(worldIn, pos, state);
  }

This is the code that drops all the items when the block is broken. You need to change it so that it doesn't call `jar.removeItem();` but you need to save the counter value to the itemstack NBT (Try following the super call back to where items are dropped, see which method it is and override that one instead). Try looking at banners, they save their pattern etc to the NBT of the stack in an easy to follow way.

Edited by Alpvax
Link to comment
Share on other sites

Ups...the new code

  @Override
  public final ArrayList<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) {
  	int meta = state.getBlock().getMetaFromState(state);
  	ItemStack ret = new ItemStack(this, 1, meta);
  	NBTTagCompound compound = new NBTTagCompound();
  	TileEntity tileEntity = world.getTileEntity(pos);
  	NBTTagCompound tileEntityTag = new NBTTagCompound();
  	tileEntity.writeToNBT(tileEntityTag);
  	compound.setTag("TileEntityData", tileEntityTag);
  	ret.setTagCompound(compound);
  	return Lists.newArrayList(ret);
  }

  @Override
  public boolean removedByPlayer(IBlockState state, World world, BlockPos pos, EntityPlayer player,	boolean willHarvest) {
  	if (willHarvest) return true; 
  	return super.removedByPlayer(state, world, pos, player, willHarvest);
  }

  @Override
  public void harvestBlock(World world, EntityPlayer player, BlockPos pos, IBlockState state, TileEntity tileEntity, ItemStack tool) {
  	super.harvestBlock(world, player, pos, state, tileEntity, tool);
  	world.setBlockToAir(pos);
  }

  @Override
  public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
  	super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
  	if (stack.hasTagCompound() && stack.getTagCompound().hasKey("TileEntityData")) {
  		NBTTagCompound tag = stack.getTagCompound().getCompoundTag("TileEntityData");
  		worldIn.getTileEntity(pos).readFromNBT(tag);
  	}
  }

 

Link to comment
Share on other sites

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello, I don't have much experience with forge but I have some java knowledge, I want to add a button that says "Explore New Packs" to the Pause Screen that opens when we enter a world in the game and press the "esc" button, but I couldn't understand it (or I don't know how to use it), although I did research from artificial intelligence or different places. I was told that I can do it using mixins but I have no idea how to do it, can you explain step by step
    • We need the crash report or debug.log, as explained in the FAQ
    • i have the decocraft mod and i can see all of them. i can click right click and place the items but as soon as i put it down it disappears. im so sad it doesnt work and i dont know how to fix it. i used cursed forge to launch minecraft as well.
    • Me and my friend wanted to make a custom mod pack hosted on aternos. I downloaded the same mods as the server then started the server and my minecraft and I joined the server without issues. Then I sent the mods to my friend compressed in a zip folder (i don’t know if that has anything to do with the issue). He unpacked it put it in his mod folder and started it but couldn't join the server and got this issue:   internal exception: java.lang.runtimeexception: server not initialized yet! call IMDLib.entityHandler (modid)   Also we are using curseforge launcher to manage the mods on client side. But as I said it works on my side but not with my friend, that’s why it doesn’t make sense   Mod list:   xplosives-1.2.0.jar whisperwoods-1.18.2-2.1.0-forge.jar waystones-forge-1.18.2-10.2.2.jar voicechat-forge-1.18.2-2.4.32.jar trashslot-forge-1.18.2-11.0.3.jar towns_and_towers_forge-1.10.0.1+1.18.2.jar torchmaster-18.2.1.jar ToolBelt-1.18.2-1.18.9.jar toms_storage-1.18.2-1.4.4.jar swingthroughgrass-1.18.2-1.9.1.jar strictly_origins_v5.1_1.18.2.jar starlight-1.0.2+forge.546ae87.jar SpartanWeaponry-1.18.2-3.0.4.jar SimpleQuarry-1.18.2-18.2.5.jar simpleplanes-1.18.2-5.2.2.jar secretrooms-1.18.2-1.1.5.jar right-click-harvest-3.2.0+1.18.2-forge.jar REIPluginCompatibilities-forge-8.0.89.jar RoughlyEnough|tems-8.3.681-forge.jar RegionsUnexploredForge-0.4.1_1+1.18.2.jar TerraBlender-forge-1.18.2-1.2.0.126.jar Recall_Potion_v1.0.3-1.18.2.jar Quark-3.2-358.jar PickleTweaks-1.18.2-6.1.2.jar Pehkui-3.7.11+1.18.2-forge.jar Origins-1.18.2-1.4.1.4-unified.jar naturalist-forge-1.1.1-1.18.2.jar mysticaloaktree-1.18.2-1.2.jar selene-1.18.2-1.17.14.jar MysticalAgradditions-1.18.2-5.1.4.jar MysticalAgriculture-1.18.2-5.1.5.jar MultiMine-1.18.7.jar moyai-1.18.2-1.1.4.jar ModernDynamicSurroundings-5.0.0.4.jar lootbeams-1.18.1-release-july1722.jar journeymap-1.18.2-5.9.7-forge.jar ironfurnaces-1.18.2-3.3.3.jar ironchest-1.18.2-13.2.11.jar industrial-foregoing-1.18.2-3.3.1.6-10.jar titanium-1.18.2-3.5.11-45.jar immersive_armors-1.5.6+1.18.2-forge.jar hardcorerevival-forge-1.18.2-8.0.1.jar HammerLib-1.18.2-18.2.16. jar gravestone-1.18.2-1.0.1.jar geckolib-forge-1.18-3.0.57.jar findme-3.0.6-forge.jar faithfulbackrooms3.0.0.7.jar elevatorid-1.18.2-1.8.4.jar EasyMagic-v3.3.0-1.18.2-Forge.jar PuzzlesLib-v3.5.9-1.18.2-Forge.jar easy_villagers-1.18.2-1.0.11.jar easy_mob_farm_1.18.2-7.1.0.jar DynamicTrees-1.18.2-1.0.4.jar dragonseeker-1.1.1-1.18.2.jar iceandfire-2.1.12-1.18.2.jar Cucumber-1.18.2-5.1.5 jar cloth-config-6.5.116-forge.jar citadel-1.11.3-1.18.2.jar chunkloaders-1.2.8a-forge-mc1.18.jar supermartijn642configlib-1.1.8-forge-mc1.18.jar supermartijn642corelib-1.1.17-forge-mc1.18.jar cave_dweller-1.18.2-1.6.4.jar carryon-1.18.2-1.17.1.12.jar caelus-forge-1.18.1-3.0.0.2.jar BotanyTrees-Forge-1.18.2-4.0.13.jar BotanyPots-Forge-1.18.2-8.1.29.jar Bookshelf-Forge-1.18.2-13.3.56.jar balm-3.2.6.jar AutoRegLib-1.7-53.jar ars_nouveau-1.18.2-2.9.0.jar curios-forge-1.18.2-5.0.9.2.jar Patchouli-1.18.2-71.1.jar architectury-4.12.94-forge.jar almostunified-forge-1.18.2-0.3.10.jar [1.18.2] SecurityCraft v1.9.8.jar
  • Topics

×
×
  • Create New...

Important Information

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