Solved Rotating armorstand doesn't affect it before doing a relog

Discussion in 'Spigot Plugin Development' started by MCrafterzz, Aug 16, 2018.

  1. Hello. I'm rotating a armorstand with this code:
    Code (Text):
    Location location = displayArmorStand.getLocation();
                    location.setYaw(Misc.roundedYaw(event.getPlayer().getEyeLocation().getYaw()));
                    displayArmorStand.teleport(location);
    And it is working but only after I quit the server and enter it again. I feel like that's totally unlogical and don't understand why.
     
  2. I have seen equivalent code work correctly, without a relog. Are you using the latest build of Spigot 1.13? If not, what version are you using exactly?
     
  3. I'm using the latest 1.13 spigot build that was downloaded from build tools.
     
  4. I just tested the latest version of ArmorStandEditor on the latest Spigot version, and it rotates just fine right in front of me, using code equivalent to the above, to the best of my knowledge. Perhaps the context in which your code gets executed plays a role? ASE uses PlayerInteractEntityEvent or a subclass thereof PlayerInteractAtEntityEvent; maybe or maybe not on the next tick; not sure. So if you're doing that, I'm out of ideas.
     
    #4 StarTux, Aug 16, 2018
    Last edited: Aug 16, 2018
  5. Here is they full code:
    Code (Text):
    @EventHandler(priority = EventPriority.HIGHEST)
        public void onBlockPlaceEvent(BlockPlaceEvent event) {
            if (event.isCancelled() == false) {
                ItemStack itemStack = event.getItemInHand();

                net.minecraft.server.v1_13_R1.ItemStack itemNms = CraftItemStack.asNMSCopy(itemStack);
                NBTTagCompound tag = itemNms.getTag();

                PlaceMachine(tag, event, "Farmer", Material.FARMLAND);
            }
        }

        private void PlaceMachine(NBTTagCompound tag, BlockPlaceEvent event, String name, Material block) {
            if (tag != null && tag.getList("Tags", 8).toString().contains(name)
                    && tag.getList("Tags", 8).toString().contains("Machine")) {
                ArmorStand armorStand = (ArmorStand) event.getPlayer().getWorld()
                        .spawnEntity(event.getBlockPlaced().getLocation().add(0.5, -0.7, 0.5), EntityType.ARMOR_STAND);
                armorStand.setGravity(false);
                armorStand.setInvulnerable(true);
                armorStand.setVisible(false);
                armorStand.setHelmet(new ItemStack(block));
                armorStand.addScoreboardTag("Machine");
                armorStand.addScoreboardTag("Enabled");
                armorStand.addScoreboardTag(name);

                // Change nbt
                CraftEntity craftEntity = (CraftEntity) armorStand;
                net.minecraft.server.v1_13_R1.Entity nmsEntity = craftEntity.getHandle();

                NBTTagCompound compound = new NBTTagCompound();
                nmsEntity.c(compound);
                compound.setByte("DisabledSlots", (byte) 16);
                nmsEntity.f(compound);

                if (name == "Farmer") {
                    ArmorStand displayArmorStand = (ArmorStand) event.getPlayer().getWorld()
                            .spawnEntity(event.getBlockPlaced().getLocation().add(0.5, -1.5, 0.5).add(
                                    Misc.blockFaceToOffset(Misc.yawToBlockFace(event.getPlayer().getEyeLocation().getYaw()))
                                            .multiply(0.5)),
                                    EntityType.ARMOR_STAND);
                    displayArmorStand.setHelmet(new ItemStack(Material.DIAMOND_HOE));
                    displayArmorStand.setGravity(false);
                    displayArmorStand.setInvulnerable(true);
                    displayArmorStand.setVisible(false);
                    displayArmorStand.addScoreboardTag("Machine");
                   
                    System.out.println(Misc.roundedYaw(event.getPlayer().getEyeLocation().getYaw()));
                    Location location = displayArmorStand.getLocation();
                    location.setYaw(Misc.roundedYaw(event.getPlayer().getEyeLocation().getYaw()) - 90);
                    location.setPitch((float)Math.toRadians(20));
                    //location.setDirection(location.toVector().subtract(Misc.blockFaceToOffset(Misc.yawToBlockFace(event.getPlayer().getEyeLocation().getYaw()))));
                    displayArmorStand.teleport(location);
                   
                    // Change nbt
                    craftEntity = (CraftEntity) displayArmorStand;
                    nmsEntity = craftEntity.getHandle();

                    compound = new NBTTagCompound();
                    nmsEntity.c(compound);
                    compound.setByte("DisabledSlots", (byte) 16);
                    nmsEntity.f(compound);
                }

                Plugin.scoreboard.getObjective("Energy").getScore(armorStand.getUniqueId().toString()).setScore(0);
            }
     
  6. Oh, so you're teleporting it on the same tick it was spawned; perhaps that is the problem. Either construct the location with the correct yaw before you call spawnEntity(), and use that as its parameter. Or, teleport it on the next tick. I prefer the first solution. ;-)
     
    • Agree Agree x 1
  7. Thank you. That did the trick.
     
    • Like Like x 1