Search in sources :

Example 16 with CommandTarget

use of net.glowstone.command.CommandTarget in project Glowstone by GlowstoneMC.

the class PlaySoundCommand method execute.

@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
    if (!testPermission(sender, commandMessages.getPermissionMessage())) {
        return true;
    }
    if (args.length < 3 || args.length == 4 || args.length == 5) {
        sendUsageMessage(sender, commandMessages);
        return false;
    }
    final World world = CommandUtils.getWorld(sender);
    String stringSound = args[0];
    String stringCategory = args[1];
    String playerPattern = args[2];
    final Sound sound = GlowSound.getVanillaSound(CommandUtils.toNamespaced(stringSound));
    final SoundCategory soundCategory = SoundUtil.buildSoundCategory(stringCategory);
    List<GlowPlayer> targets;
    boolean relativeLocation = false;
    double volume = 1;
    double minimumVolume = 0;
    double pitch = 1;
    if (sound == null) {
        new LocalizedStringImpl("playsound.invalid", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, stringSound);
        return false;
    }
    if (soundCategory == null) {
        new LocalizedStringImpl("playsound.invalid.category", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, stringCategory);
        return false;
    }
    // Manage player(s)
    if (playerPattern.startsWith("@") && playerPattern.length() > 1 && CommandUtils.isPhysical(sender)) {
        // Manage selectors
        final Location senderLocation = CommandUtils.getLocation(sender);
        final Entity[] entities = new CommandTarget(sender, args[0]).getMatched(senderLocation);
        targets = Arrays.stream(entities).filter(GlowPlayer.class::isInstance).map(GlowPlayer.class::cast).collect(Collectors.toList());
    } else {
        final GlowPlayer player = (GlowPlayer) Bukkit.getPlayerExact(playerPattern);
        if (player == null) {
            commandMessages.getGeneric(GenericMessage.NO_SUCH_PLAYER).sendInColor(ChatColor.RED, sender, playerPattern);
            return false;
        } else {
            targets = Collections.singletonList(player);
        }
    }
    if (args.length >= 9) {
        try {
            minimumVolume = Double.valueOf(args[8]);
            if (minimumVolume < 0 || minimumVolume > 1) {
                new LocalizedStringImpl("playsound.invalid.volume", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, args[8]);
                return false;
            }
        } catch (final NumberFormatException n) {
            commandMessages.getGeneric(GenericMessage.NAN).sendInColor(ChatColor.RED, sender, args[8]);
            return false;
        }
    }
    if (args.length >= 8) {
        try {
            pitch = Double.valueOf(args[7]);
            if (pitch < 0 || pitch > 2) {
                new LocalizedStringImpl("playsound.invalid.pitch", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, args[7]);
                return false;
            } else if (pitch < 0.5) {
                pitch = 0.5;
            }
        } catch (final NumberFormatException n) {
            commandMessages.getGeneric(GenericMessage.NAN).sendInColor(ChatColor.RED, sender, args[7]);
            return false;
        }
    }
    if (args.length >= 7) {
        try {
            volume = Double.valueOf(args[6]);
        } catch (final NumberFormatException n) {
            commandMessages.getGeneric(GenericMessage.NAN).sendInColor(ChatColor.RED, sender, args[6]);
            return false;
        }
    }
    if (args.length >= 6) {
        relativeLocation = args[3].startsWith("~") || args[4].startsWith("~") || args[5].startsWith("~");
    }
    for (final GlowPlayer target : targets) {
        Location soundLocation;
        Location targetLocation = target.getLocation();
        double targetVolume = volume;
        try {
            if (relativeLocation) {
                soundLocation = CommandUtils.getLocation(targetLocation, args[3], args[4], args[5]);
            } else if (args.length >= 6) {
                soundLocation = CommandUtils.getLocation(new Location(world, 0, 0, 0), args[3], args[4], args[5]);
            } else {
                soundLocation = targetLocation;
            }
        } catch (final NumberFormatException n) {
            new LocalizedStringImpl("playsound.invalid.position", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, args[3], args[4], args[5]);
            return false;
        }
        // If the target is outside the normal audible sphere
        if (targetLocation.distanceSquared(soundLocation) > Math.pow(volume, 2)) {
            if (minimumVolume <= 0) {
                new LocalizedStringImpl("playsound.too-far", commandMessages.getResourceBundle()).sendInColor(ChatColor.RED, sender, target.getName());
                return false;
            } else {
                final double deltaX = soundLocation.getX() - targetLocation.getX();
                final double deltaY = soundLocation.getX() - targetLocation.getY();
                final double deltaZ = soundLocation.getX() - targetLocation.getZ();
                final double delta = Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2) + Math.pow(deltaZ, 2));
                soundLocation = targetLocation;
                soundLocation.add(deltaX / delta, deltaY / delta, deltaZ / delta);
                targetVolume = minimumVolume;
            }
        }
        target.playSound(soundLocation, sound, soundCategory, (float) targetVolume, (float) pitch);
    }
    return true;
}
Also used : Entity(org.bukkit.entity.Entity) SoundCategory(org.bukkit.SoundCategory) CommandTarget(net.glowstone.command.CommandTarget) GlowPlayer(net.glowstone.entity.GlowPlayer) Sound(org.bukkit.Sound) GlowSound(net.glowstone.constants.GlowSound) World(org.bukkit.World) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl) Location(org.bukkit.Location)

Aggregations

CommandTarget (net.glowstone.command.CommandTarget)16 Entity (org.bukkit.entity.Entity)14 Location (org.bukkit.Location)12 LocalizedStringImpl (net.glowstone.i18n.LocalizedStringImpl)11 Player (org.bukkit.entity.Player)10 GlowPlayer (net.glowstone.entity.GlowPlayer)4 ArrayList (java.util.ArrayList)3 ResourceBundle (java.util.ResourceBundle)3 Material (org.bukkit.Material)3 BlockCommandSender (org.bukkit.command.BlockCommandSender)3 World (org.bukkit.World)2 ItemStack (org.bukkit.inventory.ItemStack)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Stream (java.util.stream.Stream)1 CommandUtils (net.glowstone.command.CommandUtils)1 GlowEnchantment (net.glowstone.constants.GlowEnchantment)1 GlowPotionEffect (net.glowstone.constants.GlowPotionEffect)1 GlowSound (net.glowstone.constants.GlowSound)1