[Finally solved! :D]Making that a player only can pick up swords.

Discussion in 'Plugin Development' started by ceoepts, Jul 10, 2012.

  1. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I need help with making that a player only drops hes swords and no other items.
    I have'nt tryed to make a code like this yet but i cant figur out how i could do it.
    Please help me :)
    sorry for my english
  2. Offline

    d33k40

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    when he dead? or just he cant drop other items?
  3. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Could you help me with that he cant drop things at all when he is alive and when he dies he drops only hes swords. :D
    Please replay!
  4. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    anyone? :S
  5. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    If you know how to do this please help me :)
  6. Offline

    mcgamer99

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Try this:
    Code:
        @EventHandler
        public void onDrop(PlayerDropItemEvent e) {
            e.setCancelled(true);
            if(e.isCancelled()) return;
        }
     
        @EventHandler
        public void onDeath(PlayerDeathEvent e) {
            Player death = (Player) e.getEntity();
            ItemStack sword = new ItemStack(Material.STONE_SWORD, 1);
         
            if(e.getEntity() == death) {
            e.getDrops().set(1, sword);
            return;
            }
        }
    With the first event the player can't drop items.
    With the second event when the player die, the drop is only a stone sword(for change to iron sword or other sword, change the Material.STONE_SWORD).

    This post has been edited 1 time. It was last edited by mcgamer99 Jul 10, 2012.
  7. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Ok thx! Thats works but i have a problem i want the enchant to stay with the sword if its possible!

    This post has been edited 1 time. It was last edited by ceoepts Jul 10, 2012.
  8. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    You'd have to get the itemstack from their inventory instead of defining a new one.
  9. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Could you give me a exaple of the code im a little bit of a newbie :p
  10. Offline

    mcgamer99

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Try this code, I modify the code of my preceding post:
    Code:
        @EventHandler
        public void onDeath(PlayerDeathEvent e) {
            Player death = (Player) e.getEntity();
            ItemStack sword = new ItemStack(Material.STONE_SWORD, 1);
            Map<Enchantment, Integer> enchantment = sword.getEnchantments();
           
            if(e.getEntity() == death) {
            e.getDrops().set(1, sword);
            if(!sword.getEnchantments().isEmpty()) {
            sword.addEnchantments(enchantment);
            return;
            }
            return;
            }
        }
  11. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Dont works that good :( sometimes im not getting a sword and when i get a sword the killer is looking to the side
    (the killer is looking to the side)i only think this is your code not sure will test without.
    and i get a error.
  12. Offline

    EnvisionRed

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I'm fairly sure you can get a list of itemstacks that the player will drop. Would it not work to just iterate through that and if the itemstack isn't a sword of some kind, remove it from the list?
  13. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Hmm then could someone help me with making that you can only pick up swords?
  14. Offline

    EnvisionRed

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Probably should use a PlayerPickUpItemEvent handler, you can get the item, check if it's a sword. If it isn't a sword, cancel the event. It would be pretty easy to make actually.
  15. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    How to make them that they only can pick up swords then sorry if i missunderstand you could you give me a exemple of the code :)
  16. Offline

    EnvisionRed

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    You learn more by doing it yourself than having us do it for you ^_^

    Anyway I already told you how to do it.
  17. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Ok :p But i dont understand 1 thing
    If it isn't a sword, cancel the event. i dont know how i can find the sword thing at all as i said i started programming java yesterday :( so i have no idea what you are talking about?
    I still want only hes sword to drop :p

    This post has been edited 1 time. It was last edited by ceoepts Jul 10, 2012.
  18. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
  19. Offline

    EnvisionRed

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Er, learn java first then?

    You can fetch the itemstack picked up in a PlayerPickUpItemEvent pretty easily. Then get the type of the itemstack, compare it to types of swords. if it doesn't match any of the swords, cancel the event. I honestly can't put this in any more straightforward terms.
  20. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Ok i'll try :S
  21. Offline

    EnvisionRed

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    When you're comparing 2 objects, either use == (yes, two equals signs) or object1.equals(object2)

    So to fix your issue, just change the one = in the if statement to ==

    This post has been edited 1 time. It was last edited by EnvisionRed Jul 10, 2012.
  22. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Im getting a error that says "Type mismatch: cannot convert from Item to boolean" in eclipse this is my code
    Code:
        @EventHandler
        public void OnlyPickUpSwords(PlayerPickupItemEvent event){
            Item item = event.getItem();
            Material Swordstone = Material.STONE_SWORD;
            if(item = Swordstone){
           
            }
    Could you help me?
    Sorry if im a bad java programmer but could you please help me with this the error is on
    if(item = Swordstone){

    This post has been edited 1 time. It was last edited by ceoepts Jul 10, 2012.
  23. Offline

    EnvisionRed

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    So because you didn't read it...
    Here's what you should do!
    You have this right now:
    Code:
    if (item = Swordstone){
     
    }
    Change it to:
    Code:
    if (item.getType() == Swordstone){
     
    }
  24. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Oh! :D That i did'nt think of putting that there i had the getType() in my code put did'nt know where to put it :p I need to remeber that :p Thx so much!

    This post has been edited 2 times. It was last edited by ceoepts Jul 10, 2012.
  25. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    But i still get a error by eclipse now "Incompatible operand types EntityType and Material"
    How can i operand EntityType and Material
    I changed the code to
    Code:
        @EventHandler
        public void OnlyPickUpSwords(PlayerPickupItemEvent event){
            EntityType item = event.getItem().getType();
            Material Swordstone = Material.STONE_SWORD;
            if (item == Swordstone){
             
            }

    This post has been edited 2 times. It was last edited by ceoepts Jul 10, 2012.
  26. Offline

    EnvisionRed

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    It should be
    Code:
    Material item = event.getItem().getType();
    not
    Code:
    EntityType item = event.getItem().getType();
  27. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    sorry if im anoying but when i set it to
    Material item = event.getItem().getType();
    i get a error here
    Material item = event.getItem().getType();
    that says
    Type mismatch: cannot convert from EntityType to Material
    and it wants me to change it to
    EntityType item = event.getItem().getType();
  28. Offline

    Father Of Time

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Because the entity referred to in this event isn't an entity, it's an Item class that inherits from entity; and this class does not have a function called "getType()".

    To fix this you need to do something such as this (going from memory).

    Code:
    Item eventitem = event.getItem();
    ItemStack eventstack = eventitem.getItemStack();
    Material eventitemmaterial = eventstack.getType();
    
    It must first be converted to ItemStack which does contain the getType() function, then grab the information you need.

    I hope this helps, good luck with your project!
  29. Offline

    ceoepts

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Hmmmm... It dont seems to work now i get a new error on the line
    if (eventitem == eventitemmaterial){
    that says "Incompatible operand types Item and Material" :(
  30. Offline

    Father Of Time

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Because you are trying to compare the Material.java class to the Item.java class; which is impossible.

    You need to do a bit of reading on type casting, it seems to be tripping you up slightly. Just remember, all of these in game objects are nothing more than Java classes, just like in your projects. So in any project you have made this far could you take one class and compare it with a completely different class? Unlikely because the compiler would have no idea how to compare the two object to one another (unless you made a custom comparator or override their hashcode function).

    To use == or .equals you need to have objects either of the same class, or that are designed to be compared to one another (which material and item are not).

    sadly, your question was too out of context, I don't know where it applied to your project; if you post the entire function that you are working on I will gladly explain how to resolve your issue; but unfortunately I cannot do so with a single condition check. ;)

    This post has been edited 2 times. It was last edited by Father Of Time Jul 10, 2012.

Share This Page