I need help repeating this line of code

Discussion in 'Plugin Development' started by MattDlr, Apr 22, 2018.

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

    MattDlr

    So, I am trying to make a pet plugin and I need help repeating this line of code so the mob will constantly teleport to me.
    Code:
    Wh.teleport(player.getLocation().add(1, 0, 0));
    Here is the full code
    Code:
    package me.bukkit.ChickenTheNugget;
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Donkey;
    
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    
    
    
    public class WhoreHai extends JavaPlugin {
    
        private static final String LivingEntity = null;
        private Donkey Wh;
    
        @Override
        public void onEnable() {
            getLogger().info("You better get ready because Whore-Hai plugin has been enabled BOOOOIIIIS!!!");
        }
    
        @Override
        public void onDisable() {
    
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            Player player = (Player) sender;
    
            if (cmd.getName().equalsIgnoreCase("wh") && sender instanceof Player) {
                   
                   
                }
    
               
                    if (args.length == 0) {
                       
                        player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "WH" + ChatColor.DARK_GRAY + "]" + ChatColor.YELLOW + " You silly goose you only did /wh thats doesn't do anything! Try adding an argument at the end like" + ChatColor.GOLD + " test" + ChatColor.YELLOW + " or " + ChatColor.GOLD + "summon" + ChatColor.YELLOW  + "!");
                       
                    }
                   
                   
                    if(args[0].equalsIgnoreCase("summon")) {
                        player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "WH" + ChatColor.DARK_GRAY + "]" + ChatColor.YELLOW + ChatColor.BOLD + " You have summond the beast!!!");
                        Wh = player.getLocation().getWorld().spawn(player.getLocation(), Donkey.class);
                        Wh.setCustomName(ChatColor.BOLD  + "Whore" + ChatColor.BOLD + "-" + ChatColor.BOLD + "Hai");
                        Wh.setAgeLock(true);
                        Wh.setBaby();
                        Wh.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1000000, 1000000));
                        Wh.teleport(player.getLocation().add(1, 0, 0));
                       
                               
                   
    
                    }
                       
    
                    if(args[0].equalsIgnoreCase("test")) {
                   
                player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "WH" + ChatColor.DARK_GRAY + "]" + ChatColor.YELLOW + " Hello, " + ChatColor.GOLD + player.getName() + ChatColor.YELLOW + " the Whore-Hai plugin is active and ready for use!");
    
                return true;
               
                    }
    
    
             return true;
           
           
    
        }
    
           
        }
    
    Any help would be awesome!
     
  2. Offline

    Zombie_Striker

    @MattDlr
    What you're asking for is a BukkitRunnable.

    However, a better way to let an entity follow a player would be to use Custom Enitites with Pathfinder goals. That way the entity is actually following the player instead of just constantly jerking to the location directly next to the player or trying to move in another direction from the player.
     
  3. Offline

    MattDlr

    I tried the pathfinder goal but I could not understand it. And I thought if I were to teleport it fast enough it would just kinda stay in the location and not jerk around.
     
  4. Offline

    Zombie_Striker

    @MattDlr
    What part of the pathfinder is confusing? It should be as simple as copying and pasting the class, and then just create a simple custom entity that just has that as its only pathfinder goal.
     
  5. Offline

    MattDlr

    @Zombie_Striker
    I don't know where to put the class and I don't know how to give my custom entity the pathfinder or how to edit the pathfinder. And every time I have tried to do this always get an error message.
     
    Last edited: Apr 23, 2018
  6. Offline

    Zombie_Striker

    @MattDlr
    The class should be in its own file somewhere inside the plugin's package.

    Here's a more in-depth tutorial on Custom Entities and pathfindergoals (Here). Code-block #4 should show you what the custom entity class should look like and code-block #5 and #6 show you how to add pathfindergoals.
     
  7. Offline

    MattDlr

    @Zombie_Striker
    Ok so I made the pathfindergoal class and now I am confused on how to add stuff to the class and how I'm supposed to connect that to the entity I have now
     
  8. Offline

    Zombie_Striker

    What 'stuff' are you referring to? If you mean the code, it's literally just copying and pasting the code from the first link.

    You will need a custom entity class to use the pathfinder. In case you are still spawning the default donkey class, you will need to use the following line instead:
    Code:
    EntityTypes.spawnEntity(new CLASS(WORLD), LOCATION);
    Where
    1. CLASS is the custom entity class that you will be creating (if you have not done so already)
    2. WORLD is the world the entity is spawned in
    3. Location is the location you want to spawn the donkey.
    To make the entity follow the player, follow the directions as posted in the second link under the "Create Your Own Pathfindergoals" section. That should also be as simple as copying and pasting.
     
  9. Offline

    MattDlr

    @Zombie_Striker
    I tried doing it, here is the code.
    Code:
    package me.bukkit.ChickenTheNugget;
    
    
    import java.security.PublicKey;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Donkey;
    
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.potion.PotionType;
    
    import net.minecraft.server.v1_12_R1.EntityHorseDonkey;
    import net.minecraft.server.v1_12_R1.EntityInsentient;
    import net.minecraft.server.v1_12_R1.Navigation;
    import net.minecraft.server.v1_12_R1.PathEntity;
    import net.minecraft.server.v1_12_R1.PathfinderGoal;
    
    import net.minecraft.server.v1_12_R1.PathfinderGoalFollowEntity;
    
    
    
    
    
    
    
    public class WhoreHai extends JavaPlugin {
    
        private static final String LivingEntity = null;
        private Donkey Wh;
    
        @Override
        public void onEnable() {
            getLogger().info("You better get ready because Whore-Hai plugin has been enabled BOOOOIIIIS!!!");
        }
    
        @Override
        public void onDisable() {
    
        }
       
        public class Donkey extends EntityHorseDonkey{
            private Object targetSelector;
    
            Public CustomHorseDonkey(org.bukkit.World world) {
                super(((CraftWorld)world).getHandle());
               
                Donkey craftDonkey = (Donkey) this.getBukkitEntity();
               
                this.setBaby(true);
               
                craftDonkey.setMaxHealth(1000000);
               
                this.setHealth(1000000);
                this.setCustomName(ChatColor.GOLD + "Whore-Hai");
                this.addPotionEffect(PotionType.REGEN);
                this.setCustomNameVisible(true);
               
                this.targetSelector.a(0, new PathfindergoalFollowEntity<EntityPlayer>(Entity.Player.class, true));
               
                this.getWorld().AddEntity(this);
               
            }
           
           
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            Player player = (Player) sender;
    
            if (cmd.getName().equalsIgnoreCase("wh") && sender instanceof Player) {
                   
                   
                }
    
               
                    if (args.length == 0) {
                       
                        player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "WH" + ChatColor.DARK_GRAY + "]" + ChatColor.YELLOW + " You silly goose you only did /wh thats doesn't do anything! Try adding an argument at the end like" + ChatColor.GOLD + " test" + ChatColor.YELLOW + " or " + ChatColor.GOLD + "summon" + ChatColor.YELLOW  + "!");
                       
                    }
       
    
                       
                       
                               
                   
    
    
                       
    
                    if(args[0].equalsIgnoreCase("test")) {
                   
                player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "WH" + ChatColor.DARK_GRAY + "]" + ChatColor.YELLOW + " Hello, " + ChatColor.GOLD + player.getName() + ChatColor.YELLOW + " the Whore-Hai plugin is active and ready for use!");
    
                return true;
               
                    }
    
    
             return true;
           
           
    
        }
    
           
        }
    
    And it did not work, do you know what i did wrong?
     
  10. Offline

    Zombie_Striker

    The 'p' in Public should be lowercase, and the A in AddEntity should be lower case. It should not even be able to compile with these mistakes.

    Is this actually your code, or did you just copy and paste/write stuff just to be put in that post? If it is your code, are you using an IDE, which should be able to point out these problems?
     
  11. Offline

    MattDlr

    That is actually my code and the IDE I am using Eclipse as my IDE.
    @Zombie_Striker
     
  12. Offline

    Zombie_Striker

    @MattDlr
    Does it show that there are problems on those lines?

    Also, it seems I forgot to ask: What exactly does not work? It does not look like you actually spawn the CustomHorseDonkey anywhere, so I'm not sure what else that plugin does that would not work.
     
  13. Offline

    MattDlr

    I have the commands /wh and /wh test that work I thought the command /wh summon summoned in that entity and no these problems did not show up on the lines
    @Zombie_Striker
     
  14. Offline

    MattDlr

    @Zombie_Striker
    Ok so I got it so the entity will tp to me constantly but when I log off then log back on it will not teleport to me I have to summon another one is there a way to make it so it will tp to me even after I log off then log back on?
     
  15. Offline

    Zombie_Striker

    @MattDlr
    This could be happening because you are comparing the player instances instead of checking for names/UUIDs. When a player relogs, the "Player" object that used to hold them will no longer be connected with the player, and you will need to update the instance.
     
  16. Offline

    MattDlr

    @Zombie_Striker
    Ok, so how can I make it so that the entity will tp to the UUID of the player that did /wh summon?
     
  17. Offline

    Zombie_Striker

    @MattDlr
    Could you post your code? Depending on how you originally stored the player instance, the best method to use may be different.
     
  18. Offline

    MattDlr

    @Zombie_Striker
    Sure.
    Code:
    package me.bukkit.ChickenTheNugget;
    
    
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Donkey;
    
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    
    
    
    
    
    
    
    public class WhoreHai extends JavaPlugin {
    
        private static final String LivingEntity = null;
        private Donkey Wh;
    
        @Override
        public void onEnable() {
            getLogger().info("You better get ready because Whore-Hai plugin has been enabled BOOOOIIIIS!!!");
           
    
           
        }
    
        @Override
        public void onDisable() {
    
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            Player player = (Player) sender;
    
            if (cmd.getName().equalsIgnoreCase("wh") && sender instanceof Player) {
                   
                   
                }
    
               
                    if (args.length == 0) {
                       
                        player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "WH" + ChatColor.DARK_GRAY + "]" + ChatColor.YELLOW + " You silly goose you only did /wh thats doesn't do anything! Try adding an argument at the end like" + ChatColor.GOLD + " test" + ChatColor.YELLOW + " or " + ChatColor.GOLD + "summon" + ChatColor.YELLOW  + "!");
                       
                    }
                   
                   
                    if(args[0].equalsIgnoreCase("summon")) {
                        player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "WH" + ChatColor.DARK_GRAY + "]" + ChatColor.YELLOW + ChatColor.BOLD + " You have summond the beast!!!");
                        Wh = player.getLocation().getWorld().spawn(player.getLocation(), Donkey.class);
                        Wh.setCustomName(ChatColor.BOLD  + "Whore" + ChatColor.BOLD + "-" + ChatColor.BOLD + "Hai");
                        Wh.setAgeLock(true);
                        Wh.setBaby();
                        Wh.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 10000000, 10000000));
                        Wh.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 10000000, 10000000));
                        Wh.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 10000000, 10000000));
                        Wh.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 10000000, 10000000));
                        Wh.setGravity(false);
                        Wh.setInvulnerable(true);
                        Bukkit.getScheduler().scheduleSyncRepeatingTask(this , new Runnable()
                   
                        {
                             public void run()
                                  {
                                         Wh.teleport(player.getLocation().add(1, 0, 0));
                                
                                        
                                  }
                        }, 0, 1);
                       
                       
                               
                   
    
                    }
    
                    if(args[0].equalsIgnoreCase("test")) {
                   
                player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.GOLD + "WH" + ChatColor.DARK_GRAY + "]" + ChatColor.YELLOW + " Hello, " + ChatColor.GOLD + player.getName() + ChatColor.YELLOW + " the Whore-Hai plugin is active and ready for use!");
    
             b    return true;
               
                    }
                    Bukkit.getScheduler().scheduleSyncRepeatingTask(this , new Runnable()
                   
                    {
                         public void run()
                              {
                                     Wh.teleport(player.getLocation().add(1, 0, 0));
                                    
                              }
                    }, 0, 1);
    
    
             return true;
           
           
    
        }
    
           
        }
    
    
    
     
  19. Offline

    Zombie_Striker

    @MattDlr
    Okay, for this method, it would be best to move the runnables into their own class, and then create an arraylist for them. Try doing the following:
    1. Create a new class. This will store the runnables. Let it implement BukkitRunnable.
    2. Create three new fields; A static Arraylist<YOUR_CLASS> field, a Player field, and a Donkey field. These variables will store all of the runnables, the player, and the donkey respectively.
    3. Create a constructor. This will require a Player variable and a Donkey variable. This will initialize the two variable above, and add the instance to the arraylist above.
    4. Then copy over the 'run' method.
    5. [Optional] If the player logs off, you should not be able to get the player's location, throwing an error. Try making sure the player is online before teleporting the donkey.
    6. Finally, add a "getPlayer()" and a "setPlayer(Player player)" method to the class. This is how we will get the player and change the player's instance if the player is not line.
    7. Then, back in the main class, whenever a player sends the command to teleport the donkey, create a new instance of the class above and provide the player and donkey instance.
    8. Now, to support relogging, create a new method to listen to the PlayerJoinEvent. In it, loop through all of the runnables stored in the static Arraylist. If the player from the runnable has the same uuid as the player that joined, set the player instance for the runnable to be equal to the one that just joined.
     
  20. Offline

    MattDlr

    Thank you!

    @Zombie_Striker
    So here is what I have.
    Code:
    package me.bukkit.ChickenTheNugget;
    
        public class StaticArray {
           
            StaticArray(){
               
            }
           
        }
    
        public class Player {
           
            Player(){
               
            }
           
        }
       
        public class Donkey {
           
            Donkey(){
               
            }
           
        }
       
        
    Where do I put the Player and Donkey variable and where would I put theBukkitRunnable

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 27, 2018
Thread Status:
Not open for further replies.

Share This Page