use of org.spongepowered.plugin.PluginContainer in project SpongeCommon by SpongePowered.
the class EventManagerRegistrationTest method successfulRegistrationWithAsmDefinedClass.
@Test
public void successfulRegistrationWithAsmDefinedClass() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
final EventManager eventManager = new TestEventManager();
final PluginContainer mock = Mockito.mock(PluginContainer.class);
final ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
writer.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "org/spongepowered/common/test/event/BombDummy", null, "java/lang/Object", null);
final MethodVisitor ctor = writer.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
ctor.visitCode();
ctor.visitVarInsn(Opcodes.ALOAD, 0);
ctor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
ctor.visitInsn(Opcodes.RETURN);
ctor.visitMaxs(1, 1);
final MethodVisitor el = writer.visitMethod(Opcodes.ACC_PUBLIC, "onBlockChange", "(Lorg/spongepowered/api/event/block/ChangeBlockEvent;)V", null, null);
el.visitCode();
el.visitAnnotation(Type.getDescriptor(Listener.class), true);
el.visitInsn(Opcodes.RETURN);
final MethodVisitor bomb = writer.visitMethod(Opcodes.ACC_PUBLIC, "throwup", "(Lcom/example/doesnt/Exist;)V", null, null);
bomb.visitCode();
bomb.visitInsn(Opcodes.RETURN);
final DefineableClassLoader loader = new ReferencedDefinableClassLoader(this.getClass().getClassLoader());
final Class<?> clazz = loader.defineClass("org.spongepowered.common.test.event.BombDummy", writer.toByteArray());
final Object o = clazz.getConstructor().newInstance();
eventManager.registerListeners(mock, o);
}
use of org.spongepowered.plugin.PluginContainer in project SpongeCommon by SpongePowered.
the class EventManagerRegistrationTest method wildcardCanRegister.
@Test
public void wildcardCanRegister() {
final EventManager eventManager = new TestEventManager();
final PluginContainer mock = Mockito.mock(PluginContainer.class);
eventManager.registerListeners(mock, new Wildcard());
}
use of org.spongepowered.plugin.PluginContainer in project SpongeCommon by SpongePowered.
the class ScheduledTaskPhaseState method foldContextForThread.
@Override
public void foldContextForThread(final BasicPluginContext context, final TickTaskBridge returnValue) {
@Nullable final Object source = context.getSource();
final PluginContainer plugin = Objects.requireNonNull(context.container, "Scheduled Task has a null plugin!");
returnValue.bridge$contextShift(((context1, stackFrame) -> {
if (source != null) {
stackFrame.pushCause(source);
}
stackFrame.pushCause(plugin);
}));
}
use of org.spongepowered.plugin.PluginContainer in project SpongeCommon by SpongePowered.
the class SpongeServiceProvider method init.
// Service Discovery
/**
* Discovers services by querying plugins with the
* {@link ProvideServiceEvent}. To be called at the appropriate moment in
* the lifecycle.
*/
public final void init() {
if (!this.services.isEmpty()) {
throw new IllegalStateException("Services have already been initialised");
}
final ServicesCategory.ServicePluginSubCategory servicePluginSubCategory = SpongeConfigs.getCommon().get().services.plugins;
// This does NOT support third party service interfaces, only impls.
for (final Service<?> candidate : this.servicesToSelect()) {
// If the configuration file has a specific plugin ID, we look for it.
final String pluginId = candidate.providePluginId(servicePluginSubCategory);
final boolean isSpecific = !pluginId.isEmpty() && !pluginId.equals("?");
final Class<?> serviceClass = candidate.getServiceClass();
final String serviceName = serviceClass.getSimpleName();
Registration<?> registration = null;
if (isSpecific) {
final Optional<PluginContainer> specificPluginContainer = Launch.instance().pluginManager().plugin(pluginId);
if (specificPluginContainer.isPresent()) {
registration = this.getSpecificRegistration(specificPluginContainer.get(), candidate);
if (registration == null) {
final PrettyPrinter prettyPrinter = new PrettyPrinter(80).add("Service Registration Failed: %s (Service Not Provided)", serviceName).hr().addWrapped("Sponge is configured to obtain the service %s from the plugin with ID %s," + "however, that plugin did not provide any service implementation when " + "requested.", serviceName, pluginId).add().add("To fix this problem, do one of the following:").add().add(" * Check that the plugin %s can actually provide the service (check the plugin" + " documentation if you need more assistance with that plugin).", pluginId).add(" * Set the config entry for this service to \"?\" to let Sponge find another " + " plugin to provide the service.").add(" * Set the config entry for this service to the ID of another plugin that can" + " provide the service.");
if (candidate.suppliesDefault()) {
prettyPrinter.add().add("Sponge will continue using the inbuilt default service.");
}
prettyPrinter.log(SpongeCommon.logger(), Level.ERROR);
}
} else {
final PrettyPrinter prettyPrinter = new PrettyPrinter(80).add("Service Registration Failed: %s (Plugin Not Found)", serviceName).hr().addWrapped("Sponge is configured to obtain the service %s from the plugin with ID %s," + "however, that plugin isn't installed.", serviceName, pluginId).add().add("To fix this problem, do one of the following:").add().add(" * Install the plugin %s", pluginId).add(" * Set the config entry for this service to \"?\" to let Sponge find another " + " plugin to provide the service.").add(" * Set the config entry for this service to the ID of another plugin that can" + " provide the service.");
if (candidate.suppliesDefault()) {
prettyPrinter.add().add("Sponge will continue using the inbuilt default service.");
}
prettyPrinter.log(SpongeCommon.logger(), Level.ERROR);
}
} else {
final Collection<PluginContainer> toQuery = Launch.instance().pluginManager().plugins();
registration = this.attemptRegistration(toQuery, candidate);
}
if (registration == null) {
// If we don't have a registration, we try a Sponge one (which is lowest priority)
registration = this.createRegistration(candidate, Launch.instance().commonPlugin());
}
// If after all that we have a registration, we... register it.
if (registration != null) {
this.services.put(candidate.getServiceClass(), registration);
SpongeCommon.logger().info("Registered service [{}] to plugin '{}'.", registration.clazz.getSimpleName(), registration.pluginContainer.metadata().id());
}
}
}
use of org.spongepowered.plugin.PluginContainer in project SpongeCommon by SpongePowered.
the class PackTest method onRegisterCommand.
@Listener
public void onRegisterCommand(final RegisterCommandEvent<Command.Parameterized> event) {
final Parameter.Value<PluginContainer> pluginContainerParameter = Parameter.plugin().key("plugin").build();
final Parameter.Value<String> nameParameter = Parameter.string().key("name").build();
final Parameter.Value<String> pathParameter = Parameter.string().key("path").build();
event.register(this.plugin, Command.builder().addParameter(Parameter.firstOf(pluginContainerParameter, nameParameter)).addParameter(pathParameter).executor(exec -> {
final Pack pack;
final String path = exec.requireOne(pathParameter);
if (exec.hasAny(pluginContainerParameter)) {
pack = Sponge.server().packRepository().pack(exec.requireOne(pluginContainerParameter));
} else {
final String packName = exec.requireOne(nameParameter);
pack = Sponge.server().packRepository().pack(packName).orElse(null);
if (pack == null) {
return CommandResult.error(Component.text("Pack " + packName + " does not exist."));
}
}
try (final Resource resource = pack.contents().requireResource(PackType.server(), ResourcePath.of(pack.id(), path))) {
final BufferedReader reader = new BufferedReader(new InputStreamReader(resource.inputStream()));
reader.lines().map(x -> Component.text(x.substring(0, Math.min(100, x.length())))).forEach(x -> exec.sendMessage(Identity.nil(), x));
} catch (final Exception e) {
e.printStackTrace();
return CommandResult.error(Component.text("Could not locate: " + e.getMessage()));
}
return CommandResult.success();
}).build(), "packtest");
}
Aggregations