Solved Need help with weighted chance system

Discussion in 'Spigot Plugin Development' started by robotnik, Jun 3, 2017.

  1. I am trying to use a weighted chance system to fill a table with loot, and im using the code I found in this thread
    https://www.spigotmc.org/threads/weighted-chance-system.242375/

    However it throws an error and Im not sure how to fix it
    Code (Text):
    [11:11:13] [Server thread/ERROR]: Could not pass event ChunkPopulateEvent to WorldSchematics2 v2.3.2
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.Chunk.a(Chunk.java:986) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.Chunk.loadNearby(Chunk.java:943) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.ChunkProviderServer.originalGetChunkAt(ChunkProviderServer.java:173) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.ChunkProviderServer.getChunkAt(ChunkProviderServer.java:140) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.ChunkProviderServer.getChunkAt(ChunkProviderServer.java:120) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.ChunkProviderServer.getChunkAt(ChunkProviderServer.java:116) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.World.getChunkAt(World.java:329) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.World.getChunkAtWorldCoords(World.java:325) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.World.getType(World.java:806) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.BlockFlowing.a(BlockFlowing.java:174) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.BlockFlowing.a(BlockFlowing.java:182) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.BlockFlowing.a(BlockFlowing.java:182) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.BlockFlowing.a(BlockFlowing.java:182) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.BlockFlowing.c(BlockFlowing.java:213) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.BlockFlowing.b(BlockFlowing.java:121) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.WorldServer.a(WorldServer.java:723) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.WorldServer.doTick(WorldServer.java:251) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:798) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    Caused by: java.lang.Error: Unresolved compilation problems:
        Method references are allowed only at source level 1.8 or above
        Type mismatch: cannot convert from Object to T
        Lambda expressions are allowed only at source level 1.8 or above
        Cannot refer to the non-final local variable needle defined in an enclosing scope
        Cannot refer to the non-final local variable chance defined in an enclosing scope
        Method references are allowed only at source level 1.8 or above

        at WorldSchematics.util.LootTable.<init>(LootTable.java:101) ~[?:?]
        at WorldSchematics.SpawnSchematic.ApplyBlockData(SpawnSchematic.java:557) ~[?:?]
        at WorldSchematics.SpawnSchematic.Spawn(SpawnSchematic.java:344) ~[?:?]
        at WorldSchematics.SpawnSchematic.Spawn(SpawnSchematic.java:187) ~[?:?]
        at WorldSchematics.ChunkListenser.onChunkPopulate(ChunkListenser.java:166) ~[?:?]
        at sun.reflect.GeneratedMethodAccessor3.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_121]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[minecraft_server.jar:git-Spigot-54ec0b8-e04a179]
    Here is the code Im using to create the weighted chance system

    Code (Text):
        public <T> T getFromWeightedMap(Map<T, Double> weights) {
            if (weights == null || weights.isEmpty()) {
                return null;
            }
            double chance = ThreadLocalRandom.current().nextDouble() * weights.values().stream().reduce(0D, Double::sum); //Line 101 where error occurs
            AtomicDouble needle = new AtomicDouble();
            return weights.entrySet().stream().filter((ent) -> {
                return needle.addAndGet(ent.getValue()) >= chance;
            }).findFirst().map(Map.Entry::getKey).orElse(null);
        }
     
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    You have Java 8 compliance in the IDE but you're running < Java 7 for the server.
     
    • Agree Agree x 1
  3. You have to set the project's SDK to Java 8.
     
  4. That seems to have fixed it, thanks!