Posted October 4, 20159 yr Well some time ago i wanted to improve my 3x3 pickaxe. People said to use MovingObjectPosition and Minecraft.getMinecraft().objectMouseOver I did that and it worked perfectly. Now after some testing to make sure everything is ok i found out a weird glitch happening sometimes when i mine from the side. It's hard to describe it with words so i made this video Here is what happens: https://www.youtube.com/watch?v=UQz2evR9Pck&feature=youtu.be Here is my pickaxe code: package net.aop.items; import java.util.ArrayList; import java.util.List; import net.aop.main.ageofpower; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.client.Minecraft; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.item.Item; import net.minecraft.item.ItemPickaxe; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.MathHelper; import net.minecraft.util.MovingObjectPosition; import net.minecraft.util.Vec3; import net.minecraft.world.World; import net.minecraftforge.common.util.ForgeDirection; public class Ppaxe extends ItemPickaxe{ public Ppaxe() { super(ageofpower.PT2); this.setTextureName("aop:powerpickaxe"); this.setCreativeTab(ageofpower.AgeOfPower); } @Override public boolean onBlockDestroyed(ItemStack itemstack, World world, Block block, int x, int y, int z, EntityLivingBase lb) { MovingObjectPosition ray = Minecraft.getMinecraft().objectMouseOver; if(ray.sideHit == 3){ for(int xpos = x - 1; xpos <= x + 1; xpos++){ for(int ypos = y -1; ypos <= y + 1; ypos++){ if(world.getBlock(xpos, ypos, z).getMaterial() == Material.rock && world.getBlock(xpos, ypos, z) != Blocks.bedrock){ int bmdata = (world.getBlockMetadata(xpos, ypos, z)); int fortune = EnchantmentHelper.getFortuneModifier(lb); boolean silk = EnchantmentHelper.getSilkTouchModifier(lb); if(!silk){ Block dblock = world.getBlock(xpos, ypos, z); dblock.dropBlockAsItemWithChance(world, xpos, ypos, z, bmdata, 1 , fortune); world.setBlockToAir(xpos, ypos, z); }else{ Block dblock = world.getBlock(xpos, ypos, z); List<ItemStack> items = new ArrayList(); if (dblock == Blocks.lit_redstone_ore) items.add(new ItemStack(Item.getItemFromBlock(Blocks.redstone_ore))); else items.add(new ItemStack(dblock)); for(final ItemStack item : items){ if(!world.isRemote){ world.spawnEntityInWorld(new EntityItem(world, xpos, ypos, z, item)); } if(world.getBlock(xpos, ypos, z) != Blocks.air || world.getBlock(xpos, ypos, z) != Blocks.bedrock){ world.setBlockToAir(xpos, ypos, z); } } } } } } }else if(ray.sideHit == 4){ for(int zpos = z - 1; zpos <= z + 1; zpos++){ for(int ypos = y - 1; ypos <= y + 1; ypos++){ if(world.getBlock(x, ypos, zpos).getMaterial() == Material.rock && world.getBlock(x, ypos, zpos) != Blocks.bedrock){ int bmdata = (world.getBlockMetadata(x, ypos, zpos)); int fortune = EnchantmentHelper.getFortuneModifier(lb); boolean silk = EnchantmentHelper.getSilkTouchModifier(lb); if(!silk){ Block dblock = world.getBlock(x, ypos, zpos); dblock.dropBlockAsItemWithChance(world, x, ypos, zpos, bmdata, 1 , fortune); world.setBlockToAir(x, ypos, zpos); }else{ Block dblock = world.getBlock(x, ypos, zpos); List<ItemStack> items = new ArrayList(); if (dblock == Blocks.lit_redstone_ore) items.add(new ItemStack(Item.getItemFromBlock(Blocks.redstone_ore))); else items.add(new ItemStack(dblock)); for(final ItemStack item : items){ if(!world.isRemote){ world.spawnEntityInWorld(new EntityItem(world, x, ypos, zpos, item)); } } if(world.getBlock(x, ypos, zpos) != Blocks.air || world.getBlock(x, ypos, zpos) != Blocks.bedrock){ world.setBlockToAir(x, ypos, zpos); } } } } } }else if(ray.sideHit == 2){ for(int xpos = x - 1; xpos <= x + 1; xpos++){ for(int ypos = y -1; ypos <= y + 1; ypos++){ if(world.getBlock(xpos, ypos, z).getMaterial() == Material.rock && world.getBlock(xpos, ypos, z) != Blocks.bedrock){ int bmdata = (world.getBlockMetadata(xpos, ypos, z)); int fortune = EnchantmentHelper.getFortuneModifier(lb); boolean silk = EnchantmentHelper.getSilkTouchModifier(lb); if(!silk){ Block dblock = world.getBlock(xpos, ypos, z); dblock.dropBlockAsItemWithChance(world, xpos, ypos, z, bmdata, 1 , fortune); world.setBlockToAir(xpos, ypos, z); }else{ Block dblock = world.getBlock(xpos, ypos, z); List<ItemStack> items = new ArrayList(); if (dblock == Blocks.lit_redstone_ore) items.add(new ItemStack(Item.getItemFromBlock(Blocks.redstone_ore))); else items.add(new ItemStack(dblock)); if(!world.isRemote){ for(final ItemStack item : items){ world.spawnEntityInWorld(new EntityItem(world, xpos, ypos, z, item)); } } if(world.getBlock(xpos, ypos, z) != Blocks.air || world.getBlock(xpos, ypos, z) != Blocks.bedrock){ world.setBlockToAir(xpos, ypos, z); } } } } } }else if(ray.sideHit == 5){ for(int zpos = z - 1; zpos <= z + 1; zpos++){ for(int ypos = y - 1; ypos <= y + 1; ypos++){ if(world.getBlock(x, ypos, zpos).getMaterial() == Material.rock && world.getBlock(x, ypos, zpos) != Blocks.bedrock){ int bmdata = (world.getBlockMetadata(x, ypos, zpos)); int fortune = EnchantmentHelper.getFortuneModifier(lb); boolean silk = EnchantmentHelper.getSilkTouchModifier(lb); if(!silk){ Block dblock = world.getBlock(x, ypos, zpos); dblock.dropBlockAsItemWithChance(world, x, ypos, zpos, bmdata, 1 , fortune); world.setBlockToAir(x, ypos, zpos); }else{ List<ItemStack> items = new ArrayList(); Block dblock = world.getBlock(x, ypos, zpos); if (dblock == Blocks.lit_redstone_ore) items.add(new ItemStack(Item.getItemFromBlock(Blocks.redstone_ore))); else items.add(new ItemStack(dblock)); if(!world.isRemote){ for(final ItemStack item : items){ world.spawnEntityInWorld(new EntityItem(world, x, ypos, zpos, item)); } } if(world.getBlock(x, ypos, zpos) != Blocks.air || world.getBlock(x, ypos, zpos) != Blocks.bedrock){ world.setBlockToAir(x, ypos, zpos); } } } } } }else if(ray.sideHit == 1){ for(int zpos = z - 1; zpos <= z + 1; zpos++){ for(int xpos = x - 1; xpos <= x + 1; xpos++){ if(world.getBlock(xpos , y, zpos).getMaterial() == Material.rock && world.getBlock(xpos, y, zpos) != Blocks.bedrock){ int bmdata = (world.getBlockMetadata(xpos, y, zpos)); int fortune = EnchantmentHelper.getFortuneModifier(lb); boolean silk = EnchantmentHelper.getSilkTouchModifier(lb); if(!silk){ Block dblock = world.getBlock(xpos, y, zpos); dblock.dropBlockAsItemWithChance(world, xpos, y, zpos, bmdata, 1 , fortune); world.setBlockToAir(xpos, y, zpos); }else{ List<ItemStack> items = new ArrayList(); Block dblock = world.getBlock(xpos, y, zpos); if (dblock == Blocks.lit_redstone_ore) items.add(new ItemStack(Item.getItemFromBlock(Blocks.redstone_ore))); else items.add(new ItemStack(dblock)); if(!world.isRemote){ for(final ItemStack item : items){ world.spawnEntityInWorld(new EntityItem(world, xpos, y, zpos, item)); } } if(world.getBlock(xpos, y, zpos) != Blocks.air || world.getBlock(xpos, y, zpos) != Blocks.bedrock){ world.setBlockToAir(xpos, y, zpos); } } } } } }else if(ray.sideHit == 0){ for(int zpos = z - 1; zpos <= z + 1; zpos++){ for(int xpos = x - 1; xpos <= x + 1; xpos++){ if(world.getBlock(xpos , y, zpos).getMaterial() == Material.rock && world.getBlock(xpos, y, zpos) != Blocks.bedrock){ int bmdata = (world.getBlockMetadata(xpos, y, zpos)); int fortune = EnchantmentHelper.getFortuneModifier(lb); boolean silk = EnchantmentHelper.getSilkTouchModifier(lb); if(!silk){ Block dblock = world.getBlock(xpos, y, zpos); dblock.dropBlockAsItemWithChance(world, xpos, y, zpos, bmdata, 1 , fortune); world.setBlockToAir(xpos, y, zpos); }else{ List<ItemStack> items = new ArrayList(); Block dblock = world.getBlock(xpos, y, zpos); if (dblock == Blocks.lit_redstone_ore) items.add(new ItemStack(Item.getItemFromBlock(Blocks.redstone_ore))); else items.add(new ItemStack(dblock)); if(!world.isRemote){ for(final ItemStack item : items){ world.spawnEntityInWorld(new EntityItem(world, xpos, y, zpos, item)); } } if(world.getBlock(xpos, y, zpos) != Blocks.air || world.getBlock(xpos, y, zpos) != Blocks.bedrock){ world.setBlockToAir(xpos, y, zpos); } } } } } }else if(ray.sideHit == -1){ } return super.onBlockDestroyed(itemstack, world, block, x, y, z, lb); } } Here is my shovel code: (They are very similiar) package net.aop.items; import java.util.ArrayList; import java.util.List; import net.aop.main.ageofpower; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.client.Minecraft; 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.Item; import net.minecraft.item.ItemSpade; import net.minecraft.item.ItemStack; import net.minecraft.util.MovingObjectPosition; import net.minecraft.world.World; public class Pshovel extends ItemSpade{ public Pshovel() { super(ageofpower.PT2); this.setTextureName("aop:powershovel"); this.setCreativeTab(ageofpower.AgeOfPower); } @Override public boolean onBlockDestroyed(ItemStack itemstack, World world, Block block, int x, int y, int z, EntityLivingBase lb) { destroyblocks(x, y, z, world); return super.onBlockDestroyed(itemstack, world, block, x, y, z, lb); } public void destroyblocks(int x , int y , int z, World world){ MovingObjectPosition ray = Minecraft.getMinecraft().objectMouseOver; if(ray.sideHit == 0){ for(int zpos = z - 1; zpos <= z + 1; zpos++){ for(int xpos = x - 1; xpos <= x + 1; xpos++){ List<Material> mat = new ArrayList(); mat.add(Material.ground); mat.add(Material.sand); mat.add(Material.gourd); mat.add(Material.snow); mat.add(Material.grass); for(final Material mat1 : mat){ if(world.getBlock(xpos, y, zpos).getMaterial() == mat1){ Block dblock = world.getBlock(xpos, y, zpos); int bmdata = (world.getBlockMetadata(xpos, y, zpos)); dblock.dropBlockAsItem(world, xpos, y, zpos, bmdata, 1); world.setBlockToAir(xpos, y, zpos); } } } } }else if(ray.sideHit == 1){ for(int zpos = z - 1; zpos <= z + 1; zpos++){ for(int xpos = x - 1; xpos <= x + 1; xpos++){ List<Material> mat = new ArrayList(); mat.add(Material.ground); mat.add(Material.sand); mat.add(Material.gourd); mat.add(Material.snow); mat.add(Material.grass); for(final Material mat1 : mat){ if(world.getBlock(xpos, y, zpos).getMaterial() == mat1){ Block dblock = world.getBlock(xpos, y, zpos); int bmdata = (world.getBlockMetadata(xpos, y, zpos)); dblock.dropBlockAsItem(world, xpos, y, zpos, bmdata, 1); world.setBlockToAir(xpos, y, zpos); } } } } }else if(ray.sideHit == 2){ for(int xpos = x - 1; xpos <= x + 1; xpos++){ for(int ypos = y - 1; ypos <= y + 1; ypos++){ List<Material> mat = new ArrayList(); mat.add(Material.ground); mat.add(Material.sand); mat.add(Material.gourd); mat.add(Material.snow); mat.add(Material.grass); for(final Material mat1 : mat){ if(world.getBlock(xpos, ypos, z).getMaterial() == mat1){ Block dblock = world.getBlock(xpos, ypos, z); int bmdata = (world.getBlockMetadata(xpos, ypos, z)); dblock.dropBlockAsItem(world, xpos, ypos, z, bmdata, 1); world.setBlockToAir(xpos, ypos, z); } } } } }else if(ray.sideHit == 3){ for(int xpos = x - 1; xpos <= x + 1; xpos++){ for(int ypos = y - 1; ypos <= y + 1; ypos++){ List<Material> mat = new ArrayList(); mat.add(Material.ground); mat.add(Material.sand); mat.add(Material.gourd); mat.add(Material.snow); mat.add(Material.grass); for(final Material mat1 : mat){ if(world.getBlock(xpos, ypos, z).getMaterial() == mat1){ Block dblock = world.getBlock(xpos, ypos, z); int bmdata = (world.getBlockMetadata(xpos, ypos, z)); dblock.dropBlockAsItem(world, xpos, ypos, z, bmdata, 1); world.setBlockToAir(xpos, ypos, z); } } } } }else if(ray.sideHit == 4){ for(int zpos = z - 1; zpos <= z + 1; zpos++){ for(int ypos = y - 1; ypos <= y + 1; ypos++){ List<Material> mat = new ArrayList(); mat.add(Material.ground); mat.add(Material.sand); mat.add(Material.gourd); mat.add(Material.snow); mat.add(Material.grass); for(final Material mat1 : mat){ if(world.getBlock(x, ypos, zpos).getMaterial() == mat1){ Block dblock = world.getBlock(x, ypos, zpos); int bmdata = (world.getBlockMetadata(x, ypos, zpos)); dblock.dropBlockAsItem(world, x, ypos, zpos, bmdata, 1); world.setBlockToAir(x, ypos, zpos); } } } } }else if(ray.sideHit == 5){ for(int zpos = z - 1; zpos <= z + 1; zpos++){ for(int ypos = y - 1; ypos <= y + 1; ypos++){ List<Material> mat = new ArrayList(); mat.add(Material.ground); mat.add(Material.sand); mat.add(Material.gourd); mat.add(Material.snow); mat.add(Material.grass); for(final Material mat1 : mat){ if(world.getBlock(x, ypos, zpos).getMaterial() == mat1){ Block dblock = world.getBlock(x, ypos, zpos); int bmdata = (world.getBlockMetadata(x, ypos, zpos)); dblock.dropBlockAsItem(world, x, ypos, zpos, bmdata, 1); world.setBlockToAir(x, ypos, zpos); } } } } } } } I am very confused because i don't know what to do. One of my thoughts is that the ray is going through and it doesn't know in which direction it should mine so it does that weird thing. However i am not sure if i can fix it.
October 4, 20159 yr Author Minecraft.getMinecraft() is client side only. You cannot use that on the server. What do you recommend using for the MovingObjectPosition value?
October 4, 20159 yr Author Yes i know that the copy-paste thing would slow down minecraft a lot but that was the first thing that came in my mind. As for the others, i am going to give it a try and post the results.
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.