Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

So i am starting a new mod. And i just started working with an axe that will cut the whole tree. I didn't write to much code and i only have made it so it destroys blocks of the same type in the vertical position. However i am getting a problem. I defined the "block" as Blocks.log and because of that, the block.DropBlockAsItem drops always as Oak Logs. Is there a way to define all wood material blocks in 1.7.10?

This is the onBlockDestroyed part of the code:

 

	@Override
public boolean onBlockDestroyed(ItemStack itemstack, World world, Block block, int x, int y, int z, EntityLivingBase entity) {

	block = Blocks.log;


			for(int ypos = y; ypos < ypos+1; ypos++)
			{

				if(world.getBlock(x, ypos+1, z) == block)
				{
					world.setBlock(x, ypos+1, z, Blocks.air);
					block.dropBlockAsItem(world, x, ypos, z, 0, 0);
				}

				else{
					return super.onBlockDestroyed(itemstack, world, block, x, y, z, entity);
				}


			}


	return super.onBlockDestroyed(itemstack, world, block, x, y, z, entity);
}

}

 

Any help would be appreciated

Use Block#getMaterial to get a Block's Material or use Block#isWood to check if a Block is wood.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

  • Author

Thank you both for the tips.

However changing the code doesn't seem to work.

Only the block that was mined with the axe (and not by code) dropped Birch, the rest was all dropped as Oak Logs.

Right now, i don't get it why this is happening. I thought it was because i set the block as log but apparently no. :/

 

This is my new code:

 

@Override
public boolean onBlockDestroyed(ItemStack itemstack, World world, Block block, int x, int y, int z, EntityLivingBase entity) {


			for(int ypos = y; ypos < ypos+1; ypos++)
			{

				if(world.getBlock(x, ypos+1, z).isWood(world, x, ypos+1, z))
				{
					block.dropBlockAsItem(world, x, ypos, z, 0, 0);
					world.setBlock(x, ypos+1, z, Blocks.air);
				}

				else{
					return super.onBlockDestroyed(itemstack, world, block, x, y, z, entity);
				}


			}


	return super.onBlockDestroyed(itemstack, world, block, x, y, z, entity);
}

  • Author

Allright, i will try that within 2 hours when i get home, i will be pleased if you dont close the topic during that time

I created an axe to do that

 

its not perfect, had major problems with hugemushroom blocks , here is the code for it

 

package me.el.LoonTools;

