is it possible to have blocks follow above players and entities

Discussion in 'Plugin Development' started by Yurikoma, Sep 15, 2012.

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


    Basically i want a block to be always above my players and nearby entities heads, is this possible?

    as in they walk with a single block following above
  2. Offline


    Possible. The best way (IMO) is to listen for PlayerMoveEvents, get the block 2 or 3 blocks above the players location, and so on... Ofc, you should check if the player actually moved from one "block" to another, and find a way to not permanently change blocks the player has been under, like adding the block with its type, data and location to a hashmap<playerName, blockSpecs> or something.
  3. Offline


    sounds good, i am planning a plugin that will need this feature, so it is good that it is possible, thanks for your feedback

    the main question though, is could i make it work for monsters and animals that are within a certain range of a player?
  4. Offline


    Aye, it would be possible. However, it wont be smooth, and it wont be directly above their head, obviously.

    but the gist of it would be to have a sync repeating task. I would suggest having it run once every 15-20 ticks, though if you have larger numbers of players, or are otherwise experiencing lag or slowdown, you should increase the time between execution

    But basically, you need to keep track of what the old block was. The best way, is to grab the blockState, then store it in a map with a location or some other identifying feature. then set the block any way you wish (if you need to utilize the blocks state, grab another instance of the blockstate).

    Now, whenever the run method is invoked, iterate through a set/collection/queue/whatever you use to store the players with blocks over their heads, and for each of them, first grab the old blockstate, and i believe you can just call .update(); to set the block back to what it normally was. (not 100% on this though) then drop it out of the map. Then, get their location, set the Y to +3 or however high it needs to be, grab the block, then do as stated above. Rinse, repeat. However, you still need to figure out how youre gonna not derp your map if the map crashes, how youre gonna handle logging in an out, etc.

    disclaimer: this was written almost at 5AM my time. im fairly tired, and the above information may be incorrect. Utilize at your own risk :p
  5. Offline


    I know how to do this with players, so it should work with mobs too.
    Not entirely sure though.
  6. Offline


    Careful with player move events. They fire so fast and so frequently, that you dont want to do anything that could take much time during them
  7. Offline


    seems like it could be a real lag problem right now
  8. Offline


    You could create your own entity, a modified version of falling block (the entity that appars when sand/gravel falls), then the entity could follow you around above your head exactly and still look like any block you want.
    BlackHaty likes this.
  9. Offline


    A FallingBlock entity does not have to conform to the grid of MC, so you can make it follow the player. *gets idea to make a plugin that allows building outside of grid*

    You can use lock booleans to make it lag less. Basically, instead of

    2. <onplayermove>
    3. //Do stuff
    4. </onplayermove>

    Rather do

    2. <onplayermove>
    3. if(lock) {
    4. lock = !lock;
    5. //Do stuff
    6. }
    7. </onplayermove>

    That suddenly makes your code only execute 10/second, instead of 20. You can make more and more locks to lower that even further.

    I would not suggest using a counter int for this, as if(1==1) is quite a bit slower than if(true)
  10. Offline


    Dinnerbone has a redcarpet tutorial on youtube that might be of some help.
  11. Offline


    Well here's the code i use.
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onPlayerMove(PlayerMoveEvent e) {
            Player player = e.getPlayer();
            Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
            if (!block.isEmpty()) {
Thread Status:
Not open for further replies.

Share This Page