Jump to content

[1.12] Tileentity not working right


Kokkie

Recommended Posts

Hello,

I'm trying to make an auto miner. When I right-click it with a fuel item or block it should take the item and activate and deactivate when the fuel runs out. But now when I right-click with a fuel item it doesn't work. And when I right-click with a fuel block it activates. Also, how do I get the slot that the player is holding?

My code so far:

Block class

public class BlockAutoMiner extends Block implements ITileEntityProvider {

	public static final PropertyDirection FACING = BlockHorizontal.FACING;
	private final boolean powered;

	public BlockAutoMiner(boolean powered) {
		super(Material.PISTON);
		this.setDefaultState(this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH));
		this.powered = powered;
	}

	@Override
	public Item getItemDropped(IBlockState state, Random rand, int fortune) {
		return Item.getItemFromBlock(AMBlocks.AUTO_MINER);
	}

	@Override
	public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state) {
		return new ItemStack(AMBlocks.AUTO_MINER);
	}

	public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) {
		this.setDefaultFacing(worldIn, pos, state);
	}

	private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state) {
		if (!worldIn.isRemote) {
			IBlockState iblockstate = worldIn.getBlockState(pos.north());
			IBlockState iblockstate1 = worldIn.getBlockState(pos.south());
			IBlockState iblockstate2 = worldIn.getBlockState(pos.west());
			IBlockState iblockstate3 = worldIn.getBlockState(pos.east());
			EnumFacing enumfacing = (EnumFacing) state.getValue(FACING);

			if (enumfacing == EnumFacing.NORTH && iblockstate.isFullBlock() && !iblockstate1.isFullBlock()) {
				enumfacing = EnumFacing.SOUTH;
			} else if (enumfacing == EnumFacing.SOUTH && iblockstate1.isFullBlock() && !iblockstate.isFullBlock()) {
				enumfacing = EnumFacing.NORTH;
			} else if (enumfacing == EnumFacing.WEST && iblockstate2.isFullBlock() && !iblockstate3.isFullBlock()) {
				enumfacing = EnumFacing.EAST;
			} else if (enumfacing == EnumFacing.EAST && iblockstate3.isFullBlock() && !iblockstate2.isFullBlock()) {
				enumfacing = EnumFacing.WEST;
			}

			worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
		}
	}

	public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY,
			float hitZ, int meta, EntityLivingBase placer) {
		return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
	}

	public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer,
			ItemStack stack) {
		worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing()), 2);
	}

	public IBlockState getStateFromMeta(int meta) {
		EnumFacing enumfacing = EnumFacing.getFront(meta);

		if (enumfacing.getAxis() == EnumFacing.Axis.Y) {
			enumfacing = EnumFacing.NORTH;
		}

		return this.getDefaultState().withProperty(FACING, enumfacing);
	}

	public int getMetaFromState(IBlockState state) {
		return ((EnumFacing) state.getValue(FACING)).getIndex();
	}

	public IBlockState withRotation(IBlockState state, Rotation rot) {
		return state.withProperty(FACING, rot.rotate((EnumFacing) state.getValue(FACING)));
	}

	public IBlockState withMirror(IBlockState state, Mirror mirrorIn) {
		return state.withRotation(mirrorIn.toRotation((EnumFacing) state.getValue(FACING)));
	}

	protected BlockStateContainer createBlockState() {
		return new BlockStateContainer(this, new IProperty[] { FACING });
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand) {
		if (this.powered) {
			EnumFacing enumfacing = (EnumFacing) stateIn.getValue(FACING);
			double d0 = (double) pos.getX() + 0.5D;
			double d1 = (double) pos.getY() + 0.5D;
			double d2 = (double) pos.getZ() + 0.5D;
			double d3 = 0.52D;
			double d4 = rand.nextDouble() * 0.6D - 0.3D;

			switch (enumfacing) {
			case WEST:
				worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D,
						new int[0]);
				break;
			case EAST:
				worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 - d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D,
						new int[0]);
				break;
			case NORTH:
				worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 + d3, 0.0D, 0.0D, 0.0D,
						new int[0]);
				break;
			case SOUTH:
				worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 - d3, 0.0D, 0.0D, 0.0D,
						new int[0]);
			default:
				break;
			}
		}
	}

	@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
			EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
		if (!worldIn.isRemote) {
			if (hand == EnumHand.MAIN_HAND) {
				if (isItemFuel(playerIn.getHeldItemMainhand())) {
					if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
						TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
						te.addFuel(getItemBurnTime(playerIn.getHeldItemMainhand()));
						playerIn.inventory.decrStackSize(EntityEquipmentSlot.MAINHAND.getIndex(), 1);
					}
					if (!powered) {
						worldIn.setBlockState(pos, AMBlocks.POWERED_AUTO_MINER.getDefaultState().withProperty(FACING,
								state.getValue(FACING)), 3);
					}
				} else {
					if (powered) {
						worldIn.setBlockState(pos,
								AMBlocks.AUTO_MINER.getDefaultState().withProperty(FACING, state.getValue(FACING)), 3);
					}
				}
				return true;
			}
		}
		return false;
	}

	public static int getItemBurnTime(ItemStack stack) {
		if (stack.isEmpty()) {
			return 0;
		} else {
			Item item = stack.getItem();
			if (!item.getRegistryName().getResourceDomain().equals("minecraft")) {
				int burnTime = net.minecraftforge.fml.common.registry.GameRegistry.getFuelValue(stack);
				if (burnTime != 0)
					return burnTime;
			}

			if (item == Item.getItemFromBlock(Blocks.WOODEN_SLAB)) {
				return 150;
			} else if (item == Item.getItemFromBlock(Blocks.WOOL)) {
				return 100;
			} else if (item == Item.getItemFromBlock(Blocks.CARPET)) {
				return 67;
			} else if (item == Item.getItemFromBlock(Blocks.LADDER)) {
				return 300;
			} else if (item == Item.getItemFromBlock(Blocks.WOODEN_BUTTON)) {
				return 100;
			} else if (Block.getBlockFromItem(item).getDefaultState().getMaterial() == Material.WOOD) {
				return 300;
			} else if (item == Item.getItemFromBlock(Blocks.COAL_BLOCK)) {
				return 16000;
			} else if (item instanceof ItemTool && "WOOD".equals(((ItemTool) item).getToolMaterialName())) {
				return 200;
			} else if (item instanceof ItemSword && "WOOD".equals(((ItemSword) item).getToolMaterialName())) {
				return 200;
			} else if (item instanceof ItemHoe && "WOOD".equals(((ItemHoe) item).getMaterialName())) {
				return 200;
			} else if (item == Items.STICK) {
				return 100;
			} else if (item != Items.BOW && item != Items.FISHING_ROD) {
				if (item == Items.SIGN) {
					return 200;
				} else if (item == Items.COAL) {
					return 1600;
				} else if (item == Items.LAVA_BUCKET) {
					return 20000;
				} else if (item != Item.getItemFromBlock(Blocks.SAPLING) && item != Items.BOWL) {
					if (item == Items.BLAZE_ROD) {
						return 2400;
					} else if (item instanceof ItemDoor && item != Items.IRON_DOOR) {
						return 200;
					} else {
						return item instanceof ItemBoat ? 400 : 0;
					}
				} else {
					return 100;
				}
			} else {
				return 300;
			}
		}
	}

	public static boolean isItemFuel(ItemStack stack) {
		return getItemBurnTime(stack) > 0;
	}

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

