bspkrs

BlockTree.java

Sep 27th, 2012
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.55 KB | None | 0 0
  1. package bspkrs.treecapitator;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import net.minecraft.src.Block;
  6. import net.minecraft.src.BlockLeavesBase;
  7. import net.minecraft.src.BlockLog;
  8. import net.minecraft.src.BlockVine;
  9. import net.minecraft.src.EntityPlayer;
  10. import net.minecraft.src.ItemStack;
  11. import net.minecraft.src.World;
  12. import bspkrs.util.*;
  13.  
  14. public class BlockTree extends BlockLog
  15. {
  16.     private EntityPlayer player;
  17.     private boolean shouldFell;
  18.     private Coord startPos;
  19.     private float axeDamage;
  20.     private int currentAxeDamage;
  21.     private ItemStack axe;
  22.     private float shearDamage;
  23.     private int currentShearDamage;
  24.     private ItemStack shears;
  25.  
  26.     public BlockTree(int i)
  27.     {
  28.         super(i);
  29.         setHardness(TreeCapitator.logHardnessNormal);
  30.         setStepSound(Block.soundWoodFootstep);
  31.         setBlockName("log");
  32.         setRequiresSelfNotify();
  33.         shouldFell = false;
  34.     }
  35.  
  36.     /**
  37.      * Called when the block is clicked by a player. Args: x, y, z, entityPlayer
  38.      */
  39.     @Override
  40.     public void onBlockClicked(World world, int x, int y, int z, EntityPlayer entityPlayer)
  41.     {
  42.         player = entityPlayer;
  43.         shouldFell = false;
  44.        
  45.         if ((!TreeCapitator.needItem || isAxeItemEquipped(entityPlayer)) && !world.isRemote)
  46.         {
  47.             blockHardness = TreeCapitator.logHardnessModified;
  48.            
  49.             if(axe != null)
  50.                 currentAxeDamage = axe.getMaxDamage() + 1 - axe.getItemDamage();
  51.             else
  52.                 currentAxeDamage = 0;
  53.            
  54.             shouldFell = true;
  55.            
  56.             if(!entityPlayer.capabilities.isCreativeMode && TreeCapitator.allowItemDamage && axe != null && currentAxeDamage <= 1)
  57.                 shouldFell = false;
  58.         }
  59.         else
  60.         {
  61.             blockHardness = TreeCapitator.logHardnessNormal;
  62.         }
  63.     }
  64.  
  65.     /**
  66.      * Called when the block is attempted to be harvested
  67.      */
  68.     @Override
  69.     public void onBlockHarvested(World world, int x, int y, int z, int md, EntityPlayer entityPlayer)
  70.     {
  71.         player = entityPlayer;
  72.         startPos = new Coord(x, y, z);
  73.        
  74.         if(((TreeCapitator.sneakAction.equalsIgnoreCase("enable") && player.isSneaking())
  75.                 || (!TreeCapitator.sneakAction.toLowerCase().contains("enable") && !player.isSneaking()))
  76.                 && !world.isRemote && !(player.capabilities.isCreativeMode && TreeCapitator.disableInCreative))
  77.         {
  78.             Coord topLog = getTopLog(world, new Coord(x, y, z));
  79.             if (leavesAround(world, topLog) > 2 && shouldFell)
  80.             {
  81.                 List<Coord> listFinal = new ArrayList<Coord>();
  82.                 List<Coord> logs = addLogs(world, new Coord(x, y, z));
  83.                 addLogsAbove(world, new Coord(x, y, z), listFinal);
  84.                 destroyBlocks(world, logs);
  85.                 if (TreeCapitator.destroyLeaves)
  86.                 {
  87.                     for(Coord pos : listFinal)
  88.                     {
  89.                         List<Coord> leaves = addLeaves(world, pos);
  90.                         removeLeavesWithLogsAround(world, leaves);
  91.                         destroyBlocksWithChance(world, leaves, 0.5F, playerHasShearsInHotbar(player));
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.     }
  97.  
  98.     public Coord getTopLog(World world, Coord pos)
  99.     {
  100.         while(world.getBlockId(pos.x, pos.y + 1, pos.z) == blockID)
  101.             pos.y++;
  102.         return pos;
  103.     }
  104.  
  105.     public int leavesAround(World world, Coord pos)
  106.     {
  107.         int i = 0;
  108.         for (int x = -1; x <= 1; x++)
  109.         {
  110.             for (int y = -1; y <= 1; y++)
  111.             {
  112.                 for (int z = -1; z <= 1; z++)
  113.                 {
  114.                     if (x != 0 || y != 0 || z != 0)
  115.                     {
  116.                         Block block = Block.blocksList[world.getBlockId(pos.x + x, pos.y + y, pos.z + z)];
  117.                         if (block != null && (block instanceof BlockLeavesBase))
  118.                             i++;
  119.                     }
  120.                 }
  121.             }
  122.         }
  123.  
  124.         return i;
  125.     }
  126.  
  127.     /**
  128.      * Defines whether or not a player can break the block with current tool and sets the local axe object if possible
  129.      */
  130.     private boolean isAxeItemEquipped(EntityPlayer entityPlayer)
  131.     {
  132.         ItemStack item = entityPlayer.getCurrentEquippedItem();
  133.         if(item != null && CommonUtils.isItemInList(item.itemID, item.getItemDamage(), TreeCapitator.axeIDList))
  134.         {
  135.             axe = item;
  136.             return true;
  137.         }
  138.         else
  139.         {
  140.             axe = null;
  141.             return false;
  142.         }
  143.     }
  144.  
  145.     /**
  146.      * Defines whether or not a player has a shear-type item in the hotbar
  147.      */
  148.     private boolean playerHasShearsInHotbar(EntityPlayer entityplayer)
  149.     {
  150.        
  151.         return shearsHotbarIndex(entityplayer) != -1;
  152.     }
  153.  
  154.     /**
  155.      * Returns the index of the left-most shear-type item in the hotbar and sets the local shears object if possible
  156.      */
  157.     private int shearsHotbarIndex(EntityPlayer entityPlayer)
  158.     {
  159.         for(int i = 0; i < 9; i++)
  160.         {
  161.             ItemStack item = entityPlayer.inventory.mainInventory[i];
  162.            
  163.             if(item != null && CommonUtils.isItemInList(item.itemID, item.getItemDamage(), TreeCapitator.shearIDList))
  164.             {
  165.                 shears = item;
  166.                 return i;
  167.             }
  168.         }
  169.         shears = null;
  170.         return -1;
  171.     }
  172.  
  173.     private void destroyBlocks(World world, List<Coord> list)
  174.     {
  175.         destroyBlocksWithChance(world, list, 1.0F, false);
  176.     }
  177.  
  178.     private void destroyBlocksWithChance(World world, List<Coord> list, float f, boolean canShear)
  179.     {
  180.         while (list.size() > 0)
  181.         {
  182.             Coord pos = list.remove(0);
  183.             int id = world.getBlockId(pos.x, pos.y, pos.z);
  184.             if(id != 0)
  185.             {
  186.                 Block block = Block.blocksList[id];
  187.                 int metadata = world.getBlockMetadata(pos.x, pos.y, pos.z);
  188.                 world.setBlockWithNotify(pos.x, pos.y, pos.z, 0);
  189.                
  190.                 if((((block instanceof BlockVine && TreeCapitator.shearVines) || (block instanceof BlockLeavesBase && TreeCapitator.shearLeaves)) && canShear)
  191.                         && !(player.capabilities.isCreativeMode && TreeCapitator.disableCreativeDrops))
  192.                 {
  193.                     dropBlockAsItem_do(world, pos.x, pos.y, pos.z, new ItemStack(id, 1, metadata & 0x03));
  194.                    
  195.                 }
  196.                 else if(!(player.capabilities.isCreativeMode && TreeCapitator.disableCreativeDrops))
  197.                 {
  198.                     block.dropBlockAsItem(world, pos.x, pos.y, pos.z, metadata, 0);
  199.                 }
  200.             }
  201.         }
  202.     }
  203.  
  204.     public List addLogs(World world, Coord pos)
  205.     {
  206.         int index = 0, lowY = pos.y, x, y, z;
  207.         List<Coord> list = new ArrayList<Coord>();
  208.         Coord newPos;
  209.  
  210.         list.add(pos);
  211.  
  212.         do
  213.         {
  214.             Coord currentLog = list.get(index);
  215.  
  216.             for (x = -1; x <= 1; x++)
  217.             {
  218.                 for (y = -1; y <= 1; y++)
  219.                 {
  220.                     for (z = -1; z <= 1; z++)
  221.                     {
  222.                         if (world.getBlockId(currentLog.x + x, currentLog.y + y, currentLog.z + z) == this.blockID)
  223.                         {
  224.                             newPos = new Coord(currentLog.x + x, currentLog.y + y, currentLog.z + z);
  225.                            
  226.                             if(TreeCapitator.maxBreakDistance == -1
  227.                                     || (Math.abs(newPos.x - startPos.x) <= TreeCapitator.maxBreakDistance
  228.                                         &&  Math.abs(newPos.z - startPos.z) <= TreeCapitator.maxBreakDistance))
  229.  
  230.                             if (!list.contains(newPos) && (newPos.y >= lowY || !TreeCapitator.onlyDestroyUpwards))
  231.                                 list.add(newPos);
  232.                         }
  233.                     }
  234.                 }
  235.             }
  236.         } while (++index < list.size());
  237.  
  238.         return list;
  239.     }
  240.  
  241.     private void addLogsAbove(World world, Coord position, List<Coord> listFinal)
  242.     {
  243.         List<Coord> listAbove = new ArrayList<Coord>();
  244.         List<Coord> list;
  245.         Coord newPosition;
  246.         int counter, index, x, z;
  247.  
  248.         listAbove.add(position);
  249.  
  250.         do
  251.         {
  252.             list = listAbove;
  253.             listAbove = new ArrayList<Coord>();
  254.  
  255.             for (Coord pos : list)
  256.             {
  257.                 counter = 0;
  258.                 for (x = -1; x <= 1; x++)
  259.                 {
  260.                     for (z = -1; z <= 1; z++)
  261.                     {
  262.                         if (world.getBlockId(pos.x + x, pos.y + 1, pos.z + z) == this.blockID)
  263.                         {
  264.                             if (!listAbove.contains(newPosition = new Coord(pos.x + x, pos.y + 1, pos.z + z)))
  265.                             {
  266.                                 listAbove.add(newPosition);
  267.                             }
  268.                             counter++;
  269.                         }
  270.                     }
  271.                 }
  272.  
  273.                 if (counter == 0)
  274.                 {
  275.                     listFinal.add(pos.clone());
  276.                 }
  277.             }
  278.  
  279.             index = -1;
  280.             while (++index < listAbove.size())
  281.             {
  282.                 Coord pos = listAbove.get(index);
  283.                 for (x = -1; x <= 1; x++)
  284.                 {
  285.                     for (z = -1; z <= 1; z++)
  286.                     {
  287.                         if (world.getBlockId(pos.x + x, pos.y, pos.z + z) == this.blockID)
  288.                         {
  289.                             if (!listAbove.contains(newPosition = new Coord(pos.x + x, pos.y, pos.z + z)))
  290.                             {
  291.                                 listAbove.add(newPosition);
  292.                             }
  293.                         }
  294.                     }
  295.                 }
  296.             }
  297.         } while (listAbove.size() > 0);
  298.     }
  299.  
  300.     public List<Coord> addLeaves(World world, Coord pos)
  301.     {
  302.         int index = -1;
  303.         List<Coord> list = new ArrayList<Coord>();
  304.  
  305.         addLeavesInDistance(world, pos, 4, list);
  306.  
  307.         while (++index < list.size())
  308.         {
  309.             Coord var5 = list.get(index);
  310.             addLeavesInDistance(world, var5, 1, list);
  311.         }
  312.  
  313.         return list;
  314.     }
  315.  
  316.     public void addLeavesInDistance(World world, Coord pos, int range, List list)
  317.     {
  318.         for (int x = -range; x <= range; x++)
  319.         {
  320.             for (int y = -range; y <= range; y++)
  321.             {
  322.                 for (int z = -range; z <= range; z++)
  323.                 {
  324.                     Block block = Block.blocksList[world.getBlockId(x + pos.x, y + pos.y, z + pos.z)];
  325.                     if (block != null && (block instanceof BlockLeavesBase || block instanceof BlockVine))
  326.                     {
  327.                         int metadata = world.getBlockMetadata(x + pos.x, y + pos.y, z + pos.z);
  328.                         if ((metadata & 8) != 0 && (metadata & 4) == 0)
  329.                         {
  330.                             Coord newPos = new Coord(x + pos.x, y + pos.y, z + pos.z);
  331.                             if (!list.contains(newPos))
  332.                             {
  333.                                 list.add(newPos);
  334.                             }
  335.                         }
  336.                     }
  337.                 }
  338.             }
  339.         }
  340.     }
  341.  
  342.     public void removeLeavesWithLogsAround(World world, List<Coord> list)
  343.     {
  344.         for (int i = 0; i < list.size();)
  345.             if (hasLogClose(world, list.get(i), 1))
  346.                 list.remove(i);
  347.             else
  348.                 i++;
  349.     }
  350.  
  351.     public boolean hasLogClose(World world, Coord pos, int i)
  352.     {
  353.         for (int x = -i; x <= i; x++)
  354.             for (int y = -i; y <= i; y++)
  355.                 for (int z = -i; z <= i; z++)
  356.                     if ((x != 0 || y != 0 || z != 0) && world.getBlockId(x + pos.x, y + pos.y, z + pos.z) == this.blockID)
  357.                         return true;
  358.  
  359.         return false;
  360.     }
  361. }
Advertisement
Add Comment
Please, Sign In to add comment