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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • When I first heard about Bitcoin back in 2018, I was skeptical. The idea of a decentralized, digital currency seemed too good to be true. But I was intrigued as I learned more about the technology behind it and its potential. I started small, investing just a few hundred dollars, dipping my toes into the cryptocurrency waters. At first, it was exhilarating to watch the value of my investment grow exponentially. I felt like I was part of the future, an early adopter of this revolutionary new asset. But that euphoria was short-lived. One day, I logged into my digital wallet only to find it empty - my Bitcoin had vanished without a trace. It turned out that the online exchange I had trusted had been hacked, and my funds were stolen. I was devastated, both financially and emotionally. All the potential I had seen in Bitcoin was tainted by the harsh reality that with decentralization came a lack of regulation and oversight. My hard-earned money was gone, lost to the ether of the digital world. This experience taught me a painful lesson about the price of trust in the uncharted territory of cryptocurrency. While the technology holds incredible promise, the risks can be catastrophic if you don't approach it with extreme caution. My Bitcoin investment gamble had failed, and I was left to pick up the pieces, wiser but poorer for having placed my faith in the wrong hands. My sincere appreciation goes to MUYERN TRUST HACKER. You are my hero in recovering my lost funds. Send a direct m a i l ( muyerntrusted ( @ ) mail-me ( . )c o m ) or message on whats app : + 1 ( 4-4-0 ) ( 3 -3 -5 ) ( 0-2-0-5 )
    • You could try posting a log (if there is no log at all, it may be the launcher you are using, the FAQ may have info on how to enable the log) as described in the FAQ, however this will probably need to be reported to/remedied by the mod author.
    • So me and a couple of friends are playing with a shitpost mod pack and one of the mods in the pack is corail tombstone and for some reason there is a problem with it, where on death to fire the player will get kicked out of the server and the tombstone will not spawn basically deleting an entire inventory, it doesn't matter what type of fire it is, whether it's from vanilla fire/lava, or from modded fire like ice&fire/lycanites and it's common enough to where everyone on the server has experienced at least once or twice and it doesn't give any crash log. a solution to this would be much appreciated thank you!
    • It is 1.12.2 - I have no idea if there is a 1.12 pack
  • Topics

×
×
  • Create New...

Important Information

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