Disable certain items from dropping on death of player

Discussion in 'Spigot Plugin Help' started by Jonathan_Storm, May 9, 2017.

  1. Deer Reader
    I have a KitPvP server where the most things are setup like i want it to, but i have 1 problem. When players die they're dropping everything in their inventory. Alle the iron-crap and stuff. I was wondering if any of you out there knew a plugin where you can disable (only certain) items from dropping on death of a player.
    So, if they've got stuff like diamond-gear and netherstars on them when they die, the things will be dropped, but if they have like a lot of iron gear it's only the Diamond Gear that's dropped when they die.

    Thanks :)

    Best Regards,
    Jonathan_Storm


    Hope you can help!
     
  2. WAS

    WAS

    Not a hard plugin to make really. If you'd like help let me know.
     
  3. Forgot to say that too ;)
     
  4. WAS

    WAS

    Here you go. Just install and edit the config to your liking. It should work for your basic needs. Only quickly tested it.

    Code (YAML):
    # Blacklist Items
    # Blacklist items will not drop when a player dies
    # For material names see: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Material.html
    # For short values see: http://minecraft.gamepedia.com/Data_values
    blacklist-items
    :
    # A diamond sword
      DIAMOND_SWORD
    :
        amount
    : 1
        short-value
    : none
    # Orange wool
      WOOL
    :
        amount
    : 1
        short-value
    : 1

    Code (Java):
    /*************************
    *
    * Copyright (c) 2017 Jordan Thompson (WASasquatch)
    *
    * Permission is hereby granted, free of charge, to any person obtaining a copy
    * of this software and associated documentation files (the "Software"), to deal
    * in the Software without restriction, including without limitation the rights
    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    * copies of the Software, and to permit persons to whom the Software is
    * furnished to do so, subject to the following conditions:
    *
    * The above copyright notice and this permission notice shall be included in
    * all copies or substantial portions of the Software.
    *
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    * SOFTWARE.
    *
    *************************/


    public class DenyPlayerDrops extends JavaPlugin implements Listener {

       private List<ItemStack> blacklist;
       private FileConfiguration conf;

       public DenyPlayerDrops() {
         createConfig();
         blacklist = new ArrayList<ItemStack>();
       }

       @Override
       public void onEnable() {
         getLogger().info(getDescription().getName() + " " + getDescription().getVersion() + " by "
             + getDescription().getAuthors().get(0));
         getLogger().info("This Spigot Plugin is provided AS IS with ABSOLUTELY NO WARRANTY");
         if (conf.getConfigurationSection("blacklist-items").getKeys(false).size() > 0) {
           for (String material : conf.getConfigurationSection("blacklist-items").getKeys(false)) {
             if (Material.valueOf(material) == null) {
               getLogger().warning("Invalid material: " + material);
               continue;
             }
             ItemStack item;
             if (conf.contains("blacklist-items." + material + ".short-value")
                 && !(conf.get("blacklist-items." + material + ".short-value").equals("none"))) {
               item = new ItemStack(Material.valueOf(material),
                   conf.getInt("blacklist-items." + material + ".amount", 1),
                   (short) conf.getInt("blacklist-items." + material + ".short-value"));
             } else {
               item = new ItemStack(Material.valueOf(material),
                   conf.getInt("blacklist-items." + material + ".amount", 1));
             }
             blacklist.add(item);
           }
         }
         getServer().getPluginManager().registerEvents(this, this);
       }

       @EventHandler(priority = EventPriority.LOWEST)
       public void onPlayerDeathDrop(PlayerDeathEvent e) {
         if (blacklist.size() > 0)
           e.getDrops().removeAll(blacklist);
       }

       private void createConfig() {

         try {

           if (!(getDataFolder().exists())) {
             getDataFolder().mkdirs();
           }

           File config = new File(getDataFolder(), "config.yml");

           if (!(config.exists())) {
             getLogger().info("Creating configuration...");
             saveDefaultConfig();
             conf = getConfig();
           } else {
             getLogger().info("Loading configuration...");
             conf = getConfig();
           }

         } catch (Exception e) {
           e.printStackTrace();
         }

       }

    }

    Attached file compiled against Spigot API 1_11_R1
     

    Attached Files:

    #5 WAS, May 9, 2017
    Last edited: May 9, 2017
    • Like Like x 3
    • Friendly Friendly x 1
  5. OMG! You are so nice!!! Can't thank you too much. Thanks alot <3
     
    • Like Like x 1
  6. There is only one problem. I cant deny it to drop arrows... Everything else works but arrows dont

    (ARROW:
    amount: 1
    short-value: none)
     
  7. WAS

    WAS

    Works fine for me. The problem is, it works off an amount of arrows. So if you are blacklisting a stack of 1 arrow, it will not catch any stack above 2-64, or below 1, which wouldn't be possible, but if it were set to 2, it wouldn't find a stack of 1.

    In order to get by that you would have change the event to something like (Untested)

    Code (Java):
      @EventHandler(priority = EventPriority.LOWEST)
       public void onPlayerDeathDrop(PlayerDeathEvent e) {
         Iterator<ItemStack> bit = blacklist.iterator();
         while (bit.hasNext()) { // Looking back, this could be a simple for loop
           ItemStack i = bit.next();
           if (i.getType().equals(Material.ARROW)) {
             Iterator<ItemStack> dit = e.getDrops().iterator();
             while(dit.hasNext()) {
               ItemStack di = dit.next();
               if (di.getType().equals(i.getType())) {
                 e.getDrops().remove(di);
               }
             }
           // Add more dynamic checks if you want such as:
           // } else if (i.getType().equals(Material.SNOWBALL) {
           //  Do Stuff
           } else {
             e.getDrops().remove(i);
           }
         }
       }
    Otherwise, you could always add 64 entries to your config for arrows. :p But that's ridiculous.
     
    #8 WAS, May 10, 2017
    Last edited: May 10, 2017
  8. I don't really know all of that coding-stuff. Actually i know nothing about it, maybe you could help? That would be SO nice <3
     
  9. But it's just wierd. Cause i can disable dropping of Steaks in stacks...
     
  10. WAS

    WAS

    Huh, thats strange. Shouldnt by logic of the code unless a built steak itemstack differs from others. Ill look into it later. You should learn how to write a plugin and compile it.
     
  11. I will. So you will try to make the plugin work for arrows to?
     
  12. WAS

    WAS

    Here. It no longer takes an amount in the the config.
     

    Attached Files:

  13. YOU ARE A LEGEND!
    Cant thank you enough!
     
  14. Is it possible to disable dropping of unenchanted bows and fishingrod, but not on enchanted bows and fishing rods?
     
  15. Yes, i could probably change it for ya when i get home ;)
     
  16. WAS

    WAS

    Look over the list of materials provided by Bukkit, just add fishing poll and bow to the list.

    Not sure why it wouldnt take them lol

    If you mean only non-magical items then that would be yet another addition to the program. Someone else can help you.
     
    #17 WAS, May 12, 2017
    Last edited: May 12, 2017
  17. You mean you can change his plugin to be able to do that? That would be so great!
     
  18. Where you going to do it for me? :)