Tileentity class

public class TileEntityAutoMiner extends TileEntity implements ITickable {

	public int FUEL_AMOUNT = 0;

	public void addFuel(int amount) {
		this.FUEL_AMOUNT += amount;
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.notifyBlockUpdate(pos, state, state, 3);
	}

	public void removeFuel() {
		if (this.FUEL_AMOUNT > 0) {
			this.FUEL_AMOUNT--;
			markDirty();
			IBlockState state = world.getBlockState(pos);
			world.notifyBlockUpdate(pos, state, state, 3);
		}
	}

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		writeUpdateTag(compound);
		return compound;
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		readUpdateTag(compound);
	}

	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
		NBTTagCompound compound = pkt.getNbtCompound();
		readUpdateTag(compound);
	}

	@Override
	public SPacketUpdateTileEntity getUpdatePacket() {
		NBTTagCompound compound = new NBTTagCompound();
		writeUpdateTag(compound);
		return new SPacketUpdateTileEntity(pos, getBlockMetadata(), compound);
	}

	@Override
	public NBTTagCompound getUpdateTag() {
		NBTTagCompound compound = super.getUpdateTag();
		writeUpdateTag(compound);
		return compound;
	}

	public void writeUpdateTag(NBTTagCompound compound) {
		compound.setInteger("FuelAmount", this.FUEL_AMOUNT);
	}

	public void readUpdateTag(NBTTagCompound compound) {
		this.FUEL_AMOUNT = compound.getInteger("FuelAmount");
	}

	@Override
	public void update() {
		if (world.getBlockState(pos).getBlock() == AMBlocks.POWERED_AUTO_MINER) {
			removeFuel();

			if (FUEL_AMOUNT <= 0) {
				FUEL_AMOUNT = 0;
				world.setBlockState(pos, AMBlocks.AUTO_MINER.getDefaultState().withProperty(BlockAutoMiner.FACING,
						world.getBlockState(pos).getValue(BlockAutoMiner.FACING)), 3);
			}
		}
	}

}

 

Edited by Kokkie

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

29 minutes ago, Kokkie said:

playerIn.getHeldItemMainhand()

You have the hand the player clicks your block with as an argument. That argument is called hand.

29 minutes ago, Kokkie said:

public static int getItemBurnTime(ItemStack stack)

Do not reinvent the wheel. That method already exists at TileEntityFurnace and it is public and static.

29 minutes ago, Kokkie said:

how do I get the slot that the player is holding?

Offhand - 40. Mainhand - player.inventory.currentItem.

 

29 minutes ago, Kokkie said:

playerIn.inventory.decrStackSize(EntityEquipmentSlot.MAINHAND.getIndex(), 1);

=>

playerIn.getHeldItem(hand).shrink(1);

 

As for your issue itself try debugging it. Set some breakpoints up, see which method fails the checks, try figuring out why.

 

Edit: Okay, just want to clarify something as my response may seem weird at the point of me talking about the hand argument as you clearly see it existing. To clarify - the issue here is me failing to see any reason as to why are you ignoring the offhand completely and only do things for the main hand.

I also tested your code with hand and method references corrected and I do not have the issue.

Edited by V0idWa1k3r
  • Like 1
Link to comment
Share on other sites

So, I now have this, and it seems to be working fine.

	@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
			EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
		if (!worldIn.isRemote && hand == EnumHand.MAIN_HAND) {
			if (TileEntityFurnace.isItemFuel(playerIn.getHeldItem(hand))) {
				if (!playerIn.isCreative()) {
					playerIn.getHeldItem(hand).shrink(1);
				}
				if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
					TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
					te.addFuel(TileEntityFurnace.getItemBurnTime(playerIn.getHeldItem(hand)));
				}
			} else if (playerIn.getHeldItem(hand).getItem() == AMItems.WRENCH) {
				if (!powered) {
					worldIn.setBlockState(pos,
							AMBlocks.POWERED_AUTO_MINER.getDefaultState().withProperty(FACING, state.getValue(FACING)),
							3);
				} else {
					worldIn.setBlockState(pos,
							AMBlocks.AUTO_MINER.getDefaultState().withProperty(FACING, state.getValue(FACING)), 3);
				}
			}
			return true;
		}
		return false;

	}

But now, every time I activate it with a wrench, it immediately changes back to the deactivated. I tested and found out it was because fuel kept getting reset to zero when activated. How can I fix this? Here's the tile entity.

public class TileEntityAutoMiner extends TileEntity implements ITickable {

	public int FUEL_AMOUNT = 0;

	public void addFuel(int amount) {
		System.out.println("Before: " + FUEL_AMOUNT);
		System.out.println("Added: " + amount);
		this.FUEL_AMOUNT += amount;
		System.out.println("After: " + FUEL_AMOUNT);
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.notifyBlockUpdate(pos, state, state, 3);
	}

