Jump to content

TE properties seemingly being set randomly


meee39
 Share

Recommended Posts

In my mod I have a TE that does a function while it has power. When it runs out of power it will consume one piece of fuel from its inventory (if there is any there). Every tick the power level decreases by one and when it gets to the end it triggers the fuel consumption. So the block has two different states: powered and unpowered. You can tell which state it is in because they have different textures. The problem is while the power ticks left are counting down the block state flickers between the powered and unpowered state. I also put in a System.out.printline() to check what is going on and it would end up like this:

Spoiler

10

0

9

0

8

0

...

Instead of:

Spoiler

10

9

8

7

6

...

Like it should do.

 

I don't know anything about packets and data syncing with FML although I know that things to do with that can cause problems so I think it might be something to do with data not being synced between client and server.

 

Code:

public class TileEntityEnderSuppressor extends TileEntity implements ITickable, IMobSuppressorTE {
	private World world = Minecraft.getMinecraft().world;
	public BlockEnderSuppressor block;
	public boolean isPublic;
	public static final int maxTicks = 300;
	public int powerTicksLeft = 0;
	
	public ItemStackHandler itemStackHandler = new ItemStackHandler(1) {
		@Override
		protected void onContentsChanged(int slot) {
			TileEntityEnderSuppressor.this.markDirty();
		}
	};
	
	public TileEntityEnderSuppressor() {}
	
	public TileEntityEnderSuppressor(BlockEnderSuppressor block) {
		this.block = block;
		this.isPublic = true;
	}
	
	@Override
	public void update() {
		if (world.getBlockState(getPos()).getBlock() instanceof BlockEnderSuppressor != true) {
			return;
		}
		
		if (world != null) {
			Random RNG = new Random();
			world.spawnParticle(EnumParticleTypes.PORTAL, RNG.nextFloat() + pos.getX(), RNG.nextFloat() + pos.getY(), RNG.nextFloat() + pos.getZ(), 0.1, 0.1, 0.1, 0);
		}
		
		if (powerTicksLeft > 0) {
			powerTicksLeft--;
			world.setBlockState(this.getPos(), world.getBlockState(getPos()).withProperty(block.is_powered, false));
		} else {
			if (itemStackHandler.getStackInSlot(0).isEmpty()) {
				world.setBlockState(this.getPos(), world.getBlockState(getPos()).withProperty(block.is_powered, true));
			} else {
				if (itemStackHandler.getStackInSlot(0).getItem() instanceof ItemEnderPearl) {
					itemStackHandler.getStackInSlot(0).shrink(1);
					setFullPowerTicks();
				}
			}
		}
	}
	
	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		
		compound.setTag("items", itemStackHandler.serializeNBT());
		
		compound.setBoolean("isPublic", isPublic);
		compound.setInteger("powerTicksLeft", powerTicksLeft);
		
		return compound;
	}
	
	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		
		if (compound.hasKey("items")) {
			itemStackHandler.deserializeNBT((NBTTagCompound)compound.getTag("items"));
		}
		
		isPublic = compound.getBoolean("isPublic");
		powerTicksLeft = compound.getInteger("powerTicksLeft");
	}

	@Override
	public boolean isPublic() {
		return isPublic;
	}

	@Override
	public void setFullPowerTicks() {
		this.powerTicksLeft = maxTicks;
	}

	@Override
	public int getPowerTicksLeft() {
		return powerTicksLeft;
	}
	
	@Override
    public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return true;
        }
        return super.hasCapability(capability, facing);
    }

    @Override
    public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return (T) itemStackHandler;
        }
        return super.getCapability(capability, facing);
    }
	
	public boolean canInteractWith(EntityPlayer playerIn) {
        return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
    }
public class BlockEnderSuppressor extends Block implements ITileEntityProvider {
	public static final PropertyBool is_powered = PropertyBool.create("is_powered");
	
	public IMobSuppressorTE TE;
	
	protected BlockEnderSuppressor(String unlocalizedName, String registryName) {
		super(Material.IRON);
		this.setUnlocalizedName(unlocalizedName);
		this.setRegistryName(registryName);
		this.setHardness(2);
		this.setResistance(6000f);
		this.setHarvestLevel("pickaxe", 3);
		this.isBlockContainer = true;
		this.setDefaultState(this.blockState.getBaseState().withProperty(is_powered, false));
		this.setCreativeTab(CreativeTab.mobSuppressorCreativeTab);
	}
	
	@Override
	protected BlockStateContainer createBlockState() {
		return new BlockStateContainer(this, is_powered);
	}
	
	@Override
	public IBlockState getStateFromMeta(int meta) {
		return this.getDefaultState().withProperty(is_powered, meta == 0);
	}
	
	@Override
	public int getMetaFromState(IBlockState state) {
		return state.getValue(is_powered) ? 1: 0;
	}
	
	@Override
	public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos) {
		return state.withProperty(is_powered, state.getValue(is_powered));
	}
	
	@Override
	public TileEntity createNewTileEntity(World worldIn, int meta) {
		TE = new TileEntityEnderSuppressor(this);
		
		return (TileEntity)TE;
	}
	
	@Override
	public void breakBlock(World world, BlockPos pos, IBlockState state) {
		super.breakBlock(world, pos, state);
		world.removeTileEntity(pos);
	}

	@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) {
			playerIn.openGui(MobSuppressors.instance, ModGUIHandler.enderSuppressorGUIID, worldIn, pos.getX(), pos.getY(), pos.getZ());
		}
		
		return true;
	}	
}