import net.minecraft.block.Block;
import net.minecraft.block.BlockAir;
import net.minecraft.block.BlockCocoa;
import net.minecraft.block.BlockDirt;
import net.minecraft.block.BlockDoublePlant;
import net.minecraft.block.BlockFlower;
import net.minecraft.block.BlockGrass;
import net.minecraft.block.BlockHugeMushroom;
import net.minecraft.block.BlockLeavesBase;
import net.minecraft.block.BlockLog;
import net.minecraft.block.BlockMushroom;
import net.minecraft.block.BlockSand;
import net.minecraft.block.BlockSnow;
import net.minecraft.block.BlockSnowBlock;
import net.minecraft.block.BlockTallGrass;
import net.minecraft.block.BlockVine;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemAxe;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class ItemLumberAxe extends ItemAxe{
public String mat;

public ItemLumberAxe(ToolMaterial material, String materialName) {
	super(material);
	mat=materialName;
	setCreativeTab(CreativeTabs.tabTools);
	setUnlocalizedName("lumberaxe"+mat);
	setTextureName(LoonTools.modid+":lumberaxe_"+mat);
}

//   EXPLODE TREE
byte[] tre= new byte[32000];
byte unchecked=0;
byte needcheck=1;
byte ignore   =2;
byte harvest  =3;

private boolean setcheck(int x, int y, int z) {
	if(x<0 || x>19 || z<0 || z>19 || y<0 || y>79) return false;
	int o=x+z*20+y*400;
	if (tre[o]==unchecked) tre[o]=needcheck;
	return true;
}

public boolean canIgnore(Block bit){
	if (bit instanceof BlockAir)return true;
	if (bit instanceof BlockGrass)return true;
	if (bit instanceof BlockSand)return true;
	if (bit instanceof BlockDirt)return true;
	if (bit instanceof BlockCocoa)return true;
	if (bit instanceof BlockVine)return true;
	if (bit instanceof BlockMushroom)return true;
	if (bit instanceof BlockSnow)return true;
	if (bit instanceof BlockSnowBlock)return true;
	if (bit instanceof BlockFlower)return true;
	if (bit instanceof BlockTallGrass)return true;
	if (bit instanceof BlockDoublePlant)return true;

	//LoonTools.log("Found uncuttable "+bit.getClass().getSimpleName());
	return false;
}

private int check(World par1World, int x, int y, int z, int xo, int yo,int zo) {
	int f=0;
	int o=x+z*20+y*400;
	if (tre[o]==needcheck){
		tre[o]=ignore;
		Block bit = par1World.getBlock(x+xo, y+yo, z+zo);
		if ((bit instanceof BlockLog)||(bit instanceof BlockLeavesBase)||(bit instanceof BlockHugeMushroom)||(bit instanceof HugeMushroomBlock)){
			f=1;
			tre[o]=harvest;
			//if (bit instanceof BlockLog){
			//	LoonTools.log("^ Found log @ "+x+xo+" "+y+yo+" "+z+zo+" ");
			//}
			for(int xb=-1;xb<2;xb++)
				for(int yb=-1;yb<2;yb++)
					for(int zb=-1;zb<2;zb++)
						if (!setcheck(x+xb,y+yb,z+zb))return 3;
		}else{
			if (!canIgnore(bit)) return 2;
		}
	}
	return f;
}

public int checkTree(World par1World,int xo,int yo,int zo){
	boolean f;
	for (f=true;f==true;){
		f=false;
		for (int y=0;y<80;y++)
			for(int z=0;z<20;z++)
				for(int x=0;x<20;x++){
					int r=check(par1World,x,y,z,xo,yo,zo);
					if (r==3) return 3;
					if (r==2) return 2;
					if (r==1) f=true;
				}

		for (int y=79;y>=0;y--)
			for(int z=19;z>=0;z--)
				for(int x=19;x>=0;x--){
					int r=check(par1World,x,y,z,xo,yo,zo);
					if (r==2) return 3;
					if (r==2) return 2;
					if (r==1) f=true;
				}
	}
	return 1;
}

private int check2(World par1World, int x, int y, int z, int xo, int yo,int zo) {
	int f=0;
	int o=x+z*20+y*400;
	if (tre[o]==needcheck){
		tre[o]=ignore;
		Block bit = par1World.getBlock(x+xo, y+yo, z+zo);
		if (bit instanceof BlockLog){
			f=1;
			tre[o]=harvest;
			//if (bit instanceof BlockLog){
			//	LoonTools.log("^ Found log @ "+x+xo+" "+y+yo+" "+z+zo+" ");
			//}
			for(int xb=-1;xb<2;xb++)
				for(int yb=-1;yb<2;yb++)
					for(int zb=-1;zb<2;zb++)
						if (!setcheck(x+xb,y+yb,z+zb))return 3;
		}else if (bit instanceof BlockLeavesBase){
		}else{
			if (!canIgnore(bit)) return 2;
		}
	}
	return f;
}

public int checkTree2(World par1World,int xo,int yo,int zo){
	boolean f;
	for (f=true;f==true;){
		f=false;
		for (int y=0;y<80;y++)
			for(int z=0;z<20;z++)
				for(int x=0;x<20;x++){
					int r=check2(par1World,x,y,z,xo,yo,zo);
					if (r==3) return 3;
					if (r==2) return 2;
					if (r==1) f=true;
				}

		for (int y=79;y>=0;y--)
			for(int z=19;z>=0;z--)
				for(int x=19;x>=0;x--){
					int r=check2(par1World,x,y,z,xo,yo,zo);
					if (r==2) return 3;
					if (r==2) return 2;
					if (r==1) f=true;
				}
	}
	return 1;
}

public void exploadTree(World par1World,int xo,int yo,int zo, EntityPlayer plr){
	for (int y=0;y<80;y++)
		for(int z=0;z<20;z++)
			for(int x=0;x<20;x++){
				int o=x+z*20+y*400;
				if (tre[o]==harvest){
					Block bit = par1World.getBlock(x+xo, y+yo, z+zo);
					int met = par1World.getBlockMetadata(x+xo, y+yo, z+zo);

					if ((bit instanceof BlockLog)||(bit instanceof BlockLeavesBase)){
						bit.harvestBlock(par1World, plr, x+xo, y+yo, z+zo,met);
						par1World.setBlockToAir(x+xo, y+yo, z+zo);
					}
				}
			}
}

private void breakMushroom(World wld, Block bit, EntityPlayer plr, boolean silk, int x, int y, int z, int met) {
	if (silk){
		ItemStack stk;
		if (bit==Blocks.brown_mushroom_block) stk = new ItemStack(LoonToolItems.brown_mushroom_block,1,met);
		else if (bit==Blocks.red_mushroom_block) stk = new ItemStack(LoonToolItems.red_mushroom_block,1,met);
		else stk = new ItemStack(bit,1,met);
		EntityItem entityitem = new EntityItem(wld, x+0.5, y+0.5, z+0.5, stk);
		entityitem.delayBeforeCanPickup = 10;
		wld.spawnEntityInWorld(entityitem);
	}else{
		bit.harvestBlock(wld, plr, x, y, z, met);
	}
	wld.setBlockToAir(x, y, z);
}

public void exploadMushroom(World par1World,int xo,int yo,int zo, EntityPlayer plr, boolean silk){
	for (int y=0;y<80;y++)
		for(int z=0;z<20;z++)
			for(int x=0;x<20;x++){
				int o=x+z*20+y*400;
				if (tre[o]==harvest){
					Block bit = par1World.getBlock(x+xo, y+yo, z+zo);
					int met = par1World.getBlockMetadata(x+xo, y+yo, z+zo);
					if (bit instanceof BlockHugeMushroom){
						breakMushroom(par1World, bit, plr, silk, x+xo, y+yo, z+zo,met);
					}else{
						bit.harvestBlock(par1World, plr, x+xo, y+yo, z+zo,met);
						par1World.setBlockToAir(x+xo, y+yo, z+zo);
					}
				}
			}
}


@Override
public boolean onBlockDestroyed(ItemStack itm, World wld,Block blk, int x, int y,int z, EntityLivingBase plr) {
	if (!wld.isRemote){
		Block bit = wld.getBlock(x, y, z);
		boolean silk=EnchantmentHelper.getSilkTouchModifier(plr);
		if ((bit instanceof BlockHugeMushroom) || (bit instanceof HugeMushroomBlock)){
			for (int n=0;n<32000;n++) tre[n]=unchecked;
			int met = wld.getBlockMetadata(x, y, z);
			breakMushroom(wld, bit, (EntityPlayer) plr, silk, x, y, z,met);
			wld.setBlockToAir(x,y,z);
			tre[2210]=needcheck;
			if (checkTree(wld,x-10,y-4,z-10)==1){
				exploadMushroom(wld,x-10,y-4,z-10,(EntityPlayer) plr,silk);
			}
		}

		if (bit instanceof BlockLog){
			//LoonTools.log("cutting tree @ "+x+" "+y+" "+z+" ");
			for (int n=0;n<32000;n++) tre[n]=unchecked;
			int met = wld.getBlockMetadata(x, y, z);
			bit.harvestBlock(wld, (EntityPlayer) plr, x, y, z,met);
			wld.setBlockToAir(x,y,z);
			tre[2210]=needcheck;
			if (checkTree(wld,x-10,y-4,z-10)==1){
				exploadTree(wld,x-10,y-4,z-10,(EntityPlayer) plr);
			}else{
				for (int n=0;n<32000;n++) tre[n]=unchecked;
				tre[2210]=needcheck;
				if (checkTree2(wld,x-10,y-4,z-10)==1){
					exploadTree(wld,x-10,y-4,z-10,(EntityPlayer) plr);
				}
			}
		}
	}
	return super.onBlockDestroyed(itm, wld, blk, x, y, z, plr);
}
}

 

  • Author

Thank you for that but i managed to make a much simplier code. But that may be helpfull for something else in my mod.

Instead of checking by ignorin blocks i chechked if the block in top of what i mined is a tree

 

Thank you for that but i managed to make a much simplier code. But that may be helpfull for something else in my mod.

Instead of checking by ignorin blocks i chechked if the block in top of what i mined is a tree

 

that is not going to work well for those larger oak trees with branches, let alone for dark oak (2x2 + irregular log parts on sides) or jungle trees. or acacia. basically the solution is birch/spruce only.

 

what others have done (there are a few existing mods that make axes do what you want) is do a backtracking search up and sideways to find all logs that comprise a tree. reason one is that that way they break all those logs for a complete effect. reason two is that they are then able to damage the axe for a number of logs broken instead of just one.

 

but that is actually a simple task (even though the branch-logs sometimes don't touch the base (vertical) logs; you can allow for one leaf block between logs). the hard task would be taking down the leaves. because if you write a bad algorithm, you could clean up entire forest with one swing of that axe.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.