Solved Is this way good to give tool kit?

Discussion in 'Plugin Development' started by Pr07o7yp3, Sep 13, 2013.

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

    Pr07o7yp3

    Is this way good to give tool kit to player when first time join world?

    Code:java
    1. @EventHandler
    2. public void onWorldChange(PlayerChangedWorldEvent event)
    3. {
    4. final Player player = event.getPlayer();
    5.  
    6. if (player.getWorld().getName().equalsIgnoreCase("pvp")
    7. {
    8. File plydat = new File("pvp/players/" + player.getName() + ".dat");
    9. if (!plydat.exists())
    10. {
    11. getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    12. @Override
    13. public void run() {
    14. if (player != null)
    15. {
    16. if (player.getWorld().getName().equalsIgnoreCase("pvp")
    17. {
    18. Inventory inv = player.getInventory();
    19.  
    20. for (int i = 268; i <= 271; i++)
    21. if (i != 269)
    22. inv.addItem(new ItemStack(i, 1));
    23.  
    24. for (int i = 298; i <= 301; i++)
    25. inv.addItem(new ItemStack(i, 1));
    26.  
    27. inv.addItem(new ItemStack(364, 32));
    28.  
    29. ItemStack potInvis = new ItemStack(373, 3);
    30. potInvis.setDurability((short)8206);
    31. inv.addItem(potInvis);
    32. }
    33. }
    34. }
    35. }, 50L);
    36. }
    37. }
    38. }
     
  2. Offline

    CubieX

    Code:
     for (int i = 268; i <= 271; i++)
    if (i != 269)
    inv.addItem(new ItemStack(i, 1));
     
    for (int i = 298; i <= 301; i++)
    inv.addItem(new ItemStack(i, 1));
    Your method is a bit messy by defining a range and then excluding some items with if().
    You can do this. However, it would be more flexible and easier to define kits by using a set or a list of item ids or their material name.
    With a HashMap for each kit, you could predefine item ids and an amount for each.

    Code:
    HashMap<Integer, Integer> myKit = new HashMap<Integer, Integer>();
    And in some method fill the map with the kits items:
    Code:
    myKit.put(268, 1);
    myKit.put(270, 1);
    myKit.put(271, 2);
    You can then use a for-loop to iterate through the map and add the item with the given amount to the players inventory.
    Example:
    Code:
    for(Integer itemID : myKit.keySet())
    {
      player.getInventory().addItem(new ItemStack(itemID, myKit.get(itemID));
    }
    I guess you know that .addItem() will return a HashMap<Integer, ItemStack> with all items that could not be added to the players inventory (because it's full)?
    You may want to check if this returned map is empty, to be sure all items have been added.
     
  3. Offline

    Pr07o7yp3

    Ok, ty for the useful information.
    But my main question was if is a good idea to set variable player as a final and use it in schedul delayed task?
    I ask that because I never did that before. :)

    Maybe the question is stupid but I'm new in the Java. :D
     
  4. Offline

    kreashenz

    Pr07o7yp3 Yes. It's usually needed is it's a local variable.
     
  5. Offline

    CubieX

    Then why did you not ask this in the first place? :p

    As kreashenz said: You have to use a "final" variable here, because it's a local one.
    This makes sure, your task will not use some newly assigned player instance in its "run()" method,
    when several of these tasks are scheduled simulataneously.
    If "player" was a field, then you would not need a "final" declaration here.
     
  6. Offline

    Pr07o7yp3

    Ok, ty guys. :)
     
Thread Status:
Not open for further replies.

Share This Page