Simple Fireballs! (Pseudo-code included)

Discussion in 'Archived: Plugin Requests' started by Darkhand81, Jan 15, 2013.

  1. Offline

    Darkhand81

    Howdy all, ever since I noticed that the 'fire' item (item #51) is animated while in a player's hand, I've wanted to use it to allow players to shoot fireballs. :)

    Here's what I'm looking to do:

    Code:
    When Item#51 is held in hand
      On right click
        Cancel the place event (to prevent the player from simply placing fire)
        Shoot a fire charge from the player in the direction he is looking
        Decrease the itemstack by 1
    You could perhaps allow both the Item in hand (other items?) and the fire charge (ghast/wither fireball?) to be configurable, but that's up to you. I'll be using buycraft to /give players the actual fire items, so no worries about how to get the items.

    That would be it!
     
  2. Offline

    Beijiru

    There is already a bunch of plugins doing this, look it up in the database.
     
  3. Offline

    Darkhand81

    I've actually found nothing that is both this simple (doesn't do a bunch of other stuff), and is active. There is an old plugin that did something similar but it hasn't been updated in quite a while and the author is inactive.
     
  4. Offline

    chasechocolate

    Try this... It might have some errors because I wrote this without an IDE. Someone just needs to compile this:
    Code:java
    1. @EventHandler
    2. public void onBlockPlace(BlockPlaceEvent event){
    3. Player player = event.getPlayer();
    4. Location loc = player.getEyeLocation().toVector().add(player.getLocation().getDirection().multiply(2)).toLocation(player.getWorld(), player.getLocation().getYaw(), player.getLocation().getPitch());
    5. if(event.getItemInHand().getType() == Material.FIRE)[
    6. //Cancel the place event
    7. event.setCancelled(true);
    8.  
    9. //Spawn the fireball
    10. Fireball fireball = player.getWorld().spawn(loc, Fireball.class);
    11. }
    12. }
     
  5. Offline

    fafaffy

    Done.
    <Edit by Moderator: Redacted mediafire url>

    This will shoot a fireball whenever you right click fire (It will also remove 1 fire).
    There's no permissions of any kind, it just works.

    Edit:
    Here's my event code:

    Code:
        ItemStack fire = new ItemStack(Material.FIRE, 1);
       
        @EventHandler(priority = EventPriority.HIGH)
        public void onInteraction(PlayerInteractEvent event){
            if(event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
                if(event.getItem().getType() == Material.FIRE){
                    event.getPlayer().getInventory().removeItem(fire);
                    event.getPlayer().launchProjectile(Fireball.class);
                    event.setCancelled(true);
                }   
            }
        }
     
    Last edited by a moderator: Nov 8, 2016
    Darkhand81 likes this.
  6. Offline

    Darkhand81

    Indeed it does just work! Thanks a ton for that!
     
  7. Offline

    Darkhand81

    Getting the occasional NPE but I'm not sure if the held item is null or the block being clicked.

    EDIT: Think it's the item:

    Code:
    Caused by: java.lang.NullPointerException
        at com.fafaffy.fireball.fireballListener.onInteraction(fireballListener.java:19)
    Line 19:
    Code:
     (event.getItem().getType() == Material.FIRE)) {
     
  8. Offline

    Darkhand81

    No idea if this is the right way to do things, but I shuffled things around and added a null check to GetItem() and I seem to be fine now:

    Code:
    public class fireballListener
    implements Listener
    {
        ItemStack fire = new ItemStack(Material.FIRE, 1);
     
        @EventHandler(priority=EventPriority.HIGH)
        public void onInteraction(PlayerInteractEvent event) {
            if (event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                if (event.getItem() == null) return; 
                if (event.getItem().getType() == Material.FIRE) {
                    event.getPlayer().getInventory().removeItem(new ItemStack[] { this.fire });
                    event.getPlayer().launchProjectile(Fireball.class);
                    event.setCancelled(true);
                }
            }
        }
    }
     

Share This Page