	public void removeFuel() {
		if (this.FUEL_AMOUNT > 0) {
			this.FUEL_AMOUNT--;
			markDirty();
			IBlockState state = world.getBlockState(pos);
			world.notifyBlockUpdate(pos, state, state, 3);
		}
	}

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		writeUpdateTag(compound);
		return compound;
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		readUpdateTag(compound);
	}

	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
		NBTTagCompound compound = pkt.getNbtCompound();
		readUpdateTag(compound);
	}

	@Override
	public SPacketUpdateTileEntity getUpdatePacket() {
		NBTTagCompound compound = new NBTTagCompound();
		writeUpdateTag(compound);
		return new SPacketUpdateTileEntity(pos, getBlockMetadata(), compound);
	}

	@Override
	public NBTTagCompound getUpdateTag() {
		NBTTagCompound compound = super.getUpdateTag();
		writeUpdateTag(compound);
		return compound;
	}

	public void writeUpdateTag(NBTTagCompound compound) {
		compound.setInteger("FuelAmount", this.FUEL_AMOUNT);
	}

	public void readUpdateTag(NBTTagCompound compound) {
		this.FUEL_AMOUNT = compound.getInteger("FuelAmount");
	}

	@Override
	public void update() {
		if (world.getBlockState(pos).getBlock() == AMBlocks.POWERED_AUTO_MINER) {
			removeFuel();
			if (FUEL_AMOUNT <= 0) {
				FUEL_AMOUNT = 0;
				world.setBlockState(pos, AMBlocks.AUTO_MINER.getDefaultState().withProperty(BlockAutoMiner.FACING,
						world.getBlockState(pos).getValue(BlockAutoMiner.FACING)), 3);
			}
		}
	}

}

 

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

When you change a blockstate of a block it's tileentity gets recreated unless you explicitly tell it to not do so. You can do so at TileEntity::shouldRefresh. Note that you should not blindly return false as this check will fire even if the block itself is completely different.

Link to comment
Share on other sites

So basicly this?

	@Override
	public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate) {
		return !(newSate.getBlock() instanceof BlockAutoMiner);
	}

Also, typo in the games code: newSate instead of newState :P

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

Apart from that I do not see anything that would reset the fuel values. I see that you are changing blockstates in your tileentity without a check if you are on a server and not a client so it could be causing your issue.

Why do you have 2 different blocks for your auto miner? Powered and not can be a property that is obtained from say your tileentity in getActualState. You will need to make sure it is synced though.

Link to comment
Share on other sites

I do not think vanilla has an example of that kind. I can provide you with an example though. It shows you how to make metadata independent properties that are obtained from data in tileentity. Making powered a property should not be much different from that.

Edited by V0idWa1k3r
Link to comment
Share on other sites

Here's another example, this time with 3 properties all controlled by TE data. The important thing is that when the data changes you need to sync it to the client, you need to call these methods, override these, and make sure that the property data is saved to NBT.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Also, is this going to work?

package com.Egietje.AutoMiner.blocks;

import java.util.Random;

import com.Egietje.AutoMiner.entities.tileentities.TileEntityAutoMiner;
import com.Egietje.AutoMiner.init.AMBlocks;
import com.Egietje.AutoMiner.init.AMItems;

import net.minecraft.block.Block;
import net.minecraft.block.BlockHorizontal;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBoat;
import net.minecraft.item.ItemDoor;
import net.minecraft.item.ItemHoe;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.item.ItemTool;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.Mirror;
import net.minecraft.util.Rotation;
import net.minecraft.util.SoundCategory;
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 BlockAutoMiner extends Block implements ITileEntityProvider {

	public static final PropertyDirection FACING = BlockHorizontal.FACING;
	public static IProperty ACTIVATED = PropertyBool.create("activated");

	public BlockAutoMiner() {
		super(Material.PISTON);
		this.setDefaultState(
				this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(ACTIVATED, false));
	}

	@Override
	public Item getItemDropped(IBlockState state, Random rand, int fortune) {
		return Item.getItemFromBlock(AMBlocks.AUTO_MINER);
	}

	@Override
	public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state) {
		return new ItemStack(AMBlocks.AUTO_MINER);
	}

	public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) {
		this.setDefaultFacing(worldIn, pos, state);
	}

	private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state) {
		if (!worldIn.isRemote) {
			IBlockState iblockstate = worldIn.getBlockState(pos.north());
			IBlockState iblockstate1 = worldIn.getBlockState(pos.south());
			IBlockState iblockstate2 = worldIn.getBlockState(pos.west());
			IBlockState iblockstate3 = worldIn.getBlockState(pos.east());
			EnumFacing enumfacing = (EnumFacing) state.getValue(FACING);

			if (enumfacing == EnumFacing.NORTH && iblockstate.isFullBlock() && !iblockstate1.isFullBlock()) {
				enumfacing = EnumFacing.SOUTH;
			} else if (enumfacing == EnumFacing.SOUTH && iblockstate1.isFullBlock() && !iblockstate.isFullBlock()) {
				enumfacing = EnumFacing.NORTH;
			} else if (enumfacing == EnumFacing.WEST && iblockstate2.isFullBlock() && !iblockstate3.isFullBlock()) {
				enumfacing = EnumFacing.EAST;
			} else if (enumfacing == EnumFacing.EAST && iblockstate3.isFullBlock() && !iblockstate2.isFullBlock()) {
				enumfacing = EnumFacing.WEST;
			}

			worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
		}
	}

	public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY,
			float hitZ, int meta, EntityLivingBase placer) {
		return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
	}

	public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer,
			ItemStack stack) {
		worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing()), 2);
	}

	public IBlockState getStateFromMeta(int meta) {
		EnumFacing enumfacing = EnumFacing.getFront(meta);

		if (enumfacing.getAxis() == EnumFacing.Axis.Y) {
			enumfacing = EnumFacing.NORTH;
		}

		return this.getDefaultState().withProperty(FACING, enumfacing);
	}

	@Override
	public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
		TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
		return state.withProperty(ACTIVATED, te.ACTIVATED);
	}

	public int getMetaFromState(IBlockState state) {
		return ((EnumFacing) state.getValue(FACING)).getIndex();
	}

	public IBlockState withRotation(IBlockState state, Rotation rot) {
		return state.withProperty(FACING, rot.rotate((EnumFacing) state.getValue(FACING)));
	}

	public IBlockState withMirror(IBlockState state, Mirror mirrorIn) {
		return state.withRotation(mirrorIn.toRotation((EnumFacing) state.getValue(FACING)));
	}

	protected BlockStateContainer createBlockState() {
		return new BlockStateContainer(this, new IProperty[] { FACING, ACTIVATED });
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand) {
		if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
			TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
			if (te.ACTIVATED) {
				EnumFacing enumfacing = (EnumFacing) stateIn.getValue(FACING);
				double d0 = (double) pos.getX() + 0.5D;
				double d1 = (double) pos.getY() + 0.5D;
				double d2 = (double) pos.getZ() + 0.5D;
				double d3 = 0.52D;
				double d4 = rand.nextDouble() * 0.6D - 0.3D;

				switch (enumfacing) {
				case WEST:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case EAST:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 - d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case NORTH:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 + d3, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case SOUTH:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 - d3, 0.0D, 0.0D, 0.0D,
							new int[0]);
				default:
					break;
				}
			}
		}
	}

	@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
			EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
		if (!worldIn.isRemote && hand == EnumHand.MAIN_HAND) {
			if (TileEntityFurnace.isItemFuel(playerIn.getHeldItem(hand))) {
				if (!playerIn.isCreative()) {
					playerIn.getHeldItem(hand).shrink(1);
				}
				if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
					TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
					te.addFuel(TileEntityFurnace.getItemBurnTime(playerIn.getHeldItem(hand)));
				}
			} else if (playerIn.getHeldItem(hand).getItem() == AMItems.WRENCH) {
				if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
					TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
					if (te.ACTIVATED) {
						te.setActivated(false);
					} else {
						te.setActivated(true);
					}
				}
			}
			return true;
		}
		return false;

	}

	@Override
	public TileEntity createNewTileEntity(World worldIn, int meta) {
		return new TileEntityAutoMiner();
	}
}
package com.Egietje.AutoMiner.entities.tileentities;

