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

    Yurikoma

    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

    HON95

    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

    Yurikoma

    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

    CorrieKay

    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

    Woobie

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

    CorrieKay

    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

    Yurikoma

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

    Squish000

    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

    Icyene

    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

    Code:Java
    1.  
    2. <onplayermove>
    3. //Do stuff
    4. </onplayermove>
    5.  


    Rather do

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


    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

    travja

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

    Woobie

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

Share This Page