Can't seem to find error in code.

Discussion in 'Plugin Development' started by CBaer4848, Nov 6, 2014.

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

    CBaer4848

    Main Class
    Code:
    package me.iAttractive.adxGUIs;
     
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin {
     
        public static Plugin plugin;
     
        public void onEnable() {
            System.out.println("adxMobs has been enabled.");
            new MobManager(this);
       
            getCommand("ncpspawn").setExecutor(new Commands(this));
       
            saveDefaultConfig();
            getConfig().options().copyDefaults(true);
            saveConfig();
       
            plugin = this;
        }
     
        public void onDisable() {
            System.out.println("adxMobs has been disabled.");
       
            saveConfig();
       
            plugin = null;
        }
        public static void registerEvents(org.bukkit.plugin.Plugin plugin,
                Listener... listeners) {
            for (Listener listener : listeners) {
                Bukkit.getServer().getPluginManager()
                        .registerEvents(listener, plugin);
            }
        }
    }
    
    Commands Class
    Code:
    package me.iAttractive.adxGUIs;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Creeper;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Villager;
    import org.bukkit.entity.Villager.Profession;
    import org.bukkit.entity.Witch;
    import org.bukkit.entity.Wolf;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class Commands extends JavaPlugin implements CommandExecutor, Listener {
     
    String wolfColor = this.method.getConfig().getString("wolfmsg");
    String witchColor = this.method.getConfig().getString("witchmsg");
    String librarianColor = this.method.getConfig().getString("librarianmsg");
    String creeperColor = this.method.getConfig().getString("creepermsg");
     
    private Main method;
     
    public Commands(Main method) {
    this.method = method;
    }
     
    // ADMIN SPAWNING COMMANDS //
    @Override
    public boolean onCommand(CommandSender sender, Command command,
    String label, String[] args) {
    if (!(sender instanceof Player)) {
    return false;
    }
    Player player = (Player) sender;
    if (label.equalsIgnoreCase("ncpspawn")) {
    if (args.length == 0) {
    player.sendMessage("§4Usage: /ncpspawn <mob>");
    player.sendMessage("§4/ncpspawn list - for a list of mobs you can spawn.");
    }
    if (args.length == 1 && args[0].equals("wolf")) {
    player.sendMessage("§aSuccess, your wolf has been spawned. Click on it for a message. Change in Config.");
    spawnMOBWolf(player);
    }
    if (args.length == 1 && args[0].equals("witch")) {
    player.sendMessage("§aSuccess, youw witch has been spawned. Click on it to open a potion shop.");
    spawnMOBWitch(player);
    }
    if (args.length == 1 && args[0].equals("librarian")) {
    player.sendMessage("§aSucccess, your librarian has been spawned. Click on it for a message. Change in Config.");
    spawnMOBLibrarian(player);
    }
    if (args.length == 1 && args[0].equals("creeper")) {
    player.sendMessage("§aSuccess, your creeper has been spawned. CLick on it for a message. Change in Config.");
    spawnMOBCreeper(player);
    }
    if (args.length == 1 && args[0].equals("list")) {
    player.sendMessage("§c----------§7adxGUI----------");
    player.sendMessage("§8Wolf");
    player.sendMessage("§8Witch");
    player.sendMessage("§8Librarian");
    player.sendMessage("§8Creeper");
    player.sendMessage("§c----------§7adxGUI----------");
    }
    // ADMIN SPAWNING COMMANDS //
    }
    return false;
    }
    // MOB SPAWN METHOD //
    public void spawnMOBLibrarian(Player player) {
    Villager vil = (Villager) player.getLocation().getWorld()
    .spawnCreature(player.getLocation(), EntityType.VILLAGER);
    vil.getCustomName();
    method.getConfig().getString("librarianname");
    vil.setCustomNameVisible(true);
    vil.setAdult();
    vil.setAgeLock(true);
    vil.setProfession(Profession.LIBRARIAN);
    vil.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 99999999,
    1000));
    vil.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION,
    99999999, 1000));
    }
     
    public void spawnMOBWolf(Player player) {
    Wolf wolf = (Wolf) player.getLocation().getWorld()
    .spawnCreature(player.getLocation(), EntityType.WOLF);
    wolf.getCustomName();
    method.getConfig().getString("wolfname");
    wolf.setCustomNameVisible(true);
    wolf.setAdult();
    wolf.setAgeLock(true);
    wolf.setBreed(false);
    wolf.setAngry(false);
    wolf.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 99999999,
    1000));
    wolf.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION,
    99999999, 1000));
    }
     
    public void spawnMOBWitch(Player player) {
    Witch witch = (Witch) player.getLocation().getWorld()
    .spawnCreature(player.getLocation(), EntityType.WOLF);
    witch.getCustomName();
    method.getConfig().getString("wolfname");
    witch.setCustomNameVisible(true);
    witch.setCanPickupItems(false);
    witch.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 99999999,
    1000));
    witch.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION,
    99999999, 1000));
    }
     
    public void spawnMOBCreeper(Player player) {
    Creeper creeper = (Creeper) player.getLocation().getWorld()
    .spawnCreature(player.getLocation(), EntityType.WOLF);
    creeper.getCustomName();
    method.getConfig().getString("wolfname");
    creeper.setCustomNameVisible(true);
    creeper.addPotionEffect(new PotionEffect(PotionEffectType.SLOW,
    99999999, 1000));
    creeper.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION,
    99999999, 1000));
    }
    }
     
    // MOB SPAWN METHOD //
     
    
    Mob Manager Class
    Code:
    package me.iAttractive.adxGUIs;
     
    import org.bukkit.Bukkit;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
     
    public class MobManager implements Listener {
     
    private Main method;
     
    public MobManager(Main method) {
    this.method = method;
    }
     
    private Commands methodCommand;
     
    public MobManager(Commands methodCommand) {
    this.methodCommand = methodCommand;
    }
     
    @EventHandler
    public void onCreatureSpawn(CreatureSpawnEvent event) {
    if (!(event.getEntity() instanceof Player)) {
    for (LivingEntity all : Bukkit.getServer().getWorld(this.method.getConfig().getString("spawnworldname")).getLivingEntities()) {
    if (all.getCustomName().equals(this.method.getConfig().getString("librarianname"))) {
    event.setCancelled(false);
    // Code
    }
     
    }
    }
    }
    }
    Plugin.yml http://pastebin.com/5JVWV2z7

    The main command /ncpspawn doesn't register in chat. It doesn't give me an error in console either. Not even an "Unknown command..."

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  2. Offline

    Watto

    CBaer4848

    Don't compare commands to 'label' use command.getName().equalsIgnoreCase
     
  3. Offline

    CBaer4848

    Other plugins that I've used like that work, and I tried it, still doesn't work. Nothing in console either.
     
  4. Offline

    Bloxcraft


    label is literally the same as command.getName()
     
  5. Offline

    teej107

    one string is equal to a command name and all of it's aliases? I don't think so.
     
  6. Offline

    leon3001

    CBaer4848 You are extending JavaPlugin in multiple classes. Only extend it in your main class. :)
     
  7. Offline

    Watto

    Bloxcraft
    No.. it's not.
    If your plugins are using the commandLabel you should change that.
    Command is the actual command
    commandLabel is the command alias that was used.
     
  8. Offline

    Bloxcraft


    TIL, well it wouldn't matter for him anyway because he has an executor for one command only, so he doesn't need to check the label/name at all.
     
  9. Offline

    Watto

    Bloxcraft

    It doesn't matter, just don't use commandLabel.
    It's better to just use Command.
     
  10. Offline

    CBaer4848

    I've tried what you said leon3001 didn't work Still no error in console or in game.
     
  11. Offline

    Skionz

    CBaer4848 No errors? Then you have to debug.
     
  12. Offline

    CBaer4848

    That's why I came here, I need help finding it :/
     
  13. Offline

    Totom3

    CBaer4848 Check your console on startup ;)
    Code:java
    1. String wolfColor = this.method.getConfig().getString("wolfmsg");
    2. String witchColor = this.method.getConfig().getString("witchmsg");
    3. String librarianColor = this.method.getConfig().getString("librarianmsg");
    4. String creeperColor = this.method.getConfig().getString("creepermsg");
    5.  
    6. private Main method;

    (In Commands.java)
    Nice NPE here. Why ? Because Java will initialize the 4 Strings before the constructor is called. At that time method is null, and calling method.getConfig() throws a NPE. How to fix ? Initialize the fields in the constructor instead.
     
Thread Status:
Not open for further replies.

Share This Page