[solved]Explosion Types

Okay so a Bit of a issue here. I’ve gotten creepers to stop exploding by using the getCause.first(Creeper.class) however now I’m trying to do something similar with ghasts…however when i add either or all three classes of LargeFireball, SmallFireball and Ghast blocks still are exploding…help?

here’s the code that’s not working :smile:

         if (event.getCause().first(Ghast.class).isPresent()){
            if (ConfigurationManager.getInstance().getConfig().getNode("Explosions", "Ghast").getValue().equals("true")){
                event.setCancelled(true);
            }
            if (ConfigurationManager.getInstance().getConfig().getNode("Explosions", "Ghast").getValue().equals("false")){
                event.setCancelled(false);
            }
        }

Right, so when debugging causes I always find it useful to print out the cause.toString() to see what it actually contains. Now, if you want to prevent damage from all Fireballs you could do something like

event.getCause().first(LargeFireball.class).isPresent()

alternatively, if you are just trying to stop Ghasts then if the explosion originated form a Ghast then it’s somewhere in the Cause. Which we can check by calling

event.getCause().any(Ghast.class)

this method returns a boolean, so you don’t have to check if it is present or not.

hahahaha amazing! thank you

I think you might have switched the setCancelled() values. If an event is cancelled, it stops the explosion from occuring. Your code prevents explosions if the configuration value is true. So if you want a true value to allow explosions, you would use this code:

if (ConfigurationManager.getInstance().getConfig().getNode("Explosions", "Ghast").getValue().equals("true")){
    event.setCancelled(false);
}
if (ConfigurationManager.getInstance().getConfig().getNode("Explosions", "Ghast").getValue().equals("false")){
    event.setCancelled(true);
}

Ok so tried the .any(Ghast.class){
}
However still not stopping the explosings from a ghast fireball

What’s the toString() of the cause look like?

        if (event.getCause().any(Ghast.class)){
            if (ConfigurationManager.getInstance().getConfig().getNode("Explosions", "Ghast").getString().equalsIgnoreCase("true")){
                event.setCancelled(true);
            }
            if (ConfigurationManager.getInstance().getConfig().getNode("Explosions", "Ghast").getString().equalsIgnoreCase("false")){
                event.setCancelled(false);
            }
        }
    }

That’s the Whole part of the main class that i’m trying to make work I already know the config is correct just trying to find out why the projectiles from the ghast won’t stop breaking blocks on impact

So we want to be able to see what the Cause actually contains, in order to do that we should print out the contents of the Cause to the console, for that we are going to need a Logger. You can read here about how to set up logging for a plugin. Once that’s done it should be as simple as calling

getLogger().info(event.getCause().toString());

inside of your listener at the top and looking at the console to see what it prints out, the string it prints represents what’s actually inside the Cause and what you can check for.

Also for future reference the Sponge javadocs located here are your best source of information regarding all of the different methods you can call. Think of it as a guide to all the tools in the sponge toolbox. Using the javadocs I can for instance look up Cause and see all the different methods I can call on it, like any() :stuck_out_tongue_winking_eye:

ok so here’s what the console says

Hmm, so we can see here why it’s not detecting the Ghast as the only thing the cause contains is an Air block. My guess is either the cause for Fireball explosions is bugged, or hasn’t been implemented yet. In which case you’ll have to wait for the implementation to complete. You could create an issue on github to let the devs know you’re waiting on the cause implementation.

1 Like

Thanks for the insight :slight_smile: and i will probably use the cause for quite some time thank you

just a heads up to anyone reading this form the Ghast Type has been added since this was last posted.