Development Assistance Entity follow help

Discussion in 'Plugin Help/Development/Requests' started by MunchMallow, May 18, 2015.

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

    MunchMallow

    Hello everybody,

    I've been working so freaking hard on getting this fixed, and no matter what I try, I always end up with an error. It has actually been bugging me for quite a while now. xD

    Whenever I type /pet pig it spawn the pig, and then it'll give me an error, and the pig won't follow me at all!
    So here are all of my classes including the error.

    Thank you in advance if you would leave a suggestion.

    The PetCommands class:
    Code:
    package me.com.MunchMallow.Commands;
    
    import me.com.MunchMallow.utils.PetMethod;
    import me.com.MunchMallow.utils.StringUtil;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Chicken;
    import org.bukkit.entity.Cow;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Pig;
    import org.bukkit.entity.Player;
    
    public class PetCommand implements CommandExecutor {
       
        public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
            Player p = (Player) sender;
            if (alias.equalsIgnoreCase("pet")) {
                if (args.length == 0) {
                    p.sendMessage("Please use a valid argument");
                    p.sendMessage("Lists of arguments:");
                    p.sendMessage("pig, chicken, cow");
                }
                else
                {
                    if (args[0].equalsIgnoreCase("pig")) {
                        p.sendMessage(StringUtil.eo + "You spawned a pig!");
                        LivingEntity pig = p.getWorld().spawn(p.getLocation(), Pig.class);
                        PetMethod.followPlayer(p, pig, 0.3);
                    }
                    else if (args[0].equalsIgnoreCase("chicken")) {
                        p.sendMessage(StringUtil.eo + "You spawned a chicken!");
                        LivingEntity chicken = p.getWorld().spawn(p.getLocation(), Chicken.class);
                        PetMethod.followPlayer(p, chicken, 0.3);
                    }
                    else if (args[0].equalsIgnoreCase("cow")) {
                        p.sendMessage(StringUtil.eo + "You spawned a cow!");
                        LivingEntity chicken = p.getWorld().spawn(p.getLocation(), Cow.class);
                        PetMethod.followPlayer(p, chicken, 0.3);
                    }
                    else
                    {
                        p.sendMessage("Oi - type something valid next time.");
                    }
                }
            }
            return false;
        }
    }
    

    The PetMethod class:
    Code:
    import me.com.MunchMallow.Main.Main;
    import net.minecraft.server.v1_8_R1.EntityInsentient;
    import org.bukkit.Bukkit;
    import org.bukkit.craftbukkit.v1_8_R1.entity.CraftEntity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    
    public class PetMethod {
    
        public static Main plugin;
    
        public PetMethod(final Main instance) {
            plugin = instance;
        }
       
        public static void followPlayer(Player player, LivingEntity entity, double d) {
            final LivingEntity e = entity;
            final Player p = player;
            final float f = (float) d;
            Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                @Override
                public void run() {
                    ((EntityInsentient) ((CraftEntity) e).getHandle()).getNavigation().a(p.getLocation().getX(), p.getLocation().getY(), p.getLocation().getZ(), f);
                }
            }, 0 * 20, 2 * 20);
        }
    }
    Error:
    Code:
    18.05 17:25:28 [Server] INFO ... 15 more
    18.05 17:25:28 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO at me.com.MunchMallow.Commands.PetCommandListener.onCommand(PetCommandListener.java:30) ~[?:?]
    18.05 17:25:28 [Server] INFO at me.com.MunchMallow.utils.PetMethod.followPlayer(PetMethod.java:22) ~[?:?]
    18.05 17:25:28 [Server] INFO at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftScheduler.scheduleSyncRepeatingTask(CraftScheduler.java:119) ~[spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:123) ~[spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO at org.bukkit.craftbukkit.v1_8_R1.scheduler.CraftScheduler.validate(CraftScheduler.java:397) ~[spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
    18.05 17:25:28 [Server] INFO at java.lang.Thread.run(Unknown Source) [?:1.7.0]
    18.05 17:25:28 [Server] INFO at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:537) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:634) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:316) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:696) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.7.0]
    18.05 17:25:28 [Server] INFO at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) [?:1.7.0]
    18.05 17:25:28 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.7.0]
    18.05 17:25:28 [Server] INFO at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:53) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(PacketPlayInChat.java:26) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java:950) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerConnection.java:1115) [spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot_server.jar:git-Spigot-c3c767f-33d5de3]
    18.05 17:25:28 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'pet' in plugin YangFu v1.1
    18.05 17:25:28 [Server] ERROR null
     
  2. Offline

    mine-care

    Ok some tips to improve your code:
    Don't cast sender to player without checking! See my signature
    Dont abuse static! Thats what causes your error!
    PetMethod class has a static variable "plugin" that is null, in order to get a value you set it by the constructor, the constructor is called only when an object is created that isnt the case here since you invole a static method...
    ADont start infinite runnables without "breaking" condition!
    Also in class PetMethod what is the point of lines 17-19? Ehy redefining those variables?
    Please have a look here:
    http://www.tutorialspoint.com/java/java_object_classes.htm
    https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
    :- ) cheers.
     
  3. Offline

    MunchMallow

    Oh woah, thanks! I got it to work now! :D Easier than I thought haha xD
    EDIT: I also did not notice I redefined the variables, oh man I feel stupid now xD
     
  4. Offline

    mine-care

    @MunchMallow great :D also dont geek stupid :p we all do mistakes...
     
Thread Status:
Not open for further replies.

Share This Page