Jump to content

[Solved]Get Blocks around another one


hnsdieter

Recommended Posts

Hello,

 

I figured out that ForgeDirection is the best way to-do this but it seems like the Code is running twice:

 

0:35:50] [Client thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.util.BlockUtil:getBlockA:14]: 457.4.-892
[20:35:50] [Client thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.block.BRuneCrafter:onBlockActivated:36]: tile.sponge
[20:35:50] [Client thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.util.BlockUtil:getBlockA:14]: 457.6.-892
[20:35:50] [Client thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.block.BRuneCrafter:onBlockActivated:36]: tile.grass
[20:35:50] [Client thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.util.BlockUtil:getBlockA:14]: 457.5.-893
[20:35:50] [Client thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.block.BRuneCrafter:onBlockActivated:36]: tile.blockEmerald
[20:35:50] [Client thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.util.BlockUtil:getBlockA:14]: 457.5.-891
[20:35:50] [Client thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.block.BRuneCrafter:onBlockActivated:36]: tile.sponge
[20:35:50] [Client thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.util.BlockUtil:getBlockA:14]: 456.5.-892
[20:35:50] [Client thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.block.BRuneCrafter:onBlockActivated:36]: hnsdieter:tile.hnscrystals:stringemitter
[20:35:50] [Client thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.util.BlockUtil:getBlockA:14]: 458.5.-892
[20:35:50] [Client thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.block.BRuneCrafter:onBlockActivated:36]: tile.air
[20:35:50] [server thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.util.BlockUtil:getBlockA:14]: 457.4.-892
[20:35:50] [server thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.block.BRuneCrafter:onBlockActivated:36]: tile.sponge
[20:35:50] [server thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.util.BlockUtil:getBlockA:14]: 457.6.-892
[20:35:50] [server thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.block.BRuneCrafter:onBlockActivated:36]: tile.grass
[20:35:50] [server thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.util.BlockUtil:getBlockA:14]: 457.5.-893
[20:35:50] [server thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.block.BRuneCrafter:onBlockActivated:36]: tile.blockEmerald
[20:35:50] [server thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.util.BlockUtil:getBlockA:14]: 457.5.-891
[20:35:50] [server thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.block.BRuneCrafter:onBlockActivated:36]: tile.sponge
[20:35:50] [server thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.util.BlockUtil:getBlockA:14]: 456.5.-892
[20:35:50] [server thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.block.BRuneCrafter:onBlockActivated:36]: hnsdieter:tile.hnscrystals:stringemitter
[20:35:50] [server thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.util.BlockUtil:getBlockA:14]: 458.5.-892
[20:35:50] [server thread/INFO] [sTDOUT]: [hnsdieter.hnsCrystals.block.BRuneCrafter:onBlockActivated:36]: tile.air

 

My Client's log...

 

My Block:

 

public class BRuneCrafter extends hnsBlock implements ITileEntityProvider{

public BRuneCrafter(String name) {
	super(Material.rock, name);
	this.setHarvestLevel("pickaxe", 2);
	this.setHardness(5.0F);
	this.setResistance(1000.0F);
}

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int int1, float f1, float f2, float f3){
        ItemStack item = player.getHeldItem();
        if(item == null){
        	return false;
        }
        if(item.getItem() == hnsItems.crystalStaff){
        	int i = 0;
        	while(i < 6){
        		Block block = BlockUtil.getBlockA(world, x, y, z, i);
        		if(block == null){
        			
        		}else{
        		System.out.println(block.getUnlocalizedName());
        		}
        		i = i + 1;
        	}
        }
        return false;
    }

@Override
public TileEntity createNewTileEntity(World world, int i) {
	return null;
}

}

 

My BlockUtil class:

 

public class BlockUtil {

public static Block getBlockA(World world, int x, int y, int z, int i){
	ForgeDirection direction = ForgeDirection.getOrientation(i);
	int xCoord = x + direction.offsetX;
	int yCoord = y + direction.offsetY;
	int zCoord = z + direction.offsetZ;
	System.out.println(xCoord + "." + yCoord + "." + zCoord);
	return world.getBlock(xCoord, yCoord, zCoord);
}

}

 

Please help me to find the derp...

Link to comment
Share on other sites

