Skip to content

Commit

Permalink
Fix Guice error when depending on a mod
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeregorix committed Oct 4, 2024
1 parent fbf0b7a commit 6fc3fc1
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
import net.minecraftforge.fml.loading.moddiscovery.ModInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.spongepowered.common.inject.SpongePluginInjectorProvider;
import org.spongepowered.plugin.PluginContainer;
import org.spongepowered.common.launch.plugin.SpongePluginContainer;
import org.spongepowered.plugin.metadata.PluginMetadata;

import java.net.URI;
Expand All @@ -41,7 +40,7 @@
import java.util.Objects;
import java.util.Optional;

public class ForgePluginContainer implements PluginContainer, SpongePluginInjectorProvider {
public class ForgePluginContainer implements SpongePluginContainer {
private final ModContainer modContainer;

private Injector injector;
Expand All @@ -52,10 +51,6 @@ private ForgePluginContainer(final ModContainer modContainer) {
this.modContainer = modContainer;
}

public ModContainer getModContainer() {
return this.modContainer;
}

@Override
public PluginMetadata metadata() {
if (this.pluginMetadata == null) {
Expand Down Expand Up @@ -84,8 +79,8 @@ public Object instance() {
}

@Override
public Injector injector() {
return this.injector;
public Optional<Injector> injector() {
return Optional.ofNullable(this.injector);
}

public void setInjector(final Injector injector) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
import net.neoforged.fml.loading.moddiscovery.ModInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.spongepowered.common.inject.SpongePluginInjectorProvider;
import org.spongepowered.plugin.PluginContainer;
import org.spongepowered.common.launch.plugin.SpongePluginContainer;
import org.spongepowered.plugin.metadata.PluginMetadata;

import java.net.URI;
Expand All @@ -41,7 +40,7 @@
import java.util.Objects;
import java.util.Optional;

public class NeoPluginContainer implements PluginContainer, SpongePluginInjectorProvider {
public class NeoPluginContainer implements SpongePluginContainer {
private final ModContainer modContainer;

private Logger logger;
Expand All @@ -53,10 +52,6 @@ private NeoPluginContainer(final ModContainer modContainer) {
this.modContainer = modContainer;
}

public ModContainer getModContainer() {
return this.modContainer;
}

@Override
public PluginMetadata metadata() {
if (this.pluginMetadata == null) {
Expand Down Expand Up @@ -89,8 +84,8 @@ public void setInstance(final Object instance) {
}

@Override
public Injector injector() {
return this.injector;
public Optional<Injector> injector() {
return Optional.ofNullable(this.injector);
}

public void setInjector(final Injector injector) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.inject;
package org.spongepowered.common.launch.plugin;

import com.google.inject.Injector;
import org.spongepowered.plugin.PluginContainer;

public interface SpongePluginInjectorProvider {
import java.util.Optional;

Injector injector();
public interface SpongePluginContainer extends PluginContainer {

Optional<Injector> injector();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import com.google.inject.AbstractModule;
import org.spongepowered.api.Sponge;
import org.spongepowered.common.inject.SpongePluginInjectorProvider;
import org.spongepowered.common.launch.plugin.SpongePluginContainer;
import org.spongepowered.plugin.PluginContainer;
import org.spongepowered.plugin.metadata.model.PluginDependency;

Expand All @@ -53,29 +53,20 @@ protected void configure() {
continue;
}

Sponge.pluginManager().plugin(dependency.id()).ifPresent(p -> {
if (!(p instanceof final SpongePluginInjectorProvider injectorProvider)) {
return;
}

bindingHelper.bindFrom(injectorProvider.injector());
});
Sponge.pluginManager().plugin(dependency.id())
.flatMap(p -> ((SpongePluginContainer) p).injector())
.ifPresent(bindingHelper::bindFrom);
}

//Indirect dependencies
// Indirect dependencies
Sponge.pluginManager()
.plugins()
.stream()
.filter(p -> p.metadata().dependency(this.container.metadata().id())
.map(PluginDependency::loadOrder)
.orElse(PluginDependency.LoadOrder.UNDEFINED) == PluginDependency.LoadOrder.BEFORE)
.forEach(p -> {
if (!(p instanceof final SpongePluginInjectorProvider injectorProvider)) {
return;
}

bindingHelper.bindFrom(injectorProvider.injector());
});
.flatMap(p -> ((SpongePluginContainer) p).injector().stream())
.forEach(bindingHelper::bindFrom);

bindingHelper.bind();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,24 @@
*/
package org.spongepowered.vanilla.launch.plugin;

import com.google.inject.Injector;
import org.apache.logging.log4j.Logger;
import org.spongepowered.common.applaunch.plugin.DummyPluginContainer;
import org.spongepowered.common.launch.plugin.SpongePluginContainer;
import org.spongepowered.plugin.PluginCandidate;
import org.spongepowered.plugin.builtin.StandardPluginContainer;

public final class VanillaDummyPluginContainer extends StandardPluginContainer implements DummyPluginContainer {
import java.util.Optional;

public final class VanillaDummyPluginContainer extends StandardPluginContainer implements SpongePluginContainer, DummyPluginContainer {

public VanillaDummyPluginContainer(final PluginCandidate candidate, final Logger logger, final Object instance) {
super(candidate, logger);
this.initializeInstance(instance);
}

@Override
public Optional<Injector> injector() {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@

import com.google.inject.Injector;
import org.spongepowered.api.Sponge;
import org.spongepowered.common.inject.SpongePluginInjectorProvider;
import org.spongepowered.common.launch.plugin.SpongePluginContainer;
import org.spongepowered.plugin.PluginCandidate;
import org.spongepowered.plugin.builtin.StandardPluginContainer;

public final class VanillaJavaPluginContainer extends StandardPluginContainer implements SpongePluginInjectorProvider {
import java.util.Optional;

public final class VanillaJavaPluginContainer extends StandardPluginContainer implements SpongePluginContainer {

private Injector injector;

Expand All @@ -45,8 +47,8 @@ protected void initializeInstance(final Object instance) {
}

@Override
public Injector injector() {
return this.injector;
public Optional<Injector> injector() {
return Optional.ofNullable(this.injector);
}

public void setInjector(final Injector injector) {
Expand Down

0 comments on commit 6fc3fc1

Please sign in to comment.