Advertisement
checconio84

MultiToolTest

Sep 13th, 2015
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.27 KB | None | 0 0
  1. package checconio.test.tuto.Items;
  2.  
  3. import java.util.Set;
  4.  
  5. import net.minecraft.block.Block;
  6. import net.minecraft.block.BlockDirt;
  7. import net.minecraft.block.state.IBlockState;
  8. import net.minecraft.entity.player.EntityPlayer;
  9. import net.minecraft.init.Blocks;
  10. import net.minecraft.item.ItemPickaxe;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.util.BlockPos;
  13. import net.minecraft.util.EnumFacing;
  14. import net.minecraft.world.World;
  15.  
  16. import com.google.common.collect.ImmutableSet;
  17. import com.google.common.collect.Sets;
  18.  
  19. public class MultiToolTest extends ItemPickaxe
  20. {
  21.  
  22.     private static Set<Block> effectiveAgainst = Sets.newHashSet(new Block[]
  23.             {
  24.             Blocks.grass, Blocks.dirt, Blocks.sand, Blocks.gravel, Blocks.snow_layer, Blocks.snow, Blocks.clay, Blocks.farmland, Blocks.soul_sand, Blocks.mycelium, Blocks.crafting_table
  25.             });
  26.  
  27.     public MultiToolTest(ToolMaterial material)
  28.     {
  29.         super(material);
  30.     }
  31.  
  32.     @Override
  33.     public Set<String> getToolClasses(ItemStack stack)
  34.     {
  35.         return ImmutableSet.of("pickaxe", "spade", "axe");
  36.     }
  37.  
  38.     @Override
  39.     public boolean canHarvestBlock(Block block)
  40.     {
  41.         return effectiveAgainst.contains(block) ? true : super.canHarvestBlock(block);
  42.     }
  43.  
  44.     @Override
  45.     public float getStrVsBlock(ItemStack stack, Block block)
  46.     {
  47.         return effectiveAgainst.contains(block) ? this.efficiencyOnProperMaterial : super.getStrVsBlock(stack, block);
  48.     }
  49.  
  50.     @Override
  51.     public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)
  52.     {
  53.         if (!player.canPlayerEdit(pos.offset(side), side, stack))
  54.         {
  55.             return false;
  56.         }
  57.        
  58.         else
  59.         {
  60.             int hook = net.minecraftforge.event.ForgeEventFactory.onHoeUse(stack, player, world, pos);
  61.             if (hook != 0)
  62.                 return hook > 0;
  63.  
  64.             IBlockState iblockstate = world.getBlockState(pos);
  65.             Block block = iblockstate.getBlock();
  66.  
  67.             if (side != EnumFacing.DOWN && world.isAirBlock(pos.up()))
  68.             {
  69.                 if (block == Blocks.grass)
  70.                 {
  71.                     return this.useHoe(stack, player, world, pos, Blocks.farmland.getDefaultState());
  72.                 }
  73.  
  74.                 if (block == Blocks.dirt)
  75.                 {
  76.                     switch (SwitchDirtType.TYPE_LOOKUP[((BlockDirt.DirtType) iblockstate.getValue(BlockDirt.VARIANT)).ordinal()])
  77.                     {
  78.                     case 1:
  79.                         return this.useHoe(stack, player, world, pos, Blocks.farmland.getDefaultState());
  80.                     case 2:
  81.                         return this.useHoe(stack, player, world, pos, Blocks.dirt.getDefaultState().withProperty(BlockDirt.VARIANT, BlockDirt.DirtType.DIRT));
  82.                     }
  83.                 }
  84.             }
  85.  
  86.             return false;
  87.         }
  88.     }
  89.  
  90.     protected boolean useHoe(ItemStack stack, EntityPlayer player, World worldIn, BlockPos target, IBlockState newState)
  91.     {
  92.         worldIn.playSoundEffect(target.getX() + 0.5F, target.getY() + 0.5F, target.getZ() + 0.5F, newState.getBlock().stepSound.getStepSound(), (newState.getBlock().stepSound.getVolume() + 1.0F) / 2.0F, newState.getBlock().stepSound.getFrequency() * 0.8F);
  93.  
  94.         if (worldIn.isRemote)
  95.         {
  96.             return true;
  97.         } else {
  98.             worldIn.setBlockState(target, newState);
  99.             stack.damageItem(1, player);
  100.             return true;
  101.         }
  102.     }
  103.  
  104.     static final class SwitchDirtType
  105.     {
  106.  
  107.         static final int[] TYPE_LOOKUP = new int[BlockDirt.DirtType.values().length];
  108.         private static final String __OBFID = "CL_00002179";
  109.  
  110.         static
  111.         {
  112.             try
  113.             {
  114.                 TYPE_LOOKUP[BlockDirt.DirtType.DIRT.ordinal()] = 1;
  115.             }
  116.            
  117.             catch (NoSuchFieldError var2)
  118.             {
  119.                 ;
  120.             }
  121.  
  122.             try
  123.             {
  124.                 TYPE_LOOKUP[BlockDirt.DirtType.COARSE_DIRT.ordinal()] = 2;
  125.             }
  126.            
  127.             catch (NoSuchFieldError var1)
  128.             {
  129.                 ;
  130.             }
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement