.

Discussion in 'Plugin Development' started by elementalgodz11, Oct 25, 2013.

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

    elementalgodz11

  2. Offline

    Necrodoom

    Why type the command and not use the drop button?

    Moved to plugin requests.

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

    BillyGalbreath

    Why was this moved here? It sounds more like a developer needing some help/advice with the API. He's not requesting a plugin be built for him..

    Anyways, here is what you are looking for:

    Code:
    ItemStack item = player.getItemInHand().clone();
    player.getWorld().dropItemNaturally(player.getLocation(), item);
    After that you just remove the item from the inventory, then force update the inventory. ;)
     
  4. Offline

    Necrodoom

    If thats what the OP wants, report thread and il move it.
     
  5. Offline

    BillyGalbreath

    Show me what code you have so far, and I'll show you where to put it.
     
  6. Offline

    Necrodoom

    Moved to "Plugin Development".
     
  7. Offline

    BillyGalbreath

    Put this in your class and be sure to update the class name to the same as your filename.

    Code:
    public class DropCmd implements CommandExecutor {
            public boolean onCommand(CommandSender cs, Command cmd, String label, String[] args) {
                    if (!cmd.getName().equalsIgnoreCase("drop"))
                            return false;
                    if (!cs.hasPermission("some.permission.node")) {
                            cs.sendMessage("You do not have permission for that command!");
                            return true;
                    }
                    if (!(cs instanceof Player)) {
                            cs.sendMessage("Only a player can use this command!");
                            return true;
                    }
                    Player player = (Player) cs;
                    ItemStack item = player.getItemInHand().clone();
                    player.getWorld().dropItemNaturally(player.getLocation(), item);
                    player.setItemInHand(null);
                    player.updateInventory();
                    cs.sendMessage("You have dropped " + item.getAmount() + " " + item.getType().toString().toLowerCase() + "."));
                    return true;
            }
    }
    Then in your onEnable() method in the main class do this:

    Code:
    getCommand("drop").setExecutor(new DropCmd());
    And dont forget to add the command and permission node to your plugin.yml

    Code:
    name: MyPlugin
    main: me.plugins.MyPlugin
    version: 1.0
    author: BillyGalbreath
    description: My awesome plugin
    commands:
        drop:
            description: Drop the item in your hand to the ground.
            usage: /<command>
    permissions:
        some.permission.node:
            description: Allows users to use the /drop command
            default: false
    ;)

    The second is not an error. Its a warning, and can be ignored. As for the first one, it looks like you are putting the code in the wrong spot. I'd have to see the whole file to know whats going on.

    Player player = (Player) cs;
    ItemStack item = player.getItemInHand().clone();
    player.getWorld().dropItemNaturally(player.getLocation(), item);
    player.setItemInHand(null);
    player.updateInventory();


    Just remove those two lines I marked out.

    Code:
    Player player = (Player) cs;
    ItemStack item = player.getItemInHand().clone();
    if (item.getType().equals(Material.DIRT)) {
    return;
    }
    player.getWorld().dropItemNaturally(player.getLocation(), item);
    player.setItemInHand(null);
    player.updateInventory();
    
    You are missing this line above the if statement:
    Code:
    ItemStack item = player.getItemInHand().clone();
    Here use this ( i added comments so you know what its doing ):

    Code:
    Player player = (Player) cs; // get the player
    ItemStack item = player.getItemInHand().clone(); // get the item in player's hand
    if (item.getType().equals(Material.AIR) || item.getType().equals(Material.DIRT)) {
    return; // dont do anything if the player is holding dirt, or nothing at all (air)
    }
    //player.getWorld().dropItemNaturally(player.getLocation(), item); // only use this to make the item in hand drop to the ground
    player.setItemInHand(null); // remove the item from the player's hand
    player.updateInventory(); // update the player inventory so they see the changes we made
    
    Change the return to true. I forgot to put that i the code above.

    This would be completely separate issue, so make a new thread and someone will help you out with that. I'm starting to doze off here (no sleep for like 3 days now) lol. Have fun ^_^

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

    kevinspl2000

    BillyGalbreath
    This is the code you want to use.
    Code:java
    1. public class DropCommand implements CommandExecutor {
    2. @Override
    3. public boolean onCommand (CommandSender sender, Command cmd, String commandLabel, String[] args) {
    4. if (!(sender instanceof Player)) {
    5. System.out.println("You must be a player to do this!")
    6. return true;
    7. }
    8. if (args.length > 0) {
    9. player.sendMessage(ChatColor.GRAY + "Usage: /drop");
    10. return true;
    11. }
    12. if (args.length == 0) {
    13. if (player.getItemInHand().getType() == Material.AIR) {
    14. player.sendMessage(ChatColor.GRAY + "You need to have an item in your hand!");
    15. return true;
    16. }
    17. player.getWorld().dropItemNaturally(player.getLocation(), player.getItemInHand());
    18. player.setItemInHand(Material.AIR);
    19. player.sendMessage(ChatColor.GRAY + "Item dropped!");
    20. return true;
    21. }
    22.  
    23. return true;
    24. }
    25. }

    EDIT:
    Also to disable drops use this:
    Code:java
    1. public class PlayerListener implements Listener {
    2. @EventHandler
    3. public void onPlayerDropItemEvent(PlayerDropItemEvent event) {
    4. event.setCancelled(true);
    5. }
    6.  
    7.  
    8. }
     
  9. Offline

    Ad237

    elementalgodz11 Why not just disable dropping for some items instead of making the player type a command. The following code makes the player not able to drop there sword. However this could easily be changed to disable dropping of other items. This is untested.

    Code:java
    1. public Material[] blocked = {Material.WOOD_SWORD, Material.STONE_SWORD,
    2. Material.IRON_SWORD, Material.GOLD_SWORD, Material.DIAMOND_SWORD};
    3.  
    4. @EventHandler
    5. public void onDrop(PlayerDropItemEvent event) {
    6. Player p = event.getPlayer();
    7. for(Material bi : blocked) {
    8. if(bi == event.getItemDrop().getItemStack().getType()) { //Check if blocked item
    9. event.setCancelled(true); //Stop them from dropping
    10. }else { //not blocked
    11. //code to remove item
    12. }
    13. }
    14. }


    elementalgodz11 That's the point. Using my code they can't drop their sword. All other items can be dropped.

    elementalgodz11 Ok well if it works use it. I just personally think that typing a command to drop an item would be annoying.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
Thread Status:
Not open for further replies.

Share This Page