Resource (WIP) ArmorStands and making Custom Pets

Discussion in 'Spigot Plugin Development' started by HeroVerseWizard, May 27, 2017.

  1. This is work in progress I will just post the code for now and later explain how it works. So My friend showed me a picture of a Pikachu who follows the Player and I planned to recreate it. I will explain all the maths and the parts of the code and explain it with Gifs later on:
    Code (Text):
    /**
         *
         * @author HeroWise This is a tutorial on how to make You are welcome to use
         *         it or redistribute it under the following conditions:
         *         <ul>
         *         <li>Don't claim these part of code or the whole as your own
         *         <li>Learn from it; Dont just opy and not understand anything
         *         <li>Intermediate experience of Java, Spigot API and understanding of Euler Angles are required
         *         <li>Don't remove this disclaimer
         *         </ul>
         *
         *
         **/
        //Main ArrayList which holds all the Armor stands together
        private ArrayList<ArmorStand> armorStands = new ArrayList<ArmorStand>();

        /**
         * Event Fires the spawn of our Pikachu
         *
         * @param PlayerToggleSneakEvent
         *            event
         *
         **/
        @EventHandler
        public void onPlayerInteract(PlayerToggleSneakEvent event) {

            /**
             * Main Holder variable = 'as'
             */
            ArmorStand as = event.getPlayer().getWorld().spawn(event.getPlayer().getLocation().clone().subtract(0, 0.72, 0),// Spawns One main Armor stand which propagates the others
                    ArmorStand.class);
            as.setVisible(false); // So the Armor Stand is invisible
            as.setCanPickupItems(false); // The Armor stnad wont pickup items; can allow if you want
            as.setArms(false); // Preference is to not to allow arms to be shown
            as.setBasePlate(false); // Preference is to not use it
            as.setGravity(false); // You can activate this if you want Gravity to be a factor
           
           
            /**
             * The head of our Pikachu
             */
            ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) SkullType.PLAYER.ordinal()); // Creates an Itemstack; If you dont understand this please learn more about the Spiot/Nukkit API
            SkullMeta skullmeta = (SkullMeta) skull.getItemMeta(); // Establishes The Metadata
            skullmeta.setOwner("Pikachu"); // Skull Owner: pikachu , You can visit www.namemc.com to see player and their skins
            skull.setItemMeta(skullmeta); // Sets Skull Meta
            as.setHelmet(skull); // Sets the Skull as the helmet of the main Armor Stand
            armorStands.add(as); // adds in the ArrayList

            as = event.getPlayer().getWorld().spawn(event.getPlayer().getLocation().clone().subtract(0, 0.4, 0), // Spawns at a certain location and spawns the "body" for our Pikachu :'3
                    ArmorStand.class);
            /**
             * Everything is self explanoatory and self preferences.
             *
             */
            as.setVisible(false);
            as.setCanPickupItems(false);
            as.setArms(false);
            as.setBasePlate(false);
            as.setGravity(false);
            as.setSmall(true);
            as.setHelmet(new ItemStack(Material.GOLD_BLOCK)); // The trick is to keep it like another armor stand (aub armor stand) work with the main one. Like Polymorhy
            armorStands.add(as); // adds in the ArrayList
           
            /**
             * The Hands of our Pikachu
             */
            as = event.getPlayer().getWorld().spawn(event.getPlayer().getLocation().clone(), ArmorStand.class);
            as.setVisible(false);
            as.setCanPickupItems(false);
            as.setArms(false);
            as.setBasePlate(false);
            as.setGravity(false);
            as.setSmall(true);
            as.setLeftArmPose(new EulerAngle(-0.7, 0, -0.5));
            as.setRightArmPose(new EulerAngle(-0.7, 0, 0.5));
            as.getEquipment().setItemInMainHand(new ItemStack(Material.GOLD_BLOCK)); // To make the blocks appear miniature
            as.getEquipment().setItemInOffHand(new ItemStack(Material.GOLD_BLOCK)); // To make the blocks appear miniature
            armorStands.add(as); // adds in the ArrayList
           
            as = event.getPlayer().getWorld().spawn(event.getPlayer().getLocation().clone().add(0.15, 0.35, -0.3),
                    ArmorStand.class);
            as.setVisible(false);
            as.setCanPickupItems(false);
            as.setArms(false);
            as.setBasePlate(false);
            as.setGravity(false);
            as.getEquipment().setItemInOffHand(new ItemStack(Material.GOLD_NUGGET));
            as.setLeftArmPose(new EulerAngle(1.5, 0, 0));
            armorStands.add(as); // adds in the ArrayList

            as = event.getPlayer().getWorld().spawn(event.getPlayer().getLocation().clone().add(-0.15, 0.35, -0.3),
                    ArmorStand.class);
            as.setVisible(false);
            as.setCanPickupItems(false);
            as.setArms(false);
            as.setBasePlate(false);
            as.setGravity(false);
            as.getEquipment().setItemInMainHand(new ItemStack(Material.GOLD_NUGGET));
            as.setRightArmPose(new EulerAngle(1.5, 0, 0));
            armorStands.add(as); // adds in the ArrayList

            as = event.getPlayer().getWorld().spawn(event.getPlayer().getLocation().clone().subtract(0, 0.4, 0),
                    ArmorStand.class);
            as.setVisible(false);
            as.setCanPickupItems(false);
            as.setArms(false);
            as.setBasePlate(false);
            as.setGravity(false);
            as.setSmall(true);
            as.setLeftArmPose(new EulerAngle(-0.8, 0, -0.5));
            as.setRightArmPose(new EulerAngle(-0.8, 0, 0.5));
            as.getEquipment().setItemInMainHand(new ItemStack(Material.GOLD_BLOCK));
            as.getEquipment().setItemInOffHand(new ItemStack(Material.GOLD_BLOCK));
            armorStands.add(as); // adds in the ArrayList

            Location l = event.getPlayer().getLocation().clone().add(-0.15, -0.8, 0.35);
            l.setYaw(90);
           
           
            as = event.getPlayer().getWorld().spawn(l, ArmorStand.class);
            as.setVisible(false);
            as.setCanPickupItems(false);
            as.setArms(false);
            as.setBasePlate(false);
            as.setGravity(false);
            as.setSmall(true);
            as.setHelmet(new ItemStack(Material.INK_SACK, 1, (short) 11));
            armorStands.add(as); // adds in the ArrayList
            /**
             * To teleport the Pickachu to Player location
             */
            new BukkitRunnable() {

                public void run() {

                       
                    // Loop through all the Armor stands
                    for (ArmorStand stand : armorStands) {
                       
                        /**
                         * This allows the Pickachu to look at the Player
                         */
                        float yaw = (float) Math
                                .toDegrees(Math.atan2(event.getPlayer().getLocation().getZ() - stand.getLocation().getZ(),
                                        event.getPlayer().getLocation().getX() - stand.getLocation().getX()))
                                - 90;
                        float pitch = (float) Math
                                .toDegrees(Math.atan2(event.getPlayer().getLocation().getZ() - stand.getLocation().getZ(),
                                        event.getPlayer().getLocation().getX() - stand.getLocation().getX()))
                                - 90;

                       
                        /**
                         *
                         * Teleport To player and Sets loc and yaw plus pitch
                         */
                        if (event.getPlayer().getLocation().distanceSquared(stand.getLocation()) >= 8) {
                            Location loc = stand.getLocation().add(event.getPlayer().getLocation().toVector()
                                    .subtract(stand.getLocation().toVector()).setY(0).normalize().multiply(0.3));
                            loc.setYaw(yaw);
                            loc.setYaw(pitch);

                            stand.teleport(loc);

                        }
                    }
                }

            }.runTaskTimer(instance, 0, 1);// To run the Runnable
            //instance is a var instantiating the main class
        }

    If you find any flaw PM me or tell me what you want me to improve on ;).

    I will soon do a Particle tutorial to do with Helices. If you do want it; Make sure to comment down ;p
     
  2. Great! Thank you so much!!
     
  3. What do you mean in this part?
    Code (Text):
           }.runTaskTimer(instance, 0, 1);// To run the Runnable
           //instance is a var instantiating the main class
    How do I fix "instance"?
     
    • Funny Funny x 1
  4. You have to have an instance of your main class that extends JavaPlugin for it to work. Schedulers don't just work automatically.
     
    • Winner Winner x 1
  5. very nice. however, i would recommend a more OOP styled approach to creating the 'pet' and its body parts. ;)
     
    • Agree Agree x 4
  6. Make a tutorial on how to make them follow you :D
     
    • Funny Funny x 2
  7. @ChefJava Thanks ;3 and Yeah I was planning to but these Kids need to understand basic Java b4 they can get some tea to digest polymurphysm(cringiest pun). I was planning to make them in methods and even maybe classes if this was big. The only thing I am worried about is understanding how the code actually works. Rest is simple.

    I might make a resource on Euler angles to extend to this tutorial
    @1010bobIzCOOL You know something? I really want to Kermit and you game me a reason to...
    I am JOKING fam; I think you need to understand more java Concepts b4 you dwindle with APis and LIbraries. If you know how this works, you can find the way to make the Pikachu follow your Player

    And to fix instance... as i stated above go back to basics b4 playing with these, they are useful trust me
    So this is an hypothetical situation; My "Main Class" is known as "Apple"
    and the Class this method is in "Banana"
    One way to do this is
    In class banana make a instance var, Apple instance;
    And then a constructor where you would pass the instance variables from onEnable as my current class(Banana) has implemented an interface known as the Listener interface
    If you need help ask me ;p
     

  8. Hi, If I have a PlayerMovementEvents in Class (Class1) but I have "ArmorStand as = (ArmorStand) player...[etc], ArmorStand.class) in another class (Class2), how can i call the variable "as" in another class? If I do Class1 c1 = new Class1(); and c1.as. it doesnt work, it's not a variable
     
    #8 Dantesudante, May 28, 2017
    Last edited: May 28, 2017
  9. @Dantesudante can you rephrase that?
    Or By what you are asking you want to access the "as" variable?
    c1.as will assume you are calling a method in a class...
     
  10. If in the class "Class1" I create this metod:
    Code (Text):
        public ItemStack a = createItem(new ItemStack(Material.SKULL_ITEM, 1, (byte) 3), "MHF_Chest");
       

        public void spawnPet(Player player) {
           
           int ab;
            ArmorStand as  = (ArmorStand) player.getLocation().getWorld().spawn(player.getLocation().add(0,-2,0), ArmorStand.class);      
            as.setCustomName( player.getName()+" 's ChestPet");
            as.setCustomNameVisible(true);
            as.setInvulnerable(true);
            as.setHelmet(a);
            as.setVisible(false);
               
           
    }

    And in the Class Event I want move the Armorstand:

    Code (Text):
     @EventHandler
       
        public void onMove(PlayerMoveEvent event) {
           
            Class1 c1 = new Class1();;  
           
            Player player = (Player)event.getPlayer();
            Location loc = player.getLocation();
            World world = loc.getWorld();
            //c1.spawnPet(player);
        }
    How I have to do?
     
    • Agree Agree x 1
  11. What version of minecraft did you use? I use 1.8.8 but I can not find the setItemInMainHand and setItemInOffHand methods.
     
    • Funny Funny x 1
  12. That's 1.9 and up I believe