Search in sources :

Example 71 with PluginContainer

use of org.spongepowered.api.plugin.PluginContainer in project LanternServer by LanternPowered.

the class CommandBanIp method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    specBuilder.arguments(GenericArguments.string(Text.of("address")), GenericArguments.optional(GenericArguments2.remainingString(Text.of("reason")))).executor((src, args) -> {
        final String target = args.<String>getOne("address").get();
        final String reason = args.<String>getOne("reason").orElse(null);
        InetAddress address;
        if (IP_PATTERN.matcher(target).matches()) {
            try {
                address = InetAddress.getByName(target);
            } catch (UnknownHostException e) {
                throw new IllegalStateException("Unable to parse a valid InetAddress: " + target, e);
            }
        } else {
            // Ip address failed, try to find a player
            Optional<Player> player = Sponge.getGame().getServer().getPlayer(target);
            if (!player.isPresent()) {
                throw new CommandException(t("commands.banip.invalid"));
            }
            address = player.get().getConnection().getAddress().getAddress();
        }
        final BanService banService = Sponge.getServiceManager().provideUnchecked(BanService.class);
        final Ban ban = Ban.builder().type(BanTypes.IP).address(address).reason(reason == null ? null : Text.of(reason)).source(src).build();
        banService.addBan(ban);
        final List<LanternPlayer> playersToKick = Lantern.getServer().getRawOnlinePlayers().stream().filter(player -> player.getConnection().getAddress().getAddress().equals(address)).collect(Collectors.toList());
        if (!playersToKick.isEmpty()) {
            final Text kickReason = t("multiplayer.disconnect.ip_banned");
            for (LanternPlayer player : playersToKick) {
                player.kick(kickReason);
            }
        }
        src.sendMessage(t("commands.banip.success", address.toString()));
        return CommandResult.success();
    });
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) Sponge(org.spongepowered.api.Sponge) BanTypes(org.spongepowered.api.util.ban.BanTypes) GenericArguments(org.spongepowered.api.command.args.GenericArguments) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) Ban(org.spongepowered.api.util.ban.Ban) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) InetAddress(java.net.InetAddress) List(java.util.List) Text(org.spongepowered.api.text.Text) Lantern(org.lanternpowered.server.game.Lantern) GenericArguments2(org.lanternpowered.server.command.element.GenericArguments2) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) Pattern(java.util.regex.Pattern) PluginContainer(org.spongepowered.api.plugin.PluginContainer) BanService(org.spongepowered.api.service.ban.BanService) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer) Player(org.spongepowered.api.entity.living.player.Player) UnknownHostException(java.net.UnknownHostException) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) Ban(org.spongepowered.api.util.ban.Ban) BanService(org.spongepowered.api.service.ban.BanService) InetAddress(java.net.InetAddress) LanternPlayer(org.lanternpowered.server.entity.living.player.LanternPlayer)

Example 72 with PluginContainer

use of org.spongepowered.api.plugin.PluginContainer in project LanternServer by LanternPowered.

the class LanternServiceManager method setProvider.

@Override
public <T> void setProvider(Object plugin, Class<T> service, T provider) {
    checkNotNull(plugin, "plugin");
    checkNotNull(service, "service");
    checkNotNull(provider, "provider");
    final Optional<PluginContainer> containerOptional = this.pluginManager.fromInstance(plugin);
    if (!containerOptional.isPresent()) {
        throw new IllegalArgumentException("The provided plugin object does not have an associated plugin container " + "(in other words, is 'plugin' actually your plugin object?)");
    }
    final PluginContainer container = containerOptional.get();
    final ProviderRegistration<T> newProvider = new Provider<>(container, service, provider);
    final ProviderRegistration<?> oldProvider = this.providers.put(service, newProvider);
    try (CauseStack.Frame frame = CauseStack.currentOrEmpty().pushCauseFrame()) {
        frame.pushCause(container);
        frame.addContext(EventContextKeys.SERVICE_MANAGER, this);
        this.eventManager.post(SpongeEventFactory.createChangeServiceProviderEvent(frame.getCurrentCause(), newProvider, Optional.ofNullable(oldProvider)));
    }
}
Also used : CauseStack(org.lanternpowered.server.event.CauseStack) PluginContainer(org.spongepowered.api.plugin.PluginContainer)

Example 73 with PluginContainer

use of org.spongepowered.api.plugin.PluginContainer in project LanternServer by LanternPowered.

the class LanternSqlService method getDataSource.

@Override
public DataSource getDataSource(@Nullable Object plugin, String jdbcConnection) throws SQLException {
    jdbcConnection = getConnectionUrlFromAlias(jdbcConnection).orElse(jdbcConnection);
    PluginContainer container = null;
    if (plugin != null) {
        container = Sponge.getPluginManager().fromInstance(plugin).orElseThrow(() -> new IllegalArgumentException("The provided plugin object does not have an associated plugin container" + " (in other words, is 'plugin' actually your plugin object?"));
    }
    final ConnectionInfo info = ConnectionInfo.fromUrl(container, jdbcConnection);
    return this.connectionCache.get(info);
}
Also used : PluginContainer(org.spongepowered.api.plugin.PluginContainer)