import com.Egietje.AutoMiner.blocks.BlockAutoMiner;
import com.Egietje.AutoMiner.init.AMBlocks;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class TileEntityAutoMiner extends TileEntity implements ITickable {

	public int FUEL_AMOUNT = 0;
	public boolean ACTIVATED = false;

	public void addFuel(int amount) {
		System.out.println("Before: " + FUEL_AMOUNT);
		System.out.println("Added: " + amount);
		this.FUEL_AMOUNT += amount;
		System.out.println("After: " + FUEL_AMOUNT);
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.notifyBlockUpdate(pos, state, state, 3);
	}

	public void removeFuel() {
		if (this.FUEL_AMOUNT > 0) {
			this.FUEL_AMOUNT--;
			markDirty();
			IBlockState state = world.getBlockState(pos);
			world.notifyBlockUpdate(pos, state, state, 3);
		}
	}
	
	public boolean isActivated() {
		return ACTIVATED;
	}
	
	public void setActivated(boolean active) {
		ACTIVATED = active;
	}

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		compound.setInteger("FuelAmount", this.FUEL_AMOUNT);
		compound.setBoolean("Activated", this.ACTIVATED);
		return compound;
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		this.FUEL_AMOUNT = compound.getInteger("FuelAmount");
		this.ACTIVATED = compound.getBoolean("Activated");
	}

	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
		super.onDataPacket(net, pkt);
		handleUpdateTag(pkt.getNbtCompound());
	}

	@Override
	public SPacketUpdateTileEntity getUpdatePacket() {
		return new SPacketUpdateTileEntity(pos, 3, getUpdateTag());
	}

	@Override
	public NBTTagCompound getUpdateTag() {
		return this.writeToNBT(new NBTTagCompound());
	}

	@Override
	public void update() {
		if (ACTIVATED) {
			removeFuel();
			if (FUEL_AMOUNT <= 0) {
				FUEL_AMOUNT = 0;
				setActivated(false);
			}
		}
	}

	@Override
	public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate) {
		return !(newSate.getBlock() instanceof BlockAutoMiner);
	}

}

 

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

1) Do not use ITileEntityProvider, override the methods found in the Block class. ITileEntityProvider is old and is based on metadata, not IBlockstate

2) You need to mark dirty in the case if (FUEL_AMOUNT <= 0) { } as well.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

So now I've got this.

package com.Egietje.AutoMiner.blocks;

import java.util.Random;

import com.Egietje.AutoMiner.entities.tileentities.TileEntityAutoMiner;
import com.Egietje.AutoMiner.init.AMBlocks;
import com.Egietje.AutoMiner.init.AMItems;

