Solved How do I make someone spawn with a custom item?

Discussion in 'Plugin Development' started by chris8787878787, Mar 23, 2014.

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

    chris8787878787

    So I want to make someone spawn with a chest called "Shop" whenever they log in. I made this so far and I want to know how to name the chest Shop and make them spawn with it. (Also how do I make the dirt and coal within it have custom names too?)
    Code:
    public class Shop extends JavaPlugin implements Listener{
     
    @Override
    public void onEnable() {
      getServer().getPluginManager().registerEvents(this, this);
    }
     
    @Override
    public void onDisable() {
     
    }
     
    @EventHandler
    public void onClick(PlayerInteractEvent e) {
      if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
      Player p = e.getPlayer();
      if(p.getItemInHand().getType() == Material.CHEST) {
          openShopForPlayer(p);
      }
     
      }
    }
     
    private void openShopForPlayer(Player p) {
      ItemStack dirt = new ItemStack(Material.DIRT, 32);
      ItemStack coal = new ItemStack(Material.COAL, 22);
      Inventory inv = Bukkit.createInventory(null, 18, "§2§lShop of epic proportions.");
      inv.addItem(dirt, coal);
      p.openInventory(inv);
     
    }
     
    }
    Please help!!!
     
  2. Offline

    chasertw123

    In order to give someone an item on join you do:
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent e) {
    3. Player p = e.getPlayer();
    4.  
    5. p.addItem(/*ItemStack Here*/);
    6. }
    7.  


    To make an item with a custom name you have to do this:
    Code:java
    1. public ItemStack whateverhere() {
    2. ItemStack i = new ItemStack(Material, amount, any data);
    3. ItemMeta im = i.getItemMeta();
    4. im.setDisplayName(String of item name here);
    5. i.setItemMeta(im);
    6.  
    7. return i;
    8. }


    If those don't work tell me. I wrote them from memory! :p

    I am not getting that error but I did make an error in my code! Here is the new code:
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent e) {
    3. Player p = e.getPlayer();
    4.  
    5. p.getInventory().addItem(/*Put ItemStack Here*/);
    6. }


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

    chris8787878787

    chasertw123
    It doesn't work? /*Itemstack here*/ what specifically should I put here
     
  4. Offline

    chasertw123

    By "ItemStack Here" I mean you have to put an ItemStack. Also just to let you know I made a mistake. Here is the fix:
    Instead of:
    Code:
    p.addItem(/*ItemStack Here*/)
    Put this:
    Code:
    p.getInventory().addItem(/*ItemStack Here*/)
    Where I say put "ItemStack Here" you need to put an ItemStack. You used them earlier in your code.
    Here is an example of a new ItemStack:
    Code:
    ItemStack i = new ItemStack(Material.COOKIE)
     
  5. Offline

    chris8787878787

    Ah I fixed it thanks! But check this out chasertw123 I made, a custom item that you get right when you spawn with custom lore and everything.
    Code:
     @EventHandler
    public void onJoin(PlayerJoinEvent e) {
        Player player = e.getPlayer();
        org.bukkit.inventory.PlayerInventory pi = player.getInventory();
        pi.setItem(0, createItem(Material.CHEST, 1, (short) 0, "§2§lShop", "§e§lRIGHT-CLICK §8to open the §2§lShop!"));
        
     
  6. Offline

    ZodiacTheories

    chris8787878787

    You don't need org.bukkit.inventory, I think you can simply use PlayerInventory pi = player.getInventory();, then import PlayerInventory.
     
  7. Offline

    Konkz

    Why did you do
    Code:
    §2§lShop
    instead of something such as
    Code:java
    1. ChatColor.GREEN + "" + ChatColor.BOLD + "Shop", ChatColor.GREEN + "" + ChatColor.BOLD + "RIGHT CLICK"


    Much better syntax. Also the reason I did the GREEN + "" + is because otherwise it would return an error as I'd have two ChatColor's together.

    This is going to make your messages easier to format and if you need to change something you can do it easier. :)
     
  8. Offline

    Booshayy

    Konkz
    If you're afraid of getting an error with your colors you can use ChatColor.RESET + "". Some useful information I recently learned.

    EDIT: I misread your post.... my bad :p
    Still useful information to have.
     
    Konkz likes this.
Thread Status:
Not open for further replies.

Share This Page