Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted
package kenijey.harshenuniverse.blocks;

import java.util.List;

import kenijey.harshenuniverse.base.BaseBlockHarshenSingleInventory;
import kenijey.harshenuniverse.base.BaseTileEntityHarshenSingleItemInventory;
import kenijey.harshenuniverse.tileentity.TileEntityHarshenSpawner;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemMonsterPlacer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
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;

public class HarshenSpawner extends BaseBlockHarshenSingleInventory implements ITileEntityProvider
{
	public HarshenSpawner() {
		super(Material.ROCK);
		setRegistryName("harshen_spawner");
		setUnlocalizedName("harshen_spawner");
		this.blockHardness=(-1);
		this.blockResistance=(-1);
	}
	
	@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
	{
		if(playerIn.getHeldItem(hand).getItem() instanceof ItemMonsterPlacer || playerIn.getHeldItem(hand).getItem() == Item.getItemFromBlock(Blocks.AIR))
			super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
		return playerIn.capabilities.isCreativeMode;
	}

	@Override
	public TileEntity createNewTileEntity(World worldIn, int meta) {
		return new TileEntityHarshenSpawner();
	}

	@Override
	public BaseTileEntityHarshenSingleItemInventory getTile() {
		return new TileEntityHarshenSpawner();
	}
	
	@SideOnly(Side.CLIENT)
	public BlockRenderLayer getBlockLayer()
    {
        return BlockRenderLayer.TRANSLUCENT;
    }
	
	@Override
	public void addCollisionBoxToList(IBlockState state, World worldIn, BlockPos pos, AxisAlignedBB entityBox,
			List<AxisAlignedBB> collidingBoxes, Entity entityIn, boolean p_185477_7_) {
		addCollisionBoxToList(pos, entityBox, collidingBoxes, NULL_AABB);
	}
	
	@Override
	public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
		return new AxisAlignedBB(0.45f, 0.05f, 0.45f, 0.55f, 0.95f, 0.55f);
	}
}

this is what i've done with the block class.

and this is its tile entity class:

package kenijey.harshenuniverse.tileentity;

import java.util.UUID;

import kenijey.harshenuniverse.HarshenSounds;
import kenijey.harshenuniverse.base.BaseTileEntityHarshenSingleItemInventory;
import kenijey.harshenuniverse.config.GeneralConfig;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.IEntityLivingData;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemMonsterPlacer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;

public class TileEntityHarshenSpawner extends BaseTileEntityHarshenSingleItemInventory
{
	private EntityLiving entityliving = null;
	private UUID spawnedEntityUUID = null;
	private ItemStack spawnedEntitysEgg = null;
	
	public Entity getEntity(ItemStack stack)
	{
		if(stack.getItem() == Item.getItemFromBlock(Blocks.AIR) || stack.equals(ItemStack.EMPTY))
		{
			this.entityliving = null;
			return null;
		}
		try
		{
			entityliving = (EntityLiving) EntityList.createEntityByIDFromName(ItemMonsterPlacer.getNamedIdFrom(stack), world);
            entityliving.onInitialSpawn(world.getDifficultyForLocation(new BlockPos(entityliving)), (IEntityLivingData)null);
            spawnedEntitysEgg = stack;
		}
		catch (NullPointerException e) {
		}
		return this.entityliving;
	}
	
	@Override
	protected void tick() 
	{
		EntityPlayer player = world.getClosestPlayer(pos.getX(), pos.getY(), pos.getZ(), GeneralConfig.harshenSpawnerDistance, false);
		if(player != null && getEntity(getItem()) != null && !player.isCreative())
			activate(player);
		
		if(entityliving==null)
			for(Entity entity : world.loadedEntityList)
				if(entity.getUniqueID() == spawnedEntityUUID)
				{
					if(entityliving == null && !entity.isEntityAlive())
						world.setBlockToAir(pos);
					if(entity.isEntityAlive() && (player == null || player.isCreative()) && entityliving == null)
						deactivate(spawnedEntitysEgg, entity);
				}
	}
	
	private void activate(EntityPlayer player)
	{
		setItemAir();
		this.entityliving.setPosition(pos.getX()+0.5, pos.getY(), pos.getZ()+0.5);
		this.entityliving.setRotationYawHead(player.getPosition().subtract(pos).getY());
		entityliving.onInitialSpawn(world.getDifficultyForLocation(new BlockPos(entityliving)), (IEntityLivingData)null);
		if(!world.isRemote)
			world.spawnEntity(this.entityliving);
		spawnedEntityUUID = entityliving.getUniqueID();
		world.playSound(pos.getX(), pos.getY(), pos.getZ(), HarshenSounds.SPAWNER_SUMMON, SoundCategory.BLOCKS, 1f, 1f, false);
	}
	
	private void deactivate(ItemStack stack, Entity entity)
	{
		setItem(stack);
		entity.setDead();
	}
}

BUT in the game, when a creeper or TNT explodes, the block just gets destroyed like a dirt block. And it doesn't happen with my other blocks, only this tile entity one. What did i miss? Thanks for helps.

Edited by kenijey

Guest
This topic is now closed to further replies.

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.