import net.minecraft.block.Block;
import net.minecraft.block.BlockHorizontal;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBoat;
import net.minecraft.item.ItemDoor;
import net.minecraft.item.ItemHoe;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.item.ItemTool;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.Mirror;
import net.minecraft.util.Rotation;
import net.minecraft.util.SoundCategory;
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 BlockAutoMiner extends Block {

	public static final PropertyDirection FACING = BlockHorizontal.FACING;
	public static IProperty ACTIVATED = PropertyBool.create("activated");

	public BlockAutoMiner() {
		super(Material.PISTON);
		this.setDefaultState(
				this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(ACTIVATED, false));
	}

	@Override
	public Item getItemDropped(IBlockState state, Random rand, int fortune) {
		return Item.getItemFromBlock(AMBlocks.AUTO_MINER);
	}

	@Override
	public ItemStack getItem(World worldIn, BlockPos pos, IBlockState state) {
		return new ItemStack(AMBlocks.AUTO_MINER);
	}

	public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) {
		this.setDefaultFacing(worldIn, pos, state);
	}

	private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state) {
		if (!worldIn.isRemote) {
			IBlockState iblockstate = worldIn.getBlockState(pos.north());
			IBlockState iblockstate1 = worldIn.getBlockState(pos.south());
			IBlockState iblockstate2 = worldIn.getBlockState(pos.west());
			IBlockState iblockstate3 = worldIn.getBlockState(pos.east());
			EnumFacing enumfacing = (EnumFacing) state.getValue(FACING);

			if (enumfacing == EnumFacing.NORTH && iblockstate.isFullBlock() && !iblockstate1.isFullBlock()) {
				enumfacing = EnumFacing.SOUTH;
			} else if (enumfacing == EnumFacing.SOUTH && iblockstate1.isFullBlock() && !iblockstate.isFullBlock()) {
				enumfacing = EnumFacing.NORTH;
			} else if (enumfacing == EnumFacing.WEST && iblockstate2.isFullBlock() && !iblockstate3.isFullBlock()) {
				enumfacing = EnumFacing.EAST;
			} else if (enumfacing == EnumFacing.EAST && iblockstate3.isFullBlock() && !iblockstate2.isFullBlock()) {
				enumfacing = EnumFacing.WEST;
			}

			worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
		}
	}

	public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY,
			float hitZ, int meta, EntityLivingBase placer) {
		return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
	}

	public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer,
			ItemStack stack) {
		worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing()), 2);
	}

	public IBlockState getStateFromMeta(int meta) {
		EnumFacing enumfacing = EnumFacing.getFront(meta);

		if (enumfacing.getAxis() == EnumFacing.Axis.Y) {
			enumfacing = EnumFacing.NORTH;
		}

		return this.getDefaultState().withProperty(FACING, enumfacing);
	}

	@Override
	public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
		TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
		return state.withProperty(ACTIVATED, te.ACTIVATED);
	}

	public int getMetaFromState(IBlockState state) {
		return ((EnumFacing) state.getValue(FACING)).getIndex();
	}

	public IBlockState withRotation(IBlockState state, Rotation rot) {
		return state.withProperty(FACING, rot.rotate((EnumFacing) state.getValue(FACING)));
	}

	public IBlockState withMirror(IBlockState state, Mirror mirrorIn) {
		return state.withRotation(mirrorIn.toRotation((EnumFacing) state.getValue(FACING)));
	}

	protected BlockStateContainer createBlockState() {
		return new BlockStateContainer(this, new IProperty[] { FACING, ACTIVATED });
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand) {
		if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
			TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
			if (te.ACTIVATED) {
				EnumFacing enumfacing = (EnumFacing) stateIn.getValue(FACING);
				double d0 = (double) pos.getX() + 0.5D;
				double d1 = (double) pos.getY() + 0.5D;
				double d2 = (double) pos.getZ() + 0.5D;
				double d3 = 0.52D;
				double d4 = rand.nextDouble() * 0.6D - 0.3D;

				switch (enumfacing) {
				case WEST:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case EAST:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 - d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case NORTH:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 + d3, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case SOUTH:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 - d3, 0.0D, 0.0D, 0.0D,
							new int[0]);
				default:
					break;
				}
			}
		}
	}

	@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
			EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
		if (!worldIn.isRemote && hand == EnumHand.MAIN_HAND) {
			if (TileEntityFurnace.isItemFuel(playerIn.getHeldItem(hand))) {
				if (!playerIn.isCreative()) {
					playerIn.getHeldItem(hand).shrink(1);
				}
				if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
					TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
					te.addFuel(TileEntityFurnace.getItemBurnTime(playerIn.getHeldItem(hand)));
				}
			} else if (playerIn.getHeldItem(hand).getItem() == AMItems.WRENCH) {
				if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
					TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
					if (te.ACTIVATED) {
						te.setActivated(false);
					} else {
						te.setActivated(true);
					}
				}
			}
			return true;
		}
		return false;

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

	@Override
	public TileEntity createTileEntity(World world, IBlockState state) {
		return new TileEntityAutoMiner();
	}
}
package com.Egietje.AutoMiner.entities.tileentities;

import com.Egietje.AutoMiner.blocks.BlockAutoMiner;
import com.Egietje.AutoMiner.init.AMBlocks;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class TileEntityAutoMiner extends TileEntity implements ITickable {

	public int FUEL_AMOUNT = 0;
	public boolean ACTIVATED = false;

	public void addFuel(int amount) {
		System.out.println("Before: " + FUEL_AMOUNT);
		System.out.println("Added: " + amount);
		this.FUEL_AMOUNT += amount;
		System.out.println("After: " + FUEL_AMOUNT);
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.notifyBlockUpdate(pos, state, state, 3);
	}

	public void removeFuel() {
		if (this.FUEL_AMOUNT > 0) {
			this.FUEL_AMOUNT--;
			markDirty();
			IBlockState state = world.getBlockState(pos);
			world.notifyBlockUpdate(pos, state, state, 3);
		}
	}
	
	public boolean isActivated() {
		return ACTIVATED;
	}
	
	public void setActivated(boolean active) {
		ACTIVATED = active;
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.notifyBlockUpdate(pos, state, state, 3);
	}

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		compound.setInteger("FuelAmount", this.FUEL_AMOUNT);
		compound.setBoolean("Activated", this.ACTIVATED);
		return compound;
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		this.FUEL_AMOUNT = compound.getInteger("FuelAmount");
		this.ACTIVATED = compound.getBoolean("Activated");
	}

	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
		super.onDataPacket(net, pkt);
		handleUpdateTag(pkt.getNbtCompound());
	}

	@Override
	public SPacketUpdateTileEntity getUpdatePacket() {
		return new SPacketUpdateTileEntity(pos, 3, getUpdateTag());
	}

	@Override
	public NBTTagCompound getUpdateTag() {
		return this.writeToNBT(new NBTTagCompound());
	}

	@Override
	public void update() {
		if (ACTIVATED) {
			removeFuel();
			if (FUEL_AMOUNT <= 0) {
				FUEL_AMOUNT = 0;
				markDirty();
				setActivated(false);
			}
		}
	}

	@Override
	public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate) {
		return !(newSate.getBlock() instanceof BlockAutoMiner);
	}

}

It seems like I didn't create the tile entity right...

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

So what's the problem?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I don't see anything wrong, so you're probably going to have to debug this yourself.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Video:

package com.Egietje.AutoMiner.entities.tileentities;

import com.Egietje.AutoMiner.blocks.BlockAutoMiner;
import com.Egietje.AutoMiner.init.AMBlocks;

