Tutorial Creating A Lore For An Item And Give It To A Player When He Joins The Server

Discussion in 'Resources' started by tkuiyeager1, Oct 10, 2015.

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

    tkuiyeager1

    Welcome,
    This tutorial will teach you how to add a lore to an item and give this item to the player when he joins the server.
    Lets start:
    First of all we extends our class to a JavaPlugin class and implementing the class to a Listener class:
    Code:
    public class Main extends JavaPlugin implements Listener {
    
    }
    Next we need to register the player join event on the OnEnable Method:
    Code:
    @Override
    public void onEnable() {
            Bukkit.getPluginManager().registerEvents(this, this);
        }
    Now we need to create our PlayerJoinEvent and get the player from the event:
    Code:
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
    }
    Now we need to create an ItemStack wich will be the item that we will add to the player inventory:
    Code:
    ItemStack item = new ItemStack(Material.DIAMOND_BLOCK);
    Now we are going to create the ItemMeta of our item:
    Code:
    ItemMeta itemMeta = item.getItemMeta();
    Now we are going to add the lore to the list:
    Code:
    itemMeta.setLore(Arrays.asList(ChatColor.GOLD + "Lore1", ChatColor.BLUE + "Lore2", ChatColor.YELLOW + "Lore3"));
    Now we need to set the ItemMeta to our item:
    Code:
    item.setItemMeta(itemMeta);
    Now we are going to get the player inventory and add our item to his inventory:
    Code:
    player.getInventory().addItem(item);
    And ofcurse you need to import everything.

    All the code:
    Code:
    public class Main extends JavaPlugin implements Listener {
    
        @Override
        public void onEnable() {
            Bukkit.getPluginManager().registerEvents(this, this);
        }
    
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            ItemStack item = new ItemStack(Material.DIAMOND_BLOCK);
            ItemMeta itemMeta = item.getItemMeta();
            itemMeta.setLore(Arrays.asList(ChatColor.GOLD + "Lore1", ChatColor.BLUE + "Lore2", ChatColor.YELLOW + "Lore3"));
            item.setItemMeta(itemMeta);
            player.getInventory().addItem(item);
        }
    
    }
    Thanks for reading.
     
    Last edited: Oct 11, 2015
  2. @tkuiyeager1
    Could you explain why setting an item's lore involves creating a class containing an incorrect named method and where on each player join an arraylist that isn't defined private, final and has type ArrayList instead of List you add 3 elements and eventually it will have thousands of elements?
     
  3. Offline

    tkuiyeager1

    @megamichiel can you explain your self better? why does it have to be private?
    Edit:
    and by saying incorrect named method do you mean the OnJoin method?
     
  4. Offline

    teej107

    @tkuiyeager1 He means that you aren't following naming conventions, aren't using encapsulation and the lore will forever expand because you are using only one List as a field and are adding to it each time a player joins.
    I just don't get why setting a lore requires a JavaPlugin, a Listener, and a PlayerJoinEvent.
     
    timtower likes this.
  5. Offline

    tkuiyeager1

    @teej107 i dont say it requiers that, i just teach them how to do this things, i think its better than just saying how to make a lore.
    and for the lore you think it should be an ArrayList? You think i should do it like this:
    Code:
    itemmeta.setLore(Arrays.asList("Lore1", "Lore2", "Lore3"));
     
  6. Offline

    timtower Administrator Administrator Moderator

    @tkuiyeager1 Or just add the strings to the list in the onEnable instead of each time the event gets called.
     
  7. Offline

    tkuiyeager1

    ok im changing the code
     
  8. @tkuiyeager1
    I don't think we need a tutorial for something that can be easily found in the JavaDocs.
     
  9. Offline

    tkuiyeager1

    @Assist i think this tutorial is good even if this is can be find easily in the JavaDocs
     
  10. Offline

    Lolmewn

    Of course you do, it's your own tutorial :p
    Coding-convention-wise there are a lot of mistakes, most already mentioned above. One that particularly annoyed me was having one-letter variables. A variable name 'e' or 'p' has no information at all about what it is. Instead, name then 'event' and 'player', after what they are.
     
  11. Offline

    tkuiyeager1

    ok thanks i will fix this
    Edit:
    @Lolmewn can you also say the mistakes the code have? i want to fix them.
     
  12. Offline

    Lolmewn

    @tkuiyeager1 Sure thing.
    Camel case your variables. This makes longer variables easier to read. For example, change itemmeta to itemMeta.
    Furthermore, your ItemStack never changes (it's the same for every player). Therefore, instead of creating a new one every PlayerJoin, just make a single one when your plugin enables.
    Maybe to add to this, normally you would put listeners into their own class. Since this example only serves one goal it is fine to throw it all in the main class, but with projects that do more than one thing (which is 99% of all projects), it's better to split the two. Again, not bad for this example, but bad if used in bigger projects.
     
  13. Offline

    tkuiyeager1

    ok thanks

    Edit:
    if i will create the itemstack in the onEnable it wont let me use it in the event
     
    Last edited: Oct 11, 2015
  14. Offline

    Xerox262

    *facepalm* Why're you making tutorials if you don't know how fields work?

    The way to do this would be to store the ItemStack outside all methods, then you can access it anywhere in that class

    Code:
    //Don't call it this, call it something more specific
    private ItemStack spawnWithThis;
    public void onEnable() {
        spawnWithThis = new ItemStack(Material.Material, int amount); // No need to specify amount if only 1 item.
        ItemMeta itemMeta = spawnWithThis.getItemMeta();
        itemMeta.setLore(Arrays.asList(ChatColor.GOLD + "Lore1", ChatColor.BLUE + "Lore2", ChatColor.YELLOW + "Lore3"));
        spawnWithThis.setItemMeta(itemMeta);
    }
     
    Last edited: Oct 18, 2015
Thread Status:
Not open for further replies.

Share This Page