Solved Invsee with correct layout?

Discussion in 'Plugin Development' started by Datdenkikniet, Jan 5, 2015.

Thread Status:
Not open for further replies.
  1. So, I'm trying to create an invsee command. I know how to open an inventory and show it to others, but I have one problem with it: the items which are in a player's hotbar are displayed at the top of the inventory opened upon using /invsee. Is there any way to change that?
    This is what I tried (didn't work):
    what I tried (open)

    Code:java
    1.  
    2. package me.DoraKlikOpDora.LemonSystem.commands;
    3.  
    4. import me.DoraKlikOpDora.LemonSystem.Main;
    5.  
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.inventory.Inventory;
    11. import org.bukkit.inventory.ItemStack;
    12.  
    13. public class InvSeeCommand implements CommandExecutor {
    14. Main plugin;
    15. public InvSeeCommand(Main instance){
    16. plugin = instance;
    17. }
    18. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    19. if (!(sender instanceof Player)){
    20. sender.sendMessage("You can't do this as console!");
    21. return true;
    22. }
    23. if (sender.hasPermission(plugin.invseeperm) || sender.hasPermission(plugin.universalPerm)){
    24. if (args.length == 1){
    25. Player toOpen = plugin.searchPlayer(args[0]);
    26. if (toOpen == null){
    27. sender.sendMessage(plugin.notOnline);
    28. return true;
    29. } else {
    30. Player player = (Player) sender;
    31. ItemStack[] contents = toOpen.getInventory().getContents();
    32. Inventory inv = plugin.getServer().createInventory(null, 9*4, args[0] + "\'s inventory");
    33. ItemStack[] referral = contents;
    34. for (int i = 0; i < referral.length; i++){
    35. if (i < 9){
    36. //shifting items in the hotbar three lines down
    37. contents[I] = referral[i+27];
    38. System.out.println(String.valueOf(i) + ">" + String.valueOf(i+27));
    39. } else {
    40. //shift items not in hotbar one line upwards
    41. contents[I] = referral[i-9];
    42. System.out.println(String.valueOf(i) + ">" + String.valueOf(i-9));
    43. }
    44. }
    45. inv.setContents(contents);
    46. plugin.openedInventories.put(inv.getTitle(), toOpen.getUniqueId());
    47. player.openInventory(inv);
    48. return true;
    49. }
    50. } else {
    51. sender.sendMessage(plugin.wrongArgumentsAmount);
    52. return true;
    53. }
    54. } else {
    55. sender.sendMessage(plugin.noperm);
    56. return true;
    57. }
    58. }
    59. }
    60.  
    61. [/I][/I]


    What I get from the code above is arranged like this: the hotbar and top two lines of the player's inventory are ignored, and the item in the remaining line fills up all of the space in it's own position in the inventory you look at (picuture)
     
    Last edited: Jan 5, 2015
  2. @Datdenkikniet You could make your own contents array rather than using the one returned by getContents(). Well, you'd probably still end up using the getContents() and just rearranging them.
     
    Datdenkikniet likes this.
  3. @AdamQpzm huh (added some infos to the OP, forgot some things :s)
     
  4. @Datdenkikniet Loop over the player's inventory, and make your own map/array/whatever. This will allow you to set the format to your heart's content :)
     
    Datdenkikniet likes this.
  5. @AdamQpzm but I did....
    (see line 31, I create my own ItemStack[] containing the inventory contents, and then rearrange them in the for loop (although this doesn't work properly))
     
  6. @Datdenkikniet Have you debugged that code? Hint: By doing ItemStack[] referral = contents you haven't created a new Object, you've created a new reference to that Object. If they're the same Object, changing one naturally changes the other ;)
     
    Datdenkikniet likes this.
  7. @AdamQpzm oh, didn't know that.... (I thought it wouldn't aswell change the other object :s)
    dammit..
    Thanks for helping me (I'll get it to work now :D) (contents.clone() does work)
     
    AdamQpzm likes this.
  8. I'm glad you've fixed your problem, but want to quickly clarify this point here, since it's pretty fundamental in OOP languages. You're right, changing one Object wouldn't change the other Object. But you have just one Object here, not two. What you actually have is two references to the same Object.

    Let me use an analogy to try and make sure that makes sense. Let's say that the Object returned by getContents() is actually a house, and the variables "contents" and "referral" are two different addresses to that house, both of which are somehow valid addresses. We'll call those addresses 1 Blue Road, and 2 Blue Road. If somebody asks me to paint the house at 1 Blue Road red, then the house at 2 Blue Road also gets painted (because they're the same house, just a different address).

    If however there were two houses and two addresses - 1 Blue Road represents the Object returned by getContents() and 2 Blue Road represents the Object returned by cloning the previous Object - the situation is different. If someone asks me to paint 1 Blue Road, then 2 Blue Road doesn't change, because it's a different house so why would it? This is the same with Objects and references. When you call methods, it doesn't matter what reference you're using, only what Object you're using - just as it doesn't matter what address I'm painting, it's what house I'm painting.

    Does that make sense? :p
     
    Datdenkikniet likes this.
  9. @AdamQpzm yes, but it already did when I you said that I created a reference to an object, not a new object :)
    (Like your explanation though)
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page