When a player right clicks the block with a key (tripwire hook) in hand, it creates an inventory that can be later accessed by right clicking that same block. I'm wondering what would be the best way to do this, to determine if the block already has an inventory, or if it needs to create a new one. I was thinking of something like this. Locations: 1: world,x,y,z 2: world,x,y,z 2: world,x,y,z Items: 1: - ItemStack list 2: - ItemStack list 3: - ItemStack list So it would be Locations.Key#, location value and Items.Key#, ItemStack list value. I could load the Locations section to a HashMap, and if the block location matches one of value, I get the key and load the ItemStack list of the same number. So if the block location matched #3, it would then get ItemStack list from Items.3 and load it in the inventory on open.
If you want it to update live, for saving, a good idea might be every time the inventory is closed. For loading, you can do it as soon as it is opened. If you're using chests as the block that keeps the items, a good idea might be to initialize the inventories when the server starts, and saving them when the server closes. It's also worth noting that if you create Inventory objects and find a way to match them up with the blocks (using your Location system, for example), you don't even need to use chests if you had something else in mind.
There are no chests involved. It just creates an inventory at the block, and that inventory can be accessed by right clicking it, whether the block is stone or a torch. Only materials I blacklisted so far is water, air, and lava. If I made the inventory type chest, would I have to save the item stacks, or would spigot save it?
Code (Text): public void onEnable(){ config.load(); } Code (Text): public void onDisable(){ config.save(); }
Oh, sorry, misinterpreted the post. Setting the inventory type to chest will not save it. If it actually is a chest, it will be saved. In your case, just load and save the inventories on startup/shutdown (respectively).
Could I change the block to a chest, but change what it looks like without changing the block type? Like a chest block but it looks like stone?
Like how GriefPrevention shows a border but doesnt really change the blocks, or anti x-ray. Can I do that with chests?
I suppose so... You could send a packet or sendBlockChange, but I think the chest updates would mess it up. Just save the inventory.
I looked at it and just thought List<Location> and List<ItemStack> as soon as I saw this, if I remember correctly Location already generates x y etc in the YamlConfiguration
String blockLoc = event.getClickedBlock().getWorld().getName() + "," + event.getClickedBlock().getX() + "," + event.getClickedBlock().getY() + "," + event.getClickedBlock().getZ(); HashMap<String, String> savedLocs = new HashMap<String, String>(); for(String key : plugin.getConfig().getConfigurationSection("Locations").getKeys(false)) { savedLocs.put(key, plugin.getConfig().getString("Locations." + key)); } if(savedLocs.containsValue(blockLoc)) { //get key, create inventory, load inventory from Items.key } else { //add blockLoc to config, create new inventory, save inventory key in config, etc. } }
So by HashMap<String, String> are you trying to save a location under some directory? Instead I suggest just making a list for each location then when loading back onto the HashMap just create a new location with that information indexName: x: x y: y z: z world: worldName as for the itemstacks being in a list i suggest just making a new YamlConfiguration data.yml and directly save/load from there