Solved No errors, but a code of the plugin is not working?

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

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

    WingsofPhoenix

    Hello,

    I'd like you to please check my code:

    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. }
    40.  
    41.  
    42.  
    43. if (cmd.getName().equalsIgnoreCase("bannedblocks") && sender instanceof Player) {
    44.  
    45. Player player = (Player) sender;
    46.  
    47. player.sendMessage(ChatColor.GREEN + "Banned Blocks:");
    48. player.sendMessage(ChatColor.RED + "TNT");
    49. player.sendMessage(ChatColor.RED + "Bedrock");
    50. return true;
    51.  
    52. }
    53.  
    54.  
    55. return false;
    56.  
    57.  
    58. }
    59.  
    60.  
    61.  
    62.  
    63.  
    64. }



    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. if (e.getBlock().getType() == Material.TNT) {
    36. if (!player.hasPermission("place.blockedtnt")) {
    37. e.setCancelled(true);
    38. }
    39. }
    40.  
    41. }
    42.  
    43. }
    44.  
    45. }
    46.  
    47.  
    48.  
    49.  
    50.  
    51.  
    52.  
    53.  
    54.  
    55.  
    56.  
    57.  
    58.  
    59.  
    60.  
    61.  



    I'd like it to block Bedrocks and TNTs. The Bedrock code seems working, it blocks TNT just like I want it to, but not TNTs. What have I done wrong?

    Thanks
     
  2. Offline

    bombom3000

    WingsofPhoenix From looking at your classes, I believe you haven't initialised your listener class properly.
    1. In the onEnable method of your main class, add:
    Code:java
    1. PluginManager pm = getServer().getPluginManager();
    2. pm.registerEvents(this.antiblock, this);

    and remove registerEvents in your antiblock class.
    2. Add this in your main class:
    Code:java
    1. public static BlockListener getInstance() {
    2. return instance;
    3. }
    4.  

    Add this:
    Code:java
    1. public Main plugin;
    2.  
    3. public PlayerListener(Main instance) {
    4. plugin = instance;
    5. }
    6.  

    after 'public class antiblock implements Listener {' in your antiblock class.

    That should be good for that, I may have missed something though. Just reply if it doesn't work!
     
  3. I would say you failed at your question xD

    besides that, do you want to block tnt-explosion or tnt-placing?
     
  4. Offline

    rete25iscool

    bombom3000

    The listener is initialized properly. He's saying that it's working but there's something apparently not working right.
    Also, what's the point of having a method that returns the instance of the class even though you're not even using it.
    He's using a constructor to initialize his listener which will work..

    What do you mean by that?
    I don't understand.
     
  5. Offline

    WingsofPhoenix


    Ahahah.. I was in a rush, apologies. What I meant to say is that the plugin blocks Bedrock (to players who lack the required permission) perfectly just like I expected, however, the same cannot be said for TNTs.
     
  6. WingsofPhoenix so your method listens to a block placed event and if the blockmaterial is tnt you cancel the event. maybe tnt is not the right material? you dont know for sure that it is tnt, so why dont you add a System.out.println(materialname) to your method, reload the plugin and place a tnt? then you can see what material tnt is made of. or send a message to your player if there are more player online while you're tring this and only send the message if you're the player who placed the block. you know what i mean? print out what happens if you place tnt and afterwards you know to what you've to listen :)
     
  7. Offline

    Cyber_Pigeon

    Your realise griefers will most likely be op when they grief, so they will have permission to place bedrock and tnt. :p
     
  8. Cyber_Pigeon but then they also would be able to place bedrock and they couldn't as they said
     
  9. Offline

    WingsofPhoenix

    Thanks for the help. I'll try all these methods and see which works best.


    The purpose of me making this plugin is to only prevent normal players from placing these banned blocks. The only OPs are my trusted ones, a few of my friends I do know, not the guys I pick randomly online.

    I seem to have got it working. I retyped the whole antiblock class and it seemed to work now. Probably some errors with the bodies. Thank you, everyone, for trying to help me solve this!

    And if any of you are having the same problem, here's the 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. import org.bukkit.material.MaterialData;
    10.  
    11.  
    12. public class antiblock implements Listener {
    13.  
    14.  
    15. public antiblock(BlockListener plugin) {
    16. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    17.  
    18.  
    19. }
    20.  
    21.  
    22. @EventHandler
    23. public void onBlockPlace(BlockPlaceEvent e) {
    24.  
    25. Player player = e.getPlayer();
    26.  
    27. if (e.getBlock().getType() == Material.BEDROCK) {
    28. if (!player.hasPermission("place.blockedbedrock")) {
    29. e.setCancelled(true);
    30. player.sendMessage(ChatColor.RED + "You may not place " + ChatColor.BLUE + ChatColor.BOLD + e.getBlock().getType());
    31. player.sendMessage(ChatColor.GREEN + "Please type in " + ChatColor.BLUE + ChatColor.BOLD + "/bannedblocks " + ChatColor.GREEN + "To see a full list of banned blocks");
    32.  
    33. }
    34.  
    35. }
    36.  
    37. if (e.getBlock().getType() == Material.TNT) {
    38. if (!player.hasPermission("place.blockedtnt")) {
    39. e.setCancelled(true);
    40. player.sendMessage(ChatColor.RED + "You may not place " + ChatColor.BLUE + ChatColor.BOLD + e.getBlock().getType());
    41. player.sendMessage(ChatColor.GREEN + "Please type in " + ChatColor.BLUE + ChatColor.BOLD + "/bannedblocks " + ChatColor.GREEN + "To see a full list of banned blocks");
    42.  
    43. }
    44. }
    45.  
    46. }
    47.  
    48.  
    49.  
    50.  
    51. }
    52.  
    53.  
    54.  
    55.  
    56.  
    57.  
    58.  
    59.  
    60.  
    61.  
    62.  
    63.  
    64.  
    65.  
    66.  
    67.  
    68.  


    Thanks

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
Thread Status:
Not open for further replies.

Share This Page