Solved Error That I Cannot Figure Out In My Code. Please Help!!!

Discussion in 'Plugin Development' started by mkezar, Nov 20, 2014.

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

    mkezar

    Hi. i am working on a plugin that gets the block of the player. here it is:
    Code:java
    1. package plugins.mkezar;
    2.  
    3. import net.minecraft.server.v1_7_R3.Material;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.PlayerChangedWorldEvent;
    10. import org.bukkit.event.player.PlayerMoveEvent;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class TrailPlugin extends JavaPlugin implements Listener {
    14.  
    15. @Override
    16. public void onEnable() {
    17. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    18.  
    19. }
    20.  
    21. @EventHandler
    22. public void onPlayerWalkEvent(PlayerMoveEvent event) {
    23. Player player = event.getPlayer();
    24. player.getLocation().add(0,-1,0).getBlock();
    25. block.setType(Material.GRASS);
    26. }
    27. }


    For some reason though, It does not like
    Code:java
    1. block

    in
    Code:java
    1. block.setType(Material.GRASS);

    Please Help!!! i dont know how to fix the block. thank you :D

    LCT10 Laxer21117

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

    Rocoty

    You never defined block. The compiler doesn't know what block is.
     
  3. Offline

    ChipDev

    Learn java please!
    You did not state what block is. You put down
    Code:java
    1. player.getLocation().add(0,-1,0).getBlock();

    not stating what that was, you should use this:
    Code:java
    1. Block block = player.getLocation().add(0,-1,0).getBlock();
     
  4. Offline

    mkezar

    braincramp. thanks. i do know java btw :p i was just braincramping like crazy because its really late and im getting really tired. thanks doe :D
     
    ChipDev likes this.
  5. Offline

    Tecno_Wizard

    mkezar, using the player move event is EXTREMELY resource intensive and should not be used unless absolutly necessary. Creating a seperate thread that runs every 20 ticks would likely be more appropriate.
     
    ChipDev and Skionz like this.
  6. Offline

    HeadGam3z

    Why import net.minecraft.server.v1_7_R3.Material when you can import org.bukkit.Material?

    o3o
     
  7. Offline

    coasterman10

    Not really, you should know that there is also a considerable amount of code that runs inside CraftBukkit executed every single time a player moves and all PlayerMoveEvent does is allow plugins to hook into this. It is true that reflection is used to call methods in event listeners, but if you have no code there, the amount of time spent doing this will be nearly zero. Usually the issue is not in listening to PlayerMoveEvent itself but actually that the developer gets careless and does all kinds of stuff which actually becomes an issue.

    You can also check if the code is actually a trouble point by doing some actual profiling (/timings) and see how much time the event actually wastes for the number of players with which you test.

    OP's code does not do too much or waste resources at all and if I remember correctly if a block is already of a certain type CraftBukkit won't waste time sending out a block change to set it again.
     
  8. Offline

    mkezar

    i fixed it all. thanks
     
  9. Offline

    Asecta

    mkezar When you're doing the player move event, I highly recommend checking they are atually moving a whole block. You can do this by adding this code at the top of the onPlayerWalkEvent method:

    Code:java
    1. if (event.getTo().getBlock() == event.getFrom().getBlock())
    2. return;
     
Thread Status:
Not open for further replies.

Share This Page