import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class TileEntityAutoMiner extends TileEntity implements ITickable {

	public int FUEL_AMOUNT = 0;
	public boolean ACTIVATED = false;
	
	public TileEntityAutoMiner() {
	}

	public void addFuel(int amount) {
		System.out.println("Before: " + FUEL_AMOUNT);
		System.out.println("Added: " + amount);
		this.FUEL_AMOUNT += amount;
		System.out.println("After: " + FUEL_AMOUNT);
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.notifyBlockUpdate(pos, state, state, 3);
	}

	public void removeFuel() {
		if (this.FUEL_AMOUNT > 0) {
			this.FUEL_AMOUNT--;
			markDirty();
			IBlockState state = world.getBlockState(pos);
			world.notifyBlockUpdate(pos, state, state, 3);
		}
	}
	
	public boolean isActivated() {
		return ACTIVATED;
	}
	
	public void setActivated(boolean active) {
		ACTIVATED = active;
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		world.notifyBlockUpdate(pos, state, state, 3);
		world.scheduleBlockUpdate(pos,this.getBlockType(),0,0);
		markDirty();
	}

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		compound.setInteger("FuelAmount", this.FUEL_AMOUNT);
		compound.setBoolean("Activated", this.ACTIVATED);
		return compound;
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		this.FUEL_AMOUNT = compound.getInteger("FuelAmount");
		this.ACTIVATED = compound.getBoolean("Activated");
	}

	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
		super.onDataPacket(net, pkt);
		handleUpdateTag(pkt.getNbtCompound());
	}

	@Override
	public SPacketUpdateTileEntity getUpdatePacket() {
		return new SPacketUpdateTileEntity(pos, 3, getUpdateTag());
	}

	@Override
	public NBTTagCompound getUpdateTag() {
		return this.writeToNBT(new NBTTagCompound());
	}

	@Override
	public void update() {
		if (ACTIVATED) {
			System.out.println(FUEL_AMOUNT);
			removeFuel();
			if (FUEL_AMOUNT <= 0) {
				FUEL_AMOUNT = 0;
				markDirty();
				setActivated(false);
			}
		}
	}
}
package com.Egietje.AutoMiner.blocks;

import java.util.Random;

import com.Egietje.AutoMiner.entities.tileentities.TileEntityAutoMiner;
import com.Egietje.AutoMiner.init.AMBlocks;
import com.Egietje.AutoMiner.init.AMItems;

import net.minecraft.block.Block;
import net.minecraft.block.BlockHorizontal;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBoat;
import net.minecraft.item.ItemDoor;
import net.minecraft.item.ItemHoe;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.item.ItemTool;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.Mirror;
import net.minecraft.util.Rotation;
import net.minecraft.util.SoundCategory;
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 BlockAutoMiner extends Block {

	public static final PropertyDirection FACING = BlockHorizontal.FACING;
	public static IProperty ACTIVATED = PropertyBool.create("activated");

	public BlockAutoMiner() {
		super(Material.PISTON);
		this.setDefaultState(
				this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(ACTIVATED, false));
	}

	public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) {
		this.setDefaultFacing(worldIn, pos, state);
	}

	private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state) {
		if (!worldIn.isRemote) {
			IBlockState iblockstate = worldIn.getBlockState(pos.north());
			IBlockState iblockstate1 = worldIn.getBlockState(pos.south());
			IBlockState iblockstate2 = worldIn.getBlockState(pos.west());
			IBlockState iblockstate3 = worldIn.getBlockState(pos.east());
			EnumFacing enumfacing = (EnumFacing) state.getValue(FACING);

			if (enumfacing == EnumFacing.NORTH && iblockstate.isFullBlock() && !iblockstate1.isFullBlock()) {
				enumfacing = EnumFacing.SOUTH;
			} else if (enumfacing == EnumFacing.SOUTH && iblockstate1.isFullBlock() && !iblockstate.isFullBlock()) {
				enumfacing = EnumFacing.NORTH;
			} else if (enumfacing == EnumFacing.WEST && iblockstate2.isFullBlock() && !iblockstate3.isFullBlock()) {
				enumfacing = EnumFacing.EAST;
			} else if (enumfacing == EnumFacing.EAST && iblockstate3.isFullBlock() && !iblockstate2.isFullBlock()) {
				enumfacing = EnumFacing.WEST;
			}

			worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
		}
	}

	public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY,
			float hitZ, int meta, EntityLivingBase placer) {
		return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
	}

	public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer,
			ItemStack stack) {
		worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing()), 2);
	}

	public IBlockState getStateFromMeta(int meta) {
		EnumFacing enumfacing = EnumFacing.getFront(meta);

		if (enumfacing.getAxis() == EnumFacing.Axis.Y) {
			enumfacing = EnumFacing.NORTH;
		}

		return this.getDefaultState().withProperty(FACING, enumfacing);
	}

	@Override
	public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
		TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
		return state.withProperty(ACTIVATED, te.ACTIVATED);
	}

	public int getMetaFromState(IBlockState state) {
		return ((EnumFacing) state.getValue(FACING)).getIndex();
	}

	public IBlockState withRotation(IBlockState state, Rotation rot) {
		return state.withProperty(FACING, rot.rotate((EnumFacing) state.getValue(FACING)));
	}

	public IBlockState withMirror(IBlockState state, Mirror mirrorIn) {
		return state.withRotation(mirrorIn.toRotation((EnumFacing) state.getValue(FACING)));
	}

	protected BlockStateContainer createBlockState() {
		return new BlockStateContainer(this, new IProperty[] { FACING, ACTIVATED });
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand) {
		if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
			TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
			if (te.ACTIVATED) {
				EnumFacing enumfacing = (EnumFacing) stateIn.getValue(FACING);
				double d0 = (double) pos.getX() + 0.5D;
				double d1 = (double) pos.getY() + 0.5D;
				double d2 = (double) pos.getZ() + 0.5D;
				double d3 = 0.52D;
				double d4 = rand.nextDouble() * 0.6D - 0.3D;

				switch (enumfacing) {
				case WEST:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case EAST:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 - d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case NORTH:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 + d3, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case SOUTH:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 - d3, 0.0D, 0.0D, 0.0D,
							new int[0]);
				default:
					break;
				}
			}
		}
	}

	@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
			EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
		if (!worldIn.isRemote && hand == EnumHand.MAIN_HAND) {
			if (TileEntityFurnace.isItemFuel(playerIn.getHeldItem(hand))) {
				if (!playerIn.isCreative()) {
					playerIn.getHeldItem(hand).shrink(1);
				}
				if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
					TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
					te.addFuel(TileEntityFurnace.getItemBurnTime(playerIn.getHeldItem(hand)));
				}
			} else if (playerIn.getHeldItem(hand).getItem() == AMItems.WRENCH) {
				if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
					TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
					if (te.ACTIVATED) {
						te.setActivated(false);
					} else {
						if (te.FUEL_AMOUNT > 0) {
							te.setActivated(true);
						}
					}
				}
			}
			return true;
		}
		return false;

	}

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

	@Override
	public TileEntity createTileEntity(World world, IBlockState state) {
		return new TileEntityAutoMiner();
	}
	
	@Override
	public boolean hasTileEntity(IBlockState state) {
		return true;
	}
}

How can I fix this?

Edited by Kokkie
Forgot to add code

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

Okay, now I've got another bug.

