Solved disallowing certain commands

Discussion in 'Plugin Development' started by vhbob, Feb 4, 2016.

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

    vhbob

    hey, im trying to not let players use the commands /tpa and /sethome when they are in water or lava but it is not working... here is my class please help:
    Code:
    package com.vhbob.tpax;
    
    import java.util.logging.Logger;
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
        public Logger logger = Bukkit.getLogger();
    
        public void onEnable() {
            logger.info("TPAX by Vhbob Has been enabled!");
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
    
        @EventHandler
        public void not(PlayerCommandPreprocessEvent e) {
            Player p = e.getPlayer();
            Location l = p.getLocation();
            Block b = l.getBlock();
            if (e.getMessage().contains("/tpa")) {
                if (b.equals(Material.WATER) || b.equals(Material.LAVA) || b.equals(Material.STATIONARY_WATER)
                        || b.equals(Material.STATIONARY_LAVA)) {
                    e.setCancelled(true);
                    p.sendMessage(ChatColor.RED + "You cannot tpa here! Move to land");
                }
            }
            if (e.getMessage().contains("/sethome")) {
                if (b.equals(Material.WATER) || b.equals(Material.LAVA) || b.equals(Material.STATIONARY_WATER)
                        || b.equals(Material.STATIONARY_LAVA)) {
                    e.setCancelled(true);
                    p.sendMessage(ChatColor.RED + "You cannot set home here! Move to land");
                }
            }
        }
    
    }
    plugin.yml:
    Code:
    main: com.vhbob.tpax.Main
    name: BoldMcGlichFix
    author: Vhbob
    version: 1.0
     
  2. Offline

    timtower Administrator Administrator Moderator

    @vhbob Did you try to debug to find where everything gets stuck?
     
  3. Offline

    vhbob

    @timtower what do you mean?? i stood in water and did "/tpa" and "asdad /tpa" and it would not work at all...
     
  4. Offline

    Xerox262

    A block cannot equal a material. and rather than checking if it contains /tpa or /sethome you should be checking it it starts with it, and you should be converting the command to lowercase so it can't be bypassed with capitals.

    Also if you modify your if slightly you can half that method. To fix your problem check the type of the block, not the actual block.
     
  5. Offline

    timtower Administrator Administrator Moderator

    @vhbob Printlines, just output something after the if statement, run the plugin, you get messages, you know where the mistake is.
     
  6. Offline

    vhbob

    @Xerox262 how would you check if the block is water or lava then?
     
  7. Offline

    Xerox262

    Block#getType() == Material.
     
  8. Offline

    vhbob

    @timtower its the part with sensing the blocks thats broke

    @Xerox262 thank you, that fixed it
     
  9. Offline

    Xerox262

    @vhbob No problem, don't forget to set the thread to solved.
     
Thread Status:
Not open for further replies.

Share This Page