Need help! Trying to build a block manipulation plugin

Discussion in 'Plugin Development' started by WingsofPhoenix, Oct 24, 2014.

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

    WingsofPhoenix

    Hello,

    I new to programming. I just learned java a week ago. This is my second try in making a plugin. I've been following many Java tutorials as well as Bukkit plugin tutorials themselves.
    Code:java
    1. package me.phoenix;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.permissions.Permission;
    8. import org.bukkit.plugin.PluginManager;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class BlockListener extends JavaPlugin {
    12.  
    13. public Permission playerPermissionBedrock = new Permission("place.blockedbedrock");
    14. public Permission playerPermissionTNT = new Permission("place.blockedtnt");
    15.  
    16. @Override
    17. public void onEnable() {
    18. getLogger().info("Grief protection V. 1.1 by SkyZ Developpers!");
    19. new antiblock(this);
    20. PluginManager pm = getServer().getPluginManager();
    21. pm.addPermission(playerPermissionBedrock);
    22. pm.addPermission(playerPermissionTNT);
    23. }
    24.  
    25. @Override
    26. public void onDisable() {
    27. getLogger().info("DISABLING CUSTOM GRIEF PROTECTION");
    28.  
    29.  
    30. }
    31. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    32.  
    33. if (cmd.getName().equalsIgnoreCase("blocklistener") && sender instanceof Player) {
    34.  
    35. Player player = (Player) sender;
    36.  
    37. player.sendMessage(ChatColor.GOLD + "Private plugin made by Phoenix566 :)");
    38.  
    39. return true;
    40. }
    41.  
    42. return false;
    43.  
    44.  
    45. }
    46.  
    47.  
    48.  
    49.  
    50. public boolean onCommand2(CommandSender sender, Command cmd, String label, String[] args) {
    51.  
    52. if (cmd.getName().equalsIgnoreCase("bannedblocks") && sender instanceof Player) {
    53.  
    54. Player player = (Player) sender;
    55.  
    56. player.sendMessage(ChatColor.GREEN + "Banned Blocks: " + ChatColor.RED + "TNT, Bedrock");
    57.  
    58. return true;
    59.  
    60. }
    61.  
    62. return false;
    63.  
    64. }
    65.  
    66.  
    67.  
    68. }



    And this..

    Code:java
    1. package me.phoenix;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    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 antiblock implements Listener {
    12.  
    13.  
    14.  
    15. public antiblock(BlockListener plugin) {
    16. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    17.  
    18.  
    19. }
    20.  
    21. @EventHandler
    22. public void onBlockPlace(BlockPlaceEvent e) {
    23.  
    24. Player player = e.getPlayer();
    25.  
    26. if (e.getBlock().getType() == Material.BEDROCK);
    27. if (!player.hasPermission("place.blockedbedrock")) {
    28. e.setCancelled(true);
    29. player.sendMessage(ChatColor.RED + "Sorry, but you lack permission to place " + ChatColor.BLUE + ChatColor.BOLD + e.getBlock().getType().toString());
    30.  
    31.  
    32. }
    33.  
    34. if (e.getBlock().getType() == Material.TNT);
    35. if (!player.hasPermission("place.blockedtnt")) {
    36. e.setCancelled(true);
    37.  
    38.  
    39.  
    40. }
    41.  
    42.  
    43. }
    44.  
    45. }
    46.  
    47.  
    48.  
    49.  
    50.  



    Here are the issues. After exporting it and placing it on my plugins folder, it has no errors whatsoever. The first command, /blocklistener, works fine. However, I was not able to execute the /bannedblocks command which gives a list of banned blocks. It sends back a white text: "/bannedblocks", the command itselt, and shows no list that I'm expecting.

    Second issue, the plugin instead, bans all blocks. What's wrong with my code? All I want is to ban TNT and Bedrocks to players that lack the permissions. Please go easy on me, as I am new to these things.

    Thanks
     
  2. Offline

    WinX64

    1) The onCommand method is called whenever a command from your plugin is executed, onCommand2 however, is not. Put the "bannedblocks" command part inside onCommand as well.
    An alternative is registering a CommandExecutor for the command.

    2) You used a semi-collon after the if statement, this will make it execute the code below regardless of the condition of your if statement, add a body instead:
    Code:
    if (e.getBlock().getType() == Material.BEDROCK) {
        //Your code inside here. This will execute if the condition is true
    }
    
     
  3. Offline

    teej107

    onCommand is an overridden method. It will be called whether you have it or not. onCommand2 isn't being called. Since you are overriding a method, put the @Override annotation above the overridden methods. It's there for the developers' convenience. Bukkit does so many things for you that it would be better to just learn Java first, then the Bukkit API.
     
  4. Offline

    WingsofPhoenix


    I was able to get the Bedrock code working, thanks. However, the same cannot be said for the TNT code. I am still able to place TNT down. Here's the edited code:

    Code:java
    1. package me.phoenix;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    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 antiblock implements Listener {
    12.  
    13.  
    14.  
    15. public antiblock(BlockListener plugin) {
    16. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    17.  
    18.  
    19. }
    20.  
    21.  
    22.  
    23.  
    24. @EventHandler
    25. public void onBlockPlace(BlockPlaceEvent e) {
    26.  
    27. Player player = e.getPlayer();
    28.  
    29. if (e.getBlock().getType() == Material.BEDROCK) {
    30. if (!player.hasPermission("place.blockedbedrock")) {
    31. e.setCancelled(true);
    32. player.sendMessage(ChatColor.RED + "Sorry, but you lack permission to place " + ChatColor.BLUE + ChatColor.BOLD + e.getBlock().getType().toString());
    33.  
    34.  
    35. }
    36.  
    37. if (e.getBlock().getType() == Material.TNT) {
    38. if (!player.hasPermission("place.blockedTNT")) {
    39. e.setCancelled(true);
    40. }
    41. }
    42.  
    43. }
    44.  
    45. }
    46.  
    47. }


    What else have I done wrong with my code?

    Thanks
     
  5. Offline

    WingsofPhoenix

Thread Status:
Not open for further replies.

Share This Page