Set & get block owners

Discussion in 'Plugin Development' started by Woobie, Nov 4, 2012.

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


    As the title says, set and get block owners.

    I place a block, its mine, no one else cant break it.
    Maybe something with hashmaps or custom ymls? I need the players to be able to break the blocks later.

    I tried looking at PreciousStones API, and source, but nope.

    I have no idea where to start..
    Any help? :D
  2. it would get quite a lot of data if you would store all the blocks placed. but if you do choose that way, store the uuid and the player in a hashmap
  3. Offline


    use metadata. Would be alot better organized than a HashMap, as you could just get the metadata with the key "owner" from a block. Though, as it would seem that is going to be a LOT of blocks, you might consider a database
  4. Offline


    Metadata gets lost on restart, no? So not as reliable.
  5. Offline



    Working on a simple game plugin, which resets the map after every game (30 minutes) so wouldnt be a big of a deal.

    I'll have a look at the metadata.
  6. have you looked at how similar plugins does it?
    i know a lot of plugins got this feature.
  7. Offline


    I actually had this idea many years ago, before Bukkit even existed. I might create the plugin when I get home; but don't count on it.
  8. Offline


    I did search plugins that does this, but couldnt find anything "simple"
  9. Offline


    Bumpety bump.
    Metadata seems too complicated for me.. is there an easier way to do it? Or could someone lead me to the right way with metadata? :)
  10. Offline


    Hippo :3
  11. Offline


    A block wrapper is a "simple" solution however it's resource intensive as a lot of memory would be required depending on how many blocks you need to store :(
  12. Offline


    Shouldnt be too many blocks, since the game is for 10-50 players with 10 minutes time to build, and map resets after the game (30 minutes)
  13. Offline


    Here, your functions, ready to use
    1. public void setBlockOwner(Block b, String name)
    2. {
    3. b.setMetadata("owner", new FixedMetadataValue(MainClassInstance,name));
    4. }
    6. public String getBlockOwner(Block b)
    7. {
    8. String owner = "";
    9. List<MetadataValue> metas = b.getMetadata("owner");
    10. for(MetadataValue meta : metas)
    11. {
    12. if(meta.getOwningPlugin().getName().equals(MainClassInstance.getName()))
    13. {
    14. owner = meta.asString();
    15. }
    16. }
    17. return owner;
    18. }

    You only have to change MainClassInstance with an actual instance of your main class
    _Yooxa_ likes this.
  14. Offline


    Havent tested yet, but how would I check on BlockBreakEvent, if the block is owned by someone else?

    EDIT: I have no idea how, and where to use that.. mind giving me an example?

    I'm trying to make personal blocks, so on BlockPlaceEvent, it saves the block owner, and on BlockBreakEvent, if the block breaker is not the owner, the event is cancelled.
  15. Did you see the second function? Just check if the player name from the BlockBreakEvent is equal to the owner of the block, if it's not, cancel the event.
    For the BlockPlaceEvent, listen to that, get the block use the first function...
  16. Offline


    But I've never used metadata before, so I dont know what is what :p
    1. @EventHandler
    2. public void onBlockPlace(BlockPlaceEvent e, Block b, String name)
    3. {
    4. b.setMetadata("owner", new FixedMetadataValue(this, name));
    5. }
    7. public String getBlockOwner(Block b)
    8. {
    9. String owner = "";
    10. List<MetadataValue> metas = b.getMetadata("owner");
    11. for(MetadataValue meta : metas)
    12. {
    13. if(meta.getOwningPlugin().getName().equals(this.getName()))
    14. {
    15. owner = meta.asString();
    16. }
    17. }
    18. return owner;
    19. }
    21. @EventHandler
    22. public void onBlockBreak(BlockBreakEvent e, Player p, Block b)
    23. {
    24. List<MetadataValue> metas = b.getMetadata("owner");
    25. for(MetadataValue meta : metas)
    26. if(!meta.getOwningPlugin().getName().equals(this.getName()))
    27. {
    28. e.setCancelled(true);
    29. p.sendMessage(ChatColor.GREEN+ "[FF] This block is not yours.");
    30. }
    32. }
    34. }

    Is this even close? :D
  17. Do you even know what functions are? Because no, this is not close.
  18. Offline


    But I've never used metadata before
    k? I dont know what is what -.-
  19. It doesn't really matter if you know what metadata is or not, i have never used it before either, just wrote your code...
    public class BreakAndPlaceListener implements Listener {
        public void onBlockPlace(BlockPlaceEvent event) {
            setBlockOwner(event.getBlock(), event.getPlayer().getName());
        public void onBlockBreak(BlockBreakEvent event) {
            if(!event.getPlayer().getName().equals(getBlockOwner(event.getBlock()))) {
        public void setBlockOwner(Block b, String name)
            b.setMetadata("owner", new FixedMetadataValue(MainClassInstance,name));
        public String getBlockOwner(Block b)
            String owner = "";
            List<MetadataValue> metas = b.getMetadata("owner");
            for(MetadataValue meta : metas)
                    owner = meta.asString();
        return owner;
    You do still have to do the imports and the package declaration.
    Also, not guaranteed to work, haven't tested anything.
    Woobie likes this.
  20. Offline


    Dude, that is amazing :D
    Works perfectly, only false is that I cant break naturally generated blocks, but that is not a problem for me, since this is a game plugin with a flatland map :)
  21. Hmm, that's an unexpected bug...
  22. Offline


    Well not really, since noone actually placed the blocks, so I cant break them either :p
    I just added this, so ops can break blocks
  23. Offline


    if the owner String is empty, it is not owned. Exclude that from the case where it cancels
    blackwolf12333 likes this.
  24. I knew i forgot something:p
Thread Status:
Not open for further replies.

Share This Page