Search in sources :

Example 36 with LocalizedStringImpl

use of net.glowstone.i18n.LocalizedStringImpl in project Glowstone by GlowstoneMC.

the class KickCommand method execute.

@Override
public boolean execute(CommandSender sender, String label, String[] args, CommandMessages commandMessages) {
    if (!testPermission(sender, commandMessages.getPermissionMessage())) {
        return true;
    }
    if (args.length == 0) {
        sendUsageMessage(sender, commandMessages);
        return false;
    }
    String playerName = args[0];
    Player player = Bukkit.getPlayerExact(playerName);
    if (player == null) {
        commandMessages.getGeneric(GenericMessage.OFFLINE).sendInColor(ChatColor.RED, sender, playerName);
        return false;
    }
    if (args.length == 1) {
        player.kickPlayer(null);
        new LocalizedStringImpl("kick.done.no-reason", commandMessages.getResourceBundle()).send(sender, player.getName());
        return true;
    }
    String reason = StringUtils.join(args, ' ', 1, args.length);
    player.kickPlayer(reason);
    new LocalizedStringImpl("kick.done", commandMessages.getResourceBundle()).send(sender, player.getName(), reason);
    return true;
}
Also used : Player(org.bukkit.entity.Player) LocalizedStringImpl(net.glowstone.i18n.LocalizedStringImpl)

Example 37 with LocalizedStringImpl

use of net.glowstone.i18n.LocalizedStringImpl 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

LocalizedStringImpl (net.glowstone.i18n.LocalizedStringImpl)37 Location (org.bukkit.Location)15 ResourceBundle (java.util.ResourceBundle)11 CommandTarget (net.glowstone.command.CommandTarget)11 Entity (org.bukkit.entity.Entity)10 Player (org.bukkit.entity.Player)10 ArrayList (java.util.ArrayList)7 Material (org.bukkit.Material)6 GlowWorld (net.glowstone.GlowWorld)5 GlowPlayer (net.glowstone.entity.GlowPlayer)5 CompoundTag (net.glowstone.util.nbt.CompoundTag)5 World (org.bukkit.World)5 GlowServer (net.glowstone.GlowServer)4 GlowBlock (net.glowstone.block.GlowBlock)4 MojangsonParseException (net.glowstone.util.mojangson.ex.MojangsonParseException)4 BlockCommandSender (org.bukkit.command.BlockCommandSender)3 ItemStack (org.bukkit.inventory.ItemStack)3 BlockStateData (net.glowstone.block.state.BlockStateData)2 InvalidBlockStateException (net.glowstone.block.state.InvalidBlockStateException)2 GlowEntity (net.glowstone.entity.GlowEntity)2