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

Hey, ive a question: Im building a Thaumcraft-Addon and Im creating a Wand-Of-Excavation item. It should harvest a block when right-clicking... But it only breaks the block with a destroy-animation, the block don't disappear and still exist... How can I fix it?

 

Heres my source:

 

package de.xthelegend.tcwands;

import ibxm.Player;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import thaumcraft.common.Thaumcraft;
import thaumcraft.common.entities.projectile.EntityExplosiveOrb;
import thaumcraft.common.items.wands.WandManager;
import thaumcraft.common.lib.utils.BlockUtils;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemFishingRod;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraft.world.WorldSettings;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.world.BlockEvent;

public class wand_of_excavation extends Item{
public wand_of_excavation()
{

	super();
	setUnlocalizedName(Reference.MODID + ":" + "wandexcavation");
	setTextureName(Reference.MODID + ":" + "wandexcavation");
	setMaxDamage(2500);
	canRepair = false;
	this.bFull3D = true;
	setCreativeTab(MainClass.tabWands);
}

@SideOnly(Side.CLIENT)
    public boolean isFull3D()
    {
        return true;
    }

    @SideOnly(Side.CLIENT)
    public boolean shouldRotateAroundWhenRendering()
    {
        return true;
    }
    
    @Override
    public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
    {
    	player.setItemInUse(stack, this.getMaxDamage());
    	return stack;
    }
    
    static HashMap<String, Long> soundDelay = new HashMap();
    static HashMap<String, Object> beam = new HashMap();
    static HashMap<String, Float> breakcount = new HashMap();
    static HashMap<String, Integer> lastX = new HashMap();
    static HashMap<String, Integer> lastY = new HashMap();
    static HashMap<String, Integer> lastZ = new HashMap();
    
    @Override
    public void onUsingTick(ItemStack stack, EntityPlayer p, int count) {
        
        String pp = "R" + p.getCommandSenderName();
        if (!p.worldObj.isRemote) {
            pp = "S" + p.getCommandSenderName();
        }
        if (soundDelay.get(pp) == null) {
            soundDelay.put(pp, 0L);
        }
        if (breakcount.get(pp) == null) {
            breakcount.put(pp, Float.valueOf(0.0f));
        }
        if (lastX.get(pp) == null) {
            lastX.put(pp, 0);
        }
        if (lastY.get(pp) == null) {
            lastY.put(pp, 0);
        }
        if (lastZ.get(pp) == null) {
            lastZ.put(pp, 0);
        }
        MovingObjectPosition mop = BlockUtils.getTargetBlock(p.worldObj, (Entity)p, false);
        Vec3 v = p.getLookVec();
        double tx = p.posX + v.xCoord * 10.0;
        double ty = p.posY + v.yCoord * 10.0;
        double tz = p.posZ + v.zCoord * 10.0;
        int impact = 0;
        if (mop != null) {
            tx = mop.hitVec.xCoord;
            ty = mop.hitVec.yCoord;
            tz = mop.hitVec.zCoord;
            impact = 5;
            if (!(p.worldObj.isRemote || soundDelay.get(pp) >= System.currentTimeMillis())) {
                p.worldObj.playSoundEffect(tx, ty, tz, "thaumcraft:rumble", 0.3f, 1.0f);
                soundDelay.put(pp, System.currentTimeMillis() + 1200);
            }
        } else {
            soundDelay.put(pp, 0L);
        }
        if (p.worldObj.isRemote) {
            beam.put(pp, Thaumcraft.proxy.beamCont(p.worldObj, p, tx, ty, tz, 2, 65382, false, impact > 0 ? 2.0f : 0.0f, beam.get(pp), impact));
        }
        if (mop != null && mop.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK && p.worldObj.canMineBlock(p, mop.blockX, mop.blockY, mop.blockZ)) {
            Block bi = p.worldObj.getBlock(mop.blockX, mop.blockY, mop.blockZ);
            int md = p.worldObj.getBlockMetadata(mop.blockX, mop.blockY, mop.blockZ);
            float hardness = bi.getBlockHardness(p.worldObj, mop.blockX, mop.blockY, mop.blockZ);
            if (hardness >= 0.0f) {
                int pot = 1;
                float speed = 0.05f + (float)pot * 0.1f;
                if (bi.getMaterial() == Material.rock || bi.getMaterial() == Material.grass || bi.getMaterial() == Material.ground || bi.getMaterial() == Material.sand) {
                    speed = 0.25f + (float)pot * 0.25f;
                }
                if (bi == Blocks.obsidian) {
                    speed*=3.0f;
                }
                if (lastX.get(pp) == mop.blockX && lastY.get(pp) == mop.blockY && lastZ.get(pp) == mop.blockZ) {
                    float bc = breakcount.get(pp).floatValue();
                    if (p.worldObj.isRemote && bc > 0.0f && bi != Blocks.air) {
                        int progress = (int)(bc / hardness * 9.0f);
                        Thaumcraft.proxy.excavateFX(mop.blockX, mop.blockY, mop.blockZ, p, Block.getIdFromBlock((Block)bi), md, progress);
                    }
                    if (p.worldObj.isRemote) {
                        if (bc >= hardness) {
                            breakcount.put(pp, Float.valueOf(0.0f));
                        } else {
                            breakcount.put(pp, Float.valueOf(bc + speed));
                        }
                    } else if (bc >= hardness ) {
                        if (this.excavate(p.worldObj, stack, p, bi, md, mop.blockX, mop.blockY, mop.blockZ)) 
                        {
                        	stack.damageItem(200,p);
                        }
                        lastX.put(pp, Integer.MAX_VALUE);
                        lastY.put(pp, Integer.MAX_VALUE);
                        lastZ.put(pp, Integer.MAX_VALUE);
                        breakcount.put(pp, Float.valueOf(0.0f));
                    } else {
                        breakcount.put(pp, Float.valueOf(bc + speed));
                    }
                } else {
                    lastX.put(pp, mop.blockX);
                    lastY.put(pp, mop.blockY);
                    lastZ.put(pp, mop.blockZ);
                    breakcount.put(pp, Float.valueOf(0.0f));
                }
            }
        } else {
            lastX.put(pp, Integer.MAX_VALUE);
            lastY.put(pp, Integer.MAX_VALUE);
            lastZ.put(pp, Integer.MAX_VALUE);
            breakcount.put(pp, Float.valueOf(0.0f));
        }
    }
    
