Jump to content

Recommended Posts

Posted

I've working in java a fair amount, but am new to working in forge, so I suspect its something obvious that I'm missing here. I am working on a mod that involves a torque system, and I need to store the direction of the torque shaft. I'm doing this by storing an integer that I check in my TESR. I am aware I could do this using forge's direction thingy, but since I need to store more data than is implemented in my code already I'm going to have this issue more in future if I don't resolve it now. All the tutorials and example code seem to do this a similar way so I am genuinely stuck here. Any help would be much appreciated.

 

I am getting my debug statements printing telling me that my variable has been assigned correctly, but by the time the debug in my accessor method fires from being called by my renderer, the variable has reset to 0. Since it is an integer, it could just be that it has reset to null, and this is what I suspect is what is happening. I have posted the complete code of the TileEntity class I'm working on. If you need any additional code to help me please let me know. As you can see the variable is private, and I've searched my other scripts and none of them call my setDirection method. I am really quite truly very stuck. Thanks in advance for any help.

 

public class TorqueShaftTileEntity extends TileEntity implements TorqueObject{
public TorqueShaft block;

private TorqueObject parent;

private int direction; //0 N/S, 1 E/W, 2 U/D

public TorqueShaftTileEntity(){
	super();
	this.block = (TorqueShaft)blockType;
	//direction = 0;
}

public int recalcFacing(World world, int x, int y, int z){
	int tempDir;

	if(world.getBlock(x + 1, y, z) instanceof TorqueObject || (world.getBlock(x + 1, y, z).hasTileEntity(0) && world.getTileEntity(x + 1, y, z) instanceof TorqueObject) ||
			world.getBlock(x - 1, y, z) instanceof TorqueObject || (world.getBlock(x - 1, y, z).hasTileEntity(0) && world.getTileEntity(x - 1, y, z) instanceof TorqueObject)) 
		tempDir = 1;
	else if(world.getBlock(x, y, z + 1) instanceof TorqueObject || (world.getBlock(x, y, z + 1).hasTileEntity(0) && world.getTileEntity(x, y, z + 1) instanceof TorqueObject) ||
			world.getBlock(x, y, z - 1) instanceof TorqueObject || (world.getBlock(x, y, z - 1).hasTileEntity(0) && world.getTileEntity(x, y, z - 1) instanceof TorqueObject)) 
		tempDir = 2;
	else tempDir = 3;

	setDirection(tempDir);
	parent = null;

	return tempDir;
}

public int incrementDirection(){
	if(++direction > 3) direction = 1;
	parent = null;
	return direction;
}

public int getDirection(){
	System.out.println("Sending Direction: " + direction);
	return direction;
}

public void setDirection(int dir){
	System.out.println("Setting Direction: " + dir);
	this.direction = dir;
}

public void checkTorquePaths(World world, int x, int y, int z){
	TorqueObject a = null;
	TorqueObject b = null;

	System.out.println("Direction for torque: " + direction);

	if(direction == 1){
		if(world.getBlock(x + 1, y, z) instanceof TorqueObject) a = (TorqueObject)world.getBlock(x + 1, y, z);
		if(world.getBlock(x - 1, y, z) instanceof TorqueObject) b = (TorqueObject)world.getBlock(x - 1, y, z);
	}

	if(direction == 2){
		if(world.getBlock(x, y, z + 1) instanceof TorqueObject) a = (TorqueObject)world.getBlock(x, y, z + 1);
		if(world.getBlock(x, y, z - 1) instanceof TorqueObject) b = (TorqueObject)world.getBlock(x, y, z - 1);
	}

	if(direction == 3){
		if(world.getBlock(x, y + 1, z) instanceof TorqueObject) a = (TorqueObject)world.getBlock(x, y + 1, z);
		if(world.getBlock(x, y - 1, z) instanceof TorqueObject) b = (TorqueObject)world.getBlock(x, y - 1, z);
	}

	if(a != null && b != null){
		if(a.getSource() == null && b.getSource() == null) this.parent = null;
		else if(a.getSource() != null && b.getSource() == null){
			this.parent = a.getSource();
		}
		else if(a.getSource() == null && b.getSource() != null) {
			this.parent = b.getSource();
		}
		else if(a.getSource() != null && b.getSource() != null){
			block.breakBlock(world, x, y, z, block, 0);
		}
	}
	else if(a != null && a.getSource() != null) this.parent = a.getSource();
	else if(b != null && b.getSource() != null) this.parent = b.getSource();
}

public void onBlockAdded(World world, int x, int y, int z) {
	System.out.println("Placing and rotating torque shaft");
	recalcFacing(world, x, y, z);
	checkTorquePaths(world, x, y, z);
	System.out.println("Direction at end of block add: " + direction);
}

public void onNeighborBlockChange(World world, int x, int y, int z){
	checkTorquePaths(world, x, y, z);
}

public TorqueObject getSource() {
	return parent;
}

public boolean useTorque(float amount) {
	return parent != null && parent.useTorque(amount);
}

public void setSource(TorqueObject other) {
	parent = other;
}

public float getRotSpeed(){
	if(parent != null) return parent.getRotSpeed();
	return 0;
}

public float getRotation(){
	if(parent != null) return parent.getRotation();
	return 0;
}

public void writeToNBT(NBTTagCompound nbt){
	nbt.setInteger("direction", this.direction);
	super.writeToNBT(nbt);
}

public void readFromNBT(NBTTagCompound nbt){
	this.direction = nbt.getInteger("direction");
	super.readFromNBT(nbt);
}
}

Posted

Ok, that makes sense. Thank you. So how do I implement that in forge? Is there some example or an API anywhere that could help me understand how to do that?

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.