SnowBall Help!

Discussion in 'Plugin Development' started by mrgreen33gamer, Apr 19, 2014.

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

    mrgreen33gamer

    Hello everyone! I need help with something! I have having some issues with my special kit.

    So i am a player at spawn, whenever i am using this kit i made, the code that will be below, i can throw snowballs and them and they take the ability, what i am trying to do is make it were when you are at spawn, you cannot damage the player with a snowball or the snowball does not do anything to the player, and when you are in the pvp arena, you can throw the snowball and the ability or damage will be taken!

    Here is my code:

    Code:java
    1.  
    2. @EventHandler
    3. public void SkinnyGuyKit(EntityDamageByEntityEvent e) {
    4. Entity ent = e.getEntity();
    5. Entity damager = e.getDamager();
    6. Player hit = (Player) ent;
    7. if (e.getDamager() instanceof Snowball) {
    8. Snowball snowball = (Snowball) damager;
    9. if (e.getEntity() instanceof Player
    10. && e.getDamager() instanceof Snowball) {
    11. Player shooter = (Player) snowball.getShooter();
    12. if (Main.skinnyguy.contains(shooter.getName())) {
    13.  
    14. hit.setVelocity(new Vector().setY(3));
    15. hit.sendMessage("Test Test and Test!");
    16. }
    17. }
    18. }
    19. }
    20.  
    21. @SuppressWarnings("deprecation")
    22. public void SkinnyGuyKitSnowBall(PlayerInteractEvent event) {
    23. Player player = event.getPlayer();
    24. int rt = 10 * 1000;
    25.  
    26. if (!(lasttime.containsKey(event.getPlayer()))) {
    27. lasttime.put(event.getPlayer(), Long.valueOf(0L));
    28. }
    29. if (Main.skinnyguy.contains(player.getName())) {
    30.  
    31. if (event.getAction() == Action.RIGHT_CLICK_AIR
    32. || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    33. if (event.getItem().getType() == Material.SNOW_BALL) {
    34. if (System.currentTimeMillis()
    35. - ((Long) lasttime.get(event.getPlayer()))
    36. .longValue() > rt) {
    37. lasttime.put(event.getPlayer(),
    38. Long.valueOf(System.currentTimeMillis()));
    39.  
    40. } else {
    41. event.setCancelled(true);
    42. player.sendMessage("Please wait 5 seconds!");
    43. player.updateInventory();
    44. }
    45. }
    46. }
    47. }
    48. }
    49.  
     
  2. Offline

    clmcd42

    Ok, there's multiple issues with this code.

    1. You didn't check to see if ent was a Player before casting it to hit.
    2. You didn't check to see if snowball.getShooter() was a Player before casting it to shooter.
    3. Your SkinnyGuyKitSnowBall method does not have @EventHandler above it.

    That's all the basic things I could see. Fix those and then tag me if it still doesn't work.
     
  3. Offline

    ever_x

    Well, if the spawn and PvP arenas are in different worlds, you can add an if statement to the entitydamage event to check if shooter.getWorld is equal to your spawn/PvP world and go ahead, or set cancelled accordingly. If not, then when players join and leave the arena, you can add their names to an arrayList and in the same place as above, check if that arrayList contains the shooters name. Oh yeah, and fix the errors in the post above first :)
     
  4. Offline

    mrgreen33gamer


    I know about the @EventHandler,but i dont understand what 1 and 2 are about. Code example?
     
  5. Offline

    Konkz

    If you don't want them to throw snowballs and such you can hook up to World Guard, and make the region in centre the "spawn" region. Then in code check if they are in region spawn and if it's true then don't do anything.
     
  6. Offline

    clmcd42

    mrgreen33gamer
    1. Basically, and entity can be a lot of things besides players. Code example of how to fix this error:
    Code:java
    1.  
    2. Entity ent = e.getEntity();
    3. Entity damager = e.getDamager();
    4. Player hit;
    5. if(ent instanceof Player) {
    6. hit = (Player) ent;
    7. } else {
    8. //Do whatever you want to do if its not a player
    9. }

    2. Same thing but with snowball.getShooter(); You can't always assume it's a Player.
     
  7. Offline

    ever_x

    mrgreen33gamer Well, not to spoon-feed, but...
    1.
    Code:java
    1. if (ent instanceof Player) Player hit = (Player) ent;
    You actually do this check on line 9, you just need to move line 6 to below the line 9 and 10 if statement.
    2.
    Code:java
    1. if (snowball.getShooter() instanceof Player) Player shooter = (Player) snowball.getShooter();
    This is exactly the same sort of thing. Before you cast the shooter to a player, you need to make sure it actually was a player that shot the snowball. This is because it could have been shot by a snowman or another custom coded mob set to throw snowballs. If that mob was treated as a player, it would cause all sorts of errors. If you can 100% tell yourself that there will be no other entities capable of throwing a snowball anywhere this code would get executed, then it should cause no problems, but just to be safe you must first do an instanceof check, just to be sure.
     
  8. Offline

    mrgreen33gamer


    Here is my formatted code: (Doesn't work)

    Code:java
    1.  
    2. @EventHandler
    3. public void SkinnyGuyKit(EntityDamageByEntityEvent e) {
    4. Entity ent = e.getEntity();
    5. Entity damager = e.getDamager();
    6. Player hit = (Player) ent;
    7. if(ent instanceof Player){
    8. if (e.getDamager() instanceof Snowball) {
    9. Snowball snowball = (Snowball) damager;
    10. if (snowball.getShooter() instanceof Player){
    11. Player shooter = (Player) snowball.getShooter();
    12. if (Main.skinnyguy.contains(shooter.getName())) {
    13.  
    14. hit.setVelocity(new Vector().setY(3));
    15. hit.sendMessage("Test Test and Test!");
    16. }
    17. }
    18. }
    19. }
    20. }
    21.  
     
  9. Offline

    coasterman10

    mrgreen33gamer You are still casting ent to a Player before doing the instanceof check. The whole purpose of the instanceof check is to avoid invalid casting exceptions, which is exactly what the code would generate if ent is not a Player.
     
  10. Offline

    mrgreen33gamer

    Sooo, your not telling me what I should do specifically...
     
  11. Offline

    GaaTavares

    You kinda should understand it. Do you know what casting means? (Not trying to be rude).
    Code:java
    1. Player hit = (Player) ent;

    you are casting as a player even without knowing if it is really a player.
     
  12. Offline

    mrgreen33gamer

    Ok first, i don't see what the problem is with my code, the even works and everything, i don't understand how to cast the event correctly. And yes, i don't complexly understand know what casting is...
     
  13. Offline

    ever_x

    @mrgreen33gamer
    OK, so for the sake of my sanity...
    Code:java
    1. public class test
    2. {
    3. @EventHandler public void SkinnyGuyKit(EntityDamageByEntityEvent e)
    4. {
    5. Entity ent = e.getEntity();
    6. Entity damager = e.getDamager();
    7. if (damager instanceof Snowball && ent instanceof Player)//Are these entites what they should be?
    8. {
    9. Player hit = (Player) ent;
    10. Snowball snowball = (Snowball) damager;//There, all cast and checked
    11. if (snowball.getShooter() instanceof Player)//Is the snowball shooter a player?
    12. {
    13. Player shooter = (Player) snowball.getShooter();
    14. if (Main.skinnyguy.contains(shooter.getName()))
    15. {
    16.  
    17. hit.setVelocity(new Vector().setY(3));
    18. hit.sendMessage("Test Test and Test!");
    19. }
    20. }
    21. }
    22. }
    23.  
    24. @SuppressWarnings("deprecation") @EventHandler//There, now this will actually work :)
    25. public void SkinnyGuyKitSnowBall(PlayerInteractEvent event)
    26. {
    27. Player player = event.getPlayer();
    28. int rt = 10 * 1000;
    29.  
    30. if (!(lasttime.containsKey(event.getPlayer())))//Don't store Player objects in an arrayList!
    31. {
    32. lasttime.put(event.getPlayer(), Long.valueOf(0L));
    33. }
    34. if (Main.skinnyguy.contains(player.getName()))
    35. {
    36.  
    37. if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
    38. {
    39. if (event.getItem().getType() == Material.SNOW_BALL)
    40. {
    41. if (System.currentTimeMillis() - ((Long) lasttime.get(event.getPlayer())).longValue() > rt)
    42. {
    43. lasttime.put(event.getPlayer(), Long.valueOf(System.currentTimeMillis()));
    44.  
    45. } else
    46. {
    47. event.setCancelled(true);
    48. player.sendMessage("Please wait 5 seconds!");
    49. player.updateInventory();
    50. }
    51. }
    52. }
    53. }
    54. }
    55. }
    56.  

    There we go, properly cast and the missing EventHandler added. I really do hope you could have worked that out yourself, because that is very basic stuff that will drag you down later on if you don't understand it.
    Just one extra note you wont understand at all, be very careful about storing a Player object in ArrayLists, instead take in a String and use player.getName(). The reason why is quite complicated but essentially it will cause a very bad memory leak.

    OK, as for your original question, are your PvP arenas and your spawn in the same world, or different Multiverse worlds?

    P.S, please let me know if the code is wrong, I just quickly typed it up!
     
  14. Offline

    mrgreen33gamer

    Yeah the whole event doesn't work.... Registered or not Registered.....
     
  15. Offline

    Esophose

  16. Offline

    mrgreen33gamer

    No duh, yes i did, read the comment above please, i did register the class and the event, the class that has the even has over 70 kits that are running on my server, yes i am sure the event and class are registered......
     
  17. Offline

    ever_x

    mrgreen33gamer
    Well, are there any errors you can post, or does it simply not work. Try simplifying the code into different sections and keep testing; it's possible some of the if statements doesn't quite work as intended, so none of the rest of the code is being executed. We can't help you without errors, so it's up to you to debug your own code!
     
  18. Offline

    mrgreen33gamer

    Well, I am trying to get help here. :p
     
Thread Status:
Not open for further replies.

Share This Page