Solved ClassCastException when using timers separate class

Discussion in 'Plugin Development' started by Colby l, Oct 14, 2013.

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

    Colby l

    I'm trying to rewrite my code to follow 'proper' Java coding practice, and when transferring the code through classes (no errors in eclipse), I used the command to start flying, which starts a scheduler, and received a ClassCastException. The error and code snippets are below. (luckily, I know exactly where it is XD)

    Error:
    Code:
    2013-10-14 21:06:40 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'fly' in plugin Fly Payment v2.3
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:192)
        at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServer.java:523)
        at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerConnection.java:959)
        at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.java:877)
        at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:834)
        at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
        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: com.ahellhound.bukkit.flypayment.Scheduler cannot be cast to org.bukkit.plugin.Plugin
        at com.ahellhound.bukkit.flypayment.Scheduler.enableTimerTier(Scheduler.java:36)
        at com.ahellhound.bukkit.flypayment.Main.onCommand(Main.java:259)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    Main class where code is executed:
    Code:java
    1. //Starts timer
    2. Scheduler scheduler = new Scheduler();
    3. scheduler.enableTimerTier(p, tier);


    Scheduler class:
    Code:java
    1. package com.ahellhound.bukkit.flypayment;
    2.  
    3. import java.util.HashMap;
    4. import java.util.HashSet;
    5. import java.util.Map;
    6.  
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.Event;
    10. import org.bukkit.event.entity.EntityDamageEvent;
    11. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    12. import org.bukkit.event.entity.EntityEvent;
    13. import org.bukkit.plugin.Plugin;
    14.  
    15. public class Scheduler {
    16.  
    17. // Hashmap of schedualr ID's
    18. private Map<Player, Integer> schedulerID = new HashMap<Player, Integer>();
    19. // Hasmap of player's with protect flight
    20. private Map<Player, Integer> protectFlightv = new HashMap<Player, Integer>();
    21. // Hasmap of players safe from fall damage
    22. private HashSet<String> safePlayers = new HashSet<String>();
    23.  
    24. public void enableTimerTier(final Player p, int tier) {
    25.  
    26. // Configuration class object
    27. Configuration objConfig = new Configuration();
    28. long timer = objConfig.getTimerAmount(tier);
    29. // get's Main Class instance
    30. final Flight objFlight = new Flight();
    31. // Time left arithmetic
    32. double timeform = (timer / 20) / 60;
    33.  
    34. final Player player = p;
    35. schedulerID.put(p, Main.getInstance().getServer().getScheduler()
    36. .scheduleSyncDelayedTask((Plugin) this, new Runnable() {
    37. public void run() {
    38. Main.getInstance().disableFlight(player);
    39. }
    40. }, timer));
    41.  
    42. p.sendMessage(ChatColor.GREEN + "You Have " + timeform
    43. + " Minutes until your flight is disabled.");
    44. if (timer > 200) {
    45. protectFlightv.put(
    46. p,
    47. Main.getInstance()
    48. .getServer()
    49. .getScheduler()
    50. .scheduleSyncDelayedTask((Plugin) this,
    51. new Runnable() {
    52. public void run() {
    53. objFlight.protectFlight(p);
    54. }
    55. }, timer - 200));
    56. }
    57.  
    58. }
    59.  
    60. public void removeFlightTimer(Player p) {
    61.  
    62. if (schedulerID.containsKey(p)) {
    63. Main.getInstance().getServer().getScheduler()
    64. .cancelTask(schedulerID.get(p));
    65. schedulerID.remove(p);
    66. }
    67. if (safePlayers.contains(p.getName())) {
    68. safePlayers.remove(p.getName());
    69. }
    70. if (protectFlightv.containsKey(p)) {
    71. Main.getInstance().getServer().getScheduler()
    72. .cancelTask(protectFlightv.get(p));
    73. protectFlightv.remove(p);
    74. }
    75. }
    76.  
    77. public void fallDamageRemoveTest(Event event) {
    78.  
    79. if (((EntityEvent) event).getEntity() instanceof Player) {
    80. Player p = ((Player) ((EntityEvent) event).getEntity());
    81. if (((EntityDamageEvent) event).getCause() == DamageCause.FALL && safePlayers.contains(((Player) ((EntityEvent) event).getEntity()).getName()) && (!p.hasPermission("flyp.takeDamage"))) {
    82.  
    83.  
    84. ((EntityDamageEvent) event).setCancelled(true);
    85. safePlayers.remove(((Player) ((EntityEvent) event).getEntity()).getName());
    86.  
    87. }
    88. }
    89.  
    90. }
    91.  
    92. public void addSafePlayer(Player p){
    93.  
    94. safePlayers.add(p.getName());
    95.  
    96. }
    97.  
    98. public void cancelFlightTimer(Player p) {
    99. if (schedulerID.containsKey(p)) {
    100. Main.getInstance().getServer().getScheduler().cancelTask(schedulerID.get(p));
    101. schedulerID.remove(p);
    102. }
    103. if (protectFlightv.containsKey(p)) {
    104. Main.getInstance().getServer().getScheduler().cancelTask(protectFlightv.get(p));
    105. protectFlightv.remove(p);
    106. }
    107. }
    108.  
    109. public void clearSafePlayers(Player p){
    110.  
    111. safePlayers.clear();
    112. }
    113.  
    114. public void removeProtectFlight(Player p){
    115. protectFlightv.remove(p);
    116.  
    117. }
    118.  
    119. }
    120.  


    Sorry there's a ton of info, but this is my first time working with many classes, and first time using events and timers in a class other then Main (I know, i'm a noob XD )

    Thanks for the help and replies!

    Pasted wrong 'Main class' snippet, fixed now.

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

    MrSparkzz

    Colby l
    It doesn't like when you cast Player to the runnable
    Code:java
    1.  
    2. .scheduleSyncDelayedTask((Plugin) this, new Runnable() {
    3.  

    What you're actually doing here is since you're getting a new Runnable() it's using this as the runnable class
    So you'd need to change it to this
    Code:java
    1.  
    2. .scheduleSyncDelayedTask(new Main(), new Runnable() {
    3.  
     
  3. Offline

    NoChanceSD

    Code:
    schedulerID.put(p, Main.getInstance().getServer().getScheduler()
    .scheduleSyncDelayedTask((Plugin) this, new Runnable() {
    In line 35/36 you are casting your Scheduler class to a plugin, replace "this" by an instance of you main class, the one that extends JavaPlugin.
     
    Colby l likes this.
  4. Offline

    MrSparkzz

    Like what I said ^^ ;)
     
  5. Offline

    NoChanceSD

    MrSparkzz Well, your message wasn't there when i posted mine :)
     
  6. Offline

    MrSparkzz

    NoChanceSD
    That's obviously because I'm a ninja :D
     
  7. Offline

    Colby l

    Changing 'this' to main instance worked, thanks!
     
Thread Status:
Not open for further replies.

Share This Page