Custom Inventory Code Help

Discussion in 'Plugin Development' started by MCJoshua345, Jan 27, 2015.

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

    MCJoshua345

    Hello! Lucky everyone, today I'm not exactly here with a error, but I have a teensy problem. I have this code when a player joins:
    http://pastebin.com/reJJ2BaR
    But... there's a few problems...
    1. I have a NoDrop plugin, but someone can open their inventory, and drag it out, click outside of the their inventory, and it vanishes into nothing.
    2. For the code where it's supposed to prevent the person from dragging the item out of the custom inventory and into their inventory., it only works for the referenced "factiontnt". For the "factiontnt" when you click it closes the inventory. For the others, you can just drag it right out and into your own inventory. You can put it back in any spot as well. I guess the code works only for the "factiontnt" because it is first?
    3. The plugin is custom for my BungeeCord Server. I've seen one plugin: http://www.spigotmc.org/resources/lobby-switch.874/ that makes a custom inventory and item for you, and the code links up with your BungeeCord server. When you click a certain item in the custom inventory that is accessed by the item it gives you, you teleport to the server
    4. *EXTRA NOT NEEDED* When you log on, and your username is MCJoshua345, then it says a custom join message. I tried this, and I think it would've worked if I didn't have this prefix code:
    http://pastebin.com/txP33BWk
    Thanks for the help!

    Keep Blocking!
    - MCJoshua345
     
    Last edited: Jan 27, 2015
  2. Offline

    Experminator

    Two things first:
    - Please write better the post.
    - Your code is not really good to read.

    ON TOPIC:
    - You don't need to reset the color, and then set it to a different color.
     
  3. Offline

    MCJoshua345

    @Experminator
    I know, probably leftover from the time when I used a bit to much Bold... It's just leftover and I'm too lazy too clean it up LOL.
    Your code is not really good to read? If you mean like it's too much clammed together, i can fix that.
     
    Experminator likes this.
  4. Offline

    mine-care

    @MCJoshua345 well yes your code is a bit unreadable. Also I noticed you copy paste code over and over and over
    http://pastebin.com/reJJ2BaR lines 60 to 81
    Just add a if(is equal to inventory's name){
    switch(item's type){
    case xxxx:
    Do stuff
    Break;
    }
    }
    This way it will be more readable and efficient because how it is now makes the same check 5 times in 1 event call.
    The error is tat you put ifs one in the other so if its a TNT , you cancel close the inv and then check if its sword (impossible since it is TNT) I sugest you repeat to study if you haven't already, the java basics of id's nested ifs and switch statement.
     
    Experminator likes this.
  5. Offline

    DemKazoo

    GOD MAKE IT STOP! PLEASE USE
    Code:
     ARGH!
     
    Experminator likes this.
  6. Offline

    Experminator

    @MCJoshua345 Please add some spaces/lines between the code, it's for me ONE block of CODE.
    I can't see where the methods ending....
    or beginning... (To much in a row, and too much curly braces).

    One thing i can read in the block of code: And a little note:
    * There is a 'setMessage()' method in PlayerJoinEvent. That would be called when a player joins.
    * You can use that instead of:
    Code:
    Bukkit.broadcastMessage("" + ChatColor.GOLD + pname + ChatColor.BOLD + ChatColor.GREEN + " has joined the server!");
    @MCJoshua345 I have formatted some code, and added a example what you could use:
    Code:
     @EventHandler
            public void onInventoryClick(InventoryClickEvent event){
                Player player = (Player) event.getWhoClicked();
                ItemStack clicked = event.getCurrentItem();
                Inventory inventory = event.getInventory();
    
                if(inventory.getName().equals(myInventory.getName())){
                   if(clicked.getType() != null || clicked.getType() != Material.AIR){
                      e.setCancelled(true);
                      player.closeInventory();
         
                      switch(clicked.getType()){
                         case STONE_SWORD:
                             // Do Stuff When Player Clicks On Stone Sword.
                             break;
                          // You can add more 'case' statements under each other. Like here.
                      }
                   }
                }
            }
    Note To Everone That Calls This Spoonfeed: This is not spoonfeed. I have only formatted the code, and add a little example of the switch statement.

    @mine-care is awesome! :p (Random Stuff At Here)

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

    MCJoshua345

    New Code:
    Code:
    //JoinEvent
    
    @EventHandler
      public void PlayerJoin(PlayerJoinEvent e) {
        Player p = e.getPlayer();
        String pname = p.getName();
      
    //Clear Inventory
    
        p.getInventory().clear();
    
    //Join Message
    
        Bukkit.broadcastMessage("" + ChatColor.GOLD + pname + ChatColor.BOLD + ChatColor.GREEN + " has joined the server!");
    
    //Server Selector
    
        ItemStack serverselector = new ItemStack(Material.WATCH);
        ItemMeta c = serverselector.getItemMeta();
        c.setDisplayName("" + ChatColor.GOLD + ChatColor.BOLD + "SERVER SELECTOR");
        serverselector.setItemMeta(c);
        p.getInventory().setItem(4, serverselector); }
    
    //When You Right Click a Clock, Open inventory
    
            @EventHandler
            public void onPlayerInteract(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            if (p.getItemInHand() != null) {
            ItemStack item = p.getItemInHand();
            if (item.getType() == Material.WATCH) {
            p.openInventory(myInventory);
            }
            }
            {}
                      }
    
    //Inventory
    
                    public static Inventory myInventory = Bukkit.createInventory(null, 18, "ServerSelector");
              static {
                     //Make it to where when you click one of the items/blocks, you teleport to the corresponding server.
                     ItemStack factiontnt = new ItemStack(Material.TNT);
                     ItemMeta m = factiontnt.getItemMeta();
                     m.setDisplayName("" + ">>" + ChatColor.RED + " Factions " + ChatColor.RESET + "<<");
                     factiontnt.setItemMeta(m);
                     myInventory.setItem(1, factiontnt);
                     ItemStack kitpvpsword = new ItemStack(Material.DIAMOND_SWORD);
                 ItemMeta a = kitpvpsword.getItemMeta();
                 a.setDisplayName("" + ">>" + ChatColor.GOLD + " KitPVP " + ChatColor.RESET + "<<");
                 kitpvpsword.setItemMeta(a);
                 myInventory.setItem(3, kitpvpsword);
                 ItemStack copsnrobbersironbars = new ItemStack(Material.IRON_FENCE);
                 ItemMeta b = copsnrobbersironbars.getItemMeta();
                 copsnrobbersironbars.setItemMeta(b);
                 b.setDisplayName("" + ">>" + ChatColor.GRAY + " Cops N' Robbers " + ChatColor.RESET + "<<");
                 copsnrobbersironbars.setItemMeta(b);
                 myInventory.setItem(5, copsnrobbersironbars);
                 Wool wool = new Wool(DyeColor.YELLOW);
                 ItemStack stack = wool.toItemStack(1);
                 ItemMeta c = stack.getItemMeta();
                 c.setDisplayName("" + ">>" + ChatColor.YELLOW + " Pizza Spleef " + ChatColor.RESET + "<<");
                 stack.setItemMeta(c);
                 myInventory.setItem(7, stack);
                 ItemStack botbblock = new ItemStack(Material.IRON_SWORD);
                 ItemMeta z = botbblock.getItemMeta();
                 z.setDisplayName("" + ">>" + ChatColor.WHITE + " BOTB " + ChatColor.RESET + "<<");
                 botbblock.setItemMeta(z);
                 myInventory.setItem(13, botbblock);
              }
    
    //Click preventor
    
              //Fix this!
            
    //TNT
    
              @EventHandler
              public void onInventoryClick(InventoryClickEvent event) {
              Player player = (Player) event.getWhoClicked(); // The player that clicked the item
              ItemStack clicked = event.getCurrentItem(); // The item that was clicked
              Inventory inventory = event.getInventory(); // The inventory that was clicked in
              if (inventory.getName().equals(myInventory.getName())) { // The inventory is our custom Inventory
              if (clicked.getType() == Material.TNT) { // The item that the player clicked it dirt
              event.setCancelled(true); // Make it so the dirt is back in its original spot
              player.closeInventory(); // Closes there inventory
            
    //Diamond Sword
    
              if (inventory.getName().equals(myInventory.getName()))
              if (clicked.getType() == Material.DIAMOND_SWORD) { // The item that the player clicked it dirt
              event.setCancelled(true); // Make it so the dirt is back in its original spot
              player.closeInventory(); // Closes there inventory
            
    //Iron Bars
    
              if (inventory.getName().equals(myInventory.getName()))
              if (clicked.getType() == Material.IRON_FENCE) { // The item that the player clicked it dirt
              event.setCancelled(true); // Make it so the dirt is back in its original spot
              player.closeInventory(); // Closes there inventory
    
    //Wool
    
              if (inventory.getName().equals(myInventory.getName()))
              if (clicked.getType() == Material.WOOL) { // The item that the player clicked it dirt
              event.setCancelled(true); // Make it so the dirt is back in its original spot
              player.closeInventory(); // Closes there inventory
              if (inventory.getName().equals(myInventory.getName()))
    
    //Iron Sword
    
              if (clicked.getType() == Material.IRON_SWORD) { // The item that the player clicked it dirt
              event.setCancelled(true); // Make it so the dirt is back in its original spot
              player.closeInventory(); // Closes there inventory
           
              }
              }
              }
              }
              }
              }
              }
    Working on recommended solutions. Thanks!
    @DemKazoo
    Like the code format now?
    @Experminator
    I tried to split it up the best I could. Thanks for the coding example as well.

    BTW, guys, sorry I'm such a noob/amateur... Just started out, learned everything I know about coding from YT vids... LOLZ

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 28, 2015
  8. Offline

    Experminator

    @MCJoshua345 It's not a problem! You may learn. We all beginning are Noob!

    But i want to know which youtuber you watch. Not every youtuber is good, and some youtubers are really bad.

    Good Youtuber's:
    * PogoStick29Dev
    * TheNewBoston

    Bad Youtuber's (I don't link to this channels.)
    * TheBCBroz

    If you want i can help you with learning, but i don't write a plugin for you without request.
     
  9. Offline

    MCJoshua345

    Hmm. I get a warning at
    Code:
    switch(clicked.getType()){
    So there's exactly a hundred errors. Each saying that I need to provide what happens for every single other item in the game... *ScreamSoLoudItBrokeTheSoundBarrierAndNoOneCouldHearIt*
    So, I really hope there is a workaround for this... Current code:
    Code:
          @EventHandler
          public void onInventoryClick(InventoryClickEvent e){
              Player p = (Player) e.getWhoClicked();
              ItemStack clicked = e.getCurrentItem();
              Inventory inventory = e.getInventory();
    
              if(inventory.getName().equals(myInventory.getName())){
                 if(clicked.getType() != null || clicked.getType() != Material.AIR){
                    e.setCancelled(true);
                    p.closeInventory();
     
                    switch(clicked.getType()){
                       case TNT:
                           e.setCancelled(true);
                           p.closeInventory();
                           break;
                       case DIAMOND_SWORD:
                           e.setCancelled(true);
                           p.closeInventory();
                           break;
                       case IRON_FENCE:
                           e.setCancelled(true);
                           p.closeInventory();
                           break;
                       case WOOL:
                           e.setCancelled(true);
                           p.closeInventory();
                           break;
                       case IRON_SWORD:
                           e.setCancelled(true);
                           p.closeInventory();
                           break;
                    }
                 }
              }
          }
    Screenshot of errors:
    http://imgur.com/gRbkoSH
    WOW. I know, right?
     
  10. Offline

    Experminator

    @MCJoshua345 I want to know know things:
    - Which import for Material did you use?
    - Why do you use that switch in this case? A One if statement is enough to handle this.

    Better way (Wow i correct my own code...):
    Code:
    Material type = clicked.getType();
    
    if(type == Material.IRON_FENCE || type == Material.STONE_SWORD){ // Add more here, then it would be cancelled.
        e.setCancelled(true);
        player.closeInventory();
    }
     
  11. Offline

    MCJoshua345

    LoonyRules | Bukkit Plugin Developer
    https://www.youtube.com/user/nGxIEpiiKZzx
    Funny, I watched TheNewBoston for Visual Basic Tutorials. He's awesome.
     
  12. Offline

    Experminator

    @MCJoshua345 I don't know him,but i will watch it to review his channel.
    But i prefer to PogoStick29Dev.
     
  13. Offline

    MCJoshua345

    1. import org.bukkit.Material;
    2. Because that's what you put in the code, and when I converted it into if and added a != null statement it showed errors for the cases.
     
  14. Offline

    Experminator

    What's your code now?
     
  15. Offline

    MCJoshua345

    @Experminator
    My code is:
    Code:
          }
          @EventHandler
          public void onInventoryClick(InventoryClickEvent e){
              Player p = (Player) e.getWhoClicked();
              ItemStack clicked = e.getCurrentItem();
              Inventory inventory = e.getInventory();
    
              if(inventory.getName().equals(myInventory.getName())){
                 if(clicked.getType() != null || clicked.getType() != Material.AIR){
                    e.setCancelled(true);
                    p.closeInventory();
     
                    Material type = clicked.getType();
                  
                    if(type == Material.TNT || type == Material.DIAMOND_SWORD || type == Material.IRON_FENCE || type == Material.WOOL || type == Material.IRON_SWORD){ // Add more here, then it would be cancelled.
                        e.setCancelled(true);
                        p.closeInventory();
                    }
                    }
                 }
              }
    It works perfectly! Now for the remaining things
    1. I have a NoDrop plugin, but someone can open their inventory, and drag it out, click outside of the their inventory, and it vanishes into nothing.
    2. The plugin is custom for my BungeeCord Server. I've seen one plugin: http://www.spigotmc.org/resources/lobby-switch.874/ that makes a custom inventory and item for you, and the code links up with your BungeeCord server. When you click a certain item in the custom inventory that is accessed by the item it gives you, you teleport to the server
    43 *EXTRA NOT NEEDED* When you log on, and your username is MCJoshua345, then it says a custom join message. I tried this, and I think it would've worked if I didn't have this prefix code:

    Code:
    @EventHandler
    public void playerChat(PlayerChatEvent e) {
            Player p = e.getPlayer();
            String pname = p.getName();
    if (p.hasPermission("lobby.member")) {
            String member = "" +"[" + ChatColor.GRAY + "Mortal" + ChatColor.RESET +  "] " + pname + "";
            p.setDisplayName(member);
    } else if (p.hasPermission("lobby.mod")) {
            String mod = "" + ChatColor.BLACK + "[" + ChatColor.RED + "Hellhound" + ChatColor.BLACK + "] " + ChatColor.RESET + ChatColor.GOLD + pname + ChatColor.RESET + "";
            p.setDisplayName(mod);
    } else if (p.hasPermission("lobby.builder")) {
        String builder = "" + ChatColor.BLACK + "[" + ChatColor.GREEN + "Cyclop" + ChatColor.BLACK + "] " + ChatColor.GOLD + pname + ChatColor.RESET + "";
        p.setDisplayName(builder);
    } else if (p.hasPermission("lobby.admin")) {
        String admin = "" + ChatColor.BLACK + "[" + ChatColor.DARK_RED + ChatColor.BOLD + "Minor God" + ChatColor.RESET + ChatColor.BLACK + "] " + ChatColor.GOLD +  pname + ChatColor.RESET + "";
        p.setDisplayName(admin);
    } else if (p.hasPermission("lobby.coowner")) {
            String coowner = "" + ChatColor.BLACK + "[" + ChatColor.DARK_BLUE + ChatColor.BOLD + "POSEIDON" + ChatColor.RESET + ChatColor.BLACK + "]" + ChatColor.BLACK + "[" + ChatColor.DARK_GREEN + ChatColor.BOLD + "Elder Cyclop" + ChatColor.RESET + ChatColor.BLACK + "] " + ChatColor.GOLD + pname + ChatColor.RESET + "";
            p.setDisplayName(coowner);
    } else if (p.hasPermission("lobby.owner")) {
        String owner = "" + ChatColor.BLACK + "[" + ChatColor.GOLD + ChatColor.BOLD + "ZEUS" + ChatColor.RESET + ChatColor.BLACK + "]" + ChatColor.RESET + ChatColor.BLACK + "[" + ChatColor.GREEN + "Cyclop" + ChatColor.RESET + ChatColor.BLACK +  "] " + ChatColor.GOLD + pname + ChatColor.RESET + "";
        p.setDisplayName(owner);
    }
    }
    I'll do some studies on the Lobby Switch plugin code.
    UPDATE: The LobbySwitch code is too confusing for a noob...
    UPDATE 2: I got the src, I fixed all the errors except one... Giving up on project...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  16. Offline

    Experminator

    @MCJoshua345 Use AsyncPlayerChatEvent. -- PlayerChatEvent is deprecated.

    @MCJoshua345 A Lobby Switch plugin is not really hard to code. It's fine to use BungeeCord with it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  17. Offline

    MCJoshua345

    @Experminator
    Quick question, from a noob to a (from the looks of it) master, what's the difference between deprecated and not deprecated.
    Also, I did add the AsyncPlayerChatEvent
    Do you know some pointers to a guide to code a Lobby Switch plugin?
     
  18. Offline

    Experminator

  19. Offline

    MCJoshua345

    @Experminator
    K, thanks, but about the Lobby Switch plugin coding? Sorry to be such a bother
     
  20. Offline

    Experminator

    Last edited by a moderator: Jan 30, 2015
  21. Offline

    BurnerDiamond

    @Experminator

    Out of pure curiosity why is BCBroz a bad YouTuber?
     
  22. Offline

    Experminator

    @BurnerDiamond Ben (TheBCBroz), write code but it's a way that can give Exceptions, and another bad things.
    Simply: He learn people the bad way how to use Java.
     
  23. Offline

    MCJoshua345

    @Experminator
    So where do I put this code and how do I use it o_o

    By the way, I'm on it's just glitching...

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jan 30, 2015
  24. Offline

    Experminator

    @MCJoshua345 I don't think you must begin with BungeeCord now...
     
  25. Offline

    MCJoshua345

    You mean a BungeeCord plugin? Or a regular Bukkit one. And why? I need more experience?
     
  26. Offline

    Experminator

    @MCJoshua345 Sorry for confusing, but i talk about BungeeCord, and given an example about Connect players to other servers.

    But i think you are not ready for coding in BungeeCord... Not enough experience.
     
  27. Offline

    MCJoshua345

    Hmmmm. Oh well. By the way, do you know which one of these plugins would allow you to teleport with a compass? Because this is my Lobby Switch 0.3 config:
    Code:
    InventoryRows: 1
    InventoryName: Server Selector
    ItemStack:
      ==: org.bukkit.inventory.ItemStack
      type: COMPASS
      meta:
        ==: ItemMeta
        meta-type: UNSPECIFIC
        display-name: "\xa74Server Selector"
    Servers:
    - TNT:1:Factions:factionsgreen:4:1
    - PAPER:1:Lobby 2:lobby2:a:2
    Version: '0.3'
    Unfortunately when you left-click the compass, you can tp out of the huge glass dome....
    When I try to change the type the console whines up an error about the ItemStack and changes it back to compass.
    Does the same thing when I try to change the Server Selector's name using color codes. Do you know anything about this plugin and it's code?
     
  28. Offline

    Experminator

    That teleporting through the class dome, is because Compasses has a vanilla feature to teleport, if player is op.

    And no, i don't know that plugin.
     
  29. Offline

    MCJoshua345

    @Experminator
    It must be a permission, because I'm not OP. I use PermissionsEX. Well, regular Members can't teleport, so I guess it's okay. But I'd really like to change it to a clock and change the color...
     
  30. Offline

    timtower Administrator Administrator Moderator

    Locked. Bungeecord requires offline mode which is not supported by Bukkit.
     
Thread Status:
Not open for further replies.

Share This Page