Solved Armor Stand coding

Discussion in 'Plugin Development' started by PhilDEV_Acc, Feb 22, 2020.

Thread Status:
Not open for further replies.
  1. Hello,

    I want to create a setup command and place spawns and such things with armour stands cause its easier. How do you code it? So what is the basis?

    Thanks to everybody.:p
     
  2. Offline

    KarimAKL

    @PhilDEV_Acc You start off with creating a new class:
    Code:Java
    1. public class SetupCommand {}


    You then make that class implement CommandExecutor:
    Code:Java
    1. public class SetupCommand implements CommandExecutor {}


    Now you'll need to implement the onCommand method:
    Code:Java
    1. public class SetupCommand implements CommandExecutor {
    2.  
    3. @Override
    4. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    5. return false;
    6. }
    7. }


    Now you can begin on the code you want to be executed when the command is executed.

    You wanted to spawn armor stands, right? In that case, something like this will do:
    Code:Java
    1. if (sender instanceof Player) {
    2. Player player = (Player) sender;
    3.  
    4. Location location = new Location(player.getWorld(), /*x*/0.0, /*y*/0.0, /*z*/0.0, /*yaw*/0.0, /*pitch*/0.0);
    5.  
    6. ArmorStand stand = player.getWorld().spawn(location, ArmorStand.class);
    7.  
    8. // You can do something with the 'stand' variable here if you want to
    9.  
    10. player.sendMessage("You spawned an ArmorStand.");
    11. }

    Write that in the onCommand method, and then return true instead of false. (if you return false, the usage will be send to the sender)

    Now, this will work but, it'll only spawn an ArmorStand at the location, x0, y0, z0, yaw0, pitch0, so let's listen to the sender to get the location:
    Code:Java
    1. if (sender instanceof Player) {
    2. Player player = (Player) sender;
    3.  
    4. if (args.length < 5) {
    5. sender.sendMessage("You need to select the location.");
    6. return true;
    7. }
    8.  
    9. double x = 0, y = 0, z = 0;
    10. float yaw = 0, pitch = 0;
    11.  
    12. try {
    13. x = Double.parseDouble(args[0]);
    14. y = Double.parseDouble(args[1]);
    15. z = Double.parseDouble(args[2]);
    16. yaw = Float.parseFloat(args[3]);
    17. pitch = Float.parseFloat(args[4]);
    18. } catch (NumberFormatException e) {
    19. player.sendMessage("All arguments need to be numbers.");
    20. return true;
    21. }
    22.  
    23. Location location = new Location(player.getWorld(), x, y, z, yaw, pitch);
    24.  
    25. ArmorStand stand = player.getWorld().spawn(location, ArmorStand.class);
    26.  
    27. // You can do something with the 'stand' variable here if you want to
    28.  
    29. player.sendMessage("You spawned an ArmorStand.");
    30. }


    Now that we're done with this, there's two last things we need to do in order for the command to work.

    The first is registering it in the plugin.yml file:
    Code:YAML
    1. commands:
    2. setup:
    3. usage: /setup <x> <y> <z> <yaw> <pitch>
    4. description: Spawns an ArmorStand at the given location

    This registers the command to the internal command map.

    The second:
    Code:Java
    1. Bukkit.getPluginCommand("setup").setExecutor(new SetupCommand());

    This gets the command from the internal command map and sets it's executor to a new instance of our command class.

    If you have any further questions, you can look at this tutorial.
    If i misunderstood your question, please let me know and i'll try to answer the real question. :7
     
  3. Ok thanks for your effort @KarimAKL I really appreciate it. But I think you misunderstood. Il aks again in more detail.
    So I have a setupcommand and I gave the player an inventory with armorstands to place. So I give this 1 armor stand a name like setspawn and when he places this armorstand on a block the block underneeth gets the spawnpoint and the armorstand disappears. I hope you understand. Sorry my english is not the best

    Thanks Philipp
     
  4. Offline

    KarimAKL

    @PhilDEV_Acc Ah, sorry for the misunderstanding.

    It sounds to me like you have an inventory of different armorstand "commands" (or whatever you'd like to call them).

    Well, it'd probably start by creating a listener class:
    Code:Java
    1. public class ArmorStandListener implements Listener {}


    Then i'd listen to the PlayerInteractEvent:
    Code:Java
    1. public class ArmorStandListener implements Listener {
    2.  
    3. @EventHandler
    4. private void onInteract(PlayerInteractEvent event) {}
    5. }


    Now, let's make some checks:
    Code:Java
    1. // We only want to listen to the main hand.
    2. // If we don't make this check, our code will be fired twice.
    3. if (event.getHand() != EquipmentSlot.HAND) return;
    4.  
    5. ItemStack item = event.getItem(); // Gets the item that was clicked.
    6. if (item == null) return; // If no item was in the player's main hand, return.
    7.  
    8. ItemMeta meta = item.getItemMeta(); // Gets the item's ItemMeta, this is just for convenience.
    9. if (!meta.hasDisplayName()) return; // If the item doesn't have a custom name, return.
    10.  
    11. String name = meta.getDisplayName(); // Gets the item's custom name.
    12.  
    13. Block block = event.getClickedBlock(); // Gets the clicked block, if any was clicked.
    14.  
    15. if (name.equals("setspawn")) { // If the item's custom name is "setspawn"
    16. if (block == null) return; // If no block was clicked, return.
    17.  
    18. Block below = block.getRelative(BlockFace.DOWN); // Gets the block below the clicked block
    19.  
    20. // I'm not sure how you set the spawnpoint but, you'd do it here.
    21.  
    22. event.setCancelled(true); // Cancel the event, so that the armorstand isn't placed
    23.  
    24. // I'm not sure if you want to remove the item after it's used but, you can just remove this code if you don't want it
    25. // This code checks if the player has more than 1 of the item in their hand, if they do, remove 1, otherwise remove all.
    26. if (item.getAmount() > 1) item.setAmount(item.getAmount() - 1);
    27. else event.getPlayer().getInventory().setItemInMainHand(null);
    28.  
    29. return; // return after you're done, so the code doesn't continue any further
    30. }
    31.  
    32. if (name.equals("...")) { // This is just an example of how you'd continue doing this
    33. return;
    34. }

    This code is inside the onInteract method.

    Of course i'm just assuming the only special thing about these armorstands are their names; you should probably do something else, like adding (& checking) a custom lore, or NBT tag.

    Reminder: Remember to register the Listener in your onEnable.

    I hope this
     
Thread Status:
Not open for further replies.

Share This Page