Solved How to prevent people from creating nether portals?

Discussion in 'Plugin Development' started by Astrophylite, Mar 31, 2016.

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

    I'm trying to create a plugin to stop people from creating nether portals if they don't have the permission "portalpermission.create". I have de-opped myself and there are no errors in the console but the nether portal gets ignited. Also, the plugin does get enabled because it is green in the /plugins list. Any ideas how to fix this?

    Code:java
    1.  
    2. package me.astrophylite.portalpermission;
    3.  
    4. import java.io.File;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.Material;
    8. import org.bukkit.block.Block;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.configuration.file.FileConfiguration;
    12. import org.bukkit.configuration.file.YamlConfiguration;
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.block.Action;
    17. import org.bukkit.event.player.PlayerInteractEvent;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21. public class PortalPermission extends JavaPlugin implements Listener {
    22.  
    23. FileConfiguration config;
    24. File cfile;
    25.  
    26. public void onEnable() {
    27. getConfig().options().copyDefaults(true);
    28. config = getConfig();
    29. saveConfig();
    30. cfile = new File(this.getDataFolder(), "config.yml");
    31.  
    32. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    33. }
    34.  
    35. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    36. if(cmd.getName().equalsIgnoreCase("portalreload")) {
    37. if(!sender.hasPermission("portalpermission.reload")) {
    38. MessageManager.sendNoPermissionMessage(sender, "portalpermission.reload");
    39. return true;
    40. }
    41.  
    42. config = YamlConfiguration.loadConfiguration(cfile);
    43. MessageManager.sendMessage(sender, "&aYou successfully reloaded PortalPermissions' configuration file!");
    44. return true;
    45. }
    46. return true;
    47. }
    48.  
    49. @EventHandler
    50. public void onPlayerInteract(PlayerInteractEvent e) {
    51. Player p = e.getPlayer();
    52. Block b = e.getClickedBlock();
    53. if(!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return;
    54. if(!(p.getItemInHand().equals(new ItemStack(Material.FLINT_AND_STEEL)))) return;
    55. if(!(b.getType() == Material.OBSIDIAN)) return;
    56. if(p.hasPermission("portalpermission.create")) return;
    57.  
    58. e.setCancelled(true);
    59. MessageManager.sendMessage(p, "&cYou are not allowed to create a nether portal!");
    60. return;
    61. }
    62. }
    63.  
    64.  


    Any help will be appreciated,
    Astrophylite.
     
  2. Offline

    mcdorli

    Check for interactevent, then if the player has a flint and steel in his hands -> cancel the event
     
  3. I don't want to prevent them from using flint&steel completely, just when trying to ignite a portal.

    Astrophylite.
     
  4. Offline

    mcdorli

    Then check if they clicked on obsidian. If you want to, ypu can check if there's a portal structure arpund that 1 obsidian block.
     
  5. Look in the code? I've already checked if they clicked on the obsidian and it's not working.
    Code:java
    1.  
    2. Block b = e.getClickedBlock();
    3. if(!(b.getType() == Material.OBSIDIAN)) return;
    4.  


    Astrophylite.
     
  6. Offline

    mcdorli

    If you want to check if something doesn not equals with something, use != instead of !(this == that)

    Why do you return at the end of the method.

    Debug, check where it stops from executing.
     
  7. Ok, I will try the other way of checking. I didn't realize I did that and I will begin to debug now.

    Astrophylite.

    EDIT: Plugin works now, thanks for the tips!
     
    Last edited: Mar 31, 2016
Thread Status:
Not open for further replies.

Share This Page