I've created the code that removes blocks etc. but it seems to be bugged out. What happens is:

  1. I feed the machine fuel.
  2. I start the machine.
  3. Machine destroys first nine blocks.
  4. Machine bugs out and won't go any further and can't be turned off.

I found this is because after running:

					BlockPos position = pos.offset(world.getBlockState(pos).getValue(BlockAutoMiner.FACING));
					world.setBlockState(position, world.getBlockState(pos));
					world.setTileEntity(position, world.getTileEntity(pos));
					world.setBlockToAir(pos)

The world isn't a "server" anymore, so it won't return false when world.isRemote is called.

Does anyone know what could be causing this?

Block class

public class BlockAutoMiner extends Block {

	public static final PropertyDirection FACING = BlockHorizontal.FACING;
	public static IProperty ACTIVATED = PropertyBool.create("activated");

	public BlockAutoMiner() {
		super(Material.PISTON);
		this.setDefaultState(
				this.blockState.getBaseState().withProperty(FACING, EnumFacing.NORTH).withProperty(ACTIVATED, false));
	}

	public void onBlockAdded(World worldIn, BlockPos pos, IBlockState state) {
		this.setDefaultFacing(worldIn, pos, state);
	}

	private void setDefaultFacing(World worldIn, BlockPos pos, IBlockState state) {
		if (!worldIn.isRemote) {
			IBlockState iblockstate = worldIn.getBlockState(pos.north());
			IBlockState iblockstate1 = worldIn.getBlockState(pos.south());
			IBlockState iblockstate2 = worldIn.getBlockState(pos.west());
			IBlockState iblockstate3 = worldIn.getBlockState(pos.east());
			EnumFacing enumfacing = (EnumFacing) state.getValue(FACING);

			if (enumfacing == EnumFacing.NORTH && iblockstate.isFullBlock() && !iblockstate1.isFullBlock()) {
				enumfacing = EnumFacing.SOUTH;
			} else if (enumfacing == EnumFacing.SOUTH && iblockstate1.isFullBlock() && !iblockstate.isFullBlock()) {
				enumfacing = EnumFacing.NORTH;
			} else if (enumfacing == EnumFacing.WEST && iblockstate2.isFullBlock() && !iblockstate3.isFullBlock()) {
				enumfacing = EnumFacing.EAST;
			} else if (enumfacing == EnumFacing.EAST && iblockstate3.isFullBlock() && !iblockstate2.isFullBlock()) {
				enumfacing = EnumFacing.WEST;
			}

			worldIn.setBlockState(pos, state.withProperty(FACING, enumfacing), 2);
		}
	}

	public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY,
			float hitZ, int meta, EntityLivingBase placer) {
		return this.getDefaultState().withProperty(FACING, placer.getHorizontalFacing().getOpposite());
	}

	public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer,
			ItemStack stack) {
		worldIn.setBlockState(pos, state.withProperty(FACING, placer.getHorizontalFacing()), 2);
	}

	public IBlockState getStateFromMeta(int meta) {
		EnumFacing enumfacing = EnumFacing.getFront(meta);

		if (enumfacing.getAxis() == EnumFacing.Axis.Y) {
			enumfacing = EnumFacing.NORTH;
		}

		return this.getDefaultState().withProperty(FACING, enumfacing);
	}

	@Override
	public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
		TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
		return state.withProperty(ACTIVATED, te.ACTIVATED);
	}

	public int getMetaFromState(IBlockState state) {
		return ((EnumFacing) state.getValue(FACING)).getIndex();
	}

	public IBlockState withRotation(IBlockState state, Rotation rot) {
		return state.withProperty(FACING, rot.rotate((EnumFacing) state.getValue(FACING)));
	}

	public IBlockState withMirror(IBlockState state, Mirror mirrorIn) {
		return state.withRotation(mirrorIn.toRotation((EnumFacing) state.getValue(FACING)));
	}

	protected BlockStateContainer createBlockState() {
		return new BlockStateContainer(this, new IProperty[] { FACING, ACTIVATED });
	}

	@Override
	@SideOnly(Side.CLIENT)
	public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand) {
		if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
			TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
			if (te.ACTIVATED) {
				EnumFacing enumfacing = (EnumFacing) stateIn.getValue(FACING);
				double d0 = (double) pos.getX() + 0.5D;
				double d1 = (double) pos.getY() + 0.5D;
				double d2 = (double) pos.getZ() + 0.5D;
				double d3 = 0.52D;
				double d4 = rand.nextDouble() * 0.6D - 0.3D;

				switch (enumfacing) {
				case WEST:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case EAST:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 - d3, d1, d2 + d4, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case NORTH:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 + d3, 0.0D, 0.0D, 0.0D,
							new int[0]);
					break;
				case SOUTH:
					worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 - d3, 0.0D, 0.0D, 0.0D,
							new int[0]);
				default:
					break;
				}
			}
		}
	}

	@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
			EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
		if (!worldIn.isRemote) {
			if (hand == EnumHand.MAIN_HAND) {
				if (TileEntityFurnace.isItemFuel(playerIn.getHeldItem(hand))) {
					if (!playerIn.isCreative()) {
						playerIn.getHeldItem(hand).shrink(1);
					}
					if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
						TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
						te.addFuel(TileEntityFurnace.getItemBurnTime(playerIn.getHeldItem(hand)));
					}
				} else if (playerIn.getHeldItem(hand).getItem() == AMItems.WRENCH) {
					if (worldIn.getTileEntity(pos) instanceof TileEntityAutoMiner) {
						TileEntityAutoMiner te = (TileEntityAutoMiner) worldIn.getTileEntity(pos);
						if (te.isActivated()) {
							te.setActivated(false);
						} else {
							if (te.FUEL_AMOUNT > 0) {
								te.setActivated(true);
							}
						}
					}
				}
			}
			return true;
		} else {
			if (hand == EnumHand.MAIN_HAND) {
				if (playerIn.getHeldItem(hand).getItem() == AMItems.WRENCH) {
					Minecraft.getMinecraft().player.swingArm(hand);
				}
			}
		}
		return false;

	}

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

	@Override
	public TileEntity createTileEntity(World world, IBlockState state) {
		return new TileEntityAutoMiner();
	}

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

Tile entity class

public class TileEntityAutoMiner extends TileEntity implements ITickable {

	public int FUEL_AMOUNT = 0;
	public boolean ACTIVATED = false;
	public boolean UPDATE = false;
	public int TICKS = 0;

