Search in sources :

Example 6 with EasterlynCore

use of com.easterlyn.EasterlynCore in project Easterlyn by Easterlyn.

the class ChannelManagementListener method onPlayerQuit.

@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
    RegisteredServiceProvider<EasterlynCore> easterlynProvider = chat.getServer().getServicesManager().getRegistration(EasterlynCore.class);
    if (easterlynProvider == null) {
        return;
    }
    User user = easterlynProvider.getProvider().getUserManager().getUser(event.getPlayer().getUniqueId());
    List<String> savedChannels = user.getStorage().getStringList(EasterlynChat.USER_CHANNELS);
    List<String> channels = savedChannels.stream().filter(channelName -> {
        Channel channel = chat.getChannels().get(channelName);
        if (channel == null) {
            return false;
        }
        channel.getMembers().remove(user.getUniqueId());
        return true;
    }).collect(Collectors.toList());
    if (channels.size() != savedChannels.size()) {
        user.getStorage().set(EasterlynChat.USER_CHANNELS, channels);
        if (!channels.contains(user.getStorage().getString(EasterlynChat.USER_CURRENT))) {
            user.getStorage().set(EasterlynChat.USER_CURRENT, null);
        }
    }
}
Also used : UserChatEvent(com.easterlyn.chat.event.UserChatEvent) Date(java.util.Date) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) Player(org.bukkit.entity.Player) UserCreationEvent(com.easterlyn.event.UserCreationEvent) TextComponent(net.md_5.bungee.api.chat.TextComponent) EventHandler(org.bukkit.event.EventHandler) AutoUser(com.easterlyn.user.AutoUser) BaseComponent(net.md_5.bungee.api.chat.BaseComponent) StringUtil(com.easterlyn.util.StringUtil) Map(java.util.Map) AsyncPlayerChatEvent(org.bukkit.event.player.AsyncPlayerChatEvent) Listener(org.bukkit.event.Listener) User(com.easterlyn.user.User) PlayerJoinEvent(org.bukkit.event.player.PlayerJoinEvent) EasterlynCore(com.easterlyn.EasterlynCore) Collection(java.util.Collection) Channel(com.easterlyn.chat.channel.Channel) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) EventPriority(org.bukkit.event.EventPriority) StringJoiner(java.util.StringJoiner) PlayerQuitEvent(org.bukkit.event.player.PlayerQuitEvent) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) RegisteredServiceProvider(org.bukkit.plugin.RegisteredServiceProvider) EasterlynChat(com.easterlyn.EasterlynChat) AutoUser(com.easterlyn.user.AutoUser) User(com.easterlyn.user.User) EasterlynCore(com.easterlyn.EasterlynCore) Channel(com.easterlyn.chat.channel.Channel) EventHandler(org.bukkit.event.EventHandler)

Example 7 with EasterlynCore

use of com.easterlyn.EasterlynCore in project Easterlyn by Easterlyn.

the class ChannelManagementListener method onUserCreate.

@EventHandler
public void onUserCreate(UserCreationEvent event) {
    RegisteredServiceProvider<EasterlynCore> easterlynProvider = chat.getServer().getServicesManager().getRegistration(EasterlynCore.class);
    if (easterlynProvider != null) {
        ConfigurationSection userSection = chat.getConfig().getConfigurationSection("auto_user");
        Map<String, String> userData = new HashMap<>();
        if (userSection != null) {
            userSection.getKeys(false).forEach(key -> userData.put(key, userSection.getString(key)));
        }
        Player player = event.getUser().getPlayer();
        if (player != null && !player.hasPlayedBefore()) {
            // TODO lang?
            new UserChatEvent(new AutoUser(easterlynProvider.getProvider(), userData), EasterlynChat.DEFAULT, event.getUser().getDisplayName() + " is new! Please welcome them.");
        }
    }
    addMainChannel(event.getUser(), null);
}
Also used : Player(org.bukkit.entity.Player) EasterlynCore(com.easterlyn.EasterlynCore) HashMap(java.util.HashMap) UserChatEvent(com.easterlyn.chat.event.UserChatEvent) AutoUser(com.easterlyn.user.AutoUser) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) EventHandler(org.bukkit.event.EventHandler)

Example 8 with EasterlynCore

use of com.easterlyn.EasterlynCore in project Easterlyn by Easterlyn.

the class MuteListener method cancelIfMute.

private void cancelIfMute(Player player, Cancellable cancellable) {
    RegisteredServiceProvider<EasterlynCore> easterlynProvider = Bukkit.getServer().getServicesManager().getRegistration(EasterlynCore.class);
    if (easterlynProvider == null) {
        return;
    }
    User user = easterlynProvider.getProvider().getUserManager().getUser(player.getUniqueId());
    if (user.getStorage().getLong(EasterlynChat.USER_MUTE, 0L) > System.currentTimeMillis()) {
        cancellable.setCancelled(true);
    }
}
Also used : User(com.easterlyn.user.User) EasterlynCore(com.easterlyn.EasterlynCore)

