Solved [NMS] Custom Mob Spawning/Moving to location

Discussion in 'Plugin Development' started by AdamDev, Dec 14, 2017.

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

    AdamDev

    Hi, as most of what the title says. I need help with spawning and moving a custom mob to a specific location. I have tried many other tutorials, even went trying it my self to see if I could get it. If I somehow find it out myself, I will post my recent code for all classes on here for anyone else who needed help on this. This is only a testing project, it might look like a real plugin but it's just a hobby that I do. As most people request code I will post most of my classes here that require help such as:

    EDIT 2: Code has been completely updated to the code I have now with CitizensAPI 1.12.2 and Bukkit 1.12.2.

    My Command for spawning NPC:
    SpawnNew.java (open)

    Code:
    package me.hype.testing.ep.commands;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    
    import me.hype.testing.ep.Core;
    import net.citizensnpcs.api.CitizensAPI;
    import net.citizensnpcs.api.npc.NPC;
    
    public class SpawnNew implements CommandExecutor {
    
        Core plugin = Core.getInstance();
        String prefix = "";
        public static NPC npc;
    
        public boolean onCommand(CommandSender sender, Command cmd, String l, String[] a) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You must be a 'Player' to run this command!");
                return true;
            }
            Player p = (Player) sender;
            int length = a.length;
            if (cmd.getName().equalsIgnoreCase("newnpc")) {
                if (!p.hasPermission("perm.new")) {
                    p.sendMessage(format(prefix + "&cYou don't have permission to run this command."));
                    return true;
                }
                if (length == 0) {
                    npc = CitizensAPI.getNPCRegistry().createNPC(EntityType.PLAYER, "&2Zombie");
                    npc.spawn(p.getLocation());
                    p.sendMessage(format(prefix+"&aSpawned NPC."));
                    return true;
                } else if (length >= 1) {
                    p.sendMessage(format(prefix+"blah"));
                    return true;
                }
            }
            return true;
        }
    
        public String format(String s) {
            return ChatColor.translateAlternateColorCodes('&', s);
        }
    }
    
    

    My Command for moving the NPC (Requires the NPC to be spawned look at command before):
    MoveTo.java (open)

    Code:
    package me.hype.testing.ep.commands;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import me.hype.testing.ep.Core;
    
    public class MoveTo implements CommandExecutor {
      
        Core plugin = Core.getInstance();
        SpawnNew sn = new SpawnNew();
        String prefix = "";
    
        public boolean onCommand(CommandSender sender, Command cmd, String l, String[] a) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You must be a 'Player' to run this command!");
                return true;
            }
            Player p = (Player) sender;
            int length = a.length;
            if (cmd.getName().equalsIgnoreCase("epmoveto")) {
                if (!p.hasPermission("perm.move")) {
                    p.sendMessage(format(prefix + "&cYou don't have permission to run this command."));
                    return true;
                }
                if (length == 0) {
                    p.sendMessage(format("blah"));
                    return true;
                } else if (length == 1) {
                    p.sendMessage(format("blah"));
                    return true;
                } else if (length == 2) {
                    p.sendMessage(format("blah"));
                    return true;
                } else if (length == 3) {
                    double x = Double.parseDouble(a[0]);
                    double y = Double.parseDouble(a[1]);
                    double z = Double.parseDouble(a[2]);
                    World w = p.getLocation().getWorld();
                    Location loc = new Location(w,x,y,z);
                    SpawnNew.npc.getNavigator().setTarget(loc);
                    p.sendMessage(format("Moving to location..."));
                    return true;
                } else if (length >= 4) {
                    p.sendMessage(format("blah"));
                    return true;
                }
            }
            return true;
        }
    
        public String format(String s) {
            return ChatColor.translateAlternateColorCodes('&', s);
        }
    }
    
    



    EDIT:
    If you are looking at this thread I have now currently solved my problem with CitizensAPI. I didn't want to use a API but from the looks of it that will happen until I find my own way. I'll post my CitizensAPI code here for further reference for anyone else.
     
    Last edited: Dec 18, 2017
  2. Offline

    AdamDev

    If anyone checks this thread, I have changed most of my code. If the problem persists I'll update this code.
     
Thread Status:
Not open for further replies.

Share This Page