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();
});
}
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)));
}
}
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);
}
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;
}
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;
}
Aggregations