Solved Block place at player location

Discussion in 'Plugin Development' started by CptnPummeluff, May 13, 2014.

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

    CptnPummeluff

    Hey guys,
    I need some help to code a plugin. It should build a wall of for example stones at the location "loc". The wall should have the dimensions of 3 blocks wide, 2 blocks high and 1 block deep. Probably I don't know how to code that. The barrier should be created after the player right_clicks with a bone. The bone thing is just for testing. Would be nice if u teach me how to code that in this example. Thanks :)
    Code:java
    1. @EventHandler
    2. public void barrier(PlayerInteractEvent e){
    3. Player p = e.getPlayer();
    4. Location loc = p.getLocation().getBlock().getLocation();
    5.  
    6. if(e.getItem().equals(Material.BONE)){
    7. if(e.getAction().equals(Action.RIGHT_CLICK_AIR)|e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
    8.  
    9. }
    10. }
    11. }
     
  2. Offline

    aninsanellama

    Line 6 will not currently work, as you are checking for a Material whereas the .getItem() method returns an ItemStack. Use if(e.getItem().getMaterial() == Material.BONE) {}.

    I'll show you how to set blocks at a certain location to a different type of block:
    Code:java
    1. // This code assumes that 'world' is a org.bukkit.World variable.
    2. world.getBlockAt(new Location(world, 0, 100, 0)).setType(Material.BEDROCK);


    That code sets the block at the location 0, 100, 0 in the world specified to bedrock. If you wanted to do this to multiple blocks, you could attempt iterating through numbers (for and while loops) and then setting them to a location, and changing them using the code provided.
     
    CptnPummeluff likes this.
  3. Offline

    CptnPummeluff

    aninsanellama
    Code:java
    1. @EventHandler
    2. public void barrier(PlayerInteractEvent e){
    3. Player p = e.getPlayer();
    4. Location loc = p.getLocation().getBlock().getLocation();
    5. World world = p.getWorld();
    6.  
    7. if(e.getItem().getType().equals(Material.BONE)){
    8. if(e.getAction().equals(Action.RIGHT_CLICK_AIR)|e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
    9. double x1 = p.getLocation().getX();
    10. double y1 = p.getLocation().getY();
    11. double z1 = p.getLocation().getZ();
    12. float f1 = p.getLocation().getYaw();
    13. float f2 = p.getLocation().getPitch();
    14. world.getBlockAt(new Location(world, x1, y1, z1)).setType(Material.STONE);
    15. }
    16. }
    17. }


    I want to spawn a wall and not a single block. How do I code that? Thx for ur efforts. :)
     
  4. Offline

    Garris0n

    I don't think you want to use "|" there.

    As for the wall, think about it. Consider how you'd make each wall.
     
  5. Offline

    aninsanellama

    This code here should work as an example to show you how to begin building a wall (straight line of one block here):
    Code:java
    1. for(double i = 0; i < 10; i++)
    2. {
    3. new Location(plugin.world, i, 5, 0).getBlock().setType(Material.BEDROCK);
    4. }

    This code iterates (if you don't know about for loops, look here) ten times, and every time the 'i' double is incremented (increased by one) a new location is specified, and the block at that place will become bedrock. The code I have provided will build a straight line along the x co-ordinate from x = 0, at the y co-ordinate 5, and the x co-ordinate 0.
     
  6. Offline

    CptnPummeluff

    Code:java
    1. for(double i = 0; i<10; i++){
    2. world.getBlockAt(new Location(world, x1 , y1, z1)).setType(Material.STONE);
    3. }


    Probably I want to spawn the wall in front of the players location. So, how can I combine the players location and the double "i"? aninsanellama
     
  7. Offline

    aninsanellama

    If you want to spawn a wall along the x co-ordinate, you'd do the following:
    Code:java
    1. for(double i = player.getLocation().getX(); i < 10; i++)
    2. {
    3. world.getBlockAt(new Location(world, i, p.getLocation.getY(), p.getLocation().getZ())).setType(Material.STONE);
    4. }

    If you wanted to change it to move along the Z co-ordinate, you'd initialize i to p.getLocation().getZ(), and then get the usual player x co-ordinate, and replace p.getLocation().getZ() with i. Example:
    Code:java
    1. for(double i = player.getLocation().getZ(); i < 10; i++)
    2. {
    3. world.getBlockAt(new Location(world, p.getLocation().getX(), p.getLocation.getY(), i)).setType(Material.STONE);
    4. }
     
  8. Offline

    ZodiacTheories

    CptnPummeluff

    You can't do
    Code:java
    1. if(e.getItem().getType().equals(Material.BONE)){
    2. if(e.getAction().equals(Action.RIGHT_CLICK_AIR)|e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){


    because .equals() returns a string, use if(e.getItem().getType==Material
     
  9. Offline

    CptnPummeluff

  10. Offline

    ZodiacTheories

  11. Offline

    wxwsk8er

    It might, but its not good priactice, bukkit was coded to work certain ways, so this might break. Anyway good luck!! If you need help let me know!!
     
    CptnPummeluff likes this.
  12. Offline

    CptnPummeluff

    aninsanellama probably this doesn't work. Nothing happens, no error just nothing. Where's the problem?
    Code:java
    1. @EventHandler
    2. public void barrier(PlayerInteractEvent e){
    3. Player p = e.getPlayer();
    4. Location loc = p.getLocation().getBlock().getLocation();
    5. World world = p.getWorld();
    6.  
    7. if(e.getItem().getType()==Material.BONE){
    8. if(e.getAction().equals(Action.RIGHT_CLICK_AIR)|e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
    9. double x1 = p.getLocation().getX();
    10. double y1 = p.getLocation().getY();
    11. double z1 = p.getLocation().getZ();
    12. float f1 = p.getLocation().getYaw();
    13. float f2 = p.getLocation().getPitch();
    14.  
    15. for(double i = p.getLocation().getX(); i < 10; i++)
    16. {
    17. world.getBlockAt(new Location(world, i, y1, z1)).setType(Material.STONE);
    18. }
    19. }
    20. }
    21. }


    Still thx for ur help :)
     
  13. Offline

    xTigerRebornx

    wxwsk8er ZodiacTheories Someone recently pointed out to me that Enums override the .equals method, so that when Enum#equals() is called, it will simply run a == check. But, using == instead of Enum#equals() is preferred, as == is null safe
     
  14. Offline

    Lecrayen

    When are you going to return a null enumerator? LOL
     
  15. Offline

    metalhedd

    It's no less likely than any other null pointer, really.
     
  16. Offline

    xTigerRebornx

    Lecrayen Well, Material#matchMaterial() can return null (I believe), or someone could simply have a variable that may end up null in some case or another
    I.E.
    Code:
    public Material mat = null;
     
    try{
      mat = Material.matchMaterial("NotARealMaterial");
      boolean b = mat.equals(Material.COBBLESTONE); // May throw an NPE
      b = (mat == Material.COBBLESTONE);
    } catch(Exception e){
     
    }
    Just some pseudo-code, haven't tested it, but should get some response or point across.
     
  17. Offline

    Lecrayen

    If you're forcing a null enumerator, you should probably move your code around. But yea, it's up to the person making the plugin to ensure it's impossible to come up with a null. Pretty much basic knowledge. (No server owner wants to see a plugin error, and if they do, it usually is just uninstalled and never looked at again)
     
  18. Offline

    xTigerRebornx

    Lecrayen Well, not everyone is a perfect developer. Your post implied that people will never make mistakes when programming, which is (mostly) never true, and in the context of Minecraft Servers (which can be modified and changed as people want to), errors can occur, making that goal of "ensuring its impossible to come up with null" impossible or more complicated then it originally was. The whole "forcing" of a null enumerator was just an example, take it in the context of the String that is passed in is read from a Config or other location, and there is a simple typo, Enum#equals() can/would throw an NPE, while == wouldn't (though they can be worked around with a try/catch). What may be basic knowledge to you may not be to new developers (or even some more advanced developers).
     
  19. Offline

    aninsanellama

    (Replying to thread owner) Sorry, my mistake. The foor loop probably would not have run because the first number was already bigger than the second, and therefore the loop's condition was already met.

    p.getLocation()+10 should replace the previous '10'.
     
  20. Offline

    Lecrayen


    Sorry, your right... I was just crabby because no one would answer my own topic. Still no answers: http://forums.bukkit.org/threads/adjacent-block-efficiency.268190/#post-2490963
     
  21. Offline

    EpicCraft21

    It is (Action.RIGHT_CLICK_AIR) || e.getAction() == (Action.RIGHT_CLICK_BLOCK)){
    Sorry if I'm necroposting, I don't know how old necroposting is considered.
     
Thread Status:
Not open for further replies.

Share This Page