I need help

Discussion in 'Plugin Development' started by Fanatixteam, Oct 28, 2014.

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

    Fanatixteam

    Hello I have a problem.
    In the event that I put Piston_base or NOTE_Block I Have The global inventar these chests.
    I need that every chest had its inventory.
    How to do it?

    main class
    Code:java
    1. public void onEnable()
    2. {
    3. inv = Bukkit.createInventory(null, 27, "§lBonus Chest");
    4. System.out.println("[BonusChest] Plugin zapnut");
    5. PluginManager pm = getServer().getPluginManager();
    6. pm.registerEvents(new me.Hugo.Listener.ChestListener(), this);
    7. }
    8.  
    9. public void onDisable()
    10. {
    11. System.out.println("[BonusChest] Plugin vypnut");
    12. }


    Chest class
    Code:java
    1. public class ChestListener
    2. implements Listener
    3. {
    4. @EventHandler
    5. public void onClick(PlayerInteractEvent e)
    6. {
    7. Player p = e.getPlayer();
    8. if ((p.hasPermission("playerchest.use")) || (p.isOp()))
    9. {
    10. if ((main.chestworld.contains(p.getLocation().getWorld().getName())) &&
    11. (e.getAction() == Action.RIGHT_CLICK_BLOCK) && (e.getClickedBlock().getType() == Material.PISTON_BASE)) {
    12. p.openInventory(main.inv);
    13. }
    14. }
    15. else {
    16. p.sendMessage(ChatColor.translateAlternateColorCodes('&', main.prefix) + " " + ChatColor.translateAlternateColorCodes('&', main.nopermschest));
    17. }
    18. if ((p.hasPermission("vipchest.use")) || (p.isOp()))
    19. {
    20. if ((main.chestworld.contains(p.getLocation().getWorld().getName())) &&
    21. (e.getAction() == Action.RIGHT_CLICK_BLOCK) && (e.getClickedBlock().getType() == Material.NOTE_BLOCK)) {
    22. p.openInventory(main.inv);
    23. }
    24. }
    25. else {
    26. p.sendMessage(ChatColor.translateAlternateColorCodes('&', main.prefix) + " " + ChatColor.translateAlternateColorCodes('&', main.nopermschest));
    27. }
    28. }
    29. }
    30.  
     
  2. For saving/loading inventories to files:
    Code:java
    1.  
    2. public void createFile(Location loc) {
    3. int x = loc.getX();
    4. int y = loc.getY();
    5. int z = loc.getZ();
    6.  
    7. File file = new File(getDataFolder() + "/chests", x+","+y+","+z+ ".yml");
    8. if (!file.exists()) {
    9. FileConfiguration config = YamlConfiguration.loadConfiguration(file);
    10.  
    11. config.set("Items.0", new ItemStack(Material.AIR));
    12.  
    13. try {
    14. config.save(file);
    15. } catch(Exception e) {
    16. }
    17. }
    18. }
    19.  
    20. public boolean exists(Location loc) {
    21. int x = loc.getX();
    22. int y = loc.getY();
    23. int z = loc.getZ();
    24. return new File(getDataFolder() + "/chests", x+","+y+","+z+ ".yml").exists();
    25. }
    26.  
    27. public Inventory getInventory(Location loc) {
    28. int x = loc.getX();
    29. int y = loc.getY();
    30. int z = loc.getZ();
    31.  
    32. File file = new File(getDataFolder() + "/chests", x+","+y+","+z+ ".yml");
    33. if (!file.exists()) return null;
    34.  
    35. FileConfiguration config = YamlConfiguration.loadConfiguration(file);
    36.  
    37. Inventory inv = Bukkit.createInventory(null, 27, "Chest");
    38. for (String key : config.getConfigurationSection("Items").getKeys(false)) {
    39. try {
    40. inv.setItem(Integer.parseInt(key),
    41. config.getItemStack("Items." + key));
    42. } catch (Exception e) {
    43.  
    44. }
    45.  
    46. return inv;
    47. }
    48.  
    49. public void saveChest(Location loc) {
    50. int x = loc.getX();
    51. int y = loc.getY();
    52. int z = loc.getZ();
    53.  
    54. if(exists(loc)) {
    55. File file = new File(getDataFolder() + "/chests", x+","+y+","+z+ ".yml");
    56. FileConfiguration config = YamlConfiguration.loadConfiguration(file);
    57. Inventory inv = getInventory(loc);
    58.  
    59. for(int i = 0; i < 27; i++) {
    60. if(loc.getItem(i) == 0) {
    61. config.set("Items." + i, null);
    62. } else {
    63. config.set("Items." + i, e.getInventory().getItem(i));
    64. }
    65. }
    66.  
    67. try {
    68. config.save(file);
    69. } catch (Exception ex) {
    70.  
    71. }
    72. }
    73. }
    74.  
     
    HungerCraftNL likes this.
  3. FisheyLP
    1) Please don't spoon-feed
    2) See the File(File, String) constructor
    3) Is that even what the OP is asking?
     
  4. Offline

    Fanatixteam

    Edit my code, where is the eror ?
    One Chest only one inventory code please
     
  5. then add a HashMap<Location, Inventory> to store the locations of the piston_base and create a inventory for it
     
  6. Offline

    CraftCreeper6

    FisheyLP
    The profile pictures says all.
    <<<<<<<<<<<<<<<<<<<<
     
  7. this for loop... your profile picture says all. why not just while(true) {
     
  8. FisheyLP while(true) is an infinite loop, for(int i = 0; i > -1; i++) is not. Not sure if that's an error on CraftCreeper6 's part or intended :p
     
  9. Offline

    CraftCreeper6

    FisheyLP AdamQpzm
    I think it's more a factor of Signatures are not supposed to be serious.

    EDIT: What does my Signature have to do with my Profile Picture?
     
  10. 0 is always bigger than -1 so its probably infinite
     
  11. FisheyLP Once it hits the maximum value, it restarts from the minimum value, which is less than -1. It's a large amount of iterations, but finite.
     
  12. that would take years to get the maximum value ^^ and i dont think that much people runs programs / plugins for more than a day without closing it
     
  13. Offline

    CraftCreeper6

    FisheyLP
    I think you'll find that it wont take years, it will take a couple of seconds depending on how much RAM your server has...
     
  14. Would depend on what the methods actually do. If it's something simple, then it definitely wouldn't take years.

    CraftCreeper6 It would take seconds if essentially nothing happened with the methods, but we don't know what these methods actually do. It would also be irrelevant the amount of RAM.
     
  15. Offline

    CraftCreeper6

    AdamQpzm
    The amount of RAM decides whether your PC crashes or not ;)
     
  16. CraftCreeper6 Yes, a lack of RAM can cause a computer to crash, but it doesn't really affect performance in this case. It'll either be able to run... or it won't. Processor will be the bottleneck, not RAM.
     
  17. Offline

    CraftCreeper6

    AdamQpzm
    Fair enough, I used to have 2 GB RAM and my PC crashed when I tried to run a program. I upgraded :)
     
  18. i have a very slow computer so it would take years for me ^^

    int goes from -2^33 to 2^33

    (it is -8589934592 to 8589934592)

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

    OffLuffy

    Thought it was 2^31 ? :confused: 32 bit, 31 since one is the + or - signifier
     
  20. Offline

    Rocoty

    OffLuffy likes this.
Thread Status:
Not open for further replies.

Share This Page