I am new to Bukkit and I want some help on sth like permissions

Discussion in 'Plugin Development' started by ykn121, Jan 10, 2015.

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

    ykn121

    First of all, here is my AntiBlock.java code:

    Code:java
    1.  
    2. package me.firstflames;
    3.  
    4. import org.bukkit.Material;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.permissions.Permission;
    10. import org.bukkit.plugin.PluginManager;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class AntiBlock extends JavaPlugin
    14. {
    15. public Permission playerPermission = new Permission("playerAbility.allowed");
    16.  
    17. @Override
    18. public void onEnable()
    19. {
    20. new BlockListener(this);
    21. PluginManager pm = getServer().getPluginManager();
    22. pm.addPermission(playerPermission);
    23.  
    24. }
    25. @Override
    26. public void onDisable()
    27. {
    28.  
    29. }
    30. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    31. {
    32. if (cmd.getName().equalsIgnoreCase("giveme") && sender instanceof Player)
    33. {
    34. Player player = (Player) sender;
    35.  
    36. if(player.hasPermission("playerAbility.allowed"))
    37. {
    38. player.setItemInHand(new ItemStack(Material.DIAMOND_AXE));
    39. }
    40. return true;
    41. }
    42. return false;
    43. }
    44. }
    45.  


    here is the BlockListener.java code:
    Code:java
    1.  
    2. package me.firstflames;
    3.  
    4.  
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.BlockPlaceEvent;
    9.  
    10.  
    11. public class BlockListener implements Listener{
    12.  
    13. public BlockListener(AntiBlock plugin)
    14. {
    15. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    16. }
    17.  
    18. @EventHandler
    19. public void onBlockPlace(BlockPlaceEvent e){
    20.  
    21. Player player = e.getPlayer();
    22.  
    23. if (!player.hasPermission("playerAbility.allowed")){
    24.  
    25. e.setCancelled(true);
    26. }
    27. }
    28. }
    29.  


    I am following the video here:

    The question is that whenever I join the server,
    I don't have the ability to place a block or use the command "giveme".
    Also, I can't use the command /deop
    It should be the permission problem, right?
    So, what is the problem in my code?

    thank you.

    (p.s. I have used permissionEx with default, haven't changed anything)
     
  2. Offline

    1Rogue

    When you have a player object just do:

    Code:java
    1. Player p = /* this is our example player */;
    2. p.hasPermission("your.example.node");[]/syntax]
     
  3. Offline

    ykn121

    I have tried and the code becomes
    Code:java
    1.  
    2. public void onBlockPlace(BlockPlaceEvent e){
    3.  
    4. Player player = e.getPlayer();
    5. player.hasPermission("playerAbilities.allowed");
    6.  
    7. if (e.getBlock().getType() == Material.SAND)
    8. {
    9. if (!player.hasPermission("playerAbilities.allowed")){
    10. player.sendMessage(ChatColor.RED + "You cannot place " + e.getBlock().getType().toString() + " down!");
    11. e.setCancelled(true);
    12. }
    13. }
    14. }
    15.  


    It still does not work.
     
  4. Offline

    nverdier

    @ykn121 Why doesn't it work? We can't read your mind, although it would make life a lot easier.
     
  5. Offline

    1Rogue

    You're not checking the return value of "hasPermission". Use an if statement.
     
  6. Offline

    nverdier

    @1Rogue Wow of course, don't know how I missed that...
     
  7. Offline

    ykn121

    sorry, I mean the plugin should make the player to get the permission "playerAbilities.allowed" such that the player can place the sandblock. However, after several testing, I believe that the code cannot give the permission to the player which the player cannot place the block. And I don't know why...
     
    Last edited: Jan 11, 2015
  8. Offline

    nverdier

    @ykn121 You have to put it in an if statement... As stated by @1Rogue
     
  9. Offline

    ykn121

    Do you mean the if-statement in line 9?

    If I am correct, the hasPermission function should return true if the player got the permission "playerAbilities.allowed". I think I am concerning how to give the permission to the player.

    Sorry if I misunderstand your suggestion.
     
  10. Offline

    nverdier

    @ykn121 You aren't doing anything with the return statement.
     
  11. Offline

    ykn121

    could you show me what the code should be?

    I think the if-statement should be the one in line 9.
     
  12. Offline

    nverdier

    @ykn121 Ok, you really don't know what you're doing. Anyone I know would suggest to learn Java before learning Bukkit. Bukkit is simply an API written in Bukkit, and learning Bukkit first would be similar to learning to run before learning to walk. Basically just learn Java and once you're sufficient at it, come back to learn Bukkit.
     
  13. Offline

    ykn121

    For a basic if-statement in Java, I have learnt it already and I have a good background in C++. For some codes like "instanceof" or "this", probably I am not an expert.

    If you are just looking at line 5, it means nothing obviously because it just returns a "false" value and that's it. It does not affect the whole program at all. Thats why I am asking whether the if-statement you mentioned has been in line 9.

    Don't just focus on line 5.
     
  14. Offline

    nverdier

  15. Offline

    ykn121

    What does your "it"refer to? line 5 or line 9?

    If u are talking about line 5, I would say that ...that is not my original version of codes. line 5 was originally not there. As I mentioned, I am new to Bukkit. I thought the hasPermission function can also give the permission to the "player". Thats why I added it after your suggestion.

    So, lets come back to the question. The Original codes are on the top of the post. I want to know why I cannot give the permission to the "player" even I think I have followed the video...
     
Thread Status:
Not open for further replies.

Share This Page