Creating an Explosion [SOLVED]

Status
This thread has been locked.

Plus.

livin
Supreme
Feedback score
16
Posts
311
Reactions
158
Resources
0
Hello, kind users of MC-Market! I am here today requesting help on a current situation I am in.

I am trying to make it, so when you throw the "bomb" (defined in the below code), after 3 seconds, it makes an explosion at the location of the bomb, then sends the "Your bomb exploded" message. But the explosion and the message doesn't seem to work!

Code:
package me.hawauh.prisonbombs;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

public class ActualBombs implements Listener {

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        if (e.getAction() == Action.RIGHT_CLICK_AIR) {
            if (e.getMaterial() == GetBombMethods.getBomb1Item().getType()) {
                p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
                final Item grenade = p.getWorld().dropItem(p.getEyeLocation(), new ItemStack(Material.FIREBALL));
                grenade.setVelocity(p.getLocation().getDirection().multiply(1.9D));
                p.sendMessage("§bYou have thrown a §7Regular Bomb");

                Bukkit.getScheduler().scheduleSyncDelayedTask(new Main(), new Runnable() {
                    public void run() {
                        grenade.getWorld().createExplosion(grenade.getLocation().getX(), grenade.getLocation().getY(),
                                grenade.getLocation().getZ(), 3, false, false);
                        p.sendMessage("§b§lBang! Your bomb exploded!");
                    }
                }, 20*3);
            }

            if (e.getMaterial() == GetBombMethods.getBomb2Item().getType()) {
                p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
                final Item grenade = p.getWorld().dropItem(p.getEyeLocation(), new ItemStack(Material.MAGMA_CREAM));
                grenade.setVelocity(p.getLocation().getDirection().multiply(1.9D));
                p.sendMessage("§bYou have thrown a §7Bunker Buster Bomb");

                Bukkit.getScheduler().scheduleSyncDelayedTask(new Main(), new Runnable() {
                    public void run() {
                        grenade.getWorld().createExplosion(grenade.getLocation(), 2);
                        grenade.getWorld().createExplosion(grenade.getLocation().getX(), grenade.getLocation().getY(),
                                grenade.getLocation().getZ(), 3, false, false);
                        p.sendMessage("§b§lBang! Your bomb exploded!");
                    }
                }, 20*3);
            }

        }
    }

}
[/SPOILER]

Main.Java:
Code:
package me.hawauh.prisonbombs;

import org.bukkit.plugin.java.JavaPlugin;

import me.hawauh.commands.MainCmd;
import me.hawauh.listener.Events;

public class Main extends JavaPlugin {
   

    public static String version = "0.1";

    public void onEnable() {
        saveDefaultConfig();

        System.out.printf("[PrisonBombs] Enabled version " + version);
        getServer().getPluginManager().registerEvents(new Events(), this);
        getServer().getPluginManager().registerEvents(new GUI(), this);
        getServer().getPluginManager().registerEvents(new ActualBombs(), this);
        getCommand("prisonbombs").setExecutor(new MainCmd());

    }

    public void onDisable() {
        saveDefaultConfig();
    }
   
}

Please could somebody help me with this issue, thank you kind friends, :)
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

NoReach__

Feedback score
0
Posts
58
Reactions
16
Resources
0
I suggest doing something along these lines
Code:
if (e.getMaterial() == GetBombMethods.getBomb1Item().getType()) {
//PUT STUFF HERE
}else{
//Log or send am message as the it did not meet the condition
}
Also instead of
Code:
 e.getMaterial();
just try
Code:
 e.getItemInHand().getType();
 

Plus.

livin
Supreme
Feedback score
16
Posts
311
Reactions
158
Resources
0
I suggest doing something along these lines
Code:
if (e.getMaterial() == GetBombMethods.getBomb1Item().getType()) {
//PUT STUFF HERE
}else{
//Log or send am message as the it did not meet the condition
}
Also instead of
Code:
 e.getMaterial();
just try
Code:
 e.getItemInHand().getType();
This does not relate to the OP, could you help me with the original post?
 

NoReach__

Feedback score
0
Posts
58
Reactions
16
Resources
0
This does not relate to the OP, could you help me with the original post?
The reason I'm saying that is to check to see if that if statement is actually being met. (If not then something would happen in the else statement).
 

Tyler

Developer
Supreme
Feedback score
14
Posts
2,589
Reactions
2,238
Resources
0
Are you getting any errors?
 
Last edited:

Plus.

livin
Supreme
Feedback score
16
Posts
311
Reactions
158
Resources
0
What is on line 93 of 'ActualBombs'?

grenade.getWorld().createExplosion(grenade.getLocation().getX(), grenade.getLocation().getY(),
grenade.getLocation().getZ(), 5, false, true);
 

Tyler

Developer
Supreme
Feedback score
14
Posts
2,589
Reactions
2,238
Resources
0
grenade.getWorld().createExplosion(grenade.getLocation().getX(), grenade.getLocation().getY(),
grenade.getLocation().getZ(), 5, false, true);
You are using a sync task right? You didn't change the code from your thread?
 

Plus.

livin
Supreme
Feedback score
16
Posts
311
Reactions
158
Resources
0
You are using a sync task right? You didn't change the code from your thread?
I'm using

new BukkitRunnable() {

public void run() {
p.sendMessage("§b§lBoom! Your bomb exploded!");
grenade.getWorld().createExplosion(grenade.getLocation().getX(), grenade.getLocation().getY(),
grenade.getLocation().getZ(), 5, false, true);
}
}.runTaskLaterAsynchronously(JavaPlugin.getPlugin(Main.class), 40);
 

Pyrazine

Software Engineer
Supreme
Feedback score
1
Posts
205
Reactions
68
Resources
0
I'm using

new BukkitRunnable() {

public void run() {
p.sendMessage("§b§lBoom! Your bomb exploded!");
grenade.getWorld().createExplosion(grenade.getLocation().getX(), grenade.getLocation().getY(),
grenade.getLocation().getZ(), 5, false, true);
}
}.runTaskLaterAsynchronously(JavaPlugin.getPlugin(Main.class), 40);
If you really think you can do anything with Bukkit asynchronously, I think you should probably stick to those "Plugin Configurations". Run it synchronously and it will work, it literally says so in the error:
Code:
       Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
            @Override
            public void run() {
                // Explosion code here!
            }
        }, 40L);
 

Tyler

Developer
Supreme
Feedback score
14
Posts
2,589
Reactions
2,238
Resources
0
I'm using

new BukkitRunnable() {

public void run() {
p.sendMessage("§b§lBoom! Your bomb exploded!");
grenade.getWorld().createExplosion(grenade.getLocation().getX(), grenade.getLocation().getY(),
grenade.getLocation().getZ(), 5, false, true);
}
}.runTaskLaterAsynchronously(JavaPlugin.getPlugin(Main.class), 40);
As said above, don't use async tasks. The error says you are trying to remove blocks asynchronously.

Also to get an instance of your main class you should use something like:
Code:
private Main plugin;
public CurrentClass(Main plugin) {
    this.plugin = plugin;
}
Current class would of course be the class you are in, and Main would be the name of your main class. Then you can access it with plugin.<method> from that class, or in your case with the tasks you just use the variable 'plugin'.
 

Plus.

livin
Supreme
Feedback score
16
Posts
311
Reactions
158
Resources
0
If you really think you can do anything with Bukkit asynchronously, I think you should probably stick to those "Plugin Configurations". Run it synchronously and it will work, it literally says so in the error:
Code:
       Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
            @Override
            public void run() {
                // Explosion code here!
            }
        }, 40L);
It's not the runnable that's the problem, the other code in the run() { method works, it's just the explosion.
 

Plus.

livin
Supreme
Feedback score
16
Posts
311
Reactions
158
Resources
0
But your error specifically states the Runnable is the issue?
I don't know, does it?[DOUBLEPOST=1497701062][/DOUBLEPOST]Solved, I changed it to runTaskLater, thanks for your help guys!
 
Last edited:
Status
This thread has been locked.
Top