Solved Need help with a pet plugin.

Discussion in 'Spigot Plugin Development' started by MattDlr, Jun 18, 2018.

  1. I am trying to make a pet plugin where the player can choose the name of the pet. How can I make it so when the player executes a command it will open the anvil inventory and when the player types in the name it changes the name of their pet to what they typed?
     
  2. Code (Java):

    List<UUID> map ...

    // whatever you do to add them... (maby a gui or whatever)

    @eventhandler
    onplayerchatevent {
    Player player = ....
    UUID uuid = player.getUniqu...;

    if (map.contains(uuid)) {
    setPetName(e.getMessage());
    map.remove(uuid);
    cancelEvent;
    }

    }
     
     
    #2 darklazerog, Jun 18, 2018
    Last edited: Jun 18, 2018
    • Agree Agree x 1
  3. How can I apply that to what I have now?

    Code (Java):
    package me.ChickenTheNugget;



    import java.util.HashMap;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Guardian;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerKickEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;




       




    public class Pets extends JavaPlugin implements Listener {

        @SuppressWarnings("unused")
        private static final String LivingEntity = null;
        private HashMap<UUID, Guardian> G = new HashMap<UUID,Guardian>();
       
       
        @EventHandler
        public void OnLeave(PlayerQuitEvent event){
            Player player = (Player) event.getPlayer();
            player.chat("/wh kill");
            }
       
        @EventHandler
        public void OnKick(PlayerKickEvent event){
            Player player = (Player) event.getPlayer();
            player.chat("/wh kill");
            }

       

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            Player player = (Player) sender;

            if (cmd.getName().equalsIgnoreCase("pet") && sender instanceof Player) {
                   
                   
                }
           

               
                    if (args.length == 0) {
                       
                        //insert wut ever here
                    }
                   
                   
                    if(args[0].equalsIgnoreCase("guardiansummon")) {
                       
                        Guardian guard = player.getLocation().getWorld().spawn(player.getLocation().add(2, 0, 0), Guardian.class);
                        G.put(player.getUniqueId(),guard);
                        G.get(player.getUniqueId()).addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 1000000000 , 1000000000));
                        G.get(player.getUniqueId()).addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 1000000000 , 1000000000));
                        G.get(player.getUniqueId()).addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 1000000000 , 1000000000));
                        G.get(player.getUniqueId()).addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1000000000 , 1000000000));

                        Bukkit.getScheduler().scheduleSyncRepeatingTask(this , new Runnable()
                   
                        {
                             public void run()
                                  {
                               
                                 G.get(player.getUniqueId()).teleport(player.getLocation().add(1, 0, 0));
                                       
                                  }
                        }, 0, 1);
                       
               
                    }

                    if(args[0].equalsIgnoreCase("guardiankill")) {
                        G.get(player.getUniqueId()).remove();
                    }

             return true;
           
             }
           
        }

           
       







     
     
  4. I changed your code:
    Code (Text):
    package me.ChickenTheNugget;



    import java.util.HashMap;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Guardian;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerKickEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;









    public class Pets extends JavaPlugin implements Listener {
    private HashMap < UUID, Guardian > pets = new HashMap < UUID, Guardian > ();


    public onEnable() {
      //STUFF
    }

    public onDisable() {
      //STUFF
    }

    @EventHandler
    public void OnLeave(PlayerQuitEvent event) {
      Player player = (Player) event.getPlayer();
      player.chat("/wh kill");
    }

    @EventHandler
    public void OnKick(PlayerKickEvent event) {
      Player player = (Player) event.getPlayer();
      player.chat("/wh kill");
    }



    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

       if (!cmd.getName().equalsIgnoreCase("pet") {
         return true;
        }

        if (!(sender instanceof Player) {
          return true;
         }
         Player player = (Player) sender;

         if (args.length == 0) {
          player.sendMessage("Correct usage: usage");
          return true;
         }

         if (args[0].equalsIgnoreCase("guardiansummon")) {

          Guardian guardian = player.getLocation().getWorld().spawn(player.getLocation().add(2, 0, 0), Guardian.class);
          guardian.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 1000000000, 1000000000));
          guardian.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 1000000000, 1000000000));
          guardian.addPotionEffect(new PotionEffect(PotionEffectType.HEALTH_BOOST, 1000000000, 1000000000));
          guardian.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 1000000000, 1000000000));
          pets.put(player.getUniqueId(), guardian);

          Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
           public void run() {
            pets.get(player.getUniqueId()).teleport(player.getLocation().add(1, 0, 0));
           }
          }, 0, 2);

         }

         if (args[0].equalsIgnoreCase("guardiankill")) {
          /Kill teh guardian and remove him from the map
         }
         return false;
        }
       }
    NOTE: Learn Java and register your events
     
    • Agree Agree x 1
  5. setCustomName("Name");
     
  6. What you have to do is find your own way of putting the players in the list. (Maby a command? )
    You could add a command where players would type /pet changename
    After that, simply enough listen on playerchatevent. (Code already given, just make the necessary changes as it is pseudo code)
    After the player types the name of the pet in the chat it will change. (Look at the code given previously)

    By the looks of what you have achieved so far, this will be no challenge if you take into account the fact that the code you need has been given to you.
     
    #6 darklazerog, Jun 18, 2018
    Last edited: Jun 18, 2018
  7. Still learn JAVAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA! Plz