Solved Problem with editing player inventory.

Discussion in 'Plugin Development' started by JoshTehGuy, Apr 20, 2015.

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

    JoshTehGuy

    I have been trying to make an exploding arrow plugin that will only work if you have TNT in your inventory, and will then take 1 TNT out. I have been having trouble with the TNT, however. Code below:

    package com.AnonymousUser;

    import org.bukkit.Material;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.inventory.ItemStack;


    public class ArrowListener implements Listener {
    public ArrowListener(MainPlugin plugin) {
    plugin.getServer().getPluginManager().registerEvents(this, plugin);
    }


    @SuppressWarnings("deprecation")

    public void onArrowFired(ProjectileHitEvent e) {
    if (e.getEntity() instanceof Arrow) {
    // get arrow
    Arrow arrow = (Arrow) e.getEntity();

    // make sure player shot it, had a pretty funny bug about skeletons blowing stuff up
    if (arrow.getShooter() instanceof Player) {
    Player player = (Player) arrow.getShooter();

    // before I added the stuff about messing with inventories and TNT, the arrows worked fine.
    if (player.getInventory().containsAtLeast(new ItemStack(Material.TNT), 1)) {
    arrow.getWorld().createExplosion(arrow.getLocation(), (float) 2.0);
    player.getInventory().removeItem(new ItemStack(Material.TNT, 1));
    player.updateInventory();
    }
    }
    }
    }
    }
     
  2. Offline

    mine-care

    First things first, please follow java naming conventions, use code tags to include your code, and as far as I know getShooter is deprecated and in the current docs it is mentioned not to be used :3
    Anyway, your code seem alright and I do t see why it wont work. Have you tried Debuging?
     
  3. Offline

    nj2miami

    *sigh* @EventHandler
     
    FisheyLP likes this.
  4. Offline

    JoshTehGuy

    I'm looking into naming conventions now, thanks.
    What would be a good alternative to getShooter() then?
    And I have tried extensive debugging.

    I guess I forgot to put an event handler on the method? I'll try it

    Oh wow, thank you so much. It works as expected, although I guess the code could be cleaned up a bit.

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Apr 21, 2015
  5. Offline

    mine-care

    @JoshTehGuy To pass the shooter you can use MetaData containing their name and on the other end (when arrow hits anything) retreive it from there.
     
Thread Status:
Not open for further replies.

Share This Page