Bastian / bstats-metrics

The different bStats Metrics classes
https://bStats.org
MIT License
98 stars 114 forks source link

[BUG] Sponge does not works #87

Closed Lorenzo0111 closed 2 years ago

Lorenzo0111 commented 3 years ago

Description The bstats-sponge 1.7 works very well with sponge but when updating it does not work.

Error When I try to load my plugin on sponge with the latest dependency it gives me this error:

[19:27:24 ERROR] [Sponge]: Could not pass GameStartedServerEvent$Impl to Plugin{id=rocketjoin, name=RocketJoin, version=1.9.1.1, description=Custom Join Messages Plugin, authors=[Lorenzo0111], source=mods\RocketJoin-1.9.1-all.jar}
java.lang.NullPointerException: null
        at org.bstats.sponge.Metrics.addCustomChart(Metrics.java:181) ~[Metrics.class:?]
        at me.lorenzo0111.rocketjoin.sponge.RocketJoinSponge.loadMetrics(RocketJoinSponge.java:151) ~[RocketJoinSponge.class:?]
        at me.lorenzo0111.rocketjoin.sponge.RocketJoinSponge.onServerStart(RocketJoinSponge.java:105) ~[RocketJoinSponge.class:?]
        at org.spongepowered.common.event.listener.GameStartedServerEventListener_RocketJoinSponge_onServerStart4.handle(Unknown Source) ~[?:?]
        at org.spongepowered.common.event.RegisteredListener.handle(RegisteredListener.java:97) ~[RegisteredListener.class:1.12.2-7.3.0]
        at org.spongepowered.common.event.SpongeEventManager.post(SpongeEventManager.java:435) ~[SpongeEventManager.class:1.12.2-7.3.0]
        at org.spongepowered.common.event.SpongeEventManager.post(SpongeEventManager.java:480) ~[SpongeEventManager.class:1.12.2-7.3.0]
        at org.spongepowered.common.SpongeImpl.postEvent(SpongeImpl.java:255) ~[SpongeImpl.class:1.12.2-7.3.0]
        at org.spongepowered.common.SpongeImpl.postState(SpongeImpl.java:263) ~[SpongeImpl.class:1.12.2-7.3.0]
        at org.spongepowered.server.SpongeVanilla.onServerStarting(SpongeVanilla.java:177) ~[SpongeVanilla.class:1.12.2-7.3.0]
        at net.minecraft.server.dedicated.DedicatedServer.handler$zpc000$vanilla$callServerStarting(SourceFile:2272) ~[nz.class:?]
        at net.minecraft.server.dedicated.DedicatedServer.func_71197_b(SourceFile:233) ~[nz.class:?]
        at net.minecraft.server.MinecraftServer.run(SourceFile:434) ~[MinecraftServer.class:?]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_291]

Source Code

Bastian commented 2 years ago

How do you initiate the Sponge Metrics class?

The Sponge Metrics class has to be injected like so:

import com.google.inject.Inject;
import org.bstats.sponge.Metrics;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameStartedServerEvent;
import org.spongepowered.api.plugin.Plugin;

@Plugin(id = "exampleplugin", name = "ExamplePlugin", version = "1.0")
public class ExamplePlugin {

    private final Metrics metrics;

    // The metricsFactory parameter gets injected using @Inject :)
    // Check out https://docs.spongepowered.org/master/en/plugin/injection.html if you don't know what @Inject does
    @Inject
    public ExamplePlugin(Metrics.Factory metricsFactory) {
        // You can find the plugin ids of your plugins on the page https://bstats.org/what-is-my-plugin-id
        int pluginId = 1234; // <-- Replace with the id of your plugin!
        metrics = metricsFactory.make(pluginId);
    }

    // Optional: Add custom charts
    @Listener
    public void onServerStart(GameStartedServerEvent event) {
        metrics.addCustomChart(new Metrics.SimplePie("chart_id", () -> "My value"));
    }

}

You must not call the constructor manually.

Lorenzo0111 commented 2 years ago

This works now, thanks