Solved Block Place Event

Discussion in 'Plugin Development' started by NoLiver92, Jun 29, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    NoLiver92

    Hi all, Quick question as im not sure exactly how to do it. i have placed a block (a chest) but i want to get the direction it was faced, N,S,E,W. How can I do this?
     
  2. Offline

    Traks

    1. Get the block - BlockPlaceEvent#getPlacedBlock()
    2. Get its state - Block#getState()
    3. Get its data - BlockState#getData()
    4. Cast to 'Directional' or 'Chest' (and of course check if it can be cast)
    4. Get the facing - Directional#getFacing()
     
  3. Offline

    Tecno_Wizard

    NoLiver92
    Get the block placed event and take the block placed on property ( that's likely not what it's called, but I'm too lazy to look it up)
    It will return a block and you can use the get x, y, and z methods of the block to determine it's relative location to the player.
    There is likely a much better way of doing it, but I am not aware of what it is.
     
  4. Offline

    NoLiver92

    Traks Thanks I understand everything to point 3, so ive got the MaterialData. But could you give me an example of the last 2 points? and what do you get as the output of point 5?

    Traks
    Ive tried this:
    Code:java
    1. Chest chest = (Chest) block;
    2. Directional dir = (Directional) chest.getData();


    and then sent it to the player as a message but i got nothing

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  5. Offline

    Traks

    Code:java
    1. Chest chest = (Chest) event.getPlacedBlock().getState().getData();
    2. BlockFace face = chest.getFacing();

    Where 'event' is an instance of BlockPlaceEvent and 'Chest' is org.bukkit.material.Chest
     
  6. Offline

    NoLiver92

    Traks Sorry heres my whole code for the listener:
    Code:java
    1. package NoLiver92.AdvancedRegions.Listeners;
    2.  
    3. import java.util.List;
    4.  
    5. import org.bukkit.Location;
    6. import org.bukkit.block.Block;
    7. import org.bukkit.block.Chest;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.BlockPlaceEvent;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.meta.ItemMeta;
    14. import org.bukkit.material.Directional;
    15. import NoLiver92.AdvancedRegions.Util.CustomItems;
    16.  
    17. public class BlockPlaceListener implements Listener
    18. {
    19. private NoLiver92.AdvancedRegions.Main.AdvancedRegions plugin;
    20.  
    21. public BlockPlaceListener(NoLiver92.AdvancedRegions.Main.AdvancedRegions plugin)
    22. {
    23. this.plugin = plugin;
    24. }
    25. @EventHandler
    26. public void onBlockPlace(BlockPlaceEvent event)
    27. {
    28. Player player = event.getPlayer();
    29. ItemStack item = event.getItemInHand();
    30. Block block = event.getBlockPlaced();
    31. CustomItems CI = new CustomItems();
    32. if(item == CI.getBaseChest(player.getName()))
    33. {
    34. //set metadata
    35. ItemMeta itemmeta = event.getItemInHand().getItemMeta();
    36. plugin.getMethods().setMetadata(block, "basechestdisplayname", itemmeta.getDisplayName());
    37. List<String> lorelist = itemmeta.getLore();
    38. for(int i = 0; i < lorelist.size(); i++)
    39. {
    40. plugin.getMethods().setMetadata(block, "lore"+i, lorelist.get(i));
    41. }
    42.  
    43. //set region
    44. Location loc = block.getLocation();
    45. Chest chest = (Chest) block;
    46. Directional dir = (Directional) chest.getData();
    47. int width = plugin.getConfig().getInt("Regions.Default Width") / 2;
    48. int height = plugin.getConfig().getInt("Regions.Default Height") / 2;
    49. player.sendMessage("DIR: " + dir.getFacing().name());
    50. //Regions region = new Regions(player, , , block.getLocation());
    51. }
    52. }
    53. }
    54.  


    CI.getBaseChest returns an itemstack with custom names and lores

    EDIT: its my if statement not firing, i thought it was but checking it now, it does not. im gonna fix that so I can continue
     
  7. Offline

    Traks

    Please replace the code you use to get the direction with the code I showed in my previous post, as yours won't work ;)
     
  8. Offline

    NoLiver92


    Ive fixed it and have got the direction working :p this is the code (i have removed the irrelivant stuff):
    Code:java
    1. Directional dir = (Directional) block.getState().getData();
    2. player.sendMessage("DIR: " + dir.getFacing().name());


    This works and i got what I needed thanks bud
     
  9. Offline

    Traks

    That's also a possibility, please mark your thread as solved by the way!
    Thread tools > Edit thread > Dropdown menu > 'Solved' > Save
     
  10. Offline

    NoLiver92

    Traks I know, i was gonna do it but got called away, doing it now
     
Thread Status:
Not open for further replies.

Share This Page