Solved Delay between two messages

Discussion in 'Plugin Development' started by MunchMallow, Dec 1, 2013.

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

    MunchMallow

    Hello everybody.

    I have spent a massive time just to figure out, how to do this. And it really just bugs me!
    The only thing I really want to do, is to implement delay between two messages. (or more messages)
    I have tried using Thread.sleep, but that just stops the whole server!

    This is what I accomplished so far!
    Code:java
    1. @EventHandler
    2. public void onTalk(PlayerInteractEntityEvent e) {
    3. Player p = e.getPlayer();
    4. Entity entity = e.getRightClicked();
    5. if (entity.getType()==EntityType.VILLAGER) {
    6. if (((LivingEntity)entity).getCustomName().equalsIgnoreCase("§6Bob")) {
    7. e.setCancelled(true);
    8. p.sendMessage("Help! I don't have any more cake!");
    9. try {
    10. Thread.sleep(10000);
    11. } catch (InterruptedException e1) {
    12. e1.printStackTrace();
    13. }
    14. p.sendMessage("Can you get me four chocolate cakes?");
    15. }
    16. }
    17. }
    18. }

    I appreciate any possible help! You learn from your mistakes, right? :)
     
  2. Offline

    sgavster

    Do not use thread.sleep.
    Use
    Code:java
    1. Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
    2. public void run() {
    3. //send message
    4. }
    5. }, 5*20);//it would be 5 seconds, so 6*20 would be 6 seconds
     
  3. Offline

    MunchMallow

    Is this correct? Because i can't seem to get it to work.
    Code:java
    1. @EventHandler
    2. public void onTalk(PlayerInteractEntityEvent e) {
    3. Player p = e.getPlayer();
    4. Entity entity = e.getRightClicked();
    5. if (entity.getType()==EntityType.VILLAGER) {
    6. if (((LivingEntity)entity).getCustomName().equalsIgnoreCase("§6Bob")) {
    7. e.setCancelled(true);
    8. Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
    9. public void run() {
    10. p.sendMessage("Help! I don't have any more cake!");
    11. p.sendMessage("Can you get me four chocolate cakes?");
    12. }
    13. }, 5*20);
    14. }
    15. }


    Bump

    Okay, I tried to do this, and there is no error in the code. But when I right click a Villager it doesn't work!
    Here is the code.
    Code:java
    1. public Main plugin;
    2.  
    3. @EventHandler
    4. public void onTalk(PlayerInteractEntityEvent e) {
    5. Entity entity = e.getRightClicked();
    6. if (entity.getType()==EntityType.VILLAGER) {
    7. if (((LivingEntity)entity).getCustomName().equalsIgnoreCase("§6Bob")) {
    8. e.setCancelled(true);
    9. final Player p = e.getPlayer();
    10. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    11. @Override
    12. public void run() {
    13. p.sendMessage("Help! I don't have any more cake!");
    14. p.sendMessage("Can you get me four chocolate cakes?");
    15. }
    16. }, 100L);
    17. }
    18. }
    19. }
    20. }
     
  4. Offline

    willy00

    Put the second message after the time (100L).
     
  5. Offline

    MunchMallow

    I did it, but there is something wrong, let me show you the stacktrace and code
    Code:java
    1. public Main plugin;
    2.  
    3. @EventHandler
    4. public void onTalk(PlayerInteractEntityEvent e) {
    5. Entity entity = e.getRightClicked();
    6. if (entity.getType()==EntityType.VILLAGER) {
    7. if (((LivingEntity)entity).getCustomName().equalsIgnoreCase("§6Bob")) {
    8. e.setCancelled(true);
    9. final Player p = e.getPlayer();
    10. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    11. @Override
    12. public void run() {
    13. p.sendMessage("Help! I don't have any more cake!");
    14. }
    15. }, 100L);
    16. p.sendMessage("Can you get me four chocolate cakes?");
    17. }
    18. }
    19. }
    20. }

    Stacktrace:
    Code:
    14:09:55 [SEVERE] Could not pass event PlayerInteractEntityEvent to MMORPG v1.0
    14:09:55 org.bukkit.event.EventException
    14:09:55 at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    14:09:55 at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    14:09:55 at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    14:09:55 at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    14:09:55 at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:1097)
    14:09:55 at net.minecraft.server.v1_6_R3.Packet7UseEntity.handle(SourceFile:36)
    14:09:55 at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
    14:09:55 at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
    14:09:55 at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
    14:09:55 at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
    14:09:55 at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
    14:09:55 at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
    14:09:55 at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
    14:09:55 at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
    14:09:55 at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    14:09:55 Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
    14:09:55 at org.apache.commons.lang.Validate.notNull(Validate.java:203)
    14:09:55 at org.bukkit.craftbukkit.v1_6_R3.scheduler.CraftScheduler.validate(CraftScheduler.java:391)
    14:09:55 at org.bukkit.craftbukkit.v1_6_R3.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:120)
    14:09:55 at org.bukkit.craftbukkit.v1_6_R3.scheduler.CraftScheduler.scheduleSyncRepeatingTask(CraftScheduler.java:116)
    14:09:55 at org.bukkit.craftbukkit.v1_6_R3.scheduler.CraftScheduler.scheduleSyncDelayedTask(CraftScheduler.java:100)
    14:09:55 at me.MunchMallow.NPC.Bob.onTalk(Bob.java:25)
    14:09:55 at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
    14:09:55 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    14:09:55 at java.lang.reflect.Method.invoke(Unknown Source)
    14:09:55 at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    14:09:55 ... 14 more
    
     
  6. Offline

    willy00

    On line 4, shouldn't it be:
    public void onPlayerInteract (PlayerInteractEvent e) {
     
  7. Offline

    Mathias Eklund

    What he names it doesn't matter.
     
  8. Offline

    MunchMallow

    Just tried, if i did that, there will be a red line under the "e.getRightClicked();"
     
  9. Offline

    L33m4n123

    Doesn't matter how he names the method but he still needs to name the event correctly :p
    Just simply due to the Reason that "PlayerInteractEvent" doesn't has a method to get the Right Clicked.. Entity it is I think in this Event. Not sure.

    So your first Event was the correct one (PlayerInteractEntityEvent)

    Your stacktrace clearly says that your variable "plugin" is null which means you did not gave it any value. You would need to assign the plugin variable the Main class. via a constructor or if it is in the main class just simply with "this"
     
    sgavster likes this.
  10. Offline

    sgavster

    As L33m4n123 said, you need to do
    Code:java
    1. private final YOUR_MAIN_CLASS plugin;
    2. public YOUR_LISTENER_CLASS(YOUR_MAIN_CLASS instance) {
    3. plugin = instance;
    4. }
     
  11. Offline

    MunchMallow

    Okay, I did what you guys said. But it still doesn't work, and there is no error.
    Code:
    Code:java
    1. public class Bob implements Listener {
    2.  
    3. public Main plugin;
    4. public Bob(Main instance) {
    5. plugin = instance;
    6. }
    7.  
    8. @EventHandler
    9. public void onPlayerInteract (PlayerInteractEntityEvent e) {
    10. Entity entity = e.getRightClicked();
    11. if (entity.getType()==EntityType.VILLAGER) {
    12. if (((LivingEntity)entity).getCustomName().equalsIgnoreCase("§6Bob")) {
    13. e.setCancelled(true);
    14. final Player p = e.getPlayer();
    15. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    16. @Override
    17. public void run() {
    18. p.sendMessage("Help! I don't have any more cake!");
    19. p.sendMessage("Can you get me four chocolate cakes?");
    20. }
    21. }, 100L);
    22. }
    23. }
    24. }
    25. }
     
  12. Offline

    1Rogue

    The way you have it now will just send both messages after 5 seconds. You want to send one of those messages before sending the other (one before the task, and one within it)
     
  13. Offline

    maxben34

    Just use a scheduler. Look into bukkit scheduling. For your situation I would recommend using 2 SyncDelayedTasks, with time separating the 2. For example one Can trigger after 0 ticks while the other can be set to after 60. This will put a 3 second delay. Just do some research on scheduling. Good luck MunchMallow
     
  14. Offline

    MunchMallow

    Okay i'm totally mindblown away now! I did all that, still doesn't work. I can't get the main class to work.
    Class "Bob"
    Code:java
    1. public class Bob implements Listener {
    2.  
    3. public Main plugin;
    4. public Bob(Main instance) {
    5. plugin = instance;
    6. }
    7.  
    8. @EventHandler
    9. public void onPlayerInteract (PlayerInteractEntityEvent e) {
    10. Entity entity = e.getRightClicked();
    11. if (entity.getType()==EntityType.VILLAGER) {
    12. if (((LivingEntity)entity).getCustomName().equalsIgnoreCase("§6Bob")) {
    13. e.setCancelled(true);
    14. final Player p = e.getPlayer();
    15. Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    16. @Override
    17. public void run() {
    18. p.sendMessage("Help! I don't have any more cake!");
    19. }
    20. }, 100L);
    21. p.sendMessage("Can you get me four chocolate cakes?");
    22. }
    23. }
    24. }
    25. }

    Class "Main"
    Code:java
    1. public final class Main extends JavaPlugin {
    2.  
    3.  
    4.  
    5. @Override
    6.  
    7. public void onEnable() {
    8. getServer().getPluginManager().registerEvents(new Bob(), this);
    9.  
    10. }
    11.  
    12.  
    13.  
    14. @Override
    15.  
    16. public void onDisable() {
    17.  
    18. }
    19.  
    20. }
     
  15. Offline

    Sagacious_Zed Bukkit Docs

    MunchMallow Does this compile?

    Show Spoiler
    What you have shown here should not compile. If it does, then something is else is wrong.
     
  16. Offline

    MunchMallow

    Woohoo! I got it fixed! Thank you all! Here is a diamond! [diamond] MORE DIAMONDS [diamond][diamond] MANY MORE! [diamondblock][diamondblock][diamondblock][diamondblock] AND A CAKE [cake]
     
Thread Status:
Not open for further replies.

Share This Page