    private boolean excavate(World world, ItemStack stack, EntityPlayer player, Block block, int md, int x, int y, int z) {
        BlockEvent.BreakEvent event;
        WorldSettings.GameType gt = WorldSettings.GameType.SURVIVAL;
        if (player.capabilities.allowEdit) {
            if (player.capabilities.isCreativeMode) {
                gt = WorldSettings.GameType.CREATIVE;
            }
        } else {
            gt = WorldSettings.GameType.ADVENTURE;
        }
        if (!(event = ForgeHooks.onBlockBreakEvent((World)world, (WorldSettings.GameType)gt, (EntityPlayerMP)((EntityPlayerMP)player), (int)x, (int)y, (int)z)).isCanceled()) {
            int fortune = 0;
            boolean silk =false;
            if (silk && block.canSilkHarvest(player.worldObj, player, x, y, z, md)) {
                ArrayList<ItemStack> items = new ArrayList<ItemStack>();
                ItemStack itemstack = BlockUtils.createStackedBlock(block, md);
                if (itemstack != null) {
                    items.add(itemstack);
                }
                ForgeEventFactory.fireBlockHarvesting(items, (World)world, (Block)block, (int)x, (int)y, (int)z, (int)md, (int)0, (float)1.0f, (boolean)true, (EntityPlayer)player);
                for (ItemStack is : items) {
                    BlockUtils.dropBlockAsItem(world, x, y, z, is, block);
                }
            } else {
                BlockUtils.dropBlockAsItemWithChance(world, block, x, y, z, md, 1.0f, fortune, player);
                block.dropXpOnBlockBreak(world, x, y, z, block.getExpDrop((IBlockAccess)world, md, fortune));
            }
            world.setBlockToAir(x, y, z);
            world.playAuxSFX(2001, x, y, z, Block.getIdFromBlock((Block)block) + (md << 12));
            return true;
        }
        return false;
    }
    
    boolean breakNeighbour(EntityPlayer p, int x, int y, int z, Block block, int md, ItemStack stack) {
        List<ForgeDirection> directions = Arrays.asList(new ForgeDirection[]{ForgeDirection.DOWN, ForgeDirection.UP, ForgeDirection.NORTH, ForgeDirection.SOUTH, ForgeDirection.EAST, ForgeDirection.WEST});
        Collections.shuffle(directions, p.worldObj.rand);
        for (ForgeDirection dir : directions) {
            if (p.worldObj.getBlock(x + dir.offsetX, y + dir.offsetY, z + dir.offsetZ) != block || p.worldObj.getBlockMetadata(x + dir.offsetX, y + dir.offsetY, z + dir.offsetZ) != md || !this.excavate(p.worldObj, stack, p, block, md, x + dir.offsetX, y + dir.offsetY, z + dir.offsetZ)) continue;
            return true;
        }
        return false;
    }
    
    @Override
    public void onPlayerStoppedUsing(ItemStack stack, World world, EntityPlayer p, int count) {
        String pp = "R" + p.getCommandSenderName();
        if (!p.worldObj.isRemote) {
            pp = "S" + p.getCommandSenderName();
        }
        if (soundDelay.get(pp) == null) {
            soundDelay.put(pp, 0L);
        }
        if (breakcount.get(pp) == null) {
            breakcount.put(pp, Float.valueOf(0.0f));
        }
        if (lastX.get(pp) == null) {
            lastX.put(pp, 0);
        }
        if (lastY.get(pp) == null) {
            lastY.put(pp, 0);
        }
        if (lastZ.get(pp) == null) {
            lastZ.put(pp, 0);
        }
        beam.put(pp, null);
        lastX.put(pp, Integer.MAX_VALUE);
        lastY.put(pp, Integer.MAX_VALUE);
        lastZ.put(pp, Integer.MAX_VALUE);
        breakcount.put(pp, Float.valueOf(0.0f));
    }
    
    @Override
    public void onUpdate(ItemStack stack, World world, Entity entity, int p_77663_4_, boolean p_77663_5_) {
        if (stack.stackTagCompound != null && stack.stackTagCompound.hasKey("ench")) {
            stack.stackTagCompound.removeTag("ench");
        }
        super.onUpdate(stack, world, entity, p_77663_4_, p_77663_5_);
    }
    
    
}

i recomend adding a debug line to excavate method

 

System.out.println("x="+x+" y="+y+" z="+z);

 

also change world.setBlockToAir(x, y, z); to set it to something else , to verify that x,y,z arnt pointing to a block that is already air

 

might help find the problem

 

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.