Jump to content

Recommended Posts

Posted

So here are my 2 classes (The block and tile entity)

 

Block

package com.biosystemstudios;

import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;

public class CannonBlock extends Block implements ITileEntityProvider {

    private Icon blockTop;
    private Icon blockFront;
    private int xFront;
    private int zFront;
    private int yFront;
    private Entity player;
public CannonBlock(int ID) {
	super(ID, Material.iron);
	this.setCreativeTab(CreativeTabs.tabBlock);
}

    
    
    /**
     * Returns a new instance of a block's tile entity class. Called on placing the block.
     */
    public TileEntity createNewTileEntity(World par1World)
    {
        return new CannonTileEntity(par1World);
    }

    /**
     * set a blocks direction
     */
    private void setDefaultDirection(World par1World, int par2, int par3, int par4)
    {
        if (!par1World.isRemote)
        {
            int l = par1World.getBlockId(par2, par3, par4 - 1);
            int i1 = par1World.getBlockId(par2, par3, par4 + 1);
            int j1 = par1World.getBlockId(par2 - 1, par3, par4);
            int k1 = par1World.getBlockId(par2 + 1, par3, par4);
            byte b0 = 3;

            if (Block.opaqueCubeLookup[l] && !Block.opaqueCubeLookup[i1])
            {
                b0 = 3;
            }

            if (Block.opaqueCubeLookup[i1] && !Block.opaqueCubeLookup[l])
            {
                b0 = 2;
            }

            if (Block.opaqueCubeLookup[j1] && !Block.opaqueCubeLookup[k1])
            {
                b0 = 5;
            }

            if (Block.opaqueCubeLookup[k1] && !Block.opaqueCubeLookup[j1])
            {
                b0 = 4;
            }

            par1World.setBlockMetadataWithNotify(par2, par3, par4, b0, 2);
        }
    }

