Solved Countdown Troubles

Discussion in 'Plugin Development' started by ccrama, Oct 26, 2013.

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

    ccrama

    Hello Bukkit

    I am trying to create a coutdown if a player interacts with glowstone dust. This is my current code:
    Code:java
    1. @EventHandler
    2. public void cloaking(PlayerInteractEvent e){
    3. final Player p = e.getPlayer();
    4. if(p.getItemInHand().getType() == Material.GLOWSTONE_DUST){
    5. p.sendMessage("NOW CLOAKED");
    6.  
    7. for (Player players : Bukkit.getOnlinePlayers())
    8. {
    9. players.hidePlayer(p);
    10.  
    11. }
    12. new BukkitRunnable(){
    13. int timeLeft = 10;
    14.  
    15. @Override
    16. public void run(){
    17. if(timeLeft > 0){
    18. timeLeft -= 1;
    19. int numb = timeLeft;
    20. p.sendMessage(numb + "SECONDS LEFT");
    21. } else {
    22. for (Player players : Bukkit.getOnlinePlayers())
    23. {
    24. players.showPlayer
    25. (p);
    26.  
    27. }
    28. this.cancel();
    29. }
    30. }
    31. }.runTaskTimer((Plugin) this, 0L, 20L); //LINE 86//
    32.  
    33.  
    34.  
    35.  
    36. }
    37. }


    I have an error on }.runTaskTimer... line. This is the console error
    Code:
    2013-10-26 22:12:03 [SEVERE] Could not pass event PlayerInteractEvent to MineFortress2 v1.1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:190)
        at net.minecraft.server.v1_6_R3.BlockPressurePlateBinary.e(BlockPressurePlateBinary.java:53)
        at net.minecraft.server.v1_6_R3.BlockPressurePlateAbstract.b(BlockPressurePlateAbstract.java:92)
        at net.minecraft.server.v1_6_R3.BlockPressurePlateAbstract.a(BlockPressurePlateAbstract.java:86)
        at net.minecraft.server.v1_6_R3.Entity.D(Entity.java:734)
        at net.minecraft.server.v1_6_R3.Entity.move(Entity.java:677)
        at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:373)
        at net.minecraft.server.v1_6_R3.Packet10Flying.handle(SourceFile:136)
        at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.ClassCastException: me.ccrama.MineFortress2.Arena cannot be cast to org.bukkit.plugin.Plugin
        at me.ccrama.MineFortress2.Arena.cloaking(Arena.java:86)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 20 more
    2013-10-26 22:12:04 [SEVERE] Could not pass event PlayerInteractEvent to MineFortress2 v1.1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:190)
        at net.minecraft.server.v1_6_R3.BlockPressurePlateBinary.e(BlockPressurePlateBinary.java:53)
        at net.minecraft.server.v1_6_R3.BlockPressurePlateAbstract.b(BlockPressurePlateAbstract.java:92)
        at net.minecraft.server.v1_6_R3.BlockPressurePlateAbstract.a(BlockPressurePlateAbstract.java:86)
        at net.minecraft.server.v1_6_R3.Entity.D(Entity.java:734)
        at net.minecraft.server.v1_6_R3.Entity.move(Entity.java:677)
        at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:373)
        at net.minecraft.server.v1_6_R3.Packet10Flying.handle(SourceFile:136)
        at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
        at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
        at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
        at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.ClassCastException: me.ccrama.MineFortress2.Arena cannot be cast to org.bukkit.plugin.Plugin
        at me.ccrama.MineFortress2.Arena.cloaking(Arena.java:86)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 20 more
    What could be the problem with my code? Is this the correct method of going about with coutdowns?

    Many thanks,
    ccrama
     
  2. Offline

    alex123099

    From what I see, you have a casting problem:
    "Caused by: java.lang.ClassCastException: me.ccrama.MineFortress2.Arena cannot be cast to org.bukkit.plugin.Plugin
    at me.ccrama.MineFortress2.Arena.cloaking(Arena.java:86)"
    Could you show us this area of code between like lines 80-90 in your Arena class?
     
  3. Offline

    ccrama

    alex123099 that is the first code bit I posted
     
  4. Offline

    ThatBox

    ccrama You need to post the entire class as we don't know which line it is.
     
  5. Offline

    alex123099

    ccrama Could you point me to which line would be line 86?
     
  6. Offline

    ccrama

    ThatBox, alex123099 Here is the class up to the error line (the class is about 5x as large as I'm posting):
    Code:java
    1. package me.ccrama.MineFortress2;
    2.  
    3.  
    4.  
    5. import me.ccrama.MineFortress2.Parteffects.ParticleEffect;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.DyeColor;
    10. import org.bukkit.Location;
    11. import org.bukkit.Material;
    12. import org.bukkit.Sound;
    13. import org.bukkit.World;
    14. import org.bukkit.entity.Item;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.event.EventHandler;
    17. import org.bukkit.event.Listener;
    18. import org.bukkit.event.entity.PlayerDeathEvent;
    19. import org.bukkit.event.player.PlayerInteractEvent;
    20. import org.bukkit.event.player.PlayerMoveEvent;
    21. import org.bukkit.event.player.PlayerPickupItemEvent;
    22. import org.bukkit.event.player.PlayerQuitEvent;
    23. import org.bukkit.inventory.ItemStack;
    24. import org.bukkit.inventory.meta.ItemMeta;
    25. import org.bukkit.material.Wool;
    26. import org.bukkit.plugin.Plugin;
    27. import org.bukkit.potion.PotionEffect;
    28. import org.bukkit.potion.PotionEffectType;
    29. import org.bukkit.scheduler.BukkitRunnable;
    30.  
    31. public class Arena implements Listener {
    32.  
    33. public Arena() {
    34. for(Player player : Bukkit.getOnlinePlayers()){
    35. player.playSound(player.getLocation(), Sound.AMBIENCE_CAVE, 7, 1);
    36. }
    37. System.out.println("starting game");
    38. Location loc2 = new Location( Bukkit.getWorld("world"), -78.9666, 78, -36.67);
    39. ItemStack item = new ItemStack(Material.WOOL, 1, (byte)11);
    40. ItemMeta bluemeta = item.getItemMeta();
    41. bluemeta.setDisplayName(ChatColor.BLUE + "BLUE TEAM INTEL");
    42. item.setItemMeta(bluemeta);
    43. Location loc = new Location(Bukkit.getWorld("world"), 79.776, 78, -72.35);
    44.  
    45. World w = Bukkit.getWorld("world");
    46. w.dropItem(loc, item);
    47. System.out.println("dropped item");
    48.  
    49. ItemStack item2 = new ItemStack(Material.WOOL, 1, (byte)14);
    50. ItemMeta redmeta = item2.getItemMeta();
    51. redmeta.setDisplayName(ChatColor.RED + "RED TEAM INTEL");
    52. item2.setItemMeta(redmeta);
    53. w.dropItem(loc2, item2);
    54. }
    55.  
    56. @EventHandler
    57. public void cloaking(PlayerInteractEvent e){
    58. final Player p = e.getPlayer();
    59. if(p.getItemInHand().getType() == Material.GLOWSTONE_DUST){
    60. p.sendMessage("NOW CLOAKED");
    61.  
    62. for (Player players : Bukkit.getOnlinePlayers())
    63. {
    64. players.hidePlayer(p);
    65.  
    66. }
    67. new BukkitRunnable(){
    68. int timeLeft = 10;
    69.  
    70. @Override
    71. public void run(){
    72. if(timeLeft > 0){
    73. timeLeft -= 1;
    74. int numb = timeLeft;
    75. p.sendMessage(numb + "SECONDS LEFT");
    76. } else {
    77. for (Player players : Bukkit.getOnlinePlayers())
    78. {
    79. players.showPlayer
    80. (p);
    81.  
    82. }
    83. this.cancel();
    84. }
    85. }
    86. }.runTaskTimer((Plugin) this, 0L, 20L);
    87.  
    88.  
    89.  
    90.  
    91. }
    92. }
     
  7. Offline

    alex123099

    ccrama There is your problem -> you are trying to convert an Arena object to a bukkit Plugin object. They have no connection between them and thus it is impossible. What you need to do is pass your class that extends JavaPlugin to the Arena class and use that JavaPlugin extending object instead of (Plugin)this
     
    ccrama likes this.
  8. Offline

    ccrama

    alex123099 So should I turn that event into a whole new class that extends JavaPlugin? Seems kind of overkill, but would it fix my troubles?
     
  9. Offline

    alex123099

    ccrama No no no, just put a parameter in the constructor of the Arena class of the type of your main class which extends the JavaPlugin -> set it to a global variable in the Arena class, and then use it.
     
  10. Offline

    ccrama

    AH, alright. Thanks alex123099

    Works flawlessly alex123099, thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page