If you need any other classes to find the problem just ask.

Link to comment
Share on other sites

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

7 hours ago, meee39 said:

I also put in a System.out.printline() to check what is going on

Does the printing run on both client and server? If so, then one may be counting down while the other is stuck at zero.

 

If you want to see what is really going on, then step through your code in the debugger, examining variables as you go.

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

1) you implement those methods

2) you call these when the data changes

Edited by Draco18s

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

That didn't make any difference. Here is the class now:

package com.leo.mobsuppressors.tileentity;

import java.util.Random;

import com.leo.mobsuppressors.blocks.BlockEnderSuppressor;

import jline.internal.Nullable;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemEnderPearl;
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.EnumFacing;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.ITickable;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

public class TileEntityEnderSuppressor extends TileEntity implements ITickable, IMobSuppressorTE {
	public static BlockEnderSuppressor block;
	public boolean isPublic;
	public static final int maxTicks = 300;
	public int powerTicksLeft = 0;
	public Random RNG = new Random();
	
	public ItemStackHandler itemStackHandler = new ItemStackHandler(1) {
		@Override
		protected void onContentsChanged(int slot) {
			TileEntityEnderSuppressor.this.markDirty();
		}
	};
	
	public TileEntityEnderSuppressor() {}
	
	public TileEntityEnderSuppressor(BlockEnderSuppressor block) {
		this.block = block;
		this.isPublic = true;
	}
	
	@Override
	public void update() {
		if (!(world.getBlockState(getPos()).getBlock() instanceof BlockEnderSuppressor)) {
			return;
		}
	
		world.spawnParticle(EnumParticleTypes.PORTAL, RNG.nextFloat() + pos.getX(), RNG.nextFloat() + pos.getY(), RNG.nextFloat() + pos.getZ(), 0.1, 0.1, 0.1, 0);
		
		if (!world.isRemote) {
			world.setBlockState(this.getPos(), world.getBlockState(getPos()).withProperty(block.is_powered, true));
			
			if (powerTicksLeft > 0) {
				powerTicksLeft--;
				world.markBlockRangeForRenderUpdate(pos, pos);
				world.notifyBlockUpdate(pos, world.getBlockState(getPos()), world.getBlockState(getPos()).withProperty(block.is_powered, true), 3);
				world.scheduleBlockUpdate(pos,this.getBlockType(),0,0);
				markDirty();
				world.setBlockState(this.getPos(), world.getBlockState(getPos()).withProperty(block.is_powered, true));
			} else {
				if (itemStackHandler.getStackInSlot(0).isEmpty()) {
					world.setBlockState(this.getPos(), world.getBlockState(getPos()).withProperty(block.is_powered, false));
				} else {
					if (itemStackHandler.getStackInSlot(0).getItem() instanceof ItemEnderPearl) {
						itemStackHandler.getStackInSlot(0).shrink(1);
						setFullPowerTicks();
						world.markBlockRangeForRenderUpdate(pos, pos);
						world.notifyBlockUpdate(pos, world.getBlockState(getPos()), world.getBlockState(getPos()).withProperty(block.is_powered, true), 3);
						world.scheduleBlockUpdate(pos,this.getBlockType(),0,0);
						markDirty();
					}
				}
			}
		}
		System.out.println(powerTicksLeft);
	}
	
	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound compound) {
		super.writeToNBT(compound);
		
		compound.setTag("items", itemStackHandler.serializeNBT());
		
		compound.setBoolean("isPublic", isPublic);
		compound.setInteger("powerTicksLeft", powerTicksLeft);
		
		return compound;
	}
	
	@Override
	public void readFromNBT(NBTTagCompound compound) {
		super.readFromNBT(compound);
		
		if (compound.hasKey("items")) {
			itemStackHandler.deserializeNBT((NBTTagCompound)compound.getTag("items"));
		}
		
		isPublic = compound.getBoolean("isPublic");
		powerTicksLeft = compound.getInteger("powerTicksLeft");
	}

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

	@Override
	public NBTTagCompound getUpdateTag() {
		return this.writeToNBT(new NBTTagCompound());
	}
	
	@Override
	public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
		super.onDataPacket(net, pkt);
		handleUpdateTag(pkt.getNbtCompound());
	}
	
	@Override
	public boolean isPublic() {
		return isPublic;
	}

	@Override
	public void setFullPowerTicks() {
		this.powerTicksLeft = maxTicks;
	}

	@Override
	public int getPowerTicksLeft() {
		return powerTicksLeft;
	}
	
	@Override
    public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return true;
        }
        return super.hasCapability(capability, facing);
    }

    @Override
    public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
        if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return (T) itemStackHandler;
        }
        return super.getCapability(capability, facing);
    }
	
	public boolean canInteractWith(EntityPlayer playerIn) {
        return !isInvalid() && playerIn.getDistanceSq(pos.add(0.5D, 0.5D, 0.5D)) <= 64D;
    }
}

 

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
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.

 Share



×
×
  • Create New...

Important Information

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