In need of coding help

Discussion in 'Plugin Development' started by mrbensplanet, Jul 28, 2014.

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

    mrbensplanet

    I am making a plugin where a player types in "/diamond" and receives a diamond, pretty simple. Well, I'm still new at this, so I don't really know how to actually make the plugin give the player a specified item. Could someone please help me out here, I would appreciated it.
     
  2. Offline

    Fhbgsdhkfbl

    mrbensplanet
    We're not going to spoonfeed code, have you even attempted at this?
     
  3. Offline

    _LB

    Have you already set up your CommandExecutor? If not, read the tutorial:
    http://wiki.bukkit.org/Plugin_Tutorial

    Once you have it set up, just get the world the player is in, and spawn an item at the player's location.
     
  4. Offline

    mrbensplanet

    _LB - By commandexecutor, do u mean the code which allows the player to insert the command? If so, yes. I am just confused on what I should use to actually give the player the diamond once they insert "/diamond"
     
  5. Offline

    Excalibur

  6. Offline

    _LB

  7. Offline

    mrbensplanet

    Ok this is what I came up with, any visible problems? _LB


    package me.mrbensplanet;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class DiamondGiver extends JavaPlugin {

    @Override
    public void onEnable() {
    getLogger().info("DiamondGiver onEnable has been enabled!");
    }

    @Override
    public void onDisable() {

    }

    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

    if (cmd.getName().equalsIgnoreCase("diamond") && sender instanceof Player) {


    Player player = (Player) sender;

    } else {
    sender.sendMessage("You must be a player!");
    return false;
    }
    return false;
    }

    if (args.length > 1) {
    sender.sendMessage("Too Many Arguments!");
    return false;

    } else
    PlayerInventory inventory = player.getInventory();
    ItemStack itemstack = new ItemStack(Material.DIAMOND, 1);

    return false;

    }

    }
     
  8. Offline

    _LB

    You don't need to interact with the player's inventory at all. You just need to drop the item at their eye location and make it have a pickup delay of 0, Minecraft does the rest for you.
     
  9. Offline

    mrbensplanet

    _LB could I get an example of the coding part for that? Thanks in advance
     
  10. Offline

    _LB

  11. Offline

    Deleted user

    _LB
    Why drop the diamond? Just add it to the inventory?
     
  12. Offline

    _LB

    Because then you have to deal with the return value of #addItem() if the inventory is full. Dropping it at the player is a more elegant solution.
     
  13. Offline

    xTigerRebornx

    _LB Doesn't take into factor that another Player could be close enough to pickup the ItemStack instead (like, if the player was standing "inside" the executor).
     
  14. Offline

    AoH_Ruthless

    _LB
    You only need one if check to see if an inventory has space on it, and there is significantly less margin for error with dropping the item on the player.
     
  15. Offline

    Deleted user

    _LB
    You could use Inventory#firstEmpty() and see if it's equal to -1

    If it is, then just send a message. Dropping a diamond means that the player may not actually get it
     
  16. Offline

    _LB

    But then you have to mess with playing the item pickup sound. To each their own.
     
  17. Offline

    themuteoneS

  18. Offline

    _Filip

  19. Offline

    themuteoneS

Thread Status:
Not open for further replies.

Share This Page