Example 74 with PluginContainer

use of org.spongepowered.api.plugin.PluginContainer in project LanternServer by LanternPowered.

the class AbstractAssetRepository method registerAsset.

Asset registerAsset(Object plugin, String id, URL url, Path file) {
    final Optional<Asset> optAsset = this.loadedAssets.get(id);
    if (optAsset != null) {
        return optAsset.get();
    }
    PluginContainer pluginContainer;
    if (plugin instanceof String) {
        final String pluginId = (String) plugin;
        // Attempt to find a plugin container that is assigned to the id,
        // if not, create a plugin container that represents the plugin
        // with the id.
        pluginContainer = this.pluginManager.getPlugin(pluginId).orElse(null);
        if (pluginContainer == null) {
            // don't register a plugin in this case, just return a asset with a dummy one.
            if (INFO_FILE_PATTERN.matcher(id).matches()) {
                return new LanternAsset(new SimplePluginContainer(pluginId), id, url, file);
            }
            // Attempt to get plugin info from the repository, and use
            // that to define the plugin container
            final URL infoUrl = getAssetURL(Paths.get(DEFAULT_ASSET_DIR).resolve(pluginId).resolve("plugin.info"));
            if (infoUrl != null) {
                try {
                    final PluginMetadata pluginMetadata = InfoPluginContainer.readPluginInfo(pluginId, infoUrl);
                    // Construct a plugin container
                    pluginContainer = new InfoPluginContainer(pluginId, pluginMetadata);
                } catch (IOException e) {
                    Lantern.getLogger().error("Failed to read plugin.info");
                }
            }
            if (pluginContainer == null) {
                // Generate a simple plugin container
                pluginContainer = new SimplePluginContainer(pluginId);
            }
            // Register the plugin container
            this.pluginManager.registerPlugin(pluginContainer);
            Lantern.getLogger().info("Registered data pack plugin: {} {}", pluginContainer.getName(), pluginContainer.getVersion().orElse("unknown"));
        }
    } else {
        // Search for the plugin container based on the instance
        pluginContainer = this.pluginManager.fromInstance(plugin).get();
    }
    checkNotNull(pluginContainer);
    final LanternAsset asset = new LanternAsset(pluginContainer, id, url, file);
    this.loadedAssets.put(id, Optional.of(asset));
    return asset;
}
Also used : SimplePluginContainer(org.lanternpowered.server.plugin.SimplePluginContainer) PluginContainer(org.spongepowered.api.plugin.PluginContainer) InfoPluginContainer(org.lanternpowered.server.plugin.InfoPluginContainer) SimplePluginContainer(org.lanternpowered.server.plugin.SimplePluginContainer) Asset(org.lanternpowered.api.asset.Asset) PluginMetadata(org.spongepowered.plugin.meta.PluginMetadata) IOException(java.io.IOException) URL(java.net.URL) InfoPluginContainer(org.lanternpowered.server.plugin.InfoPluginContainer)

Example 75 with PluginContainer

use of org.spongepowered.api.plugin.PluginContainer in project LanternServer by LanternPowered.

the class LanternEventManager method register.

public <T extends Event> RegisteredListener<T> register(Object plugin, TypeToken<T> eventType, Order order, EventListener<? super T> listener) {
    final PluginContainer pluginContainer = checkPlugin(plugin, "plugin");
    checkNotNull(eventType, "eventType");
    checkNotNull(order, "order");
    checkNotNull(listener, "listener");
    registerListenerInstance(pluginContainer, listener);
    final RegisteredListener<T> registeredListener = createRegistration(pluginContainer, eventType, order, listener);
    register(Collections.singletonList(registeredListener));
    return registeredListener;
}
Also used : PluginContainer(org.spongepowered.api.plugin.PluginContainer)

Aggregations

PluginContainer (org.spongepowered.api.plugin.PluginContainer)77 Text (org.spongepowered.api.text.Text)15 CommandResult (org.spongepowered.api.command.CommandResult)13 GenericArguments (org.spongepowered.api.command.args.GenericArguments)13 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)13 List (java.util.List)12 TranslationHelper.t (org.lanternpowered.server.text.translation.TranslationHelper.t)11 CommandException (org.spongepowered.api.command.CommandException)11 ArrayList (java.util.ArrayList)10 Optional (java.util.Optional)10 Collectors (java.util.stream.Collectors)9 Sponge (org.spongepowered.api.Sponge)9 Player (org.spongepowered.api.entity.living.player.Player)8 Collection (java.util.Collection)7 Map (java.util.Map)7 Nullable (javax.annotation.Nullable)7 CommandSource (org.spongepowered.api.command.CommandSource)7 GenericArguments2 (org.lanternpowered.server.command.element.GenericArguments2)6 CommandMapping (org.spongepowered.api.command.CommandMapping)6 ArgumentParseException (org.spongepowered.api.command.args.ArgumentParseException)6