Compilation Error (?)

Discussion in 'Plugin Development' started by rohan576, Dec 27, 2013.

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

    rohan576

    I am currently developing a plugin called SimpleSpawners. There is always an error at line 34, for some strange reason. I'm wondering if it's a compilation error. Let me explain:

    First of all, this is what you need to know about the stack-trace.
    Show Spoiler

    Code:java
    1.  
    2. 27.12 19:47:00 [Server] INFO Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R1.block.CraftBlock cannot be cast to org.bukkit.block.CreatureSpawner
    3. 27.12 19:47:00 [Server] INFO at me.Baugh70.simplespawner.listener.PlayerListener.onSpawnerBreak(PlayerListener.java:34) ~[?:?]



    Here's the thing though... this is what's at line 34 of PlayerListener.java:
    Show Spoiler

    Code:java
    1. plugin.getServer().getPluginManager().registerEvents(this, plugin);



    So... yeah. I'm wondering what exactly is going on. Every time I compile a new version of SimpleSpawners, I update the version... and that seems to update correctly, since the server log displays the version specified when compiling.

    To me, this is all very confusing, and some help would be greatly appreciated, so that I can stop running around in circles trying to debug. :)[/CODE]
     
  2. Offline

    Jogy34

    You're trying to cast a Block to a CreatureSpawner.

    It's on line 34 of your PlayerListener class in your onSpawnerBreak() method (which I highly doubt is the line that you posted)
     
  3. Offline

    rohan576

    No, the code I posted is indeed line 34. I am absolutely sure of it. This is why I'm asking for help. I assume that this is a compilation error.
     
  4. Offline

    xTigerRebornx

    rohan576 Post the whole code (In Java syntax, so we get line numbers)
     
  5. Offline

    Bart

    Whatever it is, the error is not pointing to the line of code you posted.
     
  6. Offline

    rohan576

    Code (open)

    Code:java
    1. package me.Baugh70.simplespawner.listener;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6.  
    7. import me.Baugh70.simplespawner.SimpleSpawner;
    8. import me.Baugh70.simplespawner.errormessage.ErrorMessage;
    9.  
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.Material;
    12. import org.bukkit.block.Block;
    13. import org.bukkit.block.BlockState;
    14. import org.bukkit.block.CreatureSpawner;
    15. import org.bukkit.entity.EntityType;
    16. import org.bukkit.entity.Player;
    17. import org.bukkit.event.EventHandler;
    18. import org.bukkit.event.Listener;
    19. import org.bukkit.event.block.BlockBreakEvent;
    20. import org.bukkit.event.block.BlockPlaceEvent;
    21. import org.bukkit.event.inventory.InventoryClickEvent;
    22. import org.bukkit.inventory.AnvilInventory;
    23. import org.bukkit.inventory.Inventory;
    24. import org.bukkit.inventory.InventoryView;
    25. import org.bukkit.inventory.ItemStack;
    26. import org.bukkit.inventory.meta.ItemMeta;
    27.  
    28. public class PlayerListener implements Listener {
    29. ItemStack mobSpawner = new ItemStack(Material.MOB_SPAWNER);
    30. public CreatureSpawner newSpawnerType;
    31. public HashMap<String, Integer> MIDs = new HashMap<String, Integer>();
    32.  
    33. public PlayerListener(SimpleSpawner plugin) {
    34. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    35. MIDs.put("Bat", 65);
    36. MIDs.put("Blaze", 61);
    37. MIDs.put("CaveSpider", 59);
    38. MIDs.put("Chicken", 93);
    39. MIDs.put("Cow", 92);
    40. MIDs.put("Creeper", 50);
    41. MIDs.put("EnderDragon", 63);
    42. MIDs.put("Enderman", 58);
    43. MIDs.put("Ghast", 56);
    44. MIDs.put("Giant", 53);
    45. MIDs.put("Horse", 100);
    46. MIDs.put("IronGolem", 99);
    47. MIDs.put("MagmaCube", 62);
    48. MIDs.put("MushroomCow", 96);
    49. MIDs.put("Ocelot", 98);
    50. MIDs.put("Pig", 90);
    51. MIDs.put("PigZombie", 57);
    52. MIDs.put("Sheep", 91);
    53. MIDs.put("Silverfish", 60);
    54. MIDs.put("Skeleton", 51);
    55. MIDs.put("Slime", 55);
    56. MIDs.put("SnowGolem", 97);
    57. MIDs.put("Spider", 52);
    58. MIDs.put("Squid", 94);
    59. MIDs.put("Villager", 120);
    60. MIDs.put("Witch", 66);
    61. MIDs.put("Wither", 64);
    62. MIDs.put("Wolf", 95);
    63. MIDs.put("Zombie", 54);
    64. }
    65.  
    66. public int getEntityID(String name) {
    67. List<String> names = new ArrayList<>(MIDs.keySet());
    68. List<Integer> id = new ArrayList<>(MIDs.values());
    69.  
    70. int mobID = -1;
    71. for(int i = 0;i<names.size();i++) {
    72. if(names.get(i) == name) {
    73. mobID = id.get(i);
    74. }
    75. }
    76. return mobID;
    77. }
    78.  
    79. @EventHandler
    80. public void onSpawnerBreak(BlockBreakEvent event){
    81. Block block = event.getBlock();
    82.  
    83. if(block.getType() == Material.MOB_SPAWNER) {
    84. Player player = event.getPlayer();
    85. if(player.getInventory().firstEmpty() != -1) {
    86. player.sendMessage("debug1");
    87. BlockState blockState = block.getState();
    88. String spawnerType = "";
    89. if (blockState instanceof CreatureSpawner) {
    90. CreatureSpawner spawner = (CreatureSpawner) blockState;
    91. player.sendMessage("debug2");
    92. spawnerType = spawner.getCreatureTypeName();
    93. }
    94. Inventory inv = player.getInventory();
    95.  
    96. ItemStack ws = new ItemStack(Material.MOB_SPAWNER);
    97. ItemMeta meta = ws.getItemMeta();
    98. meta.setDisplayName(spawnerType + " Spawner");
    99. ws.setItemMeta(meta);
    100. /*
    101. CreatureSpawner newSpawner = (CreatureSpawner) mobSpawner;
    102.  
    103. newSpawner.setCreatureTypeByName(spawnerType);
    104.  
    105. ItemStack itemSpawner = (ItemStack) newSpawner;
    106.  
    107. inv.addItem(itemSpawner);
    108. */
    109. inv.addItem(ws);
    110. event.setExpToDrop(0);
    111.  
    112. player.sendMessage(ChatColor.YELLOW + "SimpleSpawners " + ChatColor.AQUA + "has placed the spawner into your inventory.");
    113. }
    114. else {
    115. ErrorMessage.setMessageVariable(player);
    116. ErrorMessage.sendError("Your inventory is currently full. Spawner breaking aborted.");
    117. event.setCancelled(true);
    118. }
    119. }
    120. }
    121.  
    122. @EventHandler
    123. public void onSpawnerPutDown(BlockPlaceEvent event){
    124. Player player = event.getPlayer();
    125. Block block = event.getBlock();
    126. ItemStack itemSpawner = event.getItemInHand();
    127. Material blockType = block.getType();
    128. if(blockType == Material.MOB_SPAWNER) {
    129. BlockState blockState = block.getState();
    130. if (blockState instanceof CreatureSpawner) {
    131. CreatureSpawner spawner = (CreatureSpawner) blockState;
    132. spawner.setCreatureTypeByName(EntityType.values()[getEntityID(itemSpawner.getItemMeta().getDisplayName().replace(" Spawner", ""))].toString());
    133. player.sendMessage(ChatColor.YELLOW + "SimpleSpawners " + ChatColor.AQUA + "protects your spawner! Upon breaking the spawner, it will be placed into your inventory.");
    134. }
    135. }
    136.  
    137. }
    138.  
    139. @EventHandler
    140. public void onRename(InventoryClickEvent e){
    141. if(!e.isCancelled()){
    142. Player player = (Player)e.getWhoClicked();
    143. Inventory inv = e.getInventory();
    144. if(inv instanceof AnvilInventory){
    145. InventoryView view = e.getView();
    146. int rawSlot = e.getRawSlot();
    147. if(rawSlot == view.convertSlot(rawSlot)){
    148. if(rawSlot == 2){
    149. ItemStack item = e.getCurrentItem();
    150. if(item.getType()==Material.MOB_SPAWNER){
    151. e.setCancelled(true);
    152. player.sendMessage(ChatColor.RED+"You may not rename spawners");
    153. }
    154. }
    155. }
    156. }
    157. }
    158. }
    159. }
    160.  




    I agree. That's why this thread is about a compilation issue. Not an error about me reading line numbers incorrectly. I can assure you that I am sane and confident in myself. ;)
     
  7. Offline

    tommycake50

    Show Spoiler


    It's not a compilation error it's a runtime error.
     
  8. Offline

    rohan576

    How would I fix such a thing?...
     
  9. Offline

    Sagacious_Zed Bukkit Docs

    You fix the logic in the code.
     
    tommycake50 likes this.
  10. Offline

    tommycake50

    That's just simply not how compilers work.
    They take exactly what you write and compile it into java bytecode.
    It is the code, the stack trace says it's in the method onSpawnerBreak, it got the line number wrong however.
    It appears to be this line:
    Code:java
    1. BlockState blockState = block.getState();

    Which is odd since it makes perfect sense but logically from the stacktrace it's the only one that makes sense.
    This is a very confusing issue though.
     
Thread Status:
Not open for further replies.

Share This Page