My code works but gives out some errors..

Discussion in 'Plugin Development' started by Zigonja123, Dec 25, 2013.

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

    Zigonja123

    So, this is the code..

    Code:java
    1. package me.Zigonja.magicwand;
    2.  
    3. import com.google.common.collect.HashBasedTable;
    4. import com.google.common.collect.Table;
    5. import java.lang.reflect.Field;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Color;
    8. import org.bukkit.FireworkEffect;
    9. import org.bukkit.Location;
    10. import org.bukkit.Material;
    11. import org.bukkit.block.Block;
    12. import org.bukkit.block.BlockFace;
    13. import org.bukkit.command.Command;
    14. import org.bukkit.command.CommandSender;
    15. import org.bukkit.entity.Damageable;
    16. import org.bukkit.entity.Entity;
    17. import org.bukkit.entity.Firework;
    18. import org.bukkit.entity.LivingEntity;
    19. import org.bukkit.entity.Player;
    20. import org.bukkit.entity.Snowball;
    21. import org.bukkit.event.EventHandler;
    22. import org.bukkit.event.EventPriority;
    23. import org.bukkit.event.Listener;
    24. import org.bukkit.event.block.Action;
    25. import org.bukkit.event.block.BlockBreakEvent;
    26. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    27. import org.bukkit.event.entity.ProjectileHitEvent;
    28. import org.bukkit.event.player.PlayerInteractEvent;
    29. import org.bukkit.inventory.ItemStack;
    30. import org.bukkit.inventory.meta.FireworkMeta;
    31. import org.bukkit.inventory.meta.ItemMeta;
    32. import org.bukkit.plugin.java.JavaPlugin;
    33. import org.bukkit.potion.PotionEffect;
    34. import org.bukkit.potion.PotionEffectType;
    35. import org.bukkit.util.Vector;
    36.  
    37. public class MagicWand extends JavaPlugin
    38. implements Listener
    39. {
    40. public void onEnable()
    41. {
    42. getServer().getPluginManager().registerEvents(this, this);
    43. }
    44.  
    45. public void onDisable()
    46. {
    47. }
    48.  
    49. @EventHandler(priority=EventPriority.HIGHEST)
    50. public void onInteraction(PlayerInteractEvent e)
    51. {
    52. Player p = e.getPlayer();
    53. if (((e.getAction() == Action.LEFT_CLICK_AIR) || (e.getAction() == Action.LEFT_CLICK_BLOCK)) &&
    54. (p.getItemInHand().getType().equals(Material.BLAZE_ROD))) {
    55. ItemStack i = p.getItemInHand();
    56. ItemMeta im = i.getItemMeta();
    57. if (p.hasPermission(new Permissions().canUseRod)) {
    58. if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.GREEN + " Heal")) {
    59. if (Cooldowns.tryCooldown(p, "Heal", 60000L)) {
    60. p.launchProjectile(Snowball.class);
    61. }
    62. else {
    63. p.sendMessage(ChatColor.RED + "Heal is on CoolDown! " + Cooldowns.getCooldown(p, "Heal") / 1000L + " Seconds left.");
    64. }
    65. }
    66. if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.AQUA + " Freeze")) {
    67. if (Cooldowns.tryCooldown(p, "Freeze", 120000L)) {
    68. p.launchProjectile(Snowball.class);
    69. }
    70. else {
    71. p.sendMessage(ChatColor.RED + "Freeze is on CoolDown! " + Cooldowns.getCooldown(p, "Freeze") / 1000L + " Seconds left.");
    72. }
    73. }
    74. if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.GOLD + " KnockBack")) {
    75. if (Cooldowns.tryCooldown(p, "Knockback", 120000L)) {
    76. p.launchProjectile(Snowball.class);
    77. }
    78. else {
    79. p.sendMessage(ChatColor.RED + "Knockback is on CoolDown! " + Cooldowns.getCooldown(p, "Knockback") / 1000L + " Seconds left.");
    80. }
    81. }
    82. if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.LIGHT_PURPLE + " Buff")) {
    83. if (Cooldowns.tryCooldown(p, "Buff", 120000L)) {
    84. p.launchProjectile(Snowball.class);
    85. }
    86. else {
    87. p.sendMessage(ChatColor.RED + "Buff is on CoolDown! " + Cooldowns.getCooldown(p, "Buff") / 1000L + " Seconds left.");
    88. }
    89. }
    90. if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.DARK_GREEN + " Poison"))
    91. if (Cooldowns.tryCooldown(p, "Poison", 180000L)) {
    92. p.launchProjectile(Snowball.class);
    93. }
    94. else{
    95. p.sendMessage(ChatColor.RED + "Poison is on CoolDown! " + Cooldowns.getCooldown(p, "Poison") / 1000L + " Seconds left.");
    96. }
    97. if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.RED + " Basic Attack"))
    98. if (Cooldowns.tryCooldown(p, "Basic", 20000L)) {
    99. p.launchProjectile(Snowball.class);
    100. }
    101. else{
    102. p.sendMessage(ChatColor.RED + "Basic Attack is on CoolDown! " + Cooldowns.getCooldown(p, "Basic") / 1000L + " Seconds left.");
    103. }
    104. }
    105. else
    106. {
    107. p.sendMessage(ChatColor.RED + "Your power level isn't high enough!");
    108. p.sendMessage(ChatColor.RED + "[Legendary donator perk!]");
    109. }
    110. }
    111. }
    112. @EventHandler(priority=EventPriority.HIGHEST)
    113. public void SpellChange(PlayerInteractEvent e) {
    114. Player p = e.getPlayer();
    115. if (((e.getAction() == Action.RIGHT_CLICK_AIR) || (e.getAction() == Action.RIGHT_CLICK_BLOCK)) &&
    116. (p.getItemInHand().getType().equals(Material.BLAZE_ROD))) {
    117. ItemStack i = p.getItemInHand();
    118. ItemMeta im = i.getItemMeta();
    119. if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.WHITE + " None")) {
    120. im.setDisplayName(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.GREEN + " Heal");
    121. i.setItemMeta(im);
    122. }
    123. else if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.GREEN + " Heal")) {
    124. im.setDisplayName(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.LIGHT_PURPLE + " Buff");
    125. i.setItemMeta(im);
    126. }
    127. else if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.LIGHT_PURPLE + " Buff")) {
    128. im.setDisplayName(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.GOLD + " KnockBack");
    129. i.setItemMeta(im);
    130. }
    131. else if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.GOLD + " KnockBack")) {
    132. im.setDisplayName(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.AQUA + " Freeze");
    133. i.setItemMeta(im);
    134. }
    135. else if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.AQUA + " Freeze")) {
    136. im.setDisplayName(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.DARK_GREEN + " Poison");
    137. i.setItemMeta(im);
    138. }
    139. else if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.DARK_GREEN + " Poison")) {
    140. im.setDisplayName(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.RED + " Basic Attack");
    141. i.setItemMeta(im);
    142. }
    143. else if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.RED + " Basic Attack")) {
    144. im.setDisplayName(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.WHITE + " None");
    145. i.setItemMeta(im);
    146. }
    147. }
    148. }
    149.  
    150. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    151. {
    152. Player p = (Player)sender;
    153. if (p.hasPermission(new Permissions().canUseRod)) {
    154. if (commandLabel.equalsIgnoreCase("MagicWand")) {
    155. p.sendMessage(ChatColor.GOLD + "Magic Wand Given!");
    156. ItemStack item = new ItemStack(Material.BLAZE_ROD);
    157. ItemMeta itemmeta = item.getItemMeta();
    158. itemmeta.setDisplayName(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.WHITE + " None");
    159. item.setItemMeta(itemmeta);
    160. p.getInventory().addItem(new ItemStack[] { item });
    161. }
    162. } else {
    163. p.sendMessage(ChatColor.RED + "Your power level isn't high enough!");
    164. p.sendMessage(ChatColor.RED + "[Legendary donator perk!]");
    165. }
    166. return false;
    167. }
    168.  
    169. @EventHandler(priority=EventPriority.HIGHEST)
    170. public void EntityDamageByEntity(EntityDamageByEntityEvent event)
    171. {
    172. Entity entity = event.getDamager();
    173. if ((entity instanceof Snowball)) {
    174. Snowball snowball = (Snowball)event.getDamager();
    175. Player thrower = (Player)snowball.getShooter();
    176. Player PlayerHit = (Player)event.getEntity();
    177. if ((PlayerHit instanceof Player))
    178. {
    179. if ((thrower instanceof Player))
    180. {
    181. if (thrower.hasPermission(new Permissions().canUseRod)) {
    182. ItemStack i = thrower.getItemInHand();
    183. ItemMeta im = i.getItemMeta();
    184. if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.RED + " Basic Attack")) {
    185. thrower.sendMessage(ChatColor.GREEN + "** You have damaged " + PlayerHit.getDisplayName());
    186. PlayerHit.sendMessage(ChatColor.GREEN + "** You have been damaged by " + thrower.getDisplayName());
    187.  
    188. Player player = PlayerHit;
    189.  
    190. Damageable damag = player;
    191. double health = damag.getHealth();
    192. PlayerHit.setHealth(health - 6.0D);
    193. PlayerHit.setFireTicks(0);
    194. }
    195. if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.GREEN + " Heal")) {
    196. thrower.sendMessage(ChatColor.GREEN + "** You have healed " + PlayerHit.getDisplayName());
    197. PlayerHit.sendMessage(ChatColor.GREEN + "** You have been healed by " + thrower.getDisplayName());
    198.  
    199. Location loc = PlayerHit.getLocation();
    200.  
    201. Vector vec = new Vector(loc.getX() - loc.getX(), 0.0D, loc.getZ() - loc.getX());
    202. PlayerHit.setVelocity(vec.normalize().setY(0));
    203. PlayerHit.setVelocity(vec.normalize().setX(0));
    204. PlayerHit.setVelocity(vec.normalize().setZ(0));
    205. event.setCancelled(true);
    206.  
    207. Player player = PlayerHit;
    208.  
    209. PlayerHit.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 0, 0));
    210. PlayerHit.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 0, 0));
    211. PlayerHit.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 0, 0));
    212. PlayerHit.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 0, 0));
    213. PlayerHit.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 0, 0));
    214.  
    215. Damageable damag = player;
    216. double health = damag.getHealth();
    217. if (health >= 12.0D) {
    218. PlayerHit.setHealth(20.0D);
    219. PlayerHit.setFireTicks(0);
    220. player.sendMessage(ChatColor.GREEN + "Your Health Is now Full");
    221. }
    222. else {
    223. PlayerHit.setHealth(health + 8.0D);
    224. PlayerHit.setFireTicks(0);
    225. return;
    226. }
    227. }
    228. if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.AQUA + " Freeze")) {
    229. thrower.sendMessage(ChatColor.AQUA + "** You have frozen " + PlayerHit.getDisplayName());
    230. PlayerHit.sendMessage(ChatColor.AQUA + "** You have been frozen by " + thrower.getDisplayName());
    231.  
    232. Block block = entity.getLocation().getBlock();
    233. Block top = block.getRelative(BlockFace.UP);
    234. Block below = block.getRelative(BlockFace.DOWN);
    235.  
    236. Location loc = block.getLocation();
    237.  
    238. Vector vec = new Vector(loc.getX() - loc.getX(), 0.0D, loc.getZ() - loc.getX());
    239. PlayerHit.setVelocity(vec.normalize().setY(0));
    240. PlayerHit.setVelocity(vec.normalize().setX(0));
    241. PlayerHit.setVelocity(vec.normalize().setZ(0));
    242. event.setCancelled(true);
    243.  
    244. below.setType(Material.ICE);
    245. block.setType(Material.ICE);
    246. top.setType(Material.ICE);
    247.  
    248. loc.setX(loc.getBlockX() + 0.5D);
    249. loc.setY(loc.getBlockY());
    250. loc.setZ(loc.getBlockZ() + 0.5D);
    251. PlayerHit.teleport(loc);
    252.  
    253. PlayerHit.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 200, 25));
    254. PlayerHit.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 200, 25));
    255. PlayerHit.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 200, 25));
    256. PlayerHit.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 200, -5));
    257. }
    258.  
    259. if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.GOLD + " KnockBack")) {
    260. thrower.sendMessage(ChatColor.GOLD + "** You have Knocked back " + PlayerHit.getDisplayName());
    261. PlayerHit.sendMessage(ChatColor.GOLD + "** You have been knocked back by " + thrower.getDisplayName());
    262.  
    263. PlayerHit.setVelocity(thrower.getLocation().getDirection().multiply(10));
    264. }
    265.  
    266. if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.LIGHT_PURPLE + " Buff")) {
    267. thrower.sendMessage(ChatColor.LIGHT_PURPLE + "** You have Buffed " + PlayerHit.getDisplayName());
    268. PlayerHit.sendMessage(ChatColor.LIGHT_PURPLE + "** You have been Buffed by " + thrower.getDisplayName());
    269.  
    270. Location loc = PlayerHit.getLocation();
    271.  
    272. Vector vec = new Vector(loc.getX() - loc.getX(), 0.0D, loc.getZ() - loc.getX());
    273. PlayerHit.setVelocity(vec.normalize().setY(0));
    274. PlayerHit.setVelocity(vec.normalize().setX(0));
    275. PlayerHit.setVelocity(vec.normalize().setZ(0));
    276. event.setCancelled(true);
    277.  
    278. PlayerHit.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 300, 3));
    279. PlayerHit.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 400, 3));
    280. PlayerHit.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 400, 3));
    281. }
    282.  
    283. if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.DARK_GREEN + " Poison")) {
    284. thrower.sendMessage(ChatColor.DARK_GREEN + "** You have Poisoned " + PlayerHit.getDisplayName());
    285. PlayerHit.sendMessage(ChatColor.DARK_GREEN + "** You have been Poisoned by " + thrower.getDisplayName());
    286.  
    287. Location loc = PlayerHit.getLocation();
    288.  
    289. Vector vec = new Vector(loc.getX() - loc.getX(), 0.0D, loc.getZ() - loc.getX());
    290. PlayerHit.setVelocity(vec.normalize().setY(0));
    291. PlayerHit.setVelocity(vec.normalize().setX(0));
    292. PlayerHit.setVelocity(vec.normalize().setZ(0));
    293. event.setCancelled(true);
    294.  
    295. PlayerHit.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 300, 1));
    296. PlayerHit.addPotionEffect(new PotionEffect(PotionEffectType.HARM, 1, 1));
    297. PlayerHit.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 800, 1));
    298. }
    299. }
    300. }
    301. }
    302. }
    303. }
    304.  
    305. @EventHandler(priority=EventPriority.MONITOR, ignoreCancelled=true)
    306. public void onBreakIce(BlockBreakEvent event)
    307. {
    308. Block block = event.getBlock();
    309. if (block.getType() == Material.ICE)
    310. for (Entity entity : block.getChunk().getEntities())
    311. if ((entity instanceof LivingEntity))
    312. {
    313. LivingEntity livingEntity = (LivingEntity)entity;
    314. Block entityBlock = livingEntity.getLocation().getBlock();
    315. Block entityAbove = entityBlock.getRelative(BlockFace.UP);
    316. Block entityBelow = entityBlock.getRelative(BlockFace.DOWN);
    317. if ((entityBlock.equals(block)) || (entityAbove.equals(block)) || (entityBelow.equals(block)))
    318. {
    319. event.setCancelled(true);
    320. if (entityBlock.getType() == Material.ICE) {
    321. entityBlock.setType(Material.AIR);
    322. entityAbove.setType(Material.AIR);
    323. entityBelow.setType(Material.AIR);
    324. }
    325. if (entityAbove.getType() == Material.ICE) {
    326. entityAbove.setType(Material.AIR);
    327. entityBlock.setType(Material.AIR);
    328. entityBelow.setType(Material.AIR);
    329. }
    330. if (entityBelow.getType() == Material.ICE) {
    331. entityBlock.setType(Material.AIR);
    332. entityAbove.setType(Material.AIR);
    333. entityBelow.setType(Material.AIR);
    334. }
    335. return;
    336. }
    337. }
    338. }
    339.  
    340. @EventHandler
    341. public void onProjectileHit(ProjectileHitEvent e)
    342. {
    343. Snowball snow = (Snowball)e.getEntity();
    344. if ((snow instanceof Snowball)) {
    345. Player thrower = (Player)snow.getShooter();
    346. if ((thrower instanceof Player)) {
    347. ItemStack i = thrower.getItemInHand();
    348. ItemMeta im = i.getItemMeta();
    349.  
    350. if (((e.getEntity() instanceof Snowball)) &&
    351. (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.GREEN + " Heal"))) {
    352. Firework fw = (Firework)snow.getWorld().spawn(
    353. snow.getLocation(), Firework.class);
    354. FireworkEffect effect = FireworkEffect.builder()
    355. .withColor(Color.GREEN)
    356. .with(FireworkEffect.Type.BALL)
    357. .build();
    358. FireworkMeta fwm = fw.getFireworkMeta();
    359. fwm.clearEffects();
    360. fwm.addEffect(effect);
    361. try
    362. {
    363. Field f = fwm.getClass().getDeclaredField("power");
    364. f.setAccessible(true);
    365. f.set(fwm, Integer.valueOf(-2));
    366. } catch (SecurityException e1) {
    367. e1.printStackTrace();
    368. } catch (NoSuchFieldException e1) {
    369. e1.printStackTrace();
    370. } catch (IllegalArgumentException e1) {
    371. e1.printStackTrace();
    372. } catch (IllegalAccessException e1) {
    373. e1.printStackTrace();
    374. }
    375.  
    376. fw.setFireworkMeta(fwm);
    377. }
    378.  
    379. if ((e.getEntity() instanceof Snowball)) {
    380. if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.AQUA + " Freeze")) {
    381. Firework fw = (Firework)snow.getWorld().spawn(
    382. snow.getLocation(), Firework.class);
    383. FireworkEffect effect = FireworkEffect.builder()
    384. .withColor(Color.AQUA)
    385. .with(FireworkEffect.Type.BALL)
    386. .build();
    387. FireworkMeta fwm = fw.getFireworkMeta();
    388. fwm.clearEffects();
    389. fwm.addEffect(effect);
    390. try
    391. {
    392. Field f = fwm.getClass().getDeclaredField("power");
    393. f.setAccessible(true);
    394. f.set(fwm, Integer.valueOf(-2));
    395. } catch (SecurityException e1) {
    396. e1.printStackTrace();
    397. } catch (NoSuchFieldException e1) {
    398. e1.printStackTrace();
    399. } catch (IllegalArgumentException e1) {
    400. e1.printStackTrace();
    401. } catch (IllegalAccessException e1) {
    402. e1.printStackTrace();
    403. }
    404.  
    405. fw.setFireworkMeta(fwm);
    406. }
    407. if (((e.getEntity() instanceof Snowball)) &&
    408. (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.GOLD + " KnockBack"))) {
    409. Firework fw = (Firework)snow.getWorld().spawn(
    410. snow.getLocation(), Firework.class);
    411. FireworkEffect effect = FireworkEffect.builder()
    412. .withColor(Color.ORANGE)
    413. .with(FireworkEffect.Type.BALL)
    414. .build();
    415. FireworkMeta fwm = fw.getFireworkMeta();
    416. fwm.clearEffects();
    417. fwm.addEffect(effect);
    418. try
    419. {
    420. Field f = fwm.getClass().getDeclaredField("power");
    421. f.setAccessible(true);
    422. f.set(fwm, Integer.valueOf(-2));
    423. } catch (SecurityException e1) {
    424. e1.printStackTrace();
    425. } catch (NoSuchFieldException e1) {
    426. e1.printStackTrace();
    427. } catch (IllegalArgumentException e1) {
    428. e1.printStackTrace();
    429. } catch (IllegalAccessException e1) {
    430. e1.printStackTrace();
    431. }
    432.  
    433. fw.setFireworkMeta(fwm);
    434. }
    435.  
    436. if (((e.getEntity() instanceof Snowball)) &&
    437. (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.LIGHT_PURPLE + " Buff"))) {
    438. Firework fw = (Firework)snow.getWorld().spawn(
    439. snow.getLocation(), Firework.class);
    440. FireworkEffect effect = FireworkEffect.builder()
    441. .withColor(Color.FUCHSIA)
    442. .with(FireworkEffect.Type.BALL)
    443. .build();
    444. FireworkMeta fwm = fw.getFireworkMeta();
    445. fwm.clearEffects();
    446. fwm.addEffect(effect);
    447. try
    448. {
    449. Field f = fwm.getClass().getDeclaredField("power");
    450. f.setAccessible(true);
    451. f.set(fwm, Integer.valueOf(-2));
    452. } catch (SecurityException e1) {
    453. e1.printStackTrace();
    454. } catch (NoSuchFieldException e1) {
    455. e1.printStackTrace();
    456. } catch (IllegalArgumentException e1) {
    457. e1.printStackTrace();
    458. } catch (IllegalAccessException e1) {
    459. e1.printStackTrace();
    460. }
    461.  
    462. fw.setFireworkMeta(fwm);
    463. }
    464.  
    465. if (((e.getEntity() instanceof Snowball)) &&
    466. (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.DARK_GREEN + " Poison"))) {
    467. Firework fw = (Firework)snow.getWorld().spawn(
    468. snow.getLocation(), Firework.class);
    469. FireworkEffect effect = FireworkEffect.builder()
    470. .withColor(Color.OLIVE)
    471. .with(FireworkEffect.Type.BALL)
    472. .build();
    473. FireworkMeta fwm = fw.getFireworkMeta();
    474. fwm.clearEffects();
    475. fwm.addEffect(effect);
    476. try
    477. {
    478. Field f = fwm.getClass().getDeclaredField("power");
    479. f.setAccessible(true);
    480. f.set(fwm, Integer.valueOf(-2));
    481. } catch (SecurityException e1) {
    482. e1.printStackTrace();
    483. } catch (NoSuchFieldException e1) {
    484. e1.printStackTrace();
    485. } catch (IllegalArgumentException e1) {
    486. e1.printStackTrace();
    487. } catch (IllegalAccessException e1) {
    488. e1.printStackTrace();
    489. }
    490.  
    491. fw.setFireworkMeta(fwm);
    492. }
    493. if (((e.getEntity() instanceof Snowball)) &&
    494. (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.RED + " Basic Attack"))) {
    495. Firework fw = (Firework)snow.getWorld().spawn(
    496. snow.getLocation(), Firework.class);
    497. FireworkEffect effect = FireworkEffect.builder()
    498. .withColor(Color.RED)
    499. .with(FireworkEffect.Type.BALL)
    500. .build();
    501. FireworkMeta fwm = fw.getFireworkMeta();
    502. fwm.clearEffects();
    503. fwm.addEffect(effect);
    504. try
    505. {
    506. Field f = fwm.getClass().getDeclaredField("power");
    507. f.setAccessible(true);
    508. f.set(fwm, Integer.valueOf(-2));
    509. } catch (SecurityException e1) {
    510. e1.printStackTrace();
    511. } catch (NoSuchFieldException e1) {
    512. e1.printStackTrace();
    513. } catch (IllegalArgumentException e1) {
    514. e1.printStackTrace();
    515. } catch (IllegalAccessException e1) {
    516. e1.printStackTrace();
    517. }
    518.  
    519. fw.setFireworkMeta(fwm);
    520. }
    521. }
    522. }
    523. }
    524. }
    525.  
    526. public static class Cooldowns
    527. {
    528. private static Table<String, String, Long> cooldowns = HashBasedTable.create();
    529.  
    530. public static long getCooldown(Player player, String key) {
    531. return calculateRemainder((Long)cooldowns.get(player.getName(), key));
    532. }
    533.  
    534. public static long setCooldown(Player player, String key, long delay) {
    535. return calculateRemainder(
    536. (Long)cooldowns.put(player.getName(), key, Long.valueOf(System.currentTimeMillis() + delay)));
    537. }
    538.  
    539. public static boolean tryCooldown(Player player, String key, long delay) {
    540. if (getCooldown(player, key) <= 0L) {
    541. setCooldown(player, key, delay);
    542. return true;
    543. }
    544. return false;
    545. }
    546.  
    547. private static long calculateRemainder(Long expireTime) {
    548. return expireTime != null ? expireTime.longValue() - System.currentTimeMillis() : -9223372036854775808L;
    549. }
    550. }
    551. }


    Erros occour when i throw a normal snowball or when I left click with a normal Blaze Rod
     
  2. Offline

    AoH_Ruthless

  3. Offline

    Zigonja123

    AoH_Ruthless here it is

    Code:
    2013-12-22 18:57:08 [SEVERE] Could not pass event ProjectileHitEvent to MagicWand v1.0
    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.TimedRegisteredListener.callEvent(TimedRegisteredListener.java:30)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:478)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:463)
        at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callProjectileHitEvent(CraftEventFactory.java:650)
        at net.minecraft.server.v1_6_R3.EntityProjectile.l_(EntityProjectile.java:159)
        at net.minecraft.server.v1_6_R3.World.entityJoinedWorld(World.java:1492)
        at net.minecraft.server.v1_6_R3.World.playerJoinedWorld(World.java:1467)
        at net.minecraft.server.v1_6_R3.World.tickEntities(World.java:1336)
        at net.minecraft.server.v1_6_R3.WorldServer.tickEntities(WorldServer.java:517)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:583)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:251)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:493)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:425)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_6_R3.entity.CraftSnowball cannot be cast to org.bukkit.entity.Player
        at me.Zigonja.magicwand.MagicWand.onProjectileHit(MagicWand.java:85)
        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)
        ... 15 more
    EDIT: Could I somehow make it that it catches the error

    Help me please, I really need help!

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

    Gater12

    Zigonja123 It says clearly, you are trying to cast a snowball to a player, which Java complains "YOU CAN'T CAST A SNOWBALL TO A PLAYER".
     
  5. Offline

    Zigonja123

    @Gater12 Uhm, I can't really see that I can do to fix it could you help me please?
     
  6. Offline

    AoH_Ruthless

    Zigonja123
    Well you can catch the error by using a try / catch block and upon catching the exception, you can print the stack trace or send the player a message saying something didn't work.

    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_6_R3.entity.CraftSnowball cannot be cast to org.bukkit.entity.Player
        at me.Zigonja.magicwand.MagicWand.onProjectileHit(MagicWand.java:85)
    A ClassCastException occurred at line 85 (which is weird because that is a closing bracket in the code you posted). This means you are casting incompatible objects (i.e casting integer to a string).
    At line 54, change
    Code:java
    1. (p.getItemInHand().getType().equals(Material.BLAZE_ROD)))

    to:
    Code:java
    1. (p.getItemInHand().getType() == Material.BLAZE_ROD))


    Also, why are you importing things from google?

    Code:java
    1. import com.google.common.collect.HashBasedTable;
    2. import com.google.common.collect.Table;
    3.  


    Lastly, don't bump your post more than once every 24 hours.


    At
     
  7. Offline

    Conarnar

    What's line 85?
     
  8. Offline

    Zigonja123

    @AoH_Ruthless Those google things are for cooldowns

    Conarnar line 85 is closing bracket

    It's still giving out errors :(
     
  9. Offline

    Conarnar

    Zigonja123 That can't be right because line 85 is in the method onInteraction and the error says it is in onProjectileHit.
     
  10. Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_6_R3.entity.CraftSnowball cannot be cast to org.bukkit.entity.Player
    at me.Zigonja.magicwand.MagicWand.onProjectileHit(MagicWand.java:85)

    Learn to read stacktraces, they tell you exactly what happened
     
  11. Offline

    Zigonja123

    @Conarnar Sorry I've failed somehow :eek: here it is
    Code:java
    1. if (im.getDisplayName().equalsIgnoreCase(ChatColor.GRAY.toString() + ChatColor.BOLD + "Spell Selected: " + ChatColor.LIGHT_PURPLE + " Buff")) {
    2. /*LINE 85*/ if (Cooldowns.tryCooldown(p, "Buff", 120000L)) {
    3. p.launchProjectile(Snowball.class);
    4. }


    naithantu Aight, im new to java, this is my first bigger plugin acctually, but I don't know how to fix that cannot be cast to org.bukkit.entity.Player

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  12. Zigonja123 That can't be right.. :3
    at me.Zigonja.magicwand.MagicWand.onProjectileHit(MagicWand.java:85)
    The error is caused in onProjectileHit, while the code you copied is from onInteraction.
    The code you're looking at is probably different from the code that causes the error, recompile the plugin and then cause the error again and post the new stacktrace. The line numbers in the current stacktrace don't match the code you posted

    Edit:
    Code:java
    1. @EventHandler
    2. public void onProjectileHit(ProjectileHitEvent e)
    3. {
    4. Snowball snow = (Snowball)e.getEntity();
    5. if ((snow instanceof Snowball)) {
    6. Player thrower = (Player)snow.getShooter();
    7. if ((thrower instanceof Player)) {


    This code is wrong, you cast to snowball/player before checking if the projectile/shooter of projectile is actually a snowball/player. You need to do the instanceof checks before you cast.
    This probably causes the error, although I don't really get how the shooter could be a snowball :confused:
    Anyways, the error might not actually be caused by the line I posted. If you still get errors after fixing the instanceof checks post the new stacktrace & code :)

    Edit2:
    Also, the double () brackets are useless. Not really an issue, but it just doesn't look nice :p
     
Thread Status:
Not open for further replies.

Share This Page