Problem with my InvTeleporter! (PlayerDeathEvent)

Discussion in 'Plugin Development' started by ColdRaker, Aug 22, 2014.

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

    ColdRaker

    Hello ,

    I have a Problem with my teleporter.
    The Teleporter (commandexecutor, or whatever) works fine but if someone dies the TeleportItem gets dropped and can be picked up. that should not happen... .

    I hope someone can help me out with some examples, because this is the first time i'm making a Plugin.

    Thanks.

    Here ist the Line I need:

    Code:java
    1. @EventHandler(priority = EventPriority.NORMAL)
    2. public void onDeath(PlayerDeathEvent e) {
    3. //i want to remove only the TeleporterItem in slot 8 which would get dropped on the ground so it cant be
    4. //picked up after death.
    5. }


    Here is the full code: http://pastebin.com/PqK3kRej
     
  2. Offline

    fireblast709

    ColdRaker getDrops(), get an Iterator, loop over the Iterator, call .remove() when you encounter an item that should not be dropped.
     
  3. Offline

    ColdRaker

    @fireblast709 thanks for the fast response i dont now how to do this right now but i will look over it and try it out^^
     
  4. Offline

    megasaad44

    ColdRaker dropitemevent => check if dropped item is teleporter => cancel.
     
  5. Offline

    ColdRaker

    I already have the cancel event inside the code
    Code:java
    1. @EventHandler(priority = EventPriority.NORMAL)
    2. public void onDrop(PlayerDropItemEvent e) {
    3. if(e.getItemDrop().getItemStack().getItemMeta().getDisplayName().equalsIgnoreCase("§3§lTeleporter")) {
    4. e.setCancelled(true);
    5. }
    6. }

    doesnt work

    This is what i got so far ... is this alright? and if it is, how do i remove the item now?

    Code:java
    1. @EventHandler(priority = EventPriority.NORMAL)
    2. public void onDeath(PlayerDeathEvent e) {
    3. e.getDrops();
    4. List<String> DroppedItems = Arrays.asList(Bukkit.getName());
    5. Iterator<String> iterator = DroppedItems.iterator();
    6. if(iterator.next().contains("§3§lTeleporter")){
    7. }
    8. }


    fireblast709 could you post a code example. Its the first time I use loops and iterators

    So ich denke in jeder Zeile sind "Viele" Fehler. Wenn sich das mal jemand ansehen könnte wäre ich dankbar
    ich denke ich habe auch vieles einfach eingefügt was ich so nicht benutzen kann :/

    Code:
        @EventHandler(priority = EventPriority.NORMAL)
        public void onDeath(PlayerDeathEvent e) {
            e.getDrops();
            List<String> DroppedItems = Arrays.asList(Bukkit.getName());
            Iterator<String> iterator = DroppedItems.iterator();
            if(iterator.next().contains("§3§lTeleporter")){
                e.getDrops().remove("§3§lTeleporter");
            }
            else{
                return;
            }
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  6. Offline

    fireblast709

    ColdRaker ok so, let me get this straight. "Get an Iterator" means "Get an Iterator of event.getDrops()" :p. (I'm sorry if I was unclear). Secondly, when googling "looping over an iterator" gave me this as the first hit. Then, check for a few things:
    • Check if the ItemStack has meta (there is a method for this which you can find in the documentation)
    • Get the ItemMeta
    • Check if the ItemMeta has a displayname (once more, check the documentation ;3)
    • Check if the displayname equals your String
    • call Iterator#remove() if all of the above are true (and make sure you check them in that exact order)
     
  7. Offline

    ColdRaker

    fireblast709 great! thank you for the detailed explaination I think I can do it now. I will post the finished code later ^^

    fireblast709 I don't understand how to get the last part to work. (while-part)
    event. (e.) to aks for the meta,displayname etc (but i dont have an event, or I cant see one)
    how can I implement the Iterator#remove() to remove the found item?


    Code:
        @EventHandler(priority = EventPriority.NORMAL)
        public static void onDeath(PlayerDeathEvent e) {
            e.getDrops();
            List<String> DroppedItems = new ArrayList<>();
            DroppedItems.add("§3§lTeleporter");
            useWhileLoop(DroppedItems);
        }
     
        private static void useWhileLoop(Collection<String> aDroppedItems) {
            Iterator<String> DroppedItemsIter = aDroppedItems.iterator();
            while (DroppedItemsIter.hasNext()){
                if(e.getDrop().hasItemStack().hasItemMeta().getItemMeta().getDisplayName().equalsIgnoreCase("§3§lTeleporter")) {
                System.out.println(DroppedItemsIter.next());
               
                Iterator#remove()
                }
            }
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  8. Offline

    fireblast709

    ColdRaker you still seem to miss a lot of what I explained... For example, your Iterator should be
    Code:java
    1. event.getDrops().iterator();
    And you cannot chain all those method calls in that if statement. Also "Iterator#remove()" means "calling remove() on the Iterator instance"
     
  9. Offline

    ColdRaker

    okey thank you for the help. I think i should learn more about java at all :D
     
Thread Status:
Not open for further replies.

Share This Page