	public TileEntityAutoMiner() {
	}

	public void addFuel(int amount) {
		this.FUEL_AMOUNT += amount;
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		markDirty();
	}

	public void removeFuel() {
		if (this.FUEL_AMOUNT > 0) {
			this.FUEL_AMOUNT--;
			IBlockState state = world.getBlockState(pos);
			world.markBlockRangeForRenderUpdate(pos, pos);
			markDirty();
		}
	}

	public boolean isActivated() {
		return ACTIVATED;
	}

	public void setActivated(boolean active) {
		ACTIVATED = active;
		markDirty();
		IBlockState state = world.getBlockState(pos);
		world.markBlockRangeForRenderUpdate(pos, pos);
		world.notifyBlockUpdate(pos, state, state, 3);
		world.scheduleBlockUpdate(pos, this.getBlockType(), 0, 0);
	}

	public void addTick() {
		TICKS++;
		markDirty();
	}

	public void removeTicks() {
		TICKS = 0;
		markDirty();
	}

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		compound.setInteger("FuelAmount", this.FUEL_AMOUNT);
		compound.setBoolean("Activated", this.ACTIVATED);
		compound.setInteger("Ticks", this.TICKS);
		return compound;
	}

	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		this.FUEL_AMOUNT = compound.getInteger("FuelAmount");
		this.ACTIVATED = compound.getBoolean("Activated");
		this.TICKS = compound.getInteger("Ticks");
	}

	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
		super.onDataPacket(net, pkt);
		handleUpdateTag(pkt.getNbtCompound());
	}

	@Override
	public SPacketUpdateTileEntity getUpdatePacket() {
		return new SPacketUpdateTileEntity(pos, 3, getUpdateTag());
	}

	@Override
	public NBTTagCompound getUpdateTag() {
		return this.writeToNBT(new NBTTagCompound());
	}

	@Override
	public void update() {
		if (ACTIVATED) {
			UPDATE = true;
			removeFuel();
			if (FUEL_AMOUNT <= 0) {
				FUEL_AMOUNT = 0;
				setActivated(false);
				markDirty();
			}
			if (TICKS == 20) {
				if (!world.isRemote) {
					EnumFacing facing_ = (EnumFacing) world.getBlockState(pos).getValue(BlockAutoMiner.FACING);
					int index_ = facing_.getHorizontalIndex();
					int index = index_ == 0 ? index_ + 3 : index_ - 1;
					EnumFacing facing = EnumFacing.getHorizontal(index);
					int index1 = index_ == 3 ? index_ - 3 : index_ + 1;
					EnumFacing facing1 = EnumFacing.getHorizontal(index1);

					BlockPos position = pos.offset(world.getBlockState(pos).getValue(BlockAutoMiner.FACING));
					BlockPos position1 = position.up();
					BlockPos position2 = position.down();
					BlockPos position3 = position.offset(facing);
					BlockPos position4 = position.offset(facing1);
					BlockPos position5 = position3.up();
					BlockPos position6 = position4.up();
					BlockPos position7 = position3.down();
					BlockPos position8 = position4.down();

					ItemStack stack = new ItemStack(world.getBlockState(position).getBlock());
					ItemStack stack1 = new ItemStack(world.getBlockState(position1).getBlock());
					ItemStack stack2 = new ItemStack(world.getBlockState(position2).getBlock());
					ItemStack stack3 = new ItemStack(world.getBlockState(position3).getBlock());
					ItemStack stack4 = new ItemStack(world.getBlockState(position4).getBlock());
					ItemStack stack5 = new ItemStack(world.getBlockState(position5).getBlock());
					ItemStack stack6 = new ItemStack(world.getBlockState(position6).getBlock());
					ItemStack stack7 = new ItemStack(world.getBlockState(position7).getBlock());
					ItemStack stack8 = new ItemStack(world.getBlockState(position8).getBlock());

					world.spawnEntity(new EntityItem(world, pos.getX(), pos.getY() + 1, pos.getZ(), stack));
					world.spawnEntity(new EntityItem(world, pos.getX(), pos.getY() + 1, pos.getZ(), stack1));
					world.spawnEntity(new EntityItem(world, pos.getX(), pos.getY() + 1, pos.getZ(), stack2));
					world.spawnEntity(new EntityItem(world, pos.getX(), pos.getY() + 1, pos.getZ(), stack3));
					world.spawnEntity(new EntityItem(world, pos.getX(), pos.getY() + 1, pos.getZ(), stack4));
					world.spawnEntity(new EntityItem(world, pos.getX(), pos.getY() + 1, pos.getZ(), stack5));
					world.spawnEntity(new EntityItem(world, pos.getX(), pos.getY() + 1, pos.getZ(), stack6));
					world.spawnEntity(new EntityItem(world, pos.getX(), pos.getY() + 1, pos.getZ(), stack7));
					world.spawnEntity(new EntityItem(world, pos.getX(), pos.getY() + 1, pos.getZ(), stack8));

					world.setBlockToAir(position);
					world.setBlockToAir(position1);
					world.setBlockToAir(position2);
					world.setBlockToAir(position3);
					world.setBlockToAir(position4);
					world.setBlockToAir(position5);
					world.setBlockToAir(position6);
					world.setBlockToAir(position7);
					world.setBlockToAir(position8);
				}
			}
			if (TICKS == 40) {
				if (!world.isRemote) {
					BlockPos position = pos.offset(world.getBlockState(pos).getValue(BlockAutoMiner.FACING));
					world.setBlockState(position, world.getBlockState(pos));
					world.setTileEntity(position, world.getTileEntity(pos));
					world.setBlockToAir(pos);
				}
			}

		} else {
			if (UPDATE) {
				IBlockState state = world.getBlockState(pos);
				world.markBlockRangeForRenderUpdate(pos, pos);
			}
			UPDATE = false;
		}
		if (TICKS >= 40) {
			this.removeTicks();
		} else {
			this.addTick();
		}
	}

}

Also, no errors in the console.

Edited by Kokkie

Classes: 94

Lines of code: 12173

Other files: 206

Github repo: https://github.com/KokkieBeer/DeGeweldigeMod

Link to comment
Share on other sites

3 hours ago, Kokkie said:

Machine bugs out and won't go any further and can't be turned off.

"Bugs out" doesn't mean anything. Step through in the debugger. Tell us what really happens.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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.

Announcements



×
×
  • Create New...

Important Information

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