Search in sources :

Example 1 with EventHandler

use of io.xol.chunkstories.api.events.EventHandler in project chunkstories by Hugobros3.

the class DefaultPluginManager method registerEventListener.

@Override
public void registerEventListener(Listener listener, ChunkStoriesPlugin plugin) {
    System.out.println("Registering " + listener);
    try {
        // Get a list of all the classes methods
        Set<Method> methods = new HashSet<Method>();
        for (Method method : listener.getClass().getMethods()) methods.add(method);
        for (Method method : listener.getClass().getDeclaredMethods()) methods.add(method);
        // Filter it so only interested in @EventHandler annoted methods
        for (final Method method : methods) {
            EventHandler eventHandlerAnnotation = method.getAnnotation(EventHandler.class);
            // We look for the annotation in the method
            if (eventHandlerAnnotation == null)
                continue;
            // TODO something about priority
            if (method.getParameterTypes().length != 1 || !Event.class.isAssignableFrom(method.getParameterTypes()[0])) {
                logger().warn("Plugin " + plugin + " attempted to register an invalid EventHandler");
                continue;
            }
            Class<? extends Event> parameter = method.getParameterTypes()[0].asSubclass(Event.class);
            // Create an EventExecutor to launch the event code
            EventExecutor executor = new EventExecutor() {

                @Override
                public void fireEvent(Event event) throws Exception {
                    method.invoke(listener, event);
                }
            };
            RegisteredListener registeredListener = new RegisteredListener(listener, plugin, executor, eventHandlerAnnotation.priority());
            // Get the listeners list for this event
            Method getListeners = parameter.getMethod("getListenersStatic");
            getListeners.setAccessible(true);
            EventListeners thisEventKindOfListeners = (EventListeners) getListeners.invoke(null);
            // Add our own to it
            thisEventKindOfListeners.registerListener(registeredListener);
            registeredEventListeners.put(thisEventKindOfListeners, registeredListener);
            // Depending on the event configuration we may or may not care about the children events
            if (eventHandlerAnnotation.listenToChildEvents() != EventHandler.ListenToChildEvents.NO)
                addRegisteredListenerToEventChildren(thisEventKindOfListeners, registeredListener, eventHandlerAnnotation.listenToChildEvents() == EventHandler.ListenToChildEvents.RECURSIVE);
            logger().info("Successuflly added EventHandler for " + parameter.getName() + "in " + listener + " of plugin " + plugin);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : EventExecutor(io.xol.chunkstories.api.events.EventExecutor) EventHandler(io.xol.chunkstories.api.events.EventHandler) Event(io.xol.chunkstories.api.events.Event) EventListeners(io.xol.chunkstories.api.events.EventListeners) Method(java.lang.reflect.Method) RegisteredListener(io.xol.chunkstories.api.events.RegisteredListener) PluginLoadException(io.xol.chunkstories.api.exceptions.plugins.PluginLoadException) IOException(java.io.IOException) PluginCreationException(io.xol.chunkstories.api.exceptions.plugins.PluginCreationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashSet(java.util.HashSet)

Example 2 with EventHandler

use of io.xol.chunkstories.api.events.EventHandler in project chunkstories-core by Hugobros3.

the class RenderingEventsListener method onRenderPassesInitialization.

@EventHandler
public void onRenderPassesInitialization(RenderPassesInitEvent event) {
    RenderPasses pipeline = event.getPasses();
    WorldRenderer worldRenderer = pipeline.getWorldRenderer();
    SkyRenderer sky = new DefaultSkyRenderer(event.getPasses().getRenderingInterface().getWorldRenderer());
    worldRenderer.setSkyRenderer(sky);
    SkyPass skyPass = new SkyPass(pipeline, "sky", worldRenderer.getSkyRenderer());
    pipeline.registerRenderPass(skyPass);
    // gbuffer uses the zBuffer and outputs albedo/normal/materials
    GBuffersOpaquePass gBuffers = new GBuffersOpaquePass(pipeline, "gBuffers", new String[] { "sky.zBuffer!" }, new String[] { "albedoBuffer", "normalBuffer", "voxelLightBuffer", "specularityBuffer", "materialsBuffer" /*"zBuffer"*/
    });
    pipeline.registerRenderPass(gBuffers);
    DecalsPass decals = new DecalsPass(pipeline, "decals", new String[] { "gBuffers.albedoBuffer!", "gBuffers.zBuffer" }, new String[] {});
    pipeline.registerRenderPass(decals);
    WaterPass waterPass = new WaterPass(pipeline, "water", new String[] { "decals.albedoBuffer", "gBuffers.normalBuffer", "gBuffers.voxelLightBuffer", "gBuffers.specularityBuffer", "gBuffers.materialsBuffer", "gBuffers.zBuffer" }, new String[] {/*"albedoBuffer", "normalBuffer", "voxelLightBuffer", "specularityBuffer", "materialsBuffer", "zBuffer"*/
    }, sky);
    pipeline.registerRenderPass(waterPass);
    boolean shadows = client.getConfiguration().getBooleanOption("client.rendering.shadows");
    // a shadowmap pass requires no previous buffer and just outputs a shadowmap
    ShadowPass sunShadowPass = null;
    if (shadows) {
        sunShadowPass = new ShadowPass(pipeline, "shadowsSun", new String[] {}, new String[] { "shadowMap" }, sky);
        // note we could generalize the shadowmappass to not only the sun but also the moon, point and spotlights
        pipeline.registerRenderPass(sunShadowPass);
    }
    // aka shadows_apply in the current code, it takes the gbuffers and applies the shadowmapping to them, then outputs to the shaded pixels buffers already filled with the far terrain pixels
    ApplySunlightPass applySunlight = new ApplySunlightPass(pipeline, "applySunlight", new String[] { "water.albedoBuffer", "water.normalBuffer", "water.voxelLightBuffer", "water.specularityBuffer", "water.materialsBuffer", "water.zBuffer", "sky.shadedBuffer!" }, new String[] {/*"shadedBuffer"*/
    }, sunShadowPass);
    if (shadows)
        applySunlight.requires.add("shadowsSun.shadowMap");
    pipeline.registerRenderPass(applySunlight);
    boolean gi = client.getConfiguration().getBooleanOption("client.rendering.globalIllumination") && shadows;
    if (gi) {
        GiPass giPass = new GiPass(pipeline, "gi", new String[] { "water.albedoBuffer", "water.normalBuffer", "water.zBuffer" }, new String[] { "giBuffer" }, sunShadowPass);
        pipeline.registerRenderPass(giPass);
    }
    DefferedLightsPass lightsPass = new DefferedLightsPass(pipeline, "lights", new String[] { "applySunlight.shadedBuffer!", "water.albedoBuffer", "water.normalBuffer", "water.zBuffer" }, new String[] {});
    pipeline.registerRenderPass(lightsPass);
    // far terrain needs the shaded buffer from sky and outputs it, as well with a zbuffer
    FarTerrainPass farTerrain = new FarTerrainPass(pipeline, "farTerrain", new String[] { "lights.shadedBuffer!", "gBuffers.specularityBuffer!", "gBuffers.zBuffer!" }, new String[] {/*"shadedBuffer", "zBuffer", "specularityBuffer"*/
    });
    pipeline.registerRenderPass(farTerrain);
    BloomPass bloomPass = new BloomPass(pipeline, "bloom", new String[] { "farTerrain.shadedBuffer" }, new String[] { "bloomBuffer" });
    pipeline.registerRenderPass(bloomPass);
    ForwardPass forward = new ForwardPass(pipeline, "forward", new String[] { "farTerrain.shadedBuffer!", "farTerrain.zBuffer!" }, new String[] {});
    pipeline.registerRenderPass(forward);
    ReflectionsPass reflections = new ReflectionsPass(pipeline, "reflections", new String[] { "farTerrain.shadedBuffer", "gBuffers.normalBuffer", "gBuffers.voxelLightBuffer", "farTerrain.specularityBuffer", "farTerrain.zBuffer" }, new String[] { "reflectionsBuffer" }, sky);
    pipeline.registerRenderPass(reflections);
    // the pass declared as 'final' is considered the last one and it's outputs are shown to the screen
    PostProcessPass postprocess = new PostProcessPass(pipeline, "final", new String[] { "water.albedoBuffer", "forward.shadedBuffer", "farTerrain.zBuffer", "bloom.bloomBuffer", "reflections.reflectionsBuffer", "farTerrain.specularityBuffer" }, sunShadowPass);
    if (gi)
        postprocess.requires.add("gi.giBuffer");
    pipeline.registerRenderPass(postprocess);
}
Also used : WaterPass(io.xol.chunkstories.core.rendering.passes.WaterPass) ForwardPass(io.xol.chunkstories.core.rendering.passes.ForwardPass) GiPass(io.xol.chunkstories.core.rendering.passes.gi.GiPass) DefaultSkyRenderer(io.xol.chunkstories.core.rendering.sky.DefaultSkyRenderer) SkyRenderer(io.xol.chunkstories.api.rendering.world.SkyRenderer) FarTerrainPass(io.xol.chunkstories.core.rendering.passes.FarTerrainPass) ReflectionsPass(io.xol.chunkstories.core.rendering.passes.ReflectionsPass) ShadowPass(io.xol.chunkstories.core.rendering.passes.ShadowPass) RenderPasses(io.xol.chunkstories.api.rendering.pass.RenderPasses) WorldRenderer(io.xol.chunkstories.api.rendering.world.WorldRenderer) DecalsPass(io.xol.chunkstories.core.rendering.passes.DecalsPass) ApplySunlightPass(io.xol.chunkstories.core.rendering.passes.ApplySunlightPass) PostProcessPass(io.xol.chunkstories.core.rendering.passes.PostProcessPass) BloomPass(io.xol.chunkstories.core.rendering.passes.BloomPass) DefaultSkyRenderer(io.xol.chunkstories.core.rendering.sky.DefaultSkyRenderer) GBuffersOpaquePass(io.xol.chunkstories.core.rendering.passes.GBuffersOpaquePass) SkyPass(io.xol.chunkstories.core.rendering.passes.SkyPass) DefferedLightsPass(io.xol.chunkstories.core.rendering.passes.DefferedLightsPass) EventHandler(io.xol.chunkstories.api.events.EventHandler)

Example 3 with EventHandler

use of io.xol.chunkstories.api.events.EventHandler in project chunkstories-core by Hugobros3.

the class ItemsLogicListener method onDroppedItem.

@EventHandler
public void onDroppedItem(EventItemDroppedToWorld event) {
    // Create an EntityGroundItem and add it to the event
    Location throwLocation = event.getLocation();
    Vector3d throwForce = new Vector3d(0.0);
    // Throw it when dropping it from a player's inventory ?
    System.out.println(event.getInventoryFrom());
    if (event.getInventoryFrom() != null && event.getInventoryFrom().getHolder() != null && event.getInventoryFrom().getHolder() instanceof Entity) {
        System.out.println("from som 1");
        EntityWithInventory entity = ((EntityWithInventory) event.getInventoryFrom().getHolder());
        Location pos = entity.getLocation();
        if (entity instanceof EntityLiving) {
            System.out.println("he l i v e s");
            EntityLiving owner = (EntityLiving) entity;
            throwLocation = new Location(pos.getWorld(), pos.x(), pos.y() + ((EntityPlayer) owner).eyePosition, pos.z());
            throwForce = new Vector3d(((EntityPlayer) owner).getDirectionLookingAt()).mul(0.15 - Math2.clampd(((EntityPlayer) owner).getEntityRotationComponent().getVerticalRotation(), -45, 20) / 45f * 0.0f);
            throwForce.add(((EntityPlayer) owner).getVelocityComponent().getVelocity());
        }
    }
    EntityGroundItem thrownItem = (EntityGroundItem) core.getPluginExecutionContext().getContent().entities().getEntityDefinition("groundItem").create(throwLocation);
    thrownItem.positionComponent.setPosition(throwLocation);
    thrownItem.velocityComponent.setVelocity(throwForce);
    thrownItem.setItemPile(event.getItemPile());
    // EntityGroundItem entity = new EntityGroundItem(core.getPluginExecutionContext().getContent().entities().getEntityDefinitionByName("groundItem"), event.getLocation(), event.getItemPile());
    event.setItemEntity(thrownItem);
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) EntityLiving(io.xol.chunkstories.api.entity.EntityLiving) EntityGroundItem(io.xol.chunkstories.core.entity.EntityGroundItem) Vector3d(org.joml.Vector3d) EntityWithInventory(io.xol.chunkstories.api.entity.interfaces.EntityWithInventory) EntityPlayer(io.xol.chunkstories.core.entity.EntityPlayer) Location(io.xol.chunkstories.api.Location) EventHandler(io.xol.chunkstories.api.events.EventHandler)

Aggregations

EventHandler (io.xol.chunkstories.api.events.EventHandler)3 Location (io.xol.chunkstories.api.Location)1 Entity (io.xol.chunkstories.api.entity.Entity)1 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)1 EntityWithInventory (io.xol.chunkstories.api.entity.interfaces.EntityWithInventory)1 Event (io.xol.chunkstories.api.events.Event)1 EventExecutor (io.xol.chunkstories.api.events.EventExecutor)1 EventListeners (io.xol.chunkstories.api.events.EventListeners)1 RegisteredListener (io.xol.chunkstories.api.events.RegisteredListener)1 PluginCreationException (io.xol.chunkstories.api.exceptions.plugins.PluginCreationException)1 PluginLoadException (io.xol.chunkstories.api.exceptions.plugins.PluginLoadException)1 RenderPasses (io.xol.chunkstories.api.rendering.pass.RenderPasses)1 SkyRenderer (io.xol.chunkstories.api.rendering.world.SkyRenderer)1 WorldRenderer (io.xol.chunkstories.api.rendering.world.WorldRenderer)1 EntityGroundItem (io.xol.chunkstories.core.entity.EntityGroundItem)1 EntityPlayer (io.xol.chunkstories.core.entity.EntityPlayer)1 ApplySunlightPass (io.xol.chunkstories.core.rendering.passes.ApplySunlightPass)1 BloomPass (io.xol.chunkstories.core.rendering.passes.BloomPass)1 DecalsPass (io.xol.chunkstories.core.rendering.passes.DecalsPass)1 DefferedLightsPass (io.xol.chunkstories.core.rendering.passes.DefferedLightsPass)1