Set Block

Discussion in 'Plugin Development' started by Guybrush, Jan 20, 2011.

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

    Guybrush

    I'm having issues figuring out how to set a block at a location to a specific type, or moving a specific block to a new location. If I do getServer().getWorlds()[0].getBlockAt() then pass in coordinates and what not I can then set the type, but I can't figure out a way to move this block to a new location. I don't want to just set blocks types, but be able to move complex blocks, I know this is pretty simple, but I can't find the command...
     
  2. Offline

    MadMichi

    Hmm... afaik there is no move command for blocks.
    But you could make an own method with a list of blocks that should be moved and an offset where to move them. Then loop through block list and switch types between new and old position.
     
  3. Offline

    Guybrush

    Umm, if I have one block I want to move, and I know where I want to move it to, how do I do that? I understand how to do that with anything other than a complex block but... Complex blocks are a no go as far as I can tell.
     
  4. Offline

    MadMichi

  5. Offline

    Guybrush

    Levers, doors, switches, ladders, stairs, these all have more information in them than a type. They have to know what wall they are on for one, which way they are facing and so on. Setting type does not do that. If you just attempt to change a block to a ladder the ladder then does not show graphically and that block becomes impassible, as if an invisible barrier were there. Or at least that's how it worked on Hmod. Unfortunately I don't know how to get the complex block info... Setting it? I do see a function that might do that, setBlockData(). Of course, I'm not SURE that's what I want, but since it's type is byte, I'm almost positive that's it. Now if I could just get the data somehow... Of course a simple block.setX() would be a heck of a lot more useful and convenient, but I could work with a get data.
     
  6. Offline

    Meta1203

    Couldn't you do this from a listener?
    Code:
    Block from = plugin.getServer().getWorlds()[0].getBlockAt(x,y,z);
    Block to = getServer().getWorlds()[0].getBlockAt(x,y,z);
    to = from;
    from.setTypeId(0);
    This would move block "to" to block "from", then delete block "from".
     
  7. Offline

    Guybrush

    But part of a block is there xyz I'm pretty sure, so, this would actually take my block, put it where it already was and then turn it into air.
     
  8. Offline

    Meta1203

    Yes, but just add 2 to the X value in the "to" to move it 2 blocks west (or east, I cannot remember which...)
    So:
    Code:
    Block from = plugin.getServer().getWorlds()[0].getBlockAt(x,y,z);
    Block to = getServer().getWorlds()[0].getBlockAt(x+2,y,z);
    to = from;
    from.setTypeId(0);
     
  9. Offline

    Guybrush

    No, because when you say that to = from, we are now assigning the object to all the properties of block from, which includes block from's xyz coordinates. So all you are doing is making the block I'm trying to push reappear exactly how it already is at it's current location and instantly turning it into air. Unless of course the location is not part of the object, but I'm rather positive that it is.
     
  10. Offline

    eisental

    Since you're dealing with objects here (Block objects) they are passed by reference in java. The line 'to = from' effectively does nothing. It merely tells the compiler that the variable to now references to the same object as the from variable does, the block object at coordinate x,y,z. So, unless you do something with the to variable later, this code is equivalent to
    Code:
    plugin.getServer().getWorld()[0].getBlockAt(x,y,z).setTypeId(o);
    
    As far as I know these block objects never move, they represent a portion of the world's space.
     
  11. Offline

    Meta1203

    You are getting a block object! What does getBlockAt(x,y,z) do? Gets a temporary block object that you do one operation on! The Block objects update on creation, but in a variable, you can do multiple operations to one block.
     
  12. Offline

    Guybrush

    @ eisental. I got so incredibly lost trying to figure out what you were saying there. Took me about a minute to figure out you were just saying what I was saying. I suppose without the to.update() it wouldn't actually do anything. It would be nice if we could get a block.SetLocation(x,y,z) function. Regardless, is there any way to do what I'm trying right now without that? Looking through the code I think I may have found a way to GetData, and now recently to setData on a block. This data type is the one that corresponds to what face the ladder is attached to yes?

    @Meta1203. to = from will CHANGE the x,y,z data contained in to. to = from will not update the block in game, and even if it did it would just update to itself. If from is a chest containing three feathers at the location (3, 4, 1) and I want to move it to (1, 4,1) then getting the block to, which is at (1, 4, 1) and then saying to = from. All I am doing is saying that now to is a chest containing three feathers at the location (3, 4, 1). How does this help me? It is no longer at it's old location.
     
  13. Offline

    eisental

    Sorry for being unclear. I was commenting on some basic Java language rule that I had the feeling both of you don't really get. Maybe I'm wrong but lets try a really accurate explanation:

    When you call a = getBlockAt(x1,y1,z1), what you get in return is just a number. This number is a pointer that points to some memory area where the data of the block is stored.

    If you then say Block t = a; both variables, t and a, now point to the same place in memory, but you didn't change any Block object data and didn't move any data around except for the memory address.
    This also means that if you now call a = getBlockAt(x2,y2,z2) you get a new pointer to a different memory address where the other block data is stored. Again you didn't change any actual data, your just pointing to a different place.

    And if my explaining abilities have failed check this out: :)
    http://www.yoda.arachsys.com/java/passing.html
     
  14. Offline

    Guybrush

    Yup, got that. Long and short of it is that it won't work that way, for a fair number of different reasons.
     
Thread Status:
Not open for further replies.

Share This Page