Solved Trying to remove a block after amount of time being placed.

Discussion in 'Plugin Development' started by Calebizzthaman, Aug 2, 2017.

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

    Calebizzthaman

    Hi, in my plugin I'm adding a feature to where players are allowed to place cobwebs down. I want to make it to where after a certain amount of time the cobwebs are removed. Can someone help me?

    Code:
        @EventHandler (priority = EventPriority.HIGHEST)
        public void onPlaceCobWeb(BlockPlaceEvent e)
        {
            Material material = e.getBlock().getType();
            Location loc = e.getBlock().getLocation();
    
           
            if(material == Material.WEB)
            {
                e.setCancelled(false);
               
            }
        }
     
  2. @Calebizzthaman
    When a cobweb is placed, schedule a BukkitRunnable to be executed at a later date, and then remove the cobweb inside that runnable.
     
  3. Offline

    Calebizzthaman

    How would I go about doing that in this kind of situation?
     
  4. @Calebizzthaman
    Code:java
    1. new BukkitRunnable() {
    2. @Override
    3. public void run() {
    4. // Your code to remove the block
    5. }
    6. }.runTaskLater(plugin, yourDelay);
     
  5. Offline

    johnny boy

    May I add "yourDelay" is in milliseconds, so for one second you would put:
    Code:Java
    1.  
    2. .ranTaskLater(plugin, 1000);
    3.  

    for 1 second.
     
  6. @MemeplexOwner
    Actually, it's not in milliseconds. It's in gameticks, which are equivalent to twentieths of a second. One second would be '20'.
     
  7. Offline

    johnny boy

    Ah, just checked the docs, and it is game ticks, isn't 20 game ticks 1 second or something? I'm not really too sure.
     
  8. Offline

    Calebizzthaman

    I don't quite understand. Would I place it in my if statement in the listener?
     
  9. Offline

    Calebizzthaman

    So, when I try this. It sends me a whole bunch of errors. Here is my code.
    Btw: disregard my event "onPlaceMedicineItems"

    Code:
    public class BlockPlacing implements Listener{
       
        Plugin plugin;
       
        //Stops players from placing items when using medicine items
        @EventHandler
        public void onPlaceMedicineItems(BlockPlaceEvent e)
        {
            Player p = e.getPlayer();
            Material material = e.getBlock().getType();
           
            if(!p.isOp())
            {
                if(material == Material.YELLOW_FLOWER)
                {
                    e.setCancelled(true);
                }
            }
        }
       
        @EventHandler (priority = EventPriority.HIGHEST)
        public void onPlaceCobWeb(BlockPlaceEvent e)
        {
            Material material = e.getBlock().getType();
    
    
            if(material == Material.WEB)
                {
                e.setCancelled(false);
                e.getPlayer().sendMessage(ChatColor.GREEN + "You placed barbed wire!");
    
                new BukkitRunnable() {
                    @Override
                    public void run() {
                        e.getBlock().setType(Material.AIR);
                    }
                }.runTaskLater(plugin, 60);
               
                }
            }
     }
     
  10. Offline

    Calebizzthaman

    It says Plugin cannot = null but, it doesn't...

    @AlvinB

    [10:25:25 ERROR]: Could not pass event BlockPlaceEvent to ZombieApocalypse v1.0
    org.bukkit.event.EventException: null
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:499) [spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callBlockPlaceEvent(CraftEventFactory.java:149) [spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at net.minecraft.server.v1_12_R1.ItemStack.placeItem(ItemStack.java:207) [spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at net.minecraft.server.v1_12_R1.PlayerInteractManager.a(PlayerInteractManager.java:495) [spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:944) [spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at net.minecraft.server.v1_12_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:37) [spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at net.minecraft.server.v1_12_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:1) [spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_131]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
    at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:405) [spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.IllegalArgumentException: Plugin cannot be null
    at org.apache.commons.lang.Validate.notNull(Validate.java:192) ~[spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.validate(CraftScheduler.java:400) ~[spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.runTaskTimer(CraftScheduler.java:126) ~[spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.runTaskLater(CraftScheduler.java:109) ~[spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at org.bukkit.scheduler.BukkitRunnable.runTaskLater(BukkitRunnable.java:64) ~[spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    at me.aniketus.playerdeathzombiespawn.BlockPlacing.onPlaceCobWeb(BlockPlacing.java:51) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_131]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_131]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.jar:git-Spigot-cd6ba67-a7d074c]
    ... 18 more

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Aug 3, 2017
  11. @Calebizzthaman
    Well, you never set the plugin field to anything, making it be null. Set the field to the instance of your main class, and it should work.
     
  12. Offline

    Calebizzthaman

    @AlvinB Sorry, I know I'm asking a lot. I've just got back into coding with Java and Bukkit recently. Could you give me an example on how to do that?
     
  13. @Calebizzthaman
    Create a constructor for the Listener which takes the plugin instance as an argument, and set the field equal to this argument.
     
  14. Offline

    Calebizzthaman

    @AlvinB
    Code:
        private static Plugin instance;
    ?
     
  15. @Calebizzthaman
    No, don't use static.

    Create a constructor that takes the plugin instance and set 'instance' equal to it.
    Code:java
    1. public MyClass(Plugin plugin) {
    2. this.instance = plugin;
    3. }
    Your Java skills seem to be a little too rusty for your own good. If I were you, I'd go back and revisit some of the tutorials.
     
  16. Offline

    Calebizzthaman

    You're correct. I'm gonna do that! Thanks a lot for the help! It's working now. :)
     
Thread Status:
Not open for further replies.

Share This Page