First of all, why so complicated. Use a foreach loop over ForgeDirection.VALID_DIRECTIONS instead of using an integer. You loose the nice abstraction that ForgeDirection gives you if you do it how you do it right now.

It runs twice, once on the server once on the client.

 

I thanked this post because just yesterday I did it the "wrong" way (using the integer).  But in looking at my code I'm not exactly thrilled with the result, as I was trying to find the "best" adjacent empty block in which to spawn the item drops (due to what happens when the block is broken: it replaces itself at 1 lower meta, restricting how many times it can be harvested, but if the block-above is solid, the items jiggle upwards through the ground and tend to make it to the surface, rather that shooting out sideways).

 

Looping through the valid-directions array is cleaner, but I don't actually care about all of the directions (just the first that isn't full-cube) and would like to specifically do "down" last (the scenario where the player is above/adjacent to empty space, the items shouldn't fall into the void; in the reverse scenario they are retrievable once the player fully mines the block).

 

So unless diesieben07 has a suggestion, I'm going to leave my code alone.

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

Hmm.

 

Here's what I ended up with.

 

for(ForgeDirection dir : OreData.DROP_SEARCH_DIRECTIONS) {
if(!world.getBlock(x+dir.offsetX, y+dir.offsetY, z+dir.offsetZ).isNormalCube()) {
	EntityItem entityitem = new EntityItem(world, (double)x + d0+dir.offsetX, (double)y + d1+dir.offsetY, (double)z + d2+dir.offsetZ, stack);
	entityitem.delayBeforeCanPickup = 10;
	world.spawnEntityInWorld(entityitem);
	return;
}
}

 

Shoved the cached array into my OreData class (this being the dropBlockAsItem method of my ore blocks), as it seemed the most appropriate location I already had.

 

public static final ForgeDirection[] DROP_SEARCH_DIRECTIONS = {ForgeDirection.UP, ForgeDirection.NORTH, ForgeDirection.SOUTH, ForgeDirection.WEST, ForgeDirection.EAST, ForgeDirection.DOWN};

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

Thank you for the support!

 

This is my solution now:

public static Block[] getBlockB(World world, int x, int y, int z){
	ForgeDirection[] directions = ForgeDirection.VALID_DIRECTIONS;
	Block[] blocks = new Block[directions.length];
	for(int i = 0; i < directions.length; i++){
		int xCoord = x + directions[i].offsetX;
		int yCoord = y + directions[i].offsetY;
		int zCoord = z + directions[i].offsetZ;

		blocks[i] = world.getBlock(xCoord, yCoord, zCoord);
	}
	return blocks;
}

 

My Block:

package hnsdieter.hnsCrystals.block;

import hnsdieter.hnsCrystals.item.hnsItems;
import hnsdieter.hnsCrystals.tileentity.BRuneCrafterTileEntity;
import hnsdieter.hnsCrystals.util.BlockUtil;
import net.minecraft.block.Block;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class BRuneCrafter extends hnsBlock implements ITileEntityProvider{

public BRuneCrafter(String name) {
	super(Material.rock, name);
	this.setHarvestLevel("pickaxe", 2);
	this.setHardness(5.0F);
	this.setResistance(1000.0F);
}

@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int int1, float f1, float f2, float f3){
        if(world.isRemote){
        	
        }else{
        	ItemStack item = player.getHeldItem();
		if(item == null){
        		return false;
        	}
        	if(item.getItem() == hnsItems.crystalStaff){
        		Block[] blocks = new Block[6];
        	
        		blocks = BlockUtil.getBlockB(world, x, y, z);
        		for(int i = 0; i < blocks.length; i++){
        			Block b = blocks[i];
        			System.out.println(b.getUnlocalizedName());
        		}
        	}
        	return false;
        }
        return false;
    }

@Override
public TileEntity createNewTileEntity(World world, int i) {
	return null;
}

}

 

The console printout is kind of the same...

 

I am now only ask if it is a good solution for my problem.

 

Ty, hnsdieter

Link to comment
Share on other sites

You still use a integer... Use a foreach loop over

ForgeDirection.VALID_DIRECTIONS

. Don't know what a foreach loop is? Learn Java!

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

You loose the nice abstraction that ForgeDirection gives you

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

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.