Tutorial Create a Navigator!

Discussion in 'Resources' started by BaconStripzMan, Feb 1, 2015.

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

    BaconStripzMan

    Hey guys! My name is Bacon, also known as Jordan!
    Today I'm going to be showing you how to create a Navigator!

    So first we're gonna do this in your class!
    Code:java
    1. public class Main extends JavaPlugin implements Listener /* In your class implements Listener! (If you use a seperate class do not use extends JavaPlugin in it! */ {
    2. @Override
    3. public void onEnable(){
    4. getServer().getPluginManager().registerEvents(this, this);
    5. //Or if you wanna use a seperate class
    6. getServer().getPluginManager().registerEvents(new EventClass(), this);
    7. }
    8. }


    Then we're going to create an inventory!
    Code:java
    1. public Inventory navigator = Bukkit.createInventory(null, 9 /*Size <-*/, "This is the name");


    Now lets add our items!
    Code:java
    1. //In our onEnable
    2. ItemStack navigatoritem = new ItemStack(Material.DIAMOND_AXE);
    3. ItemMeta navigatoritemmeta = navigatoritem.getItemMeta();
    4. navigatoritemmeta.setDisplayName("§6Survival Games");
    5. navigatoritemmeta.setLore(Arrays.asList("§fBattle it out against 23 others!", "§fThere will be only one survivor.."));
    6. navigatoritem.setItemMeta(navigatoritemmeta);
    7.  
    8. navigator.setItem(0, navigatoritem);


    Now lets let them open the navigator!

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e){
    3. Player p = e.getPlayer();
    4. Action a = e.getAction();
    5. Material m = p.getItemInHand().getType();
    6. if(a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK){
    7. if(m == null){
    8. return;
    9. } else if(m == Material.COMPASS && p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("§6Navigator")){
    10. p.openInventory(navigator);
    11. }
    12. }
    13. }


    Now let's setup the clicking item!

    Code:java
    1. @EventHandler
    2. public void onPlayerClick(InventoryClickEvent e){
    3. Material m = e.getCurrentItem().getType();
    4. if(e.getInventory().equals(navigator)){
    5. e.setCancelled(true);
    6. if(m == Material.DIAMOND_AXE && e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§6Survival Games")){
    7. //Do stuff here
    8. }
    9. }
    10. }


    And there you go! You now have a navigator! Thank you for reading this, if you have any issues or need help just comment!

    Pastebin: http://pastebin.com/HEYxwaYA
     
    Last edited: Feb 3, 2015
  2. Offline

    teej107

    And I now have a NullPointerException because I didn't have an item in my hand.
     
    API_Tutorials and nverdier like this.
  3. Offline

    BaconStripzMan

    Well, what does it matter?
     
  4. Offline

    nverdier

  5. Offline

    BaconStripzMan

  6. Offline

    nverdier

  7. Offline

    BaconStripzMan

    Well, it gives no errors. So what does it matter, the code still works!
     
  8. Offline

    nverdier

  9. Offline

    Monkey_Swag

    why
     
    nverdier likes this.
  10. Offline

    BaconStripzMan

    If you don't set the items in a method it will not let you use navigatoritemmeta.whatever
     
  11. Offline

    nverdier

    @BaconStripzMan Why the static? That was the big, bolder thing... Did you not see it?
     
  12. Offline

    BaconStripzMan

    Try using itemmetas in a non-static method :p @nverdier
     
  13. Offline

    nverdier

    @BaconStripzMan What, is it difficult or something? I don't know why your telling me to do that... Statics aren't good in this situation, you realize.
     
  14. Offline

    BaconStripzMan

    Does it work? Yes..
     
  15. Offline

    nverdier

  16. Offline

    Monkey_Swag

    it's very bad coding conventions to use 'static' when not needed. In most situations, (when using the Bukkit API) you would only use static when messing with NMS. You do not need to use static on this.
     
  17. Offline

    BaconStripzMan

    You know what stuff the forums, I never get any helpful feedback or support, I'm outta here
     
  18. Offline

    nverdier

    @BaconStripzMan What we are saying is very helpful. Are you trying to make us feel bad or something?
     
  19. Offline

    BaconStripzMan

    No, maybe you could tell me how to fix it and stop arguing with something that isn't true. I've said if you don't use your item setting in a method you have got to use the static method and to set the item in a static method you have to use public "static" Inventory...

    EDIT: Fixed the NullPointerException
     
  20. Offline

    nverdier

    @BaconStripzMan What you have is all in the same class, yes? Why do you need statics at all? Just remove them.
     
  21. Offline

    BaconStripzMan

    Image 1 (No Statics):
    [​IMG]
    Image 2:
    [​IMG]
    @nverdier
     
  22. Offline

    nverdier

  23. Offline

    BaconStripzMan

    Ok thank you :D!
     
    nverdier likes this.
  24. Offline

    ChipDev

    Really.
    Im so excited when I click on an empty part of my inventory, and Im scanning the console (READING A SUPER IMPORTANT MESSAGE) and well, Then I get a tsunami of errors
    NPE AT LINE 15! ERROR ERROR!

    'Static{'
    really? Do you need to risk using static?

    Also, in the image..
    you have errors l.l
     
  25. Offline

    BaconStripzMan

    If you actually read my post you would of seen I fixed the errors.. So yeah?
     
  26. Offline

    ChipDev

    So yeah.
     
  27. Offline

    BaconStripzMan

    I no longer see any errors, do you?
     
  28. Offline

    ChipDev

     
  29. Offline

    BaconStripzMan

    Last edited: Feb 2, 2015
    ChipDev likes this.
  30. Offline

    MiniDigger

Thread Status:
Not open for further replies.

Share This Page