Example 9 with EasterlynCore

use of com.easterlyn.EasterlynCore in project Easterlyn by Easterlyn.

the class CoreCompletions method register.

public static void register(EasterlynCore plugin) {
    CommandCompletions<BukkitCommandCompletionContext> completions = plugin.getCommandManager().getCommandCompletions();
    completions.registerAsyncCompletion("integer", context -> {
        String input = context.getInput();
        if (!input.matches("-?\\d+?")) {
            return Collections.emptyList();
        }
        ArrayList<String> values = new ArrayList<>();
        if (!input.isEmpty()) {
            values.add(input);
        }
        for (int i = input.isEmpty() ? 1 : 0; i < 10; ++i) {
            values.add(input + i);
        }
        return values;
    });
    completions.setDefaultCompletion("integer", int.class, Integer.class, long.class, Long.class);
    completions.registerAsyncCompletion("decimal", context -> {
        String input = context.getInput();
        if (!input.matches("-?\\d+?\\.?\\d+?")) {
            return Collections.emptyList();
        }
        ArrayList<String> values = new ArrayList<>();
        if (!input.isEmpty()) {
            values.add(input);
        }
        for (int i = input.isEmpty() ? 1 : 0; i < 10; ++i) {
            values.add(input + i);
        }
        if (input.indexOf('.') == -1) {
            values.add(input + '.');
        }
        return values;
    });
    completions.setDefaultCompletion("decimal", double.class, Double.class, float.class, Float.class);
    completions.registerAsyncCompletion("permission", context -> {
        if (context.hasConfig("value") && !context.getIssuer().hasPermission(context.getConfig("value"))) {
            return Collections.emptyList();
        }
        if (!context.hasConfig("complete")) {
            return Collections.emptyList();
        }
        return Arrays.stream(context.getConfig("complete").split("/")).distinct().filter(completion -> StringUtil.startsWithIgnoreCase(completion, context.getInput())).collect(Collectors.toList());
    });
    completions.registerCompletion("player", context -> {
        Player issuer = context.getPlayer();
        ArrayList<String> values = new ArrayList<>();
        for (Player player : Bukkit.getOnlinePlayers()) {
            if (issuer == null || issuer.canSee(player)) {
                values.add(player.getName());
            }
        }
        return values;
    });
    completions.setDefaultCompletion("player", Player.class);
    // TODO
    // commands, date
    // location, worldLocation, material, world, chatcolor, chatformat
    completions.registerStaticCompletion("password", Arrays.asList("Hunter2", "animebuttsdrivemenuts1"));
}
Also used : Arrays(java.util.Arrays) BukkitCommandCompletionContext(co.aikar.commands.BukkitCommandCompletionContext) StringUtil(com.easterlyn.util.StringUtil) EasterlynCore(com.easterlyn.EasterlynCore) Player(org.bukkit.entity.Player) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) CommandCompletions(co.aikar.commands.CommandCompletions) ArrayList(java.util.ArrayList) Bukkit(org.bukkit.Bukkit) Player(org.bukkit.entity.Player) BukkitCommandCompletionContext(co.aikar.commands.BukkitCommandCompletionContext) ArrayList(java.util.ArrayList)

Example 10 with EasterlynCore

use of com.easterlyn.EasterlynCore in project Easterlyn by Easterlyn.

the class CoreContexts method register.

