Custom block drop from only given locations.

Discussion in 'Plugin Development' started by mollekake, Jul 10, 2013.

Thread Status:
Not open for further replies.
  1. Hello people.
    What i want to accomplish:
    A block(this case a spruce leaf block) will drop Grapes(renamed nether stalk).
    but i only want it to drop at locations i specify earlier. That way not all spruce leaf blocks drop this, but only the ones at the given locations. this is what i got so far, this is in a block break event:
    Code:java
    1. if(blocklocs.contains(event.getBlock().getLocation())){
    2. System.out.print("1");
    3. event.setCancelled(true);
    4. event.getBlock().setType(Material.AIR);
    5. event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), wineseed);
    6. event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), grapes);
    7. blocklocs.remove(event.getBlock().getLocation());
    8. System.out.print(blocklocs);
    9. }

    blocklocs is an arraylist with locations.
    my problem is that only the first block i break drops what it should. the other blocks dont drop anything, and the arraylist isnt updated after the first block break.

    this is the block creation part:
    Code:java
    1. final Block block = event.getClickedBlock().getRelative(BlockFace.UP);
    2. block.setTypeId(18);
    3. block.setData((byte)2);
    4.  
    5. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    6. public void run(){
    7. block.setData((byte)1);
    8. blocklocs.add(block.getLocation());
    9. System.out.print(blocklocs);
    10. }
    11. }, 20 * 60 * plugin.getConfig().getInt("Minutes"));


    Anyone got any ideas? Or how i could solve this another way?

    Hmm. The delayed task cant use a non-final block.


    well this is new stuff to me. Could you explain it some more? I dont quite get it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  2. anyone got any pointers for me? im stuck at this part :(
     
  3. Offline

    Remi1115

    Maybe this will come in handy.

    InArea Java method (open)

    Code:Java
    1.  
    2. public boolean inArea(Location targetLocation, Location inAreaLocation1, Location inAreaLocation2, boolean checkY){
    3. if(inAreaLocation1.getWorld().getName() == inAreaLocation2.getWorld().getName()){ // Check for worldName location1, location2
    4. if(targetLocation.getWorld().getName() == inAreaLocation1.getWorld().getName()){ // Check for worldName targetLocation, location1
    5. if((targetLocation.getBlockX() >= inAreaLocation1.getBlockX() && targetLocation.getBlockX() <= inAreaLocation2.getBlockX()) || (targetLocation.getBlockX() <= inAreaLocation1.getBlockX() && targetLocation.getBlockX() >= inAreaLocation2.getBlockX())){ // Check X value
    6. if((targetLocation.getBlockZ() >= inAreaLocation1.getBlockZ() && targetLocation.getBlockZ() <= inAreaLocation2.getBlockZ()) || (targetLocation.getBlockZ() <= inAreaLocation1.getBlockZ() && targetLocation.getBlockZ() >= inAreaLocation2.getBlockZ())){ // Check Z value
    7. if(checkY == true){ // If should check for Y value
    8. if((targetLocation.getBlockY() >= inAreaLocation1.getBlockY() && targetLocation.getBlockY() <= inAreaLocation2.getBlockY()) || (targetLocation.getBlockY() <= inAreaLocation1.getBlockY() && targetLocation.getBlockY() >= inAreaLocation2.getBlockY())){ // Check Y value
    9. return true;
    10. }
    11. }else{
    12. return true;
    13. }
    14. }
    15. }
    16. }
    17. }
    18. return false;
    19. }
    20.  



    You can use it like so:

    Show Spoiler

    Code:Java
    1.  
    2. Location blockLocation = event.getBlock().getLocation(); // From the BlockBreakEvent
    3. World world = plugin.getServer().getWorld("world");
    4. Location inAreaLocation1 = new Location(world, 200, 64, 200);
    5. Location inAreaLocation2 = new Location(world, 205, 65, 195);
    6. if(inArea(blockLocation, inAreaLocation1, inAreaLocation2, false)){ // False, because we don't want it to check for the Y coordinates.
    7. // Check if the block is of the correct type/material, and if it is, do the other stuff and give the player the special item.
    8. }
    9.  


    (Not writen in an IDE, so there could be some errors in it. You'll get the point of what I am trying to do I guess).
     
Thread Status:
Not open for further replies.

Share This Page