Deny snowball throwing

Discussion in 'Plugin Development' started by BrushPainter, Mar 23, 2014.

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

    BrushPainter

    Hey guys, I'm trying to make the projectile launch event cancel if a player is holding a snowball. So if they're holding any other item it won't cancel the snowball, only if they're holding it, the event will be cancelled. Here is my code so far, but it doesn't work:
    Code:java
    1. package me.BrushPainter.DenySnowballs;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Entity;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.entity.Snowball;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.entity.ProjectileLaunchEvent;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class Main extends JavaPlugin implements Listener{
    14.  
    15. public void onEnable() {
    16.  
    17. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    18.  
    19. getLogger().info("DenySnowballs Enabled");
    20.  
    21. }
    22.  
    23. public void onDisable() {
    24.  
    25. getLogger().info("DenySnowballs Disabled");
    26.  
    27. }
    28.  
    29. @EventHandler
    30. public void onThrow(ProjectileLaunchEvent e) {
    31.  
    32. Entity entity = e.getEntity();
    33.  
    34. if(((Player) entity).getItemInHand().getType() == Material.SNOW_BALL) {
    35.  
    36. if(e.getEntity() instanceof Snowball) {
    37.  
    38. e.setCancelled(true);
    39.  
    40. }
    41. }
    42. }
    43. }
     
  2. Offline

    The Fancy Whale

  3. Offline

    flaminsnowman99

    Do some checks first. See if the two if structures are returning true in the onThrow method. Make sure those are working correctly first. This way you can narrow down which part of your code isn't working.
     
  4. Offline

    KyleParks

    You could use the player interact event as well, since there is a few issues with that(I think).

    On line 34 you cast entity to a player, however snowmen can throw snowballs as well which might cause issues. You could add something like this first:
    Code:java
    1. if(entity instanceof Player) {


    And, do you know where your code doesn't work/stops running.
     
  5. Offline

    BrushPainter

    KyleParks Not sure, but it loads, just when I try throwing a snowball it works..

    The Fancy Whale Ok well I did that, but now it doesn't check if the player is holding a snowball.. I need that to work.

    Code:java
    1. @EventHandler
    2. public void onThrow(ProjectileLaunchEvent e) {
    3.  
    4. Entity entity = e.getEntity();
    5.  
    6. if(entity instanceof Player) {
    7.  
    8. if(e.getEntity() instanceof Snowball) {
    9.  
    10. e.setCancelled(true);
    11.  
    12. }
    13. }
    14. }


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

    The Fancy Whale

    Why does it matter if they are holding a snowball? If a snowball is thrown I'm guessing they are holding a snowball right?
     
    KyleParks likes this.
  7. Offline

    BrushPainter

    The Fancy Whale No because I need it for my paintball plugin, the guns are horse armor so they shoot snowballs...
     
  8. Offline

    The Fancy Whale

    Okay well if you want to get the player do:
    Code:java
    1. Snowball s = e.getEntity();
    2. Player shooter = s.getShooter();
    3.  
    4. if (shooter.getItemInHand().getType() == Material.SNOWBALL)
    5. {
    6. e.setCancelled(true);
    7. }
     
Thread Status:
Not open for further replies.

Share This Page