public static void register(EasterlynCore plugin) {
    ContextResolver<Long, BukkitCommandExecutionContext> longResolver = context -> {
        String firstArg = context.popFirstArg();
        Matcher matcher = INTEGER_PATTERN.matcher(firstArg);
        if (matcher.find()) {
            try {
                return Long.valueOf(matcher.group(1));
            } catch (NumberFormatException e) {
                throw new InvalidCommandArgument(CoreLang.WHOLE_NUMBER);
            }
        }
        firstArg = firstArg.toUpperCase();
        if (firstArg.matches("[IVXLCDM]+")) {
            return (long) NumberUtil.intFromRoman(firstArg);
        }
        throw new InvalidCommandArgument(CoreLang.WHOLE_NUMBER);
    };
    plugin.getCommandManager().getCommandContexts().registerContext(long.class, longResolver);
    plugin.getCommandManager().getCommandContexts().registerContext(Long.class, longResolver);
    ContextResolver<Integer, BukkitCommandExecutionContext> intResolver = context -> Math.toIntExact(longResolver.getContext(context));
    plugin.getCommandManager().getCommandContexts().registerContext(int.class, intResolver);
    plugin.getCommandManager().getCommandContexts().registerContext(Integer.class, intResolver);
    plugin.getCommandManager().getCommandContexts().registerContext(UUID.class, context -> {
        String firstArg = context.popFirstArg();
        if (firstArg == null) {
            // TODO lang
            throw new InvalidCommandArgument("UUID required");
        }
        try {
            return UUID.fromString(firstArg);
        } catch (IllegalArgumentException e) {
            throw new InvalidCommandArgument("UUID required");
        }
    // TODO allow fetching by player after
    });
    plugin.getCommandManager().getCommandContexts().registerIssuerAwareContext(BukkitCommandIssuer.class, CommandExecutionContext::getIssuer);
    plugin.getCommandManager().getCommandContexts().registerIssuerAwareContext(Player.class, new IssuerAwareContextResolver<>() {

        @Override
        public Player getContext(BukkitCommandExecutionContext context) throws InvalidCommandArgument {
            // noinspection unchecked
            if (context.hasFlag(SELF) || context.hasFlag(ONLINE_WITH_PERM) && context.getIssuer().isPlayer() && context.getCmd().getRequiredPermissions().stream().noneMatch(perm -> context.getIssuer().hasPermission(perm.toString().replace(".self", ".other")))) {
                return getSelf(context.getIssuer());
            }
            if (context.hasFlag(ONLINE)) {
                return getOnline(context.getIssuer(), context.popFirstArg());
            }
            if (context.hasFlag(OFFLINE)) {
                return getOffline(context.getIssuer(), context.popFirstArg());
            }
            // Default/ONLINE_WITH_PERM behavior: Attempt to match online, fall through to self.
            Player player = null;
            String firstArg = context.getFirstArg();
            if (firstArg != null && firstArg.length() > 3) {
                try {
                    player = getOnline(context.getIssuer(), firstArg);
                } catch (InvalidCommandArgument ignored) {
                // If user is not specified, fall through to self.
                }
            }
            if (player != null) {
                context.popFirstArg();
                return player;
            }
            return getSelf(context.getIssuer());
        }

        @NotNull
        private Player getSelf(@NotNull BukkitCommandIssuer issuer) throws InvalidCommandArgument {
            if (issuer.isPlayer()) {
                return issuer.getPlayer();
            }
            throw new InvalidCommandArgument(CoreLang.NO_CONSOLE);
        }

        @NotNull
        private Player getOnline(@NotNull BukkitCommandIssuer issuer, @Nullable String argument) throws InvalidCommandArgument {
            if (argument == null) {
                throw new InvalidCommandArgument(CoreLang.INVALID_PLAYER, "{value}", "null");
            }
            Player player = PlayerUtil.matchOnlinePlayer(issuer.getIssuer(), argument);
            if (player == null) {
                throw new InvalidCommandArgument(CoreLang.INVALID_PLAYER, "{value}", argument);
            }
            return player;
        }

        @NotNull
        private Player getOffline(@NotNull BukkitCommandIssuer issuer, @Nullable String argument) throws InvalidCommandArgument {
            if (argument == null) {
                throw new InvalidCommandArgument(CoreLang.INVALID_PLAYER, "{value}", "null");
            }
            Player player;
            try {
                player = PlayerUtil.matchPlayer(issuer.getIssuer(), argument, true, plugin);
            } catch (IllegalAccessException e) {
                ReportableEvent.call("Called PlayerUtil#matchPlayer on the main thread while executing!", e, 5);
                player = PlayerUtil.matchOnlinePlayer(issuer.getIssuer(), argument);
            }
            if (player == null) {
                throw new InvalidCommandArgument(CoreLang.INVALID_PLAYER, "{value}", argument);
            }
            return player;
        }
    });
    plugin.getCommandManager().getCommandContexts().registerIssuerAwareContext(ConsoleCommandSender.class, context -> {
        if (!(context.getIssuer().getIssuer() instanceof ConsoleCommandSender)) {
            throw new InvalidCommandArgument(CoreLang.ONLY_CONSOLE);
        }
        return (ConsoleCommandSender) context.getIssuer().getIssuer();
    });
    plugin.getCommandManager().getCommandContexts().registerIssuerAwareContext(User.class, context -> {
        Player player = (Player) plugin.getCommandManager().getCommandContexts().getResolver(Player.class).getContext(context);
        return plugin.getUserManager().getUser(player.getUniqueId());
    });
    plugin.getCommandManager().getCommandContexts().registerContext(Date.class, context -> {
        String firstArg = context.popFirstArg();
        long duration = NumberUtil.parseDuration(firstArg);
        return new Date(Math.addExact(System.currentTimeMillis(), duration));
    });
    plugin.getCommandManager().getCommandContexts().registerContext(ChatColor.class, new ContextResolver<>() {

        @Override
        public ChatColor getContext(BukkitCommandExecutionContext context1) throws InvalidCommandArgument {
            ChatColor matched = Colors.getOrDefault(context1.popFirstArg(), null);
            if (matched == null) {
                invalid(context1);
            }
            if (matched == ChatColor.RESET || !context1.hasFlag("colour") && !context1.hasFlag("format")) {
                // Reset is a special case - used to clear colour settings
                return matched;
            }
            boolean format = matched == ChatColor.BOLD || matched == ChatColor.UNDERLINE || matched == ChatColor.STRIKETHROUGH || matched == ChatColor.MAGIC;
            if (context1.hasFlag("colour") && format || context1.hasFlag("format") && !format) {
                invalid(context1);
            }
            return matched;
        }

        private void invalid(BukkitCommandExecutionContext context1) {
            throw new InvalidCommandArgument(MessageKeys.PLEASE_SPECIFY_ONE_OF, "{valid}", Arrays.stream(org.bukkit.ChatColor.values()).filter(chatColor -> context1.hasFlag("format") ? chatColor.isFormat() : !context1.hasFlag("colour") || chatColor.isColor()).map(Enum::name).collect(Collectors.joining(", ", "[", "]")));
        }
    });
    // TODO lang for invalid args
    plugin.getCommandManager().getCommandContexts().registerIssuerAwareContext(World.class, context -> {
        String worldName = context.getFirstArg();
        if (worldName == null) {
            if (context.isOptional() && context.getIssuer().isPlayer()) {
                return context.getIssuer().getPlayer().getWorld();
            }
            throw new InvalidCommandArgument("No world specified!");
        }
        World world = plugin.getServer().getWorld(worldName.toLowerCase());
        if (world == null) {
            if (context.isOptional() && context.getIssuer().isPlayer()) {
                return context.getIssuer().getPlayer().getWorld();
            }
            throw new InvalidCommandArgument("No world specified!");
        }
        context.popFirstArg();
        return world;
    });
}
Also used : Arrays(java.util.Arrays) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) InvalidCommandArgument(co.aikar.commands.InvalidCommandArgument) Date(java.util.Date) ContextResolver(co.aikar.commands.contexts.ContextResolver) Player(org.bukkit.entity.Player) BukkitCommandExecutionContext(co.aikar.commands.BukkitCommandExecutionContext) IssuerAwareContextResolver(co.aikar.commands.contexts.IssuerAwareContextResolver) Matcher(java.util.regex.Matcher) PlayerUtil(com.easterlyn.util.PlayerUtil) World(org.bukkit.World) ChatColor(net.md_5.bungee.api.ChatColor) CommandExecutionContext(co.aikar.commands.CommandExecutionContext) User(com.easterlyn.user.User) NumberUtil(com.easterlyn.util.NumberUtil) MessageKeys(co.aikar.commands.MessageKeys) EasterlynCore(com.easterlyn.EasterlynCore) ReportableEvent(com.easterlyn.event.ReportableEvent) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) BukkitCommandIssuer(co.aikar.commands.BukkitCommandIssuer) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) Colors(com.easterlyn.util.Colors) Player(org.bukkit.entity.Player) Matcher(java.util.regex.Matcher) BukkitCommandIssuer(co.aikar.commands.BukkitCommandIssuer) World(org.bukkit.World) NotNull(org.jetbrains.annotations.NotNull) ConsoleCommandSender(org.bukkit.command.ConsoleCommandSender) Date(java.util.Date) InvalidCommandArgument(co.aikar.commands.InvalidCommandArgument) ChatColor(net.md_5.bungee.api.ChatColor) BukkitCommandExecutionContext(co.aikar.commands.BukkitCommandExecutionContext) BukkitCommandExecutionContext(co.aikar.commands.BukkitCommandExecutionContext) CommandExecutionContext(co.aikar.commands.CommandExecutionContext)

Aggregations

EasterlynCore (com.easterlyn.EasterlynCore)17 User (com.easterlyn.user.User)11 Player (org.bukkit.entity.Player)11 EventHandler (org.bukkit.event.EventHandler)9 UserChatEvent (com.easterlyn.chat.event.UserChatEvent)5 AutoUser (com.easterlyn.user.AutoUser)4 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 EasterlynChat (com.easterlyn.EasterlynChat)3 Channel (com.easterlyn.chat.channel.Channel)3 StringUtil (com.easterlyn.util.StringUtil)3 Collections (java.util.Collections)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Collectors (java.util.stream.Collectors)3 UserCreationEvent (com.easterlyn.event.UserCreationEvent)2 BlockUpdateManager (com.easterlyn.util.BlockUpdateManager)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2