Rename chest with code

Discussion in 'Plugin Development' started by crzytlp, Jul 26, 2014.

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

    crzytlp

    Hi. Can i rename chests ? I tries but i cant .actualy i dont know if its possible . I want this because i checking InventoryOpenEvent with inventory title or name
     
  2. Offline

    valon750

    crzytlp

    Eh... it would require jumping through a few hoops, such as perhaps waiting for an interact event on the chest.. cancelling the event BUT grabbing the chests content, then opening up a new inventory for the player with said content.

    Of course the issue then is if multiple people attempt to open it.. you'll also need to account for saving the contents.

    May I ask why exactly you need the change the chests name upon opening, as opposed to simply changing the title of the chest before opening?
     
  3. Offline

    xTigerRebornx

    crzytlp If you want to actually change the name, could either use Reflection or edit the Chest's NBT Data (CustomName is a StringTag that stores the name).
    Otherwise, could do what valon750 suggested and make a new Inventory and copy contents.
     
  4. Offline

    crzytlp

    Basicly i create a chest which has some items in it when player open up the specific chest which i created i broadcast a message to server player found the chest bla bla bla then i break the chest but i need chest name to break the or location but i cant figure out the location solution and i cant rename the chest and i m stuck now :( valon750

    Code:java
    1. package io.github.taifuru.talipplugin;
    2.  
    3. import java.util.Random;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.inventory.InventoryOpenEvent;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15.  
    16. public class RandomChests implements Listener
    17. {
    18. private final TalipPlugin plugin;
    19.  
    20. public RandomChests(TalipPlugin instance)
    21. {
    22. plugin = instance;
    23. }
    24.  
    25. public void chestItemAdd(Location location, Inventory inventory)
    26. {
    27. ItemStack itemStack = new ItemStack(Material.APPLE);
    28.  
    29. int chestSayisi = plugin.getConfig().getInt("ChestSayisi.sayi");
    30.  
    31. Random rand = new Random();
    32. int chestSirasi = -1;
    33.  
    34. while(true)
    35. {
    36. if( chestSirasi <= 0)
    37. {
    38. chestSirasi = ( 1 ) + rand.nextInt()%chestSayisi;
    39.  
    40. }else if (chestSirasi > 0)
    41. break;
    42. }
    43.  
    44. int itemSayisi = plugin.getConfig().getInt("ChestSayisi." + chestSirasi + ".10" + chestSirasi );
    45.  
    46. for(int i=1; i <= itemSayisi; i++)
    47. {
    48. itemStack.setType( Material.getMaterial( plugin.getConfig().getString( "ChestSayisi." + chestSirasi + "." + i ) ) );
    49. inventory.addItem( itemStack );
    50. }
    51. }
    52.  
    53. public static void chestSpawn(TalipPlugin p)
    54. {
    55. int coords = p.getConfig().getInt("ChestSayisi.chestSpawnCoords");
    56.  
    57. while(true)
    58. {
    59. Random r = new Random();
    60. double x = ( -coords ) + ( ( coords ) - ( -coords )) * r.nextDouble();
    61. double y = ( 64 ) + ( ( 70 ) - ( 64 )) * r.nextDouble();
    62. double z = ( -coords ) + ( ( coords ) - ( -coords )) * r.nextDouble();
    63.  
    64. Location loc = new Location(Bukkit.getWorld( p.getConfig().getString("ChestSayisi.world") ), x, y, z);
    65.  
    66. if( ( loc.getBlock().getType() == Material.AIR ) )
    67. {
    68. Bukkit.getServer().broadcastMessage( ChatColor.BLUE + "[" + p.getConfig().getString("ChestSayisi.randomChest") + "] " + ChatColor.GRAY + p.getConfig().getString("ChestSayisi.chestSpawn") + " :" );
    69. Bukkit.getServer().broadcastMessage( ChatColor.BLUE + "[" + p.getConfig().getString("ChestSayisi.randomChest") + "] " + ChatColor.GREEN + "(" + (int) x + "," + (int) y + "," + (int) z + ")" );
    70.  
    71. loc.getBlock().setType(Material.CHEST);
    72.  
    73. break;
    74. }
    75. }
    76. }
    77.  
    78. @EventHandler
    79. public void chestOpenUp( InventoryOpenEvent event )
    80. {
    81. Player player = (Player) event.getPlayer();
    82. Inventory inv = event.getInventory();
    83.  
    84. if( event.getInventory().getTitle().equalsIgnoreCase( plugin.getConfig().getString("ChestSayisi.randomLoot") ) )
    85. {
    86. Location loc = player.getLocation();
    87. loc = loc.add(5, -5, 5);
    88.  
    89. for(int y=0; y < 11; y++)
    90. {
    91. for(int x=0; x < 11; x++)
    92. {
    93. for(int z =0; z < 11; z++ )
    94. {
    95. if( loc.getBlock().getType() == Material.CHEST )
    96. {
    97. if( inv.getTitle().equalsIgnoreCase( plugin.getConfig().getString("ChestSayisi.randomLoot") ) )
    98. {
    99. chestItemAdd(loc, event.getInventory());
    100. loc.getBlock().breakNaturally();
    101. Bukkit.getServer().broadcastMessage(ChatColor.BLUE + "[" + plugin.getConfig().getString("ChestSayisi.randomChest") + "] " + ChatColor.GREEN + player.getName().toString() + ": " + ChatColor.GRAY + plugin.getConfig().getString("ChestSayisi.chestFind"));
    102. }
    103. }
    104. loc = loc.add(0,0,-1);
    105. }
    106. loc = loc.add(-1,0,11);
    107. }
    108. loc = loc.add(11,1,0);
    109. }
    110. loc = loc.add(0,-11,0);
    111. }
    112. }
    113.  
    114. public TalipPlugin getPlugin() {
    115. return plugin;
    116. }
    117.  
    118. }
    119.  


    This my RandomChests class file xTigerRebornx valon750
     
  5. Offline

    xTigerRebornx

    crzytlp A Player can only ever have one Inventory open at once (InventoryView is considered as one Inventory in this context), so having something that logs the Location of the chest when they open it to the Player would work (a Map<String, Location> would suffice), then when they close it, you can simply grab them from the Map and destroy the chest at the Location in the map, then remove them from the Map after destroying the Block.
     
  6. Offline

    crzytlp

    how can i know player open up the specific chest witout the name. i cant get the location with InteractInventoryEvent xTigerRebornx
     
  7. Offline

    xTigerRebornx

    crzytlp PlayerInteractEvent, you can get a Block from this. chests are blocks, therefor you are able to get the chest they've interacted with.
    Also, in your code, you set the chest. When you set the chest, you have its Location. You need that to determine that the chest the Player opens is your chest.
     
  8. Offline

    crzytlp

    xTigerRebornx Himm then i need store the locations they can be thousands chest around if i store in list or map it can be ram eater
     
  9. Offline

    xTigerRebornx

    crzytlp Are all chests your "chest"?
     
  10. Offline

    crzytlp

    xTigerRebornx Not all are mine they can be normal chests and loot chests
     
  11. Offline

    xTigerRebornx

    crzytlp You could use Metadata to differentiate between your special chest and normal ones.
     
  12. Offline

    Dubehh

    crzytlp
    You could save the location of the chest,
    then when a player opens a chest, and its location is equal to the saved location, do something with it.
     
  13. Offline

    crzytlp

    xTigerRebornx it looks like the only way using metadata now i m going to learn how metadata works thanks for helping

    Dubehh but it can be 1000 chests around its so much location saving

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

    xTigerRebornx

    crzytlp It honestly shouldn't be that bad, even with 1000 chests (you can replace the Location with a wrapper class that uses less intensive things like 3 ints and a String (world's name)). Metadata isn't persistent, keep that in mind.
     
  15. Offline

    Dubehh

    crzytlp
    Then MetaData is the way for you.
    I thought you ment 'treasures'. Not a 1000 chests
     
  16. Offline

    crzytlp

  17. Offline

    xTigerRebornx

    crzytlp You could completely go with Metadata, completely go with Maps, or mix them.
    I'd use Metadata for both determining if the chest is 'your chest' and the Player's current open Inventory Location.
    You can simply attach Metadata to the Player that determines both if they have your Inventory open and if it is your chest (a FixedMetadataValue storing a clone of the Location would work). You'd attach the Metadata to a Player if the Block they clicked has the Metadata that decides if it is your chest.
     
Thread Status:
Not open for further replies.

Share This Page