Solved Throwable Items

Discussion in 'Plugin Development' started by HelGod, Mar 25, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    HelGod

    How would i make a slimeball throw able and when it lands it should explode. i am pretty sure it can be done with the playerinteractevent when someone right clicks the item. Also how would i create an explosion where the slimeball lands?
     
  2. Offline

    caseif

    This isn't possible, as it would require a separate Slimeball class. I'm not sure if you could do this with a modified version of Craftbukkit, but with vanilla Bukkit, it's not doable.
     
  3. Offline

    chasechocolate

    I would make a scheduler to check if the block one block below the slimeball isn't air, then remove the drop and make an explosion. For throwing items, check this thread.
     
  4. Offline

    Ivan

    You can use eggs or any other projectile, so you could use projectilehitevent to get notified on impact.
     
  5. Offline

    HelGod

    This code makes the slimeball throwable but there is no explosion :( can someone help?
    Code:
    @EventHandler
        public void slimeGrenade(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            World world = player.getWorld();
                if (player.getItemInHand().getType() == Material.SLIME_BALL) {
                    if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_AIR) {
                        final Item grenade = world.dropItem(player.getEyeLocation(), new ItemStack(Material.SLIME_BALL));
                        grenade.setVelocity(player.getEyeLocation().getDirection());
                        new BukkitRunnable(){
                            @Override
                            public void run(){
                                grenade.remove();
                                grenade.getWorld().createExplosion(grenade.getLocation(), 5F);
                            }                       
                        }.runTaskLater(plugin, 100L);
                    }
                }
            }
        }
    Bump! Can someone help me?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  6. Offline

    TheButlah

    Save the entity I'd of the fireball somewhere, schedule a synchronous delayed task for however long you want the fuse to be, and in that task get the entity representing the fireball using the ID you just saved, and create an explosion (it's something like world.createExplosion(location))
     
  7. Offline

    HelGod

    I am still new to java and i know what a delayed task is but i don't know how to set it up :p

    Edit: I tried and it threw out errors i tried these task
    Code:
    new BukkitRunnable(){
    @Override
    public void run(){
    drop.remove();
    drop.getWorld().createExplosion(drop.getLocation(), 5F);
    Code:
    plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() {
     
                                    @Override
                                    public void run() {
                                        grenade.getWorld().createExplosion(grenade.getLocation(), 5F);
                                        grenade.remove();
                                    }
                                }, 40L);
    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  8. Offline

    ZeusAllMighty11

    Well in the original code, you were removing the entity and then trying to get the world of the nonexistant entity.

    what errors you getting?
     
  9. Offline

    HelGod

    Code:
    20:28:44 [SEVERE] Could not pass event PlayerInteractEvent to Grenades
    v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.TimedRegisteredListener.callEvent(TimedRegisteredLi
    stener.java:26)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:479)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:464)
            at org.bukkit.craftbukkit.v1_5_R1.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:178)
            at org.bukkit.craftbukkit.v1_5_R1.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:148)
            at net.minecraft.server.v1_5_R1.PlayerConnection.a(PlayerConnection.java
    :615)
            at net.minecraft.server.v1_5_R1.Packet15Place.handle(SourceFile:58)
            at org.spigotmc.netty.NettyNetworkManager.b(NettyNetworkManager.java:174
    )
            at net.minecraft.server.v1_5_R1.PlayerConnection.d(PlayerConnection.java
    :113)
            at net.minecraft.server.v1_5_R1.ServerConnection.b(SourceFile:35)
            at org.spigotmc.netty.NettyServerConnection.b(NettyServerConnection.java
    :66)
            at net.minecraft.server.v1_5_R1.MinecraftServer.r(MinecraftServer.java:5
    82)
            at net.minecraft.server.v1_5_R1.DedicatedServer.r(DedicatedServer.java:2
    29)
            at net.minecraft.server.v1_5_R1.MinecraftServer.q(MinecraftServer.java:4
    71)
            at net.minecraft.server.v1_5_R1.MinecraftServer.run(MinecraftServer.java
    :403)
            at net.minecraft.server.v1_5_R1.ThreadServerApplication.run(SourceFile:5
    73)
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:203)
            at org.bukkit.craftbukkit.v1_5_R1.scheduler.CraftScheduler.validate(Craf
    tScheduler.java:390)
            at org.bukkit.craftbukkit.v1_5_R1.scheduler.CraftScheduler.runTaskTimer(
    CraftScheduler.java:119)
            at org.bukkit.craftbukkit.v1_5_R1.scheduler.CraftScheduler.runTaskLater(
    CraftScheduler.java:103)
            at me.God_Tenshi.scb.Grenades.onClick(Grenades.java:28)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
            ... 17 more
    >
    That is the error
     
  10. Offline

    Technius

    "plugin" is null.
     
  11. Offline

    _Filip

    Use this.
     
  12. Offline

    HelGod

    i need to cast Plugin when i use this. and there is still the same error and
    Technius
    I know it is null but i don't know how to fix :(
     
  13. Offline

    chasechocolate

    HelGod you are most likely not setting a value to your "plugin" variable. Use this:
    Code:java
    1. private <main class> plugin;
    2. public <this class>(<main class> plugin){
    3. this.plugin = plugin;
    4. }
     
  14. Offline

    HelGod

    This is my main class
    Code:
    public class Main extends JavaPlugin {
     
        public static Main plugin
        public final Grenades grenades = new Grenades(null);
        public final playerListener pl = new playerListener();
        public static Main instance;
     
        public void onEnable() {
            System.out.println("Grenades is Enabled!");
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(grenades, this);
            pm.registerEvents(pl, this);
        }
     
        public void onDisable() {
            System.out.println("Grenades is Disabled!");
        }
    }
    My listener
    Code:
    List<UUID> droppedItems = new ArrayList<UUID>();
        private Main plugin;
       
        public Grenades(Main plugin){
            this.plugin = plugin;
            }
     
        @EventHandler
        public void onClick(PlayerInteractEvent event) {
            plugin = Main.instance;
            Player player = event.getPlayer();
            Inventory playerInv = player.getInventory();
            World world = player.getWorld();
            if (playerInv.contains(new ItemStack(Material.SLIME_BALL, 1))) {
                if (event.getAction() == Action.RIGHT_CLICK_AIR) {
                    final Item grenade = world.dropItem(player.getEyeLocation(),
                            new ItemStack(Material.SLIME_BALL));
                    droppedItems.add(grenade.getUniqueId());
                    grenade.setVelocity(player.getEyeLocation().getDirection());
                    Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
                        public void run() {
                        grenade.getWorld().createExplosion(grenade.getLocation(), 5F);
                        grenade.remove();
                        }
                    }, 40L);
                }
            }
        }
    }
    
     
  15. Offline

    Technius

    Code:
    plugin = Main.instance;
    Main.instance is null. By the way, you do not need to set plugin every single time because you already set plugin inside of the Listener's constructor.
     
  16. Offline

    HelGod

    Oh, ok
     
Thread Status:
Not open for further replies.

Share This Page