Protection

Discussion in 'Plugin Development' started by bigbeno37, Jun 22, 2012.

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

    bigbeno37

    Hey there!

    I am currently coding a basic Bukkit plugin which will protect any diamond blocks placed and stop any other people from destroying those blocks. However, I need someone to look at the code and answer my questions. The classes are here:

    TestPlugin: http://pastie.org/4131654
    [UPDATED] TestPluginListener: http://pastie.org/4135166

    Now the questions. Okay, firstly, how would I go about protecting diamond blocks under a certain username? And secondly, when testing the plugin, it displayed this on the console:

    Code:
    2012-06-22 18:57:44 [SEVERE] Wrong method arguments used for event type registered
    I have the @EventHandler in there, so could someone please explain why that is happening?

    Thanks to any replies regarding this issue,
    bigbeno37
     
  2. Offline

    javoris767

    Maybe you have to list a priority, or is that optional :confused:
     
  3. Offline

    CorrieKay

    a priority is optional. defaults to normal.

    the problem is you have two event types as parameters in your listener. You cannot have two parameters in a listener method. Only the event youre listening for.

    Regarding actually protecting the blocks, a good idea is to have a HashMap<Block,String> where the block is the diamond block, and the string is the owner.getName() (owner is an instance of Player)

    OR you can use metadata. basically, you'd create a class that implements Metadatable, and pass specific information in (such as the name of the player).

    whenever you break a block, you grab the metadata by the string you saved it as, grab the string inside the metadata, and check if its the breakers name. if not, cancel the event, etc

    You need to choose a serialized data storage schema for data persistance, i suggest sql, but if youre feeling lazy, you can use a custom yml configuration (everyones gonna shark me over saying that though, so once you feel comfortable, try and learn sql to store this data)
     
  4. Offline

    bigbeno37

    Thanks for the reply CorrieKay. I have two questions for you:

    1) How do you use a HashMap? I have little to no experience with Bukkit API or with this command, so a brief overview of what it is and how to use it would be nice.

    2) How do I setup a storage YML file? What code would I need to put in for that to work?

    I have redone my code and now it has more stuff in it but its sorta easier to change different values around now. The plugin loads fine and when I place the diamond block, it simply says 'You have protected a diamond block' as it should say. Now whenever I break a block, it comes up with a load of errors on the console. The first post has been updated to include the latest codes and the server.log for anyone to look at.
     
  5. Offline

    CorrieKay

    a hashmap is a unique-key/nonunique-value type data system. Its not proprietary to bukkit, its a basic java thing :p

    But basically, you give it two types to store, and you set it up like this:
    HashMap<type1,type2> hashMapName = new HashMap<type1,type2>();

    Lets take for instance you wanted to keep a block with a value of a String. In this case, the string would be the name of the player that owns the block.

    First, you set up the hashmap:
    HashMap<Block,String> blockProtectMap = new HashMap<Block,String>();

    then whenever you need to add a protection to it,
    Code:
    public void onBlockBreak(BlockPlaceEvent event){
      if(event.getBlock().getType == Material.DIAMOND_BLOCK){
        Block dblock = event.getBlock();
        blockProtectMap.put(dblock,event.getPlayer().getName());
      }
    }
    The use of a hashmap is that all keys must be unique. If you place a key object that would return true on an equals() check to any other key in the map, it will override that key and its value with whatever is in the put() method.

    You can grab a value by key, but you cannot (directly) grab a key by its value (since values are NON unique, and are not implied unique, even if you know they are unique)

    Lets say someone breaks a block, and you need to check protections.:

    Code:
    public void onBlockBreak(BlockBreakEvent event){
      if(blockProtectMap.containsKey(event.getBlock())){//no need to check for diamond block, if its in the block protect map, you wanted it protected.
        if(!blockProtectMap.get(event.getBlock()).equals(event.getPlayer().getName())){//remember, you grab the protector-players name VALUE by the block KEY!
          event.setCancelled(true);
          //warn the player
        }
      }
    }
    
    The reason we dont store the player object directly, is due to memory leak issues. Technically its possible to store the player object somewhat safely, but its a bitch to set it up where theyre removed when they need to be, and even then its not technically 100% guarenteed to work, so its a tad risky. Just use the players string name, and you'll be fine :3
     
  6. Offline

    bigbeno37

    Okay, that fixed the errors appearing on the console, but I still need some more help. I have imported the HashMap (Thanks for that CorrieKay) and the original post has been updated with the latest classes. The server.log file is not needed as there aren't any errors.

    The problem I am having is that when the BlockBreakEvent happens, it is supposed to say 'You have destroyed your diamond block'. However this isn't the case. Could someone please look over this last detail as I have no idea as to where I went wrong.
     
  7. Offline

    CorrieKay

    Aye, in the block break event method, use event.getBlock().getLocation()

    Youre asking for a key of type Block, but it only has keys of type Location, therefore it will ALWAYS return false when youre asking if a block is a key c:

    also, remove the location from the map when they break it, otherwise the location will still be locked to that player.
     
Thread Status:
Not open for further replies.

Share This Page