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?
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
use metadata. Would be alot better organized than a HashMap, as you could just get the metadata with the key "owner" from a block. http://wiki.bukkit.org/Plugin_Tutorial#Metadata. Though, as it would seem that is going to be a LOT of blocks, you might consider a database
mollekake fireblast709 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.
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.
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?
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
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)
Here, your functions, ready to use Code:java 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) { if(meta.getOwningPlugin().getName().equals(MainClassInstance.getName())) { owner = meta.asString(); } } return owner; } You only have to change MainClassInstance with an actual instance of your main class
Thanks! 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.
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...
But I've never used metadata before, so I dont know what is what Code:JAVA @EventHandlerpublic void onBlockPlace(BlockPlaceEvent e, Block b, String name) { b.setMetadata("owner", new FixedMetadataValue(this, name)); } public String getBlockOwner(Block b) { String owner = ""; List<MetadataValue> metas = b.getMetadata("owner"); for(MetadataValue meta : metas) { if(meta.getOwningPlugin().getName().equals(this.getName())) { owner = meta.asString(); } } return owner; } @EventHandler public void onBlockBreak(BlockBreakEvent e, Player p, Block b) { List<MetadataValue> metas = b.getMetadata("owner"); for(MetadataValue meta : metas) if(!meta.getOwningPlugin().getName().equals(this.getName())) { e.setCancelled(true); p.sendMessage(ChatColor.GREEN+ "[FF] This block is not yours."); } } } Is this even close?
It doesn't really matter if you know what metadata is or not, i have never used it before either, just wrote your code... Code: public class BreakAndPlaceListener implements Listener { @EventHandler public void onBlockPlace(BlockPlaceEvent event) { setBlockOwner(event.getBlock(), event.getPlayer().getName()); } @EventHandler public void onBlockBreak(BlockBreakEvent event) { if(!event.getPlayer().getName().equals(getBlockOwner(event.getBlock()))) { event.setCancelled(true); } } 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) { if(meta.getOwningPlugin().getName().equals(MainClassInstance.getName())) { 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.
Dude, that is amazing 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
Well not really, since noone actually placed the blocks, so I cant break them either I just added this, so ops can break blocks Code: if(!event.getPlayer().isOp()){