    public void onBlockPlacedBy(World par1World, int x, int y, int z, EntityLiving par5EntityLiving, ItemStack par6ItemStack)
    {
        int l = MathHelper.floor_double((double)(par5EntityLiving.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
        yFront = y;
        CannonTileEntity cannontileentity = (CannonTileEntity)par1World.getBlockTileEntity(x, y, z);
        
        if (l == 0)
        {
            par1World.setBlockMetadataWithNotify(x, y, z, 2, 2);
            xFront = x;
            zFront = z-1;
            if(cannontileentity != null){
            	cannontileentity.setFrontOfCannon(xFront, yFront, zFront);
            	cannontileentity.setFrontDirection("z", true);
            }
        }

        if (l == 1)
        {
            par1World.setBlockMetadataWithNotify(x, y, z, 5, 2);
            xFront = x+1;
            zFront = z;
            if(cannontileentity != null){
            	cannontileentity.setFrontOfCannon(xFront, yFront, zFront);
            	cannontileentity.setFrontDirection("x", false);
            }
        }

        if (l == 2)
        {
            par1World.setBlockMetadataWithNotify(x, y, z, 3, 2);
            xFront = x;
            zFront = z+1;
            if(cannontileentity != null){
            	cannontileentity.setFrontOfCannon(xFront, yFront, zFront);
            	cannontileentity.setFrontDirection("z", false);
            }
        }

        if (l == 3)
        {
            par1World.setBlockMetadataWithNotify(x, y, z, 4, 2);
            xFront = x-1;
            zFront = z;
            if(cannontileentity != null){
            	cannontileentity.setFrontOfCannon(xFront, yFront, zFront);
            	cannontileentity.setFrontDirection("x", true);
            }
        }
    }

    
@Override
    public void registerIcons(IconRegister par1IconRegister)
    {
        this.blockIcon = par1IconRegister.registerIcon("et:cannonOther");
        this.blockFront = par1IconRegister.registerIcon("et:cannonFront");
        this.blockTop = par1IconRegister.registerIcon("et:cannonTop");
    }


    public Icon getBlockTextureFromSideAndMetadata(int par1, int par2)
    {
    	/* 
    	 * par1 = 1 - Top
    	 * par1 = 0 - Bottom
    	 */
        return par1 == 1 ? this.blockTop : (par1 == 0 ? this.blockIcon : (par1 != par2 ? this.blockIcon : this.blockFront));
    }
    
    public void onBlockAdded(World par1World, int x, int y, int z)
    {
        super.onBlockAdded(par1World, x, y, z);
        this.setDefaultDirection(par1World, x, y, z);
    }
    
    public boolean onBlockActivated(World par1World, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9)
    {
    	if (par1World.isRemote)
    	{
    		return true;
    	}else{
    		CannonTileEntity cannontileentity = (CannonTileEntity)par1World.getBlockTileEntity(x, y, z);
    		if(cannontileentity != null){
    			cannontileentity.powerOn();
    			if(cannontileentity.getPowered() == true){
    				player.sendChatToPlayer(cannontileentity.destroy());
    			}
    		}
	    return true;
       }
    }


}

 

TileEntity

package com.biosystemstudios;

import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityTNTPrimed;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class CannonTileEntity extends TileEntity{
private boolean powered;
private World world;
private int frontX;
private int frontY;
private int frontZ;
private long coolDownTime;
private String frontAxis;
private boolean negativeOnAxis;

public CannonTileEntity(World par1World){
	world = par1World;
}

/**
 * Reads a tile entity from NBT.
 */
public void readFromNBT(NBTTagCompound par1NBTTagCompound)
{
	super.readFromNBT(par1NBTTagCompound);
	this.powered = par1NBTTagCompound.getBoolean("power");
	this.frontX = par1NBTTagCompound.getInteger("fx");
	this.frontY = par1NBTTagCompound.getInteger("fy");
	this.frontZ = par1NBTTagCompound.getInteger("fz");
	this.coolDownTime = par1NBTTagCompound.getLong("cdt");
	this.frontAxis = par1NBTTagCompound.getString("fa");
	this.negativeOnAxis = par1NBTTagCompound.getBoolean("na");   
}

/**
 * Writes a tile entity to NBT.
 */
public void writeToNBT(NBTTagCompound par1NBTTagCompound)
{
	super.writeToNBT(par1NBTTagCompound);
	par1NBTTagCompound.setBoolean("power", this.powered);
	par1NBTTagCompound.setInteger("fx", frontX);
	par1NBTTagCompound.setInteger("fy", frontY);
	par1NBTTagCompound.setInteger("fz", frontZ);
	par1NBTTagCompound.setLong("cdt", coolDownTime);
	par1NBTTagCompound.setString("fa", this.frontAxis);
	par1NBTTagCompound.setBoolean("na", this.negativeOnAxis);
}

public boolean getPowered(){
	return powered;
}
public void powerOn(){
	powered = true;
}

public String destroy(){
	if(coolDownTime <= (world.getTotalWorldTime())){
		if(world.getBlockId(xCoord, yCoord-1, zCoord) == Block.obsidian.blockID){
			double Dir = 1.0;
			float power = 2.0f;

			if(negativeOnAxis){Dir = -1.0;}

			for(int steps = 1; steps < 256; steps++){
				if(steps<=3)
					power += 0.2f;
				if(frontAxis == "x")
					world.createExplosion(null, frontX + (steps*Dir), ((double)frontY)+0.5, frontZ, power, true);

				if(frontAxis == "z")
					world.createExplosion(null, frontX, frontY+0.5, ((double)frontZ) + (steps*Dir), power, true);

			}

			powered = false;
			coolDownTime = (world.getTotalWorldTime()) + 30*20;
			return "Fired Cannon, please wait " + getCoolDownTime() + " Seconds Before Using Again.";

		}else{
			return "Cannon needs an obsidian base to operate";
		}
	}else{
		return "Cannon Cooling down: "+getCoolDownTime() + " Seconds Left";
	}
}

public void setFrontOfCannon(int x, int y, int z){
	frontX = x;
	frontY = y;
	frontZ = z;
}

public void setFrontDirection(String axis, boolean nDirection){
	frontAxis = axis;
	negativeOnAxis = nDirection;
}

public long getCoolDownTime(){
	return (coolDownTime - world.getTotalWorldTime())/20;
}


}

 

It works fine when I first place the block down. The cool down timer works, the front of block values but when I save and quit and re-enter the world and right click the block it seems to have forgotten not only the cool down timer but also where the front of the block is (instantiated in my block class)

 

Not too sure what is going wrong. I think it may have something to do with my NBTTagCompound in my tile entity class but not quite sure

Bumper Cars!

Posted

I've been having a similar issue.  You're doing things a little differently than I am, but the solution will likely work for both of us.

 

Whatever it is.

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.

Posted

I've been having a similar issue.  You're doing things a little differently than I am, but the solution will likely work for both of us.

 

Whatever it is.

 

It's strange because I'm using TileEntityNote (Tile Entity for the note block) as a reference but the solution is just not coming to me :P

Bumper Cars!

Posted

It's strange because I'm using TileEntityNote (Tile Entity for the note block) as a reference but the solution is just not coming to me :P

 

I was using another mod as a reference.  The sloped blocks one, actually (the source is up on Github, IIRC).

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.

Posted

I was using another mod as a reference.  The sloped blocks one, actually (the source is up on Github, IIRC).

 

Hmmm considering you're having the same issue I think the both of us are just missing like 1-5 lines of code from making these NBT Tag things work lol

Bumper Cars!

Posted

Probably!

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.

Posted

I think this may be because the values after quit/load are not being propagated from the server side to the client.  I had a similar problem until I added:

public Packet getDescriptionPacket()
   {
   NBTTagCompound tag = new NBTTagCompound();
   this.writeToNBT(tag);
   return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, tag);
  }

and

@Override
public void onDataPacket(INetworkManager net, Packet132TileEntityData packet)
{ This function looks similar to a readFromNBT function, Google some examples }

The first function is serverside, and the second is clientside. 

 

In my updateEntity, I also have a call (when necessary) to:

PacketDispatcher.sendPacketToAllPlayers(getDescriptionPacket());

it keeps things in sync between the server and client.  I didn't see an updateEntity in your code, so I don't know if that would be relevant to you.

Posted

I think this may be because the values after quit/load are not being propagated from the server side to the client.  I had a similar problem until I added:

public Packet getDescriptionPacket()
   {
   NBTTagCompound tag = new NBTTagCompound();
   this.writeToNBT(tag);
   return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, tag);
  }

and

@Override
public void onDataPacket(INetworkManager net, Packet132TileEntityData packet)
{ This function looks similar to a readFromNBT function, Google some examples }

The first function is serverside, and the second is clientside. 

 

In my updateEntity, I also have a call (when necessary) to:

PacketDispatcher.sendPacketToAllPlayers(getDescriptionPacket());

it keeps things in sync between the server and client.  I didn't see an updateEntity in your code, so I don't know if that would be relevant to you.

 

Hmm can't seem to find anything on onDataPacket /:

Bumper Cars!

Posted

ok so here are some of the methods I added to hopefully fix my issue but alas it is still not working

 

public Packet getDescriptionPacket()
   {
   NBTTagCompound tag = new NBTTagCompound();
   this.writeToNBT(tag);
   return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, tag);
  }

@Override
public void onDataPacket(INetworkManager net, Packet132TileEntityData packet)
{
	NBTTagCompound tag = packet.customParam1;
	this.powered = tag.getBoolean("power");
	this.frontX = tag.getInteger("fx");
	this.frontY = tag.getInteger("fy");
	this.frontZ = tag.getInteger("fz");
	this.coolDownTime = tag.getLong("cdt");
	this.frontAxis = tag.getString("fa");
	this.negativeOnAxis = tag.getBoolean("na"); 
	this.readFromNBT(tag);
}

 

/: this is starting to get extremely frustrating

Bumper Cars!

Posted

Ok, found the issue: You have the constructor CannonTileEntity(World). Don't do that.

Your TileEntity should not have a constructor (the world object is automatically set!). Otherwise Minecraft can't recreate your TE from disk.

 

Thank you thank you thank you xD that was the problem. It was the only thing preventing me from releasing this mod :P now time to prep for the release

Bumper Cars!

Posted

This helped me get solved too.  We were in fact both making the same mistakes!

 

width=800 height=449http://s8.postimg.org/f4ybhigdh/2013_03_29_20_54_34.png[/img]

 

Woo, mosaics!

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.

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.