[Commands] Not working

Discussion in 'Plugin Development' started by Spysical, Feb 27, 2012.

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

    Spysical

    At the moment I'm writing my first plugin.

    I've tried everything I tought was right. But it didn't work out.

    I've got the following wich doesn't work. In playerlistener

    Code:
    import java.util.EventListener;
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.BlockFace;
    import org.bukkit.block.BlockState;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.material.MaterialData;
    import org.bukkit.material.Wool;
    public class BPMPlayerListener extends PlayerListener {
    private final BukkitPluginMax plugin;
    public static boolean carpet = false;
    public DyeColor Kleur = DyeColor.valueOf(LoadSettings.COLOR);
    public BPMPlayerListener(BukkitPluginMax instance) {
    plugin = instance;
    }
    @EventListener
    public void onPlayerInteract(PlayerInteractEvent event) {
    BPMSignClickEvent signClickEvent = new BPMSignClickEvent(plugin);
    }
    @EventListener
    public void onPlayerJoin(final PlayerJoinEvent event) {
    Player player = event.getPlayer();
    System.out.println(player + " has joined!");
    }
    @EventListener
    public void onPlayerMove(PlayerMoveEvent event) {
    Player player = event.getPlayer();
    Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
     
    if (!BukkitPluginMax.hasCarpet(player)) {
    return;
    }
    if ((!block.isEmpty()) && (!block.isLiquid())) {
    if (block.getType() == Material.WOOL) {
    BlockState state = block.getState();
    MaterialData data = state.getData();
    if (data instanceof Wool) {
    Wool wool = (Wool) data;
    if (wool.getColor() == DyeColor.valueOf(Kleur)) {
    return;
    }
    }
    }
    plugin.putBlock(player, block.getState());
    block.setType(Material.WOOL);
    BlockState state = block.getState();
    MaterialData data = state.getData();
    if (data instanceof Wool) {
    Wool wool = (Wool) data;
    wool.setColor(DyeColor.valueOf(Kleur));
    state.update();
    }
    }
    }
    }
     
    
    And in my LoadConfig:
    Code:
    import org.bukkit.DyeColor;
    public class LoadSettings {
        static int LENGTH;
        static DyeColor COLOR;
        public static void loadMain() {
            String propertiesFile = BukkitPluginMax.maindirectory + "MainConfig.properties";
            PluginProperties properties = new PluginProperties(propertiesFile);
            properties.load();
            LENGTH = properties.getInteger("Length", 10);
            COLOR = properties.getProperty("Kleur");
            System.out.println("______---------=======LOADING SETTINGS=======---------______");
            System.out.println("Color = " + COLOR);
            System.out.println("Length= " + LENGTH);
            properties.save("===BukkitPluginMax Main Configuration===");
        }
    }
    
    Can someone help me? I'm not able to get the DyeColor out of the config and making my path that color.


    Neither am I able to get other commands than carpet working. This is my main plugin file.
    Code:
    private BPMCommandExecutor myCExecutor;
    @Override
        public void onEnable() {
            // TODO: Place any custom enable code here, such as registering events
            new File(maindirectory).mkdirs();
            if (!BPM.exists()) {
                try {
                    BPM.createNewFile();
                } catch (IOException e) {
                }
            }
            LoadSettings.loadMain();
            getServer().getPluginManager().registerEvent(Type.PLAYER_MOVE, playerListener, Priority.High, this);
            getServer().getPluginManager().registerEvent(Type.BLOCK_BREAK, blockListener, Priority.High, this);
            getServer().getPluginManager().registerEvent(Type.BLOCK_PLACE, blockListener, Priority.High, this);
            getServer().getPluginManager().registerEvent(Type.BLOCK_DAMAGE, blockListener, Priority.High, this);
            getServer().getPluginManager().registerEvent(Type.ENTITY_DAMAGE, entityListener, Priority.High, this);
            getServer().getPluginManager().registerEvent(Type.ENTITY_EXPLODE, entityListener, Priority.High, this);   
            getServer().getPluginManager().registerEvent(Type.EXPLOSION_PRIME, entityListener, Priority.High, this);
            System.out.println(this + " is now enabled!");
            myCExecutor = new BPMCommandExecutor(this);
            getCommand("carpet").setExecutor(myCExecutor);
            getCommand("watcher").setExecutor(myCExecutor);
            getCommand("level").setExecutor(myCExecutor);
        }
    
    This is my CommandExecutor:
    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    class BPMCommandExecutor implements CommandExecutor {
        private BukkitPluginMax plugin;
        public BPMCommandExecutor(BukkitPluginMax plugin) {
            this.plugin = plugin;
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            boolean een = true;
            boolean twee = true;
            Player player = null;
            if (sender instanceof Player) {
                player = (Player) sender;
            }
            if ((player.hasPermission("BukkitPluginMax.fun.carpet") || player.hasPermission("BukkitPluginMax.fun.*")) || player.isOp()) {
                if (cmd.getName().equalsIgnoreCase("carpet")) {
                    if (args.length > 0) {
                        return false;
                    }
                    if (sender instanceof Player) {
                        plugin.setCarpet(player, !plugin.hasCarpet(player));               
                    } else {
                        sender.sendMessage(ChatColor.RED + "I haven't the foggiest idea who you are, no carpet for you sir!");
                    }
                } else if (cmd.getName().equalsIgnoreCase("carpet2")) {
                    if (player == null) {
                        sender.sendMessage("this command can only be run by a player");
                        return true;
                    }
                }
                return true;
            }
            if ((player.hasPermission("BukkitPluginMax.fun.level") || player.hasPermission("BukkitPluginMax.fun.*")) || player.isOp()) {
                int level = player.getLevel();
                if (cmd.getName().equalsIgnoreCase("level")) {
                    if (args.length >= 3) {
                        sender.sendMessage(ChatColor.RED + "Too many arguments!");
                        return true;
                    } else if (args.length == 1) {
                        Player other = (Bukkit.getServer().getPlayer(args[0]));
                        if (other == null) {
                            sender.sendMessage(ChatColor.RED + args[0] + " is not online!");
                            return true;
                        }
                        other.setLevel(level + 1);
                    } else if (args.length == 0) {
                        player = (Player) sender;
                        player.setLevel(level + 1);
                    }
                } else if (cmd.getName().equalsIgnoreCase("level2")) {
                    if (player == null) {
                        sender.sendMessage("this command can only be run by a player");
                        return true;
                    }
                }
                return true;
            }
            if ((player.hasPermission("BukkitPluginMax.fun.watcher") || player.hasPermission("BukkitPluginMax.fun.*")) || player.isOp()) {
                Player other = (Bukkit.getServer().getPlayer(args[0]));
                if (cmd.getName().equalsIgnoreCase("watcher")) {
                    if (args.length >= 2) {
                        sender.sendMessage(ChatColor.RED + "Too many arguments!");
                        return true;
                    } else if (args.length == 1 && args.length < 2) {
                        sender.sendMessage(ChatColor.RED + "Sorry this is not yet supported!");
                        return true;
                    } else if (args.length == 0) {
                        if (BPMBlockListener.watcher) {
                            BPMBlockListener.watcher = false;
                            return true;
                        } else if (!BPMBlockListener.watcher) {
                            BPMBlockListener.watcher = true;
                            return true;
                        }
                    }
                } else if (cmd.getName().equalsIgnoreCase("watcher")) {
                    if (player == null) {
                        sender.sendMessage("this command can only be run by a player");
                        return true;
                    }
                }
            } else {
                return true;
            }
            return true;
        }
    }
    
    I'm doing this for school. And since the teacher can't help me out. I'll be very thankfull if one of you could!

    Thanks for the effort!
     
  2. Offline

    Muddr

    looking real quick.. change @Override to @EventListener
    Code:
    @Override
        public void onPlayerMove(PlayerMoveEvent event) {
            Player player = event.getPlayer();
     
    Spysical likes this.
  3. Offline

    Spysical

    Muddr
    I've changed it, this is what I get when I try to build:
    incompatible types
    found : java.util.EventListener
    required: java.lang.annotation.Annotation
    @EventListener
     
  4. To me it looks like your using the old event system not the new system for RB4

    This here may help

    I am no expert but this is where i would start and change them to the latest even handler. (pm.registerEvents(handler, this);)

    As for the incompatable types try removing the import for java.util.EventListener and then if using eclipse import it using the help menu.

    Like i say i am no expert so make sure to backup first.
    Hope this helps

    Damo.
     
  5. code always work, so that not the problem.
    the code is not working as you want that it work, that's different then that it not work.
     
  6. Offline

    Spysical

    I've changed my code to work with the new system.

    But can someone help me out on the following:
    Playerlistener.java:
    public DyeColor Kleur = DyeColor.valueOf(LoadSettings.COLOR);
    wool.getColor() == DyeColor.valueOf(Kleur);

    loadsetttings.java:
    static String COLOR;
    COLOR = properties.getProperty("Kleur");
    The rest works. But this won't.
     
  7. Kleur is already a dye color :confused: so why do you need .valueOf again?
     
  8. Offline

    Spysical

    I recoded all my code, but when I try to use any of my commands they simply won't work.

    Could someone help me out a bit? I'm new to all this :)

    MainFile:
    PHP:
    package net.nijholt.bukkitpluginmax;
     
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.LinkedList;
    import java.util.Map;
    import java.util.Set;
    import org.bukkit.ChatColor;
    import org.bukkit.block.BlockState;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class 
    BukkitPluginMax extends JavaPlugin {
     
        public final 
    BPMBlockListener bl = new BPMBlockListener(this);
        public final 
    BPMEntityListener el = new BPMEntityListener(this);
        public final 
    BPMPlayerListener pl = new BPMPlayerListener(this);
        private 
    Set<Playercarpets = new HashSet<Player>();
        private 
    Map<PlayerLinkedList<BlockState>> blocks = new HashMap<PlayerLinkedList<BlockState>>();
        private 
    Set<Playerwatch = new HashSet<Player>();
     
        public 
    void onDisable() {
            
    // TODO: Place any custom disable code here.
            
    System.out.println(this " is now disabled!");
        }
     
        public 
    void onEnable() {
            
    // TODO: Place any custom enable code here, such as registering events
            
    PluginManager pm getServer().getPluginManager();
            
    pm.registerEvents(plthis);
            
    pm.registerEvents(blthis);
            
    pm.registerEvents(elthis);
            
    this.getConfig().options().copyDefaults(true);
            
    saveConfig();
            
    System.out.println(this " is now enabled!");
            
    getCommand("bpm").setExecutor(new CommandExecutor() {
                @
    Override
                
    public boolean onCommand(CommandSender csCommand cmndString aliasString[] args) {
                    if (
    args.length == 0) {
                        
    cs.sendMessage(ChatColor.GREEN "BukkitPluginMax help:");
                        
    cs.sendMessage(ChatColor.RED "/bpm carpet" ChatColor.GREEN " gives a carpet with configurated length");
                        return 
    true;
                    } else if (
    args[0].equalsIgnoreCase("carpet")) {
                        if (
    cs instanceof Player) {
                            
    Player player = (Playercs;
                            if ((
    player.hasPermission("BukkitPluginMax.fun.carpet") || player.hasPermission("BukkitPluginMax.fun.*")) || player.isOp()) {
                                
    setCarpet(player, !hasCarpet(player));
                            } else {
                                
    player.sendMessage(ChatColor.RED "You aren't special enough for a red carpet.");
     
                            }
                        }
                        return 
    true;
                    } else if (
    args[0].equalsIgnoreCase("level")) {
                        if (
    cs instanceof Player) {
                            
    Player player = (Playercs;
                            if ((
    player.hasPermission("BukkitPluginMax.fun.level") || player.hasPermission("BukkitPluginMax.fun.*")) || player.isOp()) {
                                if (
    args.length == 1) {
                                    
    int NewLevel Integer.parseInt(args[1]);
                                    
    player.setLevel(NewLevel);
                                    return 
    true;
                                } else {
                                    
    int level player.getLevel();
                                    
    player.setLevel(level 1);
                                    return 
    true;
                                }
                            } else {
                                
    cs.sendMessage(ChatColor.RED "You aren't special enough for a red carpet.");
                            }
                        }
                        return 
    true;
                    } else if (
    args[0].equalsIgnoreCase("watch")) {
                        if (
    cs instanceof Player) {
                            
    Player player = (Playercs;
                            if ((
    player.hasPermission("BukkitPluginMax.fun.watch") || player.hasPermission("BukkitPluginMax.fun.*")) || player.isOp()) {
                                
    setWatched(player, !isWatched(player));
                                return 
    true;
                            } else {
                                
    cs.sendMessage(ChatColor.RED "I haven't the foggiest idea who you are, no carpet for you sir!");
                                return 
    true;
                            }
                        }
                        return 
    true;
                    }
     
                    return 
    true;
                }
            });
        }
     
        public 
    boolean hasCarpet(Player player) {
            return 
    carpets.contains(player);
        }
     
        public 
    void setCarpet(Player playerboolean enabled) {
            if (
    enabled) {
                
    carpets.add(player);
            } else {
                
    carpets.remove(player);
            }
        }
     
        public 
    boolean isWatched(Player player) {
            return 
    watch.contains(player);
        }
     
        public 
    void setWatched(Player playerboolean enabled) {
            if (
    enabled) {
                
    watch.add(player);
            } else {
                
    watch.remove(player);
            }
        }
     
        private 
    LinkedList<BlockStategetBlockList(Player player) {
            
    LinkedList<BlockStateresult blocks.get(player);
     
            if (
    result == null) {
                
    result = new LinkedList<BlockState>();
                
    blocks.put(playerresult);
            }
     
            return 
    result;
        }
     
        public 
    void putBlock(Player playerBlockState block) {
            
    LinkedList<BlockState> list = getBlockList(player);
     
            if (list.
    size() > getConfig().getInt("fun.length")) {
                
    BlockState old = list.removeFirst();
     
                
    old.update(true);
            }
     
            list.
    add(block);
        }
    }
    plugin.yml:
    Code:
    author: Max Nijholt
    database: false
    description: A plugin written for school
    generator: http://dinnerbone.com/minecraft/tools/pluginator/
    main: net.nijholt.bukkitpluginmax.BukkitPluginMax
    name: SchoolPlugin
    startup: postworld
    version: pre-alpha06
    commands:
      bpm:
      carpet:
          description: Add a carpet.
          permission: BukkitPluginMax.fun.carpet
          usage: /<command> [player]
      level:
          description: Add levels.
          permission: BukkitPluginMax.fun.level
          usage: /<command> [amount]
      watch:
          description: Get what you do.
          permission: BukkitPluginMax.fun.watcher
          usage: /<command> [player]
    permissions:
      BukkitPluginMax.fun.*:
            description: Gives all fun commands
            children:
                BukkitPluginMax.fun.carpet: true
                BukkitPluginMax.fun.watch: true
                BukkitPluginMax.fun.level: true
      BukkitPluginMax.fun.carpet:
            description: Add a carpet
      BukkitPluginMax.fun.watch:
          description: Get what you do.
      BukkitPluginMax.fun.level:
          description: Add 1 level.
    
     
  9. Code:
    public final anyclass bl = new anyclass(this);
    (anyclass can stand for, obviously, any class)
    "this" will be null until the constructor of the class the variable is in gets called. but every time you use something like the thing above it gets called before the constructor meaning that this is null.
    also bpm has no info (I'm not sure if you can do this so it's like with switch)
    and a little addition: I would make a separate class for the command handler just makes it easier to look at.
     
  10. Offline

    Spysical

    every thing seems to work properly, exept when i log in:
    Code:
    [SEVERE] Could not pass event PlayerJoinEvent to MaxPlugin
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:303)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:459)
    at net.minecraft.server.ServerConfigurationManager.c(ServerConfigurationManager.java:132)
    at net.minecraft.server.NetLoginHandler.b(NetLoginHandler.java:129)
    at net.minecraft.server.NetLoginHandler.a(NetLoginHandler.java:41)
    at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:61)
    at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:551)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:449)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
    at net.nijholt.bukkitpluginmax.BPMPlayerListener.onPlayerJoin(BPMPlayerListener.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301)
    ... 9 more
    
    My PlayerListener:
    PHP:
    package net.nijholt.bukkitpluginmax;
     
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.BlockFace;
    import org.bukkit.block.BlockState;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.material.MaterialData;
    import org.bukkit.material.Wool;
     
    /**
    *
    * @author Spysical aka Max Nijholt
    *
    */
    public class BPMPlayerListener implements Listener {
     
        private final 
    BukkitPluginMax plugin;
        public static 
    boolean carpet false;
        private static 
    BPMEconomyBasic eco;
    //    public DyeColor Kleur = LoadSettings.COLOR;
     
        
    public BPMPlayerListener(BukkitPluginMax instance) {
            
    plugin instance;
        }
     
        @
    EventHandler
        
    public void onPlayerInteract(PlayerInteractEvent event) {
        }
     
        @
    EventHandler
        
    public void onPlayerJoin(PlayerJoinEvent event) {
            
    Player player = (Playerevent.getPlayer(); // The player who joined
            
    if (plugin.LoginBonusing && player instanceof Player) {
                
    eco.addMoney(plugin.LoginBonusplayer);
            }
        }
     
        @
    EventHandler
        
    public void onPlayerMove(PlayerMoveEvent event) {
            
    Player player event.getPlayer();
            
    Block block player.getLocation().getBlock().getRelative(BlockFace.DOWN);
     
     
            if (!
    plugin.hasCarpet(player)) {
                return;
            }
     
            if ((!
    block.isEmpty()) && (!block.isLiquid())) {
                if (
    block.getType() == Material.WOOL) {
                    
    BlockState state block.getState();
                    
    MaterialData data state.getData();
     
                    if (
    data instanceof Wool) {
                        
    Wool wool = (Wooldata;
                        if (
    wool.getColor() == DyeColor.RED) {
                            return;
                        }
                    }
                }
     
                
    plugin.putBlock(playerblock.getState());
                
    block.setType(Material.WOOL);
     
                
    BlockState state block.getState();
                
    MaterialData data state.getData();
     
                if (
    data instanceof Wool) {
                    
    Wool wool = (Wooldata;
                    
    wool.setColor(DyeColor.RED);
                    
    state.update();
                }
            }
        }
    }
     
Thread Status:
Not open for further replies.

Share This Page