Search in sources :

Example 6 with PlayerChangeChannelEvent

use of br.net.fabiozumbi12.UltimateChat.Bukkit.API.PlayerChangeChannelEvent in project UltimateChat by FabioZumbi12.

the class UCCommands method registerChAliases.

private void registerChAliases() {
    // register ch cmds aliases
    for (String cmd : UChat.get().getConfig().getChCmd()) {
        unregisterCmd(cmd);
        manager.register(UChat.get().instance(), CommandSpec.builder().arguments(new ChannelCommandElement(Text.of("channel"))).description(Text.of("Join in a channel if you have permission.")).executor((src, args) -> {
            {
                if (src instanceof Player) {
                    Player p = (Player) src;
                    if (!args.<UCChannel>getOne("channel").isPresent()) {
                        throw new CommandException(getHelpChannel(src).build());
                    }
                    UCChannel ch = args.<UCChannel>getOne("channel").get();
                    if (!UChat.get().getPerms().channelReadPerm(p, ch) && !UChat.get().getPerms().channelWritePerm(p, ch)) {
                        throw new CommandException(UCUtil.toText(UChat.get().getLang().get("channel.nopermission").replace("{channel}", ch.getName())));
                    }
                    if (!ch.availableInWorld(p.getWorld())) {
                        UChat.get().getLang().sendMessage(p, UChat.get().getLang().get("channel.notavailable").replace("{channel}", ch.getName()));
                        return CommandResult.success();
                    }
                    if (ch.isMember(p)) {
                        UChat.get().getLang().sendMessage(p, UChat.get().getLang().get("channel.alreadyon").replace("{channel}", ch.getName()));
                        return CommandResult.success();
                    }
                    if (!ch.getPassword().isEmpty()) {
                        UChat.get().getLang().sendMessage(p, UChat.get().getLang().get("channel.password").replace("{channel}", ch.getAlias()));
                        return CommandResult.success();
                    }
                    // fire event
                    PlayerChangeChannelEvent event = new PlayerChangeChannelEvent(p, UChat.get().getPlayerChannel(src), ch);
                    Sponge.getEventManager().post(event);
                    if (event.isCancelled()) {
                        return CommandResult.success();
                    }
                    ch.addMember(p);
                    UChat.get().getLang().sendMessage(p, UChat.get().getLang().get("channel.entered").replace("{channel}", ch.getName()));
                }
                return CommandResult.success();
            }
        }).build(), cmd);
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) PlayerChangeChannelEvent(br.net.fabiozumbi12.UltimateChat.Sponge.API.PlayerChangeChannelEvent)

Example 7 with PlayerChangeChannelEvent

use of br.net.fabiozumbi12.UltimateChat.Bukkit.API.PlayerChangeChannelEvent in project UltimateChat by FabioZumbi12.

the class UCCommands method uchat.

private CommandCallable uchat() {
    CommandSpec msgtoggle = CommandSpec.builder().arguments(GenericArguments.optional(GenericArguments.requiringPermission(GenericArguments.player(Text.of("player")), "uchat.msgtoggle.others"))).description(Text.of("Disable private messages.")).permission("uchat.cmd.msgtoggle").executor((src, args) -> {
        {
            if (args.<Player>getOne("player").isPresent()) {
                Player p = args.<Player>getOne("player").get();
                if (UChat.get().msgTogglePlayers.contains(p.getName())) {
                    UChat.get().msgTogglePlayers.remove(p.getName());
                    UChat.get().getLang().sendMessage(p, "cmd.msgtoggle.enabled");
                } else {
                    UChat.get().msgTogglePlayers.add(p.getName());
                    UChat.get().getLang().sendMessage(p, "cmd.msgtoggle.disabled");
                }
                return CommandResult.success();
            }
            if (src instanceof Player) {
                Player p = (Player) src;
                if (UChat.get().msgTogglePlayers.contains(p.getName())) {
                    UChat.get().msgTogglePlayers.remove(p.getName());
                    UChat.get().getLang().sendMessage(p, "cmd.msgtoggle.enabled");
                } else {
                    UChat.get().msgTogglePlayers.add(p.getName());
                    UChat.get().getLang().sendMessage(p, "cmd.msgtoggle.disabled");
                }
            } else {
                throw new CommandException(Text.of("Only players can use this command or add a player name as last argument."), true);
            }
            return CommandResult.success();
        }
    }).build();
    CommandSpec delchannel = CommandSpec.builder().arguments(new ChannelCommandElement(Text.of("channel"))).description(Text.of("Deletes a channel.")).permission("uchat.cmd.delchannel").executor((src, args) -> {
        {
            // uchat delchannel <channel>
            Optional<UCChannel> optch = args.getOne("channel");
            if (!optch.isPresent()) {
                throw new CommandException(UChat.get().getLang().getText("channel.dontexist"), true);
            }
            UCChannel ch = optch.get();
            List<String> toAdd = new ArrayList<>(ch.getMembers());
            toAdd.forEach(m -> {
                UChat.get().getDefChannel(src instanceof Player ? ((Player) src).getWorld().getName() : null).addMember(m);
                // fire event
                if (Sponge.getServer().getPlayer(m).isPresent()) {
                    PlayerChangeChannelEvent event = new PlayerChangeChannelEvent(Sponge.getServer().getPlayer(m).get(), null, UChat.get().getDefChannel(src instanceof Player ? ((Player) src).getWorld().getName() : null));
                    Sponge.getEventManager().post(event);
                }
            });
            UChat.get().getConfig().delChannel(ch);
            UChat.get().getLang().sendMessage(src, UChat.get().getLang().get("cmd.delchannel.success").replace("{channel}", ch.getName()));
            return CommandResult.success();
        }
    }).build();
    CommandSpec newchannel = CommandSpec.builder().arguments(GenericArguments.string(Text.of("channel")), GenericArguments.string(Text.of("alias")), GenericArguments.string(Text.of("color"))).description(Text.of("Creates a new channel channel.")).permission("uchat.cmd.newchannel").executor((src, args) -> {
        {
            // uchat newchannel <channel> <alias> <color>
            String ch = args.<String>getOne("channel").get();
            String alias = args.<String>getOne("alias").get();
            String color = args.<String>getOne("color").get();
            if (color.length() != 2 || !color.matches("(&([a-fk-or0-9]))$")) {
                throw new CommandException(UChat.get().getLang().getText("channel.invalidcolor"), true);
            }
            UCChannel newch = new UCChannel(ch, alias, color);
            try {
                UChat.get().getConfig().addChannel(newch);
                registerChannelAliases();
            } catch (Exception e) {
                e.printStackTrace();
            }
            UChat.get().getLang().sendMessage(src, UChat.get().getLang().get("cmd.newchannel.success").replace("{channel}", newch.getName()));
            return CommandResult.success();
        }
    }).build();
    CommandSpec chconfig = CommandSpec.builder().arguments(new ChannelCommandElement(Text.of("channel")), GenericArguments.choices(Text.of("key"), getChKeys()), GenericArguments.string(Text.of("value"))).description(Text.of("Edit the config of a channel.")).permission("uchat.cmd.chconfig").executor((src, args) -> {
        {
            // uchat chconfig <channel> <key> <value>
            Optional<UCChannel> optch = args.getOne("channel");
            if (!optch.isPresent()) {
                throw new CommandException(UChat.get().getLang().getText("channel.dontexist"), true);
            }
            UCChannel ch = optch.get();
            String key = args.<String>getOne("key").get();
            String value = args.<String>getOne("value").get();
            if (!ch.getProperties().containsKey(key)) {
                throw new CommandException(UChat.get().getLang().getText("cmd.chconfig.invalidkey"), true);
            }
            UChat.get().getConfig().delChannel(ch);
            ch.setProperty(key, value);
            try {
                UChat.get().getConfig().addChannel(ch);
            } catch (Exception e) {
                e.printStackTrace();
            }
            UChat.get().getLang().sendMessage(src, "cmd.chconfig.success");
            return CommandResult.success();
        }
    }).build();
    CommandSpec reload = CommandSpec.builder().description(Text.of("Command to reload uchat.")).permission("uchat.cmd.reload").executor((src, args) -> {
        {
            // uchat reload
            try {
                UChat.get().reload();
                UChat.get().getLang().sendMessage(src, "plugin.reloaded");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return CommandResult.success();
        }
    }).build();
    CommandSpec clear = CommandSpec.builder().description(Text.of("Clear your chat.")).permission("uchat.cmd.clear").executor((src, args) -> {
        {
            if (src instanceof Player) {
                Player p = (Player) src;
                // uchat clear
                for (int i = 0; i < 100; i++) {
                    p.sendMessage(Text.of(" "));
                }
                UChat.get().getLang().sendMessage(src, "cmd.clear.cleared");
            }
            return CommandResult.success();
        }
    }).build();
    CommandSpec clearAll = CommandSpec.builder().description(Text.of("Clear the chat of all online players.")).permission("uchat.cmd.clear-all").executor((src, args) -> {
        {
            // uchat clear-all
            for (Player play : Sponge.getServer().getOnlinePlayers()) {
                for (int i = 0; i < 100; i++) {
                    if (!play.isOnline()) {
                        continue;
                    }
                    play.sendMessage(Text.of(" "));
                }
            }
            return CommandResult.success();
        }
    }).build();
    CommandSpec spy = CommandSpec.builder().description(Text.of("Turn on the social spy.")).permission("uchat.cmd.spy").executor((src, args) -> {
        {
            if (src instanceof Player) {
                Player p = (Player) src;
                // uchat spy
                if (!UChat.get().isSpy.contains(p.getName())) {
                    UChat.get().isSpy.add(p.getName());
                    UChat.get().getLang().sendMessage(src, "cmd.spy.enabled");
                } else {
                    UChat.get().isSpy.remove(p.getName());
                    UChat.get().getLang().sendMessage(src, "cmd.spy.disabled");
                }
            }
            return CommandResult.success();
        }
    }).build();
    CommandSpec ignorePlayer = CommandSpec.builder().arguments(GenericArguments.player(Text.of("player"))).description(Text.of("Ignore a player.")).permission("uchat.cmd.ignore.player").executor((src, args) -> {
        {
            if (src instanceof Player) {
                Player p = (Player) src;
                // uchat ignore player
                Player pi = args.<Player>getOne("player").get();
                if (pi.equals(p)) {
                    throw new CommandException(UChat.get().getLang().getText("cmd.ignore.self"), true);
                }
                if (!UChat.get().getPerms().canIgnore(p, pi)) {
                    UChat.get().getLang().sendMessage(p, UChat.get().getLang().get("chat.cantignore"));
                    return CommandResult.success();
                }
                if (UCMessages.isIgnoringPlayers(p.getName(), pi.getName())) {
                    UCMessages.unIgnorePlayer(p.getName(), pi.getName());
                    UChat.get().getLang().sendMessage(p, UChat.get().getLang().get("player.unignoring").replace("{player}", pi.getName()));
                } else {
                    UCMessages.ignorePlayer(p.getName(), pi.getName());
                    UChat.get().getLang().sendMessage(p, UChat.get().getLang().get("player.ignoring").replace("{player}", pi.getName()));
                }
            }
            return CommandResult.success();
        }
    }).build();
    CommandSpec ignoreChannel = CommandSpec.builder().arguments(new ChannelCommandElement(Text.of("channel"))).description(Text.of("Ignore a channel.")).permission("uchat.cmd.ignore.channel").executor((src, args) -> {
        {
            if (src instanceof Player) {
                Player p = (Player) src;
                // uchat ignore channel
                Optional<UCChannel> optch = args.getOne("channel");
                if (!optch.isPresent()) {
                    throw new CommandException(UChat.get().getLang().getText("channel.dontexist"), true);
                }
                UCChannel ch = optch.get();
                if (!UChat.get().getPerms().canIgnore(p, ch)) {
                    UChat.get().getLang().sendMessage(p, UChat.get().getLang().get("chat.cantignore"));
                    return CommandResult.success();
                }
                if (ch.isIgnoring(p.getName())) {
                    ch.unIgnoreThis(p.getName());
                    UChat.get().getLang().sendMessage(src, UChat.get().getLang().get("channel.notignoring").replace("{channel}", ch.getName()));
                } else {
                    ch.ignoreThis(p.getName());
                    UChat.get().getLang().sendMessage(src, UChat.get().getLang().get("channel.ignoring").replace("{channel}", ch.getName()));
                }
            }
            return CommandResult.success();
        }
    }).build();
    CommandSpec mute = CommandSpec.builder().arguments(GenericArguments.player(Text.of("player")), GenericArguments.optional(new ChannelCommandElement(Text.of("channel")))).description(Text.of("Mute a player.")).permission("uchat.cmd.mute").executor((src, args) -> {
        {
            // uchat mute player channel
            Player play = args.<Player>getOne("player").get();
            if (args.<UCChannel>getOne("channel").isPresent()) {
                UCChannel ch = args.<UCChannel>getOne("channel").get();
                if (ch.isMuted(play.getName())) {
                    ch.unMuteThis(play.getName());
                    UChat.get().getLang().sendMessage(src, UChat.get().getLang().get("channel.unmuted.this").replace("{player}", play.getName()).replace("{channel}", ch.getName()));
                    if (play.isOnline()) {
                        UChat.get().getLang().sendMessage(play, UChat.get().getLang().get("channel.player.unmuted.this").replace("{channel}", ch.getName()));
                    }
                } else {
                    ch.muteThis(play.getName());
                    UChat.get().getLang().sendMessage(src, UChat.get().getLang().get("channel.muted.this").replace("{player}", play.getName()).replace("{channel}", ch.getName()));
                    if (play.isOnline()) {
                        UChat.get().getLang().sendMessage(play, UChat.get().getLang().get("channel.player.muted.this").replace("{channel}", ch.getName()));
                    }
                }
            } else {
                if (UChat.get().mutes.contains(play.getName())) {
                    UChat.get().mutes.remove(play.getName());
                    UChat.get().unMuteInAllChannels(play.getName());
                    UChat.get().getLang().sendMessage(src, UChat.get().getLang().get("channel.unmuted.all").replace("{player}", play.getName()));
                    if (play.isOnline()) {
                        UChat.get().getLang().sendMessage(play, "channel.player.unmuted.all");
                    }
                } else {
                    UChat.get().mutes.add(play.getName());
                    UChat.get().muteInAllChannels(play.getName());
                    UChat.get().getLang().sendMessage(src, UChat.get().getLang().get("channel.muted.all").replace("{player}", play.getName()));
                    if (play.isOnline()) {
                        UChat.get().getLang().sendMessage(play, "channel.player.muted.all");
                    }
                }
            }
            return CommandResult.success();
        }
    }).build();
    CommandSpec tempmute = CommandSpec.builder().arguments(GenericArguments.integer(Text.of("time")), GenericArguments.player(Text.of("player"))).description(Text.of("Temporary mute a player.")).permission("uchat.cmd.tempmute").executor((src, args) -> {
        {
            // uchat tempmute time player
            Player play = args.<Player>getOne("player").get();
            int time = args.<Integer>getOne("time").get();
            if (UChat.get().mutes.contains(play.getName())) {
                UChat.get().getLang().sendMessage(src, "channel.already.muted");
            } else {
                UChat.get().mutes.add(play.getName());
                UChat.get().muteInAllChannels(play.getName());
                UChat.get().getLang().sendMessage(src, UChat.get().getLang().get("channel.tempmuted.all").replace("{player}", play.getName()).replace("{time}", String.valueOf(time)));
                if (play.isOnline()) {
                    UChat.get().getLang().sendMessage(play, UChat.get().getLang().get("channel.player.tempmuted.all").replace("{time}", String.valueOf(time)));
                }
                // mute counter
                Task.builder().execute(new MuteCountDown(play.getName(), time)).interval(1, TimeUnit.SECONDS).name("Mute Counter").submit(UChat.get().instance());
            }
            return CommandResult.success();
        }
    }).build();
    CommandSpec help = CommandSpec.builder().description(Text.of("Se help about commands.")).executor((src, args) -> {
        {
            sendHelp(src);
            return CommandResult.success();
        }
    }).build();
    // uchat <args...>
    return CommandSpec.builder().description(Text.of("Main command for uchat.")).executor((src, args) -> {
        {
            // no args
            src.sendMessage(UCUtil.toText("&b---------------- " + UChat.get().instance().getName() + " " + UChat.get().instance().getVersion().get() + " ---------------"));
            src.sendMessage(UCUtil.toText("&bDeveloped by &6" + UChat.get().instance().getAuthors().get(0) + "."));
            src.sendMessage(UCUtil.toText("&bFor more information about the commands, type [" + "&6/uchat ?&b]."));
            src.sendMessage(UCUtil.toText("&b---------------------------------------------------"));
            return CommandResult.success();
        }
    }).child(help, "?").child(chconfig, "chconfig").child(delchannel, "delchannel").child(newchannel, "newchannel").child(reload, "reload").child(clear, "clear").child(clearAll, "clear-all").child(spy, "spy").child(CommandSpec.builder().child(ignorePlayer, "player").child(ignoreChannel, "channel").build(), "ignore").child(mute, "mute").child(tempmute, "tempmute").child(msgtoggle, "msgtoggle").build();
}
Also used : TextActions(org.spongepowered.api.text.action.TextActions) java.util(java.util) ConsoleSource(org.spongepowered.api.command.source.ConsoleSource) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) Builder(org.spongepowered.api.text.Text.Builder) Sponge(org.spongepowered.api.Sponge) Collectors(java.util.stream.Collectors) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) TimeUnit(java.util.concurrent.TimeUnit) MessageChannelEvent(org.spongepowered.api.event.message.MessageChannelEvent) Text(org.spongepowered.api.text.Text) PlayerChangeChannelEvent(br.net.fabiozumbi12.UltimateChat.Sponge.API.PlayerChangeChannelEvent) org.spongepowered.api.command.args(org.spongepowered.api.command.args) Task(org.spongepowered.api.scheduler.Task) Player(org.spongepowered.api.entity.living.player.Player) MessageEvent(org.spongepowered.api.event.message.MessageEvent) UChatReloadEvent(br.net.fabiozumbi12.UltimateChat.Sponge.API.UChatReloadEvent) TextColors(org.spongepowered.api.text.format.TextColors) org.spongepowered.api.command(org.spongepowered.api.command) Player(org.spongepowered.api.entity.living.player.Player) PlayerChangeChannelEvent(br.net.fabiozumbi12.UltimateChat.Sponge.API.PlayerChangeChannelEvent) MalformedURLException(java.net.MalformedURLException) CommandSpec(org.spongepowered.api.command.spec.CommandSpec)

Aggregations

PlayerChangeChannelEvent (br.net.fabiozumbi12.UltimateChat.Sponge.API.PlayerChangeChannelEvent)4 PlayerChangeChannelEvent (br.net.fabiozumbi12.UltimateChat.Bukkit.API.PlayerChangeChannelEvent)3 Player (org.bukkit.entity.Player)2 Player (org.spongepowered.api.entity.living.player.Player)2 MessageChannelEvent (org.spongepowered.api.event.message.MessageChannelEvent)2 MessageEvent (org.spongepowered.api.event.message.MessageEvent)2 Text (org.spongepowered.api.text.Text)2 UChatReloadEvent (br.net.fabiozumbi12.UltimateChat.Sponge.API.UChatReloadEvent)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 java.util (java.util)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Sponge (org.spongepowered.api.Sponge)1 org.spongepowered.api.command (org.spongepowered.api.command)1 org.spongepowered.api.command.args (org.spongepowered.api.command.args)1 ConsoleSource (org.spongepowered.api.command.source.ConsoleSource)1 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)1 SpongeEventFactory (org.spongepowered.api.event.SpongeEventFactory)1