Adding blocks to players inv VIA explosion.

Discussion in 'Plugin Development' started by webbhead, Jul 24, 2015.

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

    webbhead

    What I am trying to do is add all of the blocks from the explosion of a "MineBomb" (A Minebomb is a Firework_Charge) to the players inventory. What I did is add the player to a hashmap then check if they were in the hashmap to for that location then give them blocks but it did not work.

    Here is my InteractEvent and ExplodeEvent:

    Code:
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            final Player p = e.getPlayer();
            ItemStack hand = p.getInventory().getItemInHand();
            Action a = e.getAction();
            if (a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK) {
                if (hand.getItemMeta().getDisplayName().equals("§6§lMineBomb") && hand.getType() == Material.FIREWORK_CHARGE) {
                    final Item bomb = p.getWorld().dropItem(p.getEyeLocation(), new ItemStack(Material.FIREWORK_CHARGE));
                    if (hand.getAmount() > 1) {
                        hand.setAmount(hand.getAmount() - 1);
                    } else {
                        p.getInventory().removeItem(hand);
                    }
                    bomb.setVelocity(p.getEyeLocation().getDirection());
                    p.sendMessage("§6§lMineBombs§8 >§e You threw a MineBomb!");
                    destroyedBlocks.put(bomb.getLocation(), p);
                    bomb.setPickupDelay(300);
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                        @Override
                        public void run() {
                            createFirework(bomb.getWorld(), bomb.getLocation());
                            bomb.getWorld().createExplosion(bomb.getLocation(), 2.5F, true);
                            bomb.remove();
                        }
                    }, 60L);
                } else {
                    return;
                }
            }
        }
       
        @EventHandler
        public void onEntityExplode(EntityExplodeEvent e) {
            if (destroyedBlocks.containsKey(e.getLocation())) {
                List<Block> blocks = e.blockList();
                for (Location loc : destroyedBlocks.keySet()) {
                    if (loc.equals(e.getLocation())) {
                        Player p = destroyedBlocks.get(e.getLocation());
                        for (Block b : blocks){
                            ItemStack drop = new ItemStack(b.getType());
                            p.sendMessage("as");
                                p.getInventory().addItem(drop);
                            }
                        }
                    }
                }
            }
     
  2. Offline

    SuperSniper

    for(Block blocks : e.blockList()) {
    p.getInventory().addItem(blocks);
     
  3. Offline

    Eos

  4. Offline

    webbhead

    OOOOHHH My bad!! lol
    The hashmap is:
    Code:
    HashMap<Location, Player> destroyedBlocks = new HashMap<>();
    I'll give it a try.

    --EDIT--
    Didn't work lol just realized... you can't add blocks straight up. Cause it isn't an ItemStack.

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Jul 26, 2015
  5. Offline

    Eos

    Why is the Location your key and the value your player? Should be the other way around.

    Code:
    HashMap<Player, Location> destroyedBlocks = new HashMap<Player, Location>();
    
     
  6. Offline

    webbhead

    Fixed that.

    I just changed the EntityExplodeEvent to this:
    Code:
        @EventHandler
        public void onEntityExplode(EntityExplodeEvent e) {
            Player p = (Player) e.getEntity();
                if (destroyedBlocks.containsKey(p)) {
                        for (Block b : e.blockList()) {
                            if (e.getLocation().equals(destroyedBlocks.get(p))) {
                            ItemStack drop = new ItemStack(b.getType());
                                p.getInventory().addItem(drop);
                        }
                    }
                }
            }
    But it still doesn't work, and I am completely confused.

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Jul 26, 2015
  7. Offline

    Eos

    Does the your minebomb explode on right click or does it spew out errors?
     
  8. Offline

    webbhead

    No errors are thrown at all.
     
  9. Offline

    Eos

    @webbhead
    Does anything happen when you right click?
     
  10. Offline

    webbhead

    Yes everything works except the giving of blocks.

    bump

    I really need to get this plugin done, I cannot figure this out. :(

    @Eos I have an idea to why this isn't working but I don't know a way around it... My idea is that for the ExplodeEvent that it isn't working correctly because Im calling world.createExplosion which doesn't have an entity that is exploding it, and it is just an explosion that has no real source I guess. Idk how to explain it, but do you know a way around this.

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than triple posting.>

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

    Eos

    I would help you right now, but i'm working on a plugin for someone, it's hard to think right now.
     
  12. Offline

    webbhead

    Alright.
     
  13. Offline

    webbhead

    Update: I just updated my code and added two hashmaps, but this still just doesn't work and I cannot seem to figure it out.. Here is the code:

    Code:
        HashMap<Location, Player> lp = new HashMap<Location, Player>();
        HashMap<Player, Location> pl = new HashMap<Player, Location>();
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            final Player p = e.getPlayer();
            ItemStack hand = p.getInventory().getItemInHand();
            Action a = e.getAction();
            if (a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK) {
                if (hand != null) {
                if (hand.getType().equals(Material.FIREWORK_CHARGE)) {
                if (hand.getItemMeta().getDisplayName().equals("§6§lMineBomb")) {
                    final Item bomb = p.getWorld().dropItem(p.getEyeLocation(), new ItemStack(Material.FIREWORK_CHARGE));
                    if (hand.getAmount() > 1) {
                        hand.setAmount(hand.getAmount() - 1);
                    } else {
                        p.getInventory().removeItem(hand);
                    }
                    bomb.setVelocity(p.getEyeLocation().getDirection());
                    p.sendMessage("§6§lMineBombs§8 >§e You threw a MineBomb!");
                    bomb.setPickupDelay(300);
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                        @Override
                        public void run() {
                            lp.put(bomb.getLocation(), p);
                            pl.put(p, bomb.getLocation());
                            spawnTNT(bomb.getWorld(), bomb.getLocation());
                            createFirework(bomb.getWorld(), bomb.getLocation());
                            bomb.remove();
                        }
                    }, 60L);
                        } else {
                            return;
                        }
                    } else {
                        return;
                    }
                }
            }
        }
       
        @EventHandler
        public void onEntityExplode(EntityExplodeEvent e) {
            if (e.getEntity() instanceof TNTPrimed) {
                Player p = lp.get(e.getLocation());
                Location l = pl.get(p);
                if (lp.containsKey(e.getLocation())) {
                    for (Block block : e.blockList()) {
                        if (e.getLocation().equals(l)) {
                            ItemStack b = new ItemStack(block.getType());
                            p.getInventory().addItem(b);
                        }
                    }
                    lp.remove(l);
                    pl.remove(p);
                }
            }
        }
     
  14. Offline

    webbhead

  15. @webbhead
    When using World#createExplosion, no EntityExplodeEvent is called because there is no entity. Make the map's key Location again and the value Player @Eos Why should it even be the other way around? You can't get a player from an explode event. @webbhead You need to listen for the BlockExplodeEvent
     
  16. Offline

    webbhead

    Thanks Ill give that a go! Didn't know there was a BlockExplodeEvent haha I guess Im a noob lol!
     
  17. @webbhead
    Nah, it was only until like 2 weeks ago until I found out about that event too :p.
     
Thread Status:
Not open for further replies.

Share This Page