Coder Social home page Coder Social logo

eventbus's Introduction

Forge Logo

MinecraftForge

Stable Release Latest Release Discord Support

Forge is a free, open-source modding API all of your favourite mods use!

Version Support
1.20.x Active
1.19.x Legacy

Installing Forge

Go to the Forge website and select the Minecraft version you wish to get Forge for from the list.

You can download the installer for the Recommended Build or the Latest build there. Latest builds may have newer features but may be more unstable as a result. The installer will attempt to install Forge into your vanilla launcher environment, where you can then create a new profile using that version and play the game!

For support and questions, visit the Support Forum or the Forge Discord server.

Creating Mods

See the "Getting Started" section in the Forge Documentation.

Contribute to Forge

If you wish to actually inspect Forge, submit PRs or otherwise work with Forge itself, you're in the right place!

See the guide to setting up a Forge workspace.

Pull requests

See the "Making Changes and Pull Requests" section in the Forge documentation.

Please read the contributing guidelines found here before making a pull request.

Contributor License Agreement

We require all contributors to acknowledge the Forge Contributor License Agreement. Please ensure you have a valid email address associated with your GitHub account to do this. If you have previously signed it, you should be OK.

Donate

Forge is a large project with many collaborators working on it around the clock. Forge is and will always remain free to use and modify. However, it costs money to run such a large project as this, so please consider becoming a patron.

eventbus's People

Contributors

coehlrich avatar cpw avatar gigaherz avatar ichttt avatar lexmanos avatar mezz avatar sirywell avatar tterrag1098 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

eventbus's Issues

Cannot listen to abstract Event classes

It is not possible to listen to an Event that is declared as an abstract class. The code in EventBus.addToListeners attempts to construct an instance of the event before using that instance to obtain the ListenerList.

Constructor<?> ctr = eventType.getConstructor();
ctr.setAccessible(true);
Event event = (Event)ctr.newInstance();

Note that this is not a new issue with the separation: examination shows the same code in 1.12 and earlier versions for Forge, with the same issue.

AIOB during event build, occasionally

[31Dec.2020 16:49:56.583] [modloading-worker-5/ERROR] [net.minecraftforge.fml.javafmlmod.FMLModContainer/LOADING]: Failed to create mod instance. ModID: cookingforblockheads, class net.blay09.mods.cookingforblockheads.CookingForBlockheads
java.lang.ExceptionInInitializerError: null
at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:?]
at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:?]
at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:?]
at java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) ~[?:?]
at java.lang.reflect.Constructor.newInstance(Constructor.java:481) ~[?:?]
at net.minecraftforge.eventbus.api.EventListenerHelper.computeListenerList(EventListenerHelper.java:98) ~[eventbus-3.0.5-service.jar:?]
at net.minecraftforge.eventbus.api.EventListenerHelper.getListenerListInternal(EventListenerHelper.java:63) ~[eventbus-3.0.5-service.jar:?]
at net.minecraftforge.eventbus.api.EventListenerHelper.getListenerList(EventListenerHelper.java:48) ~[eventbus-3.0.5-service.jar:?]
at net.minecraftforge.eventbus.EventBus.addToListeners(EventBus.java:263) ~[eventbus-3.0.5-service.jar:?]
at net.minecraftforge.eventbus.EventBus.addListener(EventBus.java:239) ~[eventbus-3.0.5-service.jar:?]
at net.minecraftforge.eventbus.EventBus.addListener(EventBus.java:231) ~[eventbus-3.0.5-service.jar:?]
at net.minecraftforge.eventbus.EventBus.addListener(EventBus.java:186) ~[eventbus-3.0.5-service.jar:?]
at net.minecraftforge.eventbus.EventBus.addListener(EventBus.java:180) ~[eventbus-3.0.5-service.jar:?]
at net.minecraftforge.eventbus.EventBus.addListener(EventBus.java:174) ~[eventbus-3.0.5-service.jar:?]
at net.blay09.mods.cookingforblockheads.CookingForBlockheads.(CookingForBlockheads.java:86) ~[cookingforblockheads:9.2.2]
at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:?]
at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:?]
at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:?]
at java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) ~[?:?]
at java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:124) ~[?:?]
at jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:346) ~[?:?]
at java.lang.Class.newInstance(Class.java:604) ~[?:?]
at net.minecraftforge.fml.javafmlmod.FMLModContainer.constructMod(FMLModContainer.java:81) ~[forge:35.1]
at net.minecraftforge.fml.ModContainer.lambda$buildTransitionHandler$4(ModContainer.java:120) ~[forge:?]
at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1800) [?:?]
at java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1792) [?:?]
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290) [?:?]
at java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1016) [?:?]
at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1665) [?:?]
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1598) [?:?]
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183) [?:?]
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 0
at java.util.ArrayList.add(ArrayList.java:455) ~[?:?]
at java.util.ArrayList.add(ArrayList.java:467) ~[?:?]
at net.minecraftforge.eventbus.ListenerList$ListenerListInst.addChild(ListenerList.java:239) ~[eventbus-3.0.5-service.jar:?]
at net.minecraftforge.eventbus.ListenerList$ListenerListInst.(ListenerList.java:180) ~[eventbus-3.0.5-service.jar:?]
at net.minecraftforge.eventbus.ListenerList$ListenerListInst.(ListenerList.java:143) ~[eventbus-3.0.5-service.jar:?]
at net.minecraftforge.eventbus.ListenerList.resizeLists(ListenerList.java:97) ~[eventbus-3.0.5-service.jar:?]
at net.minecraftforge.eventbus.ListenerList.(ListenerList.java:52) ~[eventbus-3.0.5-service.jar:?]
at net.minecraftforge.fml.event.server.FMLServerStartedEvent.(FMLServerStartedEvent.java) ~[forge:?]
... 31 more

ListerList resize thread issue.

Yet another potential listener list threading issue.
https://gist.githubusercontent.com/yrsegal/4f9ca65d13b74dbf69dea24e6a9d548a/raw/6e6b9e6d05be7fbc72e1e4be7156301688e13e11/Crashlog

"Reported" Here: https://www.minecraftforge.net/forum/topic/75210-race-condition-within-listenerlist-resize-behavior/

Seems to be an issue when a event bus is created, while a event listener is being added. But I am unable to reproduce.

I've modified the ParallelEventTest to try and reproduce, but it doesn't seem to be working. It gets stuck on the sync lock, as it should.

package net.minecraftforge.eventbus.test;

import net.minecraftforge.eventbus.ListenerList;
import net.minecraftforge.eventbus.api.BusBuilder;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.testjar.DummyEvent;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.powermock.reflect.Whitebox;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;

public class ParallelEventTest
{
    private static final int BUS_COUNT = 16;
    private static final int LISTENER_COUNT = 1000;
    private static final int RUN_ITERATIONS = 1000;

    private final AtomicLong COUNTER = new AtomicLong();
    private ExecutorService POOL;

    @BeforeEach
    public void setup() {
        COUNTER.set(0);
        POOL = Executors.newWorkStealingPool();
    }

    @AfterEach
    public void destroy() throws InterruptedException {
        POOL.shutdown();
        while(!POOL.isTerminated())
            Thread.sleep(10);
    }

    private <T> void executeAll(Collection<T> stream, final Consumer<T> task) {
        List<Callable<?>> tasks = new ArrayList<>();
        stream.forEach(m -> tasks.add(() -> {
            task.accept(m);
            return null;
        }));
        try {
            POOL.invokeAll(tasks);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    //@Disabled
    @RepeatedTest(100)
    public void testMultipleThreadsMultipleBus() throws InterruptedException {
        Set<IEventBus> busSet = new HashSet<>();

        Collection<Callable<IEventBus>> builders = new ArrayList<>();
        for (int i = 0; i < BUS_COUNT; i++) {
            builders.add(BusBuilder.builder().setTrackPhases(false)::build); //make buses for concurrent testing
        }
        POOL.invokeAll(builders).stream().map(f -> {
            try {
                return f.get();
            } catch (InterruptedException | ExecutionException e) {
                throw new RuntimeException(e);
            }
        }).forEach(busSet::add);

        executeAll(busSet, iEventBus -> { //execute parallel listener adding
            for (int i = 0; i < LISTENER_COUNT; i++) {
                BusBuilder.builder().setTrackPhases(false).build();
                iEventBus.addListener(this::handle);
            }
        });
        executeAll(busSet, iEventBus -> { //post events parallel
            for (int i = 0; i < RUN_ITERATIONS; i++)
                iEventBus.post(new DummyEvent.GoodEvent());
        });

        long expected = BUS_COUNT * LISTENER_COUNT * RUN_ITERATIONS;
        Assertions.assertEquals(expected, COUNTER.get());
    }

    //@Disabled
    @RepeatedTest(100)
    public void testMultipleThreadsOneBus() {
        IEventBus bus = BusBuilder.builder().setTrackPhases(false).build();

        Set<Runnable> toAdd = new HashSet<>();

        for (int i = 0; i < LISTENER_COUNT; i++) { //prepare parallel listener adding
            toAdd.add(() -> {
                BusBuilder.builder().setTrackPhases(false).build();
                bus.addListener(this::handle);
            });
        }
        executeAll(toAdd, Runnable::run); //execute parallel listener adding

        toAdd = new HashSet<>();
        for (int i = 0; i < RUN_ITERATIONS; i++) //prepare parallel event posting
            toAdd.add(() -> bus.post(new DummyEvent.GoodEvent()));
        executeAll(toAdd, Runnable::run); //post events parallel

        try {
            long expected = LISTENER_COUNT * RUN_ITERATIONS;
            final ListenerList listenerList = Whitebox.invokeMethod(new DummyEvent.GoodEvent(), "getListenerList");
            int busid = Whitebox.getInternalState(bus, "busID");
            Assertions.assertAll(
                    ()->Assertions.assertEquals(expected, COUNTER.get()),
                    ()->Assertions.assertEquals(LISTENER_COUNT, listenerList.getListeners(busid).length - 1)

            );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void handle(DummyEvent.GoodEvent event) {
        COUNTER.incrementAndGet();
    }
}

ASMClassLoader delegates class loading to the wrong class loader

return Class.forName(name, resolve, Thread.currentThread().getContextClassLoader());
forces classes to be loaded with the thread's context class loader instead of whatever loaded the subscribing class in the first place.

The thread context class loader is not an universally appropriate substitute, it breaks with threads living outside any application context such as the ForkJoinPool common pool. This breaks popular uses such as parallelStream() or the default executor CompletableFuture overloads, leading to exceptions such as this:

java.lang.NoClassDefFoundError: org/cyclops/cyclopscore/modcompat/capabilities/CapabilityConstructorRegistry$ItemStackEventListener
        at net.minecraftforge.eventbus.ASMEventHandler_73_ItemStackEventListener_onItemStackLoad_AttachCapabilitiesEvent.invoke(.dynamic)
        at MC-BOOTSTRAP/[email protected]/net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:85)
        at MC-BOOTSTRAP/[email protected]/net.minecraftforge.eventbus.EventBus.post(EventBus.java:302)
        at MC-BOOTSTRAP/[email protected]/net.minecraftforge.eventbus.EventBus.post(EventBus.java:283)
        at TRANSFORMER/[email protected]/net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:584)
        at TRANSFORMER/[email protected]/net.minecraftforge.event.ForgeEventFactory.gatherCapabilities(ForgeEventFactory.java:578)
        at TRANSFORMER/[email protected]/net.minecraftforge.common.capabilities.CapabilityProvider.doGatherCapabilities(CapabilityProvider.java:87)
        at TRANSFORMER/[email protected]/net.minecraftforge.common.capabilities.CapabilityProvider.getCapabilities(CapabilityProvider.java:101)
        at TRANSFORMER/[email protected]/net.minecraftforge.common.capabilities.CapabilityProvider.areCapsCompatible(CapabilityProvider.java:113)
        at TRANSFORMER/[email protected]/net.minecraft.world.item.ItemStack.m_41658_(ItemStack.java:411)
        at TRANSFORMER/[email protected]/com.ldtteam.aequivaleo.api.util.Comparators.lambda$static$0(Comparators.java:39)
        at TRANSFORMER/[email protected]/com.ldtteam.aequivaleo.compound.container.itemstack.ItemStackContainer.compareTo(ItemStackContainer.java:167)
        at TRANSFORMER/[email protected]/com.ldtteam.aequivaleo.compound.container.itemstack.ItemStackContainer.compareTo(ItemStackContainer.java:23)
        at java.base/java.util.TreeMap.compare(TreeMap.java:1569)
        at java.base/java.util.TreeMap.addEntryToEmptyMap(TreeMap.java:776)
        at java.base/java.util.TreeMap.put(TreeMap.java:785)
        at java.base/java.util.TreeMap.put(TreeMap.java:534)
        at java.base/java.util.TreeSet.add(TreeSet.java:255)
        at TRANSFORMER/[email protected]/com.ldtteam.aequivaleo.recipe.equivalency.InstancedEquivalency.<init>(InstancedEquivalency.java:21)
        at TRANSFORMER/[email protected]/com.ldtteam.aequivaleo.bootstrap.WorldBootstrapper.lambda$doBootstrapInstancedEquivalencies$0(WorldBootstrapper.java:111)
        at TRANSFORMER/[email protected]/com.ldtteam.aequivaleo.bootstrap.WorldBootstrapper.lambda$doBootstrapInstancedEquivalencies$1(WorldBootstrapper.java:133)
        at TRANSFORMER/[email protected]/com.ldtteam.aequivaleo.instanced.InstancedEquivalencyHandlerRegistry.process(InstancedEquivalencyHandlerRegistry.java:35)
        at TRANSFORMER/[email protected]/com.ldtteam.aequivaleo.bootstrap.WorldBootstrapper.lambda$doBootstrapInstancedEquivalencies$2(WorldBootstrapper.java:104)
        at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
        at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:992)
        at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
        at java.base/java.util.stream.ForEachOps$ForEachTask.compute(ForEachOps.java:290)
        at java.base/java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:754)
        at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
        at java.base/java.util.concurrent.ForkJoinTask.invoke(ForkJoinTask.java:686)
        at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateParallel(ForEachOps.java:159)
        at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateParallel(ForEachOps.java:173)
        at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:233)
        at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
        at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:765)
        at TRANSFORMER/[email protected]/com.ldtteam.aequivaleo.bootstrap.WorldBootstrapper.doBootstrapInstancedEquivalencies(WorldBootstrapper.java:104)
        at TRANSFORMER/[email protected]/com.ldtteam.aequivaleo.bootstrap.WorldBootstrapper.onWorldReload(WorldBootstrapper.java:49)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
        at TRANSFORMER/[email protected]/com.ldtteam.aequivaleo.analysis.AequivaleoReloadListener$AequivaleoWorldAnalysisRunner.reloadEquivalencyData(AequivaleoReloadListener.java:454)
        at TRANSFORMER/[email protected]/com.ldtteam.aequivaleo.analysis.AequivaleoReloadListener$AequivaleoWorldAnalysisRunner.run(AequivaleoReloadListener.java:444)
        at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1804)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
        at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.ClassNotFoundException: org.cyclops.cyclopscore.modcompat.capabilities.CapabilityConstructorRegistry$ItemStackEventListener
        ... 44 more

Such a class load issue additionally incurs failure to load the affected class in the future with throwing NoClassDefFoundError. In the above example any later attempts to use the CapabilityConstructorRegistry$ItemStackEventListener type fail on other threads, including the regular server thread which presumably has the context class loader set properly.

To fix the problem ASMEventHandler will most likely have to grab+store the appropriate class loader upon its creation (may be as simple method.getDeclaringClass().getClassLoader()?) and use that to define the generated class. In practice this would mean having an ASMClassLoader instance for each target class loader encountered, adapting a single instance every time is not advisable.

More specifically:

  • add a classLoader instance field to ASMEventHandler
  • populate classLoader with method.getDeclaringClass().getClassLoader() in the constructor
  • change LOADER to be ConcurrentHashMap<ClassLoader, ASMClassLoader>
  • add a classLoader field to ASMClassLoader and populate it from the constructor
  • make ASMClassLoader.loadClass use classLoader instead of Thread.currentThread().getContextClassLoader()
  • change the LOADER.define invocation to LOADER.computeIfAbsent(classLoader, ASMClassLoader::new).define

Listeners are not cleaned up immediately when calling EventBus#unregister

ListenerList#forceRebuild simply sets a boolean to true, and relies on the next event post to do the actual cleanup:

https://github.com/MinecraftForge/EventBus/blob/master/src/main/java/net/minecraftforge/eventbus/ListenerList.java#L223-L231

This means that if unregister is called and the event is never fired again (such as for startup events) the event listener will not be released from memory.

Additionally, there's no way to do this manually even though getListeners is public, since it requires the bus ID which is not exposed.

Toposort based event loading

Sometimes, an event listener just want to be before/after one specific other listener rather than shifting the whole phase up or down. In this case, topological sort may help and the compatibility with the old index/comparable based sort is retained.

Error Launching MC 1.14.3 + Forge 27.0.25

Unable to download re-requisite librararies:

Failed to download file.
Name: eventbus-0.10.3-milestone.0.1+1a5fa31-service.jar
URL: https://modloaders.forgecdn.net/647622546/maven/net/minecraftforge/eventbus/0.10.3-milestone.0.1+1a5fa31-service/eventbus-0.10.3-milestone.0.1+1a5fa31-service.jar
Error details: The requested URL returned error: 403 Forbidden
Filename on disk: eventbus-0.10.3-milestone.0.1+1a5fa31-service.jar
Path: C:\Users\jbruneau\Twitch\Minecraft\Install\libraries\net\minecraftforge\eventbus\0.10.3-milestone.0.1+1a5fa31-service\eventbus-0.10.3-milestone.0.1+1a5fa31-service.jar
Exists: Nonexistent

Accessing the URL in Google Chrome:

(Error)
(Code)AccessDenied(/Code)
(Message)Access Denied(/Message)
(RequestId)76B6FEECF7DC890D(/RequestId)
(HostId)
9InSbQL46MY9DjXXhq3Y6JvrGxy6E9YW9kL2xY/mJewFxLAWoA5FURhSQgGj1vNot2s5JliM0d4=
(/HostId)
(/Error)

Cannot listen to abstract Event classes

It is not possible to listen to an Event that is declared as an abstract class. The code in EventBus.addToListeners attempts to construct an instance of the event before using that instance to obtain the ListenerList.

Constructor<?> ctr = eventType.getConstructor();
ctr.setAccessible(true);
Event event = (Event)ctr.newInstance();

Note that this is not a new issue with the separation: examination shows the same code in 1.12 and earlier versions for Forge, with the same issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.