Jump to content

[1.7.2] Using recursion to find block in a cable system.


SwordsMiner

Recommended Posts

Hey guys, I am trying to find a block in a system of cables, the idea is simple:

 

Block 1 says "I need BlockLOL.class", Wire 1 says "I dont have it? Do you wire 2?", Wire 2 says "Its next to me! here!", and it goes back down the line. However right now I cant even get the nearby blocks, and when I was able to somehow, it wouldnt detect any cables and would stop. Heres my code:

 

General wire, BlockWire

package com.revereor.blocks;

import java.util.ArrayList;

import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

import com.revereor.blocks.util.BlockHelper;
import com.revereor.tileentity.TileEntityWire;

public class BlockWire extends BlockContainer{

protected TileEntityWire tileEntity;

public BlockWire() {
	super(Material.cloth);
	this.setStepSound(soundTypeCloth);
	this.setHardness(0.1F);
}

public BlockWire(Material material) {
	super(material);
}

public BlockGeneral getBlockFromSystem(Class type, BlockWire last){
	Block[] nearby = BlockHelper.getDirectNearbyBlocks(tileEntity);
	for(Block bl : nearby){
		if(type.isInstance(bl))
			return (BlockGeneral) bl;
		if(type.isInstance(this))
			return (BlockGeneral) this;
		ArrayList<BlockWire> nearbyWires = getNearbyWires();
		for(BlockWire wire : nearbyWires){
			BlockGeneral bg = wire.getBlockFromSystem(type, this);
			if(bg != null)
				return bg;
		}
	}
	return null;
}

public ArrayList<BlockWire> getNearbyWires(){
	ArrayList<BlockWire> wires = new ArrayList<BlockWire>();
	Block[] nearby = BlockHelper.getDirectNearbyBlocks(tileEntity);
	for(Block bl : nearby){
		if(!Block.isEqualTo(bl, this)){
			if(bl instanceof BlockWire){
				wires.add((BlockWire)bl);
			}
		}			
	}
	return wires;
}

public void onBlockAdded(World world,int x, int y, int z){
	tileEntity.x=x;
	tileEntity.y=y;
	tileEntity.z=z;
}

@Override
public TileEntity createNewTileEntity(World var1, int var2) {
	tileEntity = new TileEntityWire();
	return tileEntity;
}



}

 

BlockRemoteControlCenter, block thats trying to get control center.

 

package com.revereor.blocks;

import com.revereor.tileentity.TileEntityWire;

import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;

public class BlockRemoteControlCenter extends BlockGeneral {

public BlockRemoteControlCenter() {
	super(Material.iron);
	this.setStepSound(soundTypeMetal);
}

 public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
     {
	BlockControlCenter con = this.getControlCenter();
	if(con != null){
		System.out.println(con);
	} else System.out.println("no control center found");
	return false; 
     }

@Override
public TileEntityWire setTileEntity() {
	return new TileEntityWire();
}



}

 

Block Control center, The block we need.

package com.revereor.blocks;

import com.revereor.tileentity.TileEntityWire;

import net.minecraft.block.material.Material;

public class BlockControlCenter extends BlockGeneral{

public BlockControlCenter() {
	super(Material.iron);
	this.setStepSound(soundTypeMetal);
}

@Override
public TileEntityWire setTileEntity() {
	return new TileEntityWire();
}

}

 

BlockGeneral, the abstract base of functional blocks, but not wires.

package com.revereor.blocks;

import java.util.ArrayList;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

import com.revereor.blocks.util.BlockHelper;
import com.revereor.tileentity.TileEntityWire;

public abstract class BlockGeneral extends BlockWire{

protected BlockGeneral(Material p_i45394_1_) {
	super(p_i45394_1_);
}

public BlockGeneral getBlock(Class type){
	ArrayList<BlockWire> wires = getNearbyWires();
	for(BlockWire wire : wires){
		BlockGeneral bl = wire.getBlockFromSystem(type,this);
		if(bl != null){
			return bl;
		}
	}
	return null;

}

public BlockControlCenter getControlCenter(){
	return (BlockControlCenter) getBlock(BlockControlCenter.class);
}

public abstract TileEntityWire setTileEntity();

@Override
public TileEntity createNewTileEntity(World var1, int var2) {
	tileEntity = setTileEntity();
	return tileEntity;
}
}

 

BlockHelper, used to get blocks

package com.revereor.blocks.util;

import net.minecraft.block.Block;
import net.minecraft.tileentity.TileEntity;

import com.revereor.main.Main;

public class BlockHelper {

public static boolean isDirect(int x, int tx, int y, int ty, int z, int tz){
	return     ((x == tx+1 || x == tx-1) && !(y == ty+1 || y == ty-1) && !(z == tz+1 || z == tz-1)) 
			|| (!(x == tx+1 || x == tx-1) && (y == ty+1 || y == ty-1) && !(z == tz+1 || z == tz-1))
			|| (!(x == tx+1 || x == tx-1) && !(y == ty+1 || y == ty-1) && (z == tz+1 || z == tz-1));
}

public static boolean isIndirect(int x, int tx, int y, int ty, int z, int tz){
	return !isDirect(x, tx, y, ty, z, tz);
}

public static Block[] getDirectNearbyBlocks(TileEntity tileEntity){
	Block[] blks = new Block[6];
	for(int x = tileEntity.xCoord-1; x < tileEntity.xCoord+1; x++){
		for(int y = tileEntity.yCoord-1; y < tileEntity.yCoord+1; y++){
			for(int z = tileEntity.zCoord-1; z < tileEntity.zCoord+1; z++){
				if(isDirect(x, tileEntity.xCoord, y, tileEntity.yCoord, z, tileEntity.zCoord)){
					for(int i = 0; i < blks.length; i++){
						if(blks[i] == null){
							blks[i] = tileEntity.getWorldObj().getBlock(x, y, z);
						} else if(i == blks.length){
							Main.print("Exceeded maximum blocks while getting nearby blocks.");
						}
					}
				}
			}
		}
	}
	return blks;
}

}

 

Open to any other ideas to get this!

Link to comment
Share on other sites

One of your main problems is the field tileEntity declared in your BlockWire class. You cannot do that as every wire in the game will share the same tileEntity. That's not what you want and it will cause crashes.

Link to comment
Share on other sites

One of your main problems is the field tileEntity declared in your BlockWire class. You cannot do that as every wire in the game will share the same tileEntity. That's not what you want and it will cause crashes.

 

How should I go about this then? Previously it worked for me when I used onBlockAdded and saved x y and z but, yes, as your implying it all went down hill with the Tile Entity. How should I go about finding the x, y, and z correctly?

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.