Solved Getting entities nearby and teleport without affecting Yaw and pitch help.

Discussion in 'Plugin Development' started by q8minecraft, Nov 16, 2015.

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

    q8minecraft

    The title maybe kinda misleading, but couldn't find any better one as always.. *sigh*

    anyways, I wanna tell you that I've googled and looked for help for AGES, haven't found anything that could help me, otherwise I wouldn't be making this thread..

    First, I have an if condition already set that checks for the item in the player hands and a for loop to check for nearby entities, if there's an entity near 5 blocks of all directions of the player, it reduces the item use by 1, and when the item reaches 0 uses it disappears. But if there's no entities nearby, it just sends a message to the player notifying them that there's no one near them. I've tried doing that, but all I want is to check for all the entities (which I already have that going on) but reduce the item's use by 1 and only one, the one I have right now reduces them by each entity that is in range. And also sends the message that I have on if there's no one nearby..
    Hopefully this makes enough sense.
    Code:
                    for (Entity entity : p.getLocation().getWorld().getNearbyEntities(loc, 5, 5, 5)) {
                        if (entity instanceof Damageable && !(entity.equals(p))) {
                            p.getLocation().getWorld().strikeLightning(entity.getLocation());
                            use--;
                            p.sendMessage(
                                    ChatColor.RED + "You have " + ChatColor.GREEN + use + ChatColor.RED + " Uses left!");
                            if (use <= 0) {
                                p.setItemInHand(null);
                                use = 5;
                            }
                        }
                        else if(entity.getLocation().distance(loc) > 5){
                            p.sendMessage(ChatColor.RED + "No one is near you!");
                        }
                    }
    For the other problem I'm having is to check the yaw and pitch for the player in a runnable, this is what I have so far:
    Code:
                            double lox = p.getLocation().getX();
                            double loy = p.getLocation().getY();
                            double loz = p.getLocation().getZ();
    
                            float yaw = p.getLocation().getYaw();
                            float pitch = p.getLocation().getPitch();
                           
                            Location tploc = new Location(p.getWorld(), lox, loy + 0.1, loz);
                            tploc.setPitch(pitch);
                            tploc.setYaw(yaw);
                            p.teleport(tploc);
    the problem here is that my screen keeps getting so shaky and I can barely move my head before getting the yaw and pitch set back again to its original..

    Thank you in advance for the help! <3
     
  2. Offline

    Zombie_Striker

    Looks like for this one, you want to use a boolean. set it to false at the start, and if there is an entity that gets struck by lightning, set that boolean to true. If that boolean is true, use--;
    Why not just add the yaw and pitch to the location constructor?
     
  3. Offline

    q8minecraft

    I'll try that, thanks!

    I have tried everything, I added it to the constructor, set the location after and before teleporting, literally everything. My screen still shakes so badly.
    @Zombie_Striker
     
  4. Offline

    Monollyth

    @q8minecraft Your uses are getting subtracted for each entity because you put the 'use--;' in the loop. Put that and the message you send to the player outside of the loop. Only put what you want to do for each entity in the loop.
     
  5. Offline

    q8minecraft

    I've figured out the first issue with getting the nearby entities.
    For the second issue which is teleporting without affecting the Pitch and Yaw is annoying me a bit.. Here's whats happening. You can clearly see how shaky the screen is.


    here's the entire code if you need it.
    Code (open)

    Code:
    package me.zed.spells;
    
    import java.math.RoundingMode;
    import java.text.NumberFormat;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Damageable;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.permissions.Permission;
    import org.bukkit.scheduler.BukkitRunnable;
    
    public class FireSerpent implements Listener, CommandExecutor {
    
        Main plugin;
    
        FireSerpent(Main plugin) {
            this.plugin = plugin;
        }
    
        private Permission spells = new Permission("spells.give");
        private int use = 5;
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
            if (cmd.getName().equalsIgnoreCase("flamepulse")) {
                if (sender instanceof Player) {
                    if (sender.hasPermission(spells)) {
                        Player p = (Player) sender;
                        ItemStack pulse = new ItemStack(Material.PAPER);
                        ItemMeta pulsemeta = pulse.getItemMeta();
                        pulsemeta.setDisplayName(ChatColor.DARK_PURPLE + "Flame Pulse Spell");
                        pulsemeta.setLore(plugin.pl);
                        pulse.setItemMeta(pulsemeta);
    
                        p.getInventory().addItem(pulse);
                    } else {
                        sender.sendMessage(ChatColor.RED + "You don't have the required permission!");
                    }
                }
            }
            return false;
        }
    
        @EventHandler
        public void fireserpent(PlayerInteractEvent event) {
            final Player p = event.getPlayer();
           
           
            if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_AIR) {
                if (p.getInventory().getItemInHand().getType() == Material.PAPER
                        && p.getInventory().getItemInHand().getItemMeta().getLore().equals(plugin.pl)) {
                    use--;
                    p.sendMessage(ChatColor.RED + "You have " + ChatColor.GREEN + use + ChatColor.RED + " Uses left!");
                    if (use <= 0) {
                        p.setItemInHand(null);
                        use = 5;
                    }
    
                    new BukkitRunnable() {
                        double t = 2 * Math.PI / 16;
                        Location loc = p.getLocation();
                        double r = 1;
                        double phi = 0;
    
                       
                       
                        public void run() {
                            phi += Math.PI / 10;
                           
    
                            for (Entity entity : loc.getWorld().getNearbyEntities(loc, 10, 10, 10)) {
                                if (entity instanceof Damageable) {
                                    if (!(entity.equals(p))) {
                                        Location entityloc = entity.getLocation();
                                        for (double theta = 0; theta <= 2 * Math.PI; theta += Math.PI / 40) {
                                            double r = 2.5;
                                            double xr = r * Math.cos(theta) * Math.sin(phi);
                                            double yr = r * Math.cos(phi) + 1.5;
                                            double zr = r * Math.sin(theta) * Math.sin(phi);
                                            entityloc.add(xr, yr, zr);
                                            ParticleEffect.REDSTONE.display(0, 0, 0, 0, 1, entityloc, 100);
                                            entityloc.subtract(xr, yr, zr);
                                        }
                                    }
                                }
                            }
    
                            t = t + Math.PI / 16;
                            double x = r * Math.cos(t);
                            double y = 0.5 * t;
                            double z = r * Math.sin(t);
                            loc.add(x, y, z);
                            ParticleEffect.FLAME.display(0, 0, 0, 0, 1, loc, 100);
                            p.playSound(loc, Sound.NOTE_STICKS, (float) 2F, 2F);
                            loc.subtract(x, y, z);
    
                            double x1 = -r * Math.cos(t);
                            double y1 = 0.5 * t;
                            double z1 = -r * Math.sin(t);
                            loc.add(x1, y1, z1);
                            ParticleEffect.FLAME.display(0, 0, 0, 0, 1, loc, 100);
                            loc.subtract(x1, y1, z1);
    
                            ParticleEffect.REDSTONE.display(0, 0, 0, 0, 1, loc, 100);
                           
                           
                            double lox = p.getLocation().getX();
                            double loy = p.getLocation().getY() + 0.1;
                            double loz = p.getLocation().getZ();
    
                            float yaw = p.getLocation().getYaw();
                            float pitch = p.getLocation().getPitch();
                           
                           
                            Location tploc = new Location(p.getLocation().getWorld(), lox, loy, loz, yaw, pitch);
                            p.getLocation().setYaw(yaw);
                            p.getLocation().setPitch(pitch);
                            p.teleport(tploc);
    
                            if (t > 20) {
                                for (Entity entitynear : loc.getWorld().getNearbyEntities(loc, 10, 10, 10)) {
                                    if (entitynear instanceof Damageable) {
                                        if (!(entitynear.equals(p))) {
                                            ((Damageable) entitynear).damage(15);
                                            NumberFormat format = NumberFormat.getInstance();
                                            format.setRoundingMode(RoundingMode.DOWN);
                                            format.setMaximumFractionDigits(1);
    
                                            String formattedPlayerHealth = format
                                                    .format(((Damageable) entitynear).getHealth());
                                            p.sendMessage(formattedPlayerHealth);
                                        }
                                    }
                                }
                                this.cancel();
                            }
                        }
                    }.runTaskTimer(plugin, 0, 0);
                }
            }
        }
    }
    
     
  6. Offline

    Zombie_Striker

    @q8minecraft
    Since you're just making the player "float", you could just set the player's velocity to 0.1 and have the player "fly" without being teleported.

    Just replace player.teleport with player.setvelocity(player.getVelocity().add(0.0,0.1,0.0));
     
    q8minecraft likes this.
  7. Offline

    q8minecraft

    @Zombie_Striker oh, haven't thought about that actually, thought teleporting was efficient enough xD, thanks!
     
Thread Status:
Not open for further replies.

Share This Page