Best way to track custom blocks?

Discussion in 'Spigot Plugin Development' started by Drtro, May 24, 2015.

  1. When you want a custom item to do something, you just set a name or lore and check for that name or lore (example: TNT Bow). But how do you get custom blocks to do things, since it's name/lore is lost when it's placed. I made a Philosopher's Stone that is an obsidian block and each time it's mined, it drops a random ore and is replaced. Is there a way to make this event only happen on the specific custom block and not all obsidian blocks?
     
  2. Store the block's location.
     
  3. Check if the obsidian placed is a 'custom' block in the place-block event, and store its location to a hashmap. Then, on the break-block event, compare the block to the location in your hashmap.

    Edit: Depending on what you want to do, a hashmap may not be necessary, you could just use a list.
     
  4. He would have to use a file, or a database.
     
  5. Well yes, if he wants to keep the information across reloads/restarts.
    But in terms of efficiency, it makes sense to get the info (and save to a hashmap/list) in the onEnable and set it in the onDisable (from the hashmap/list), and modify the hashmap/list only. That way, the amount of times you access the file/database is reduced.
     
  6. That would be redundant. FileConfiguration#set() is not writing to the file, but is updating the Map containing all of the keys and their values. When you invoke FileConfiguration#save(), the data in the Map is actually written to the file; however, if he is using a database, then you are correct.
     
  7. I can't get a lore or custom name from a block place event.
     
  8. Blocks don't have display names, or lore.
     
  9. That's exactly my point and why I made this thread to find an alternative solution.

    [​IMG]

    My car is broke. "Yeah your car is broke."
     
  10. I wasn't sure whether you were asking a question, or what since we already gave you the solution and it had nothing to do with display names, or lore.
     
  11. How do I know it's the philosopher's stone obsidian and not a normal obsidian block is what I was asking, since blocks don't have names or lores.
     
  12. Store its location when the block is placed. When a player breaks the block check if that location is in the file, map, or whatever, and if it is then your block is the 'custom' one.
     
  13. But like I said, how do I know it's the philosopher's obsidian block and not a normal obsidian block, when storing the location?
     
  14. Oh, I misunderstood. You could use the PlayerInteractEvent, get the item in the player's hand, store the location, cancel the event, etc. You could also try using the BlockPlaceEvent and getting the item in the player's hand. The ItemStack may still be there since the event is called before the block is actually placed.
     
    • Like Like x 1
    • Informative Informative x 1
  15. Will try the latter. I always thought it wouldn't, so it wouldn't work if they were placing the last one in their hand.
     
  16. It must work also that way, because you can cancel the block place event, which means, the item is in the players hand while the event is called, if it's not changed by another plugin, but it's not usuall.
     
    • Informative Informative x 1
  17. Thank you. Guess I can scrap up my other custom block ideas I scrapped in my custom item plugin for my server. Didn't know if it were possible. Wish Precious Stones would change to custom blocks instead of all diamond blocks being claim blocks for example. It's sort of what I'm recreating, except for custom blocks for my server.