Cause damage in Intervals?

Discussion in 'Plugin Development' started by Orcem12, May 21, 2012.

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

    Orcem12

    I don't have code at the moment just some snippets. I've tried making cooldowns to cause damage, scheduler, and other various methods of causing damage in intervals. So far I'm unsuccessful. I've tried this:
    Code:
                plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                    public void run() {
                        if(plugin.HelmetNull.contains(ply)){
                            ply.damage(plugin.getConfig().getInt("int_Damage"));
                            if(ply.isDead()){
                                plugin.HelmetNull.remove(ply);
                            }
                        }
                    }
                }, 60L);
    But that doesn't run but returns a null pointer...

    Any suggestions?

    Background info on Method:
    • ply is the parameters for this method (Player ply)
    • HelmetNull is a player Array.
    Error:
    Code:
    19:50:54 [SEVERE] Could not pass event PlayerMoveEvent to Contaminate
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:303)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:459)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:209)
            at net.minecraft.server.Packet10Flying.handle(SourceFile:126)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
            at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
            at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:7
    8)
            at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:558)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:450)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
            at Orcem.Block.Contamination.AirFactory.NullHelmet(AirFactory.java:13)
            at Orcem.Block.Contamination.PlayerListener.onPlayerMove(PlayerListener.
    java:32)
            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:301)
            ... 10 more
    >
     
  2. Offline

    Ranzdo

    We need the soruce of the whole file to see where line 13 in Airfactory.java. It is there where the nullpointer is thrown from.
     
  3. Offline

    Orcem12

    I understand this, I tried to provide all the information needed because I've changed the code somewhat.
    I'll try to provide everything:
    Code:java
    1.  
    2. package Orcem.Block.Contamination;
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.entity.Player;
    6.  
    7. public class AirFactory {
    8. private Contaminate plugin;
    9. public AirFactory(Contaminate plugin){
    10. this.plugin = plugin;
    11. }
    12. public void NullHelmet(final Player p){
    13. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    14. public void run() {
    15. if(plugin.HelmetNull.contains(p)){
    16. p.damage(plugin.getConfig().getInt("int_Damage"));
    17. //p.sendMessage(ChatColor.DARK_RED+"");
    18. if(p.isDead()){
    19. plugin.HelmetNull.remove(p);
    20. }
    21. }
    22. }
    23. }, 60L);
    24. }
    25.  

    Player Listener:
    Code:java
    1.  
    2. ---
    3.  
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.PlayerMoveEvent;
    9.  
    10. public class PlayerListener implements Listener {
    11. /*
    12.   * -
    13.   *
    14.   * -
    15.   *
    16.   */
    17. private - plugin;
    18. public PlayerListener(Contaminate plugin){
    19. this.plugin = plugin;
    20. }
    21. AirFactory AirF = new AirFactory(plugin);
    22. @EventHandler
    23. public void onPlayerMove(PlayerMoveEvent e){
    24. final Player p = (Player)e.getPlayer();
    25. //Check which list is being used.
    26. if(plugin.getConfig().getBoolean("-")){
    27. if(plugin.getConfig().getIntegerList("-").contains(Material.AIR.getId())){
    28. if(plugin.getConfig().getBoolean("-")){
    29. if(p.getInventory().getHelmet() == null){
    30. if(plugin.getConfig().getInt("int_Damage") != 0){
    31. if(!plugin.HelmentNull.contains(p)){
    32. plugin.HelmentNull.add(p);
    33. AirF.NullHelmet(p);
    34. }
    35. }
    36.  
     
  4. Offline

    Ranzdo

    If you have modified the soruce, can you paste your current stack-trace?
     
  5. Offline

    Orcem12

    Ctrl-z is good friend of mine. I posted Listener as well where the error came from.
     
  6. Offline

    Ranzdo

    So, sorry if I do not follow, but where is the error? Is it still on line 13 in Airfactory.java? Or do you have a new stack-trace I can see?
     
  7. Offline

    Orcem12

    Correct, I restored the old code before I started messing with it. I apologize for the confusion. Every line in Stack Trace matches the code provided.
     
  8. Offline

    Ranzdo

    Ah, ok, then I know why it does not work.

    The plugin reference in Airfactory.java is null, becouse the reference you pass to create the instance is null (the plugin variable). Let me show what is wrong and how to fix it.

    In the listener class change
    Code:java
    1.  
    2.  
    3. public PlayerListener(Contaminate plugin){
    4. this.plugin = plugin;
    5. }
    6. //This is run before the constructor is run, therefore the passed reference (plugin) is still null.
    7. AirFactory AirF = new AirFactory(plugin);
    8.  
    9.  


    to


    Code:java
    1.  
    2. AirFactory AirF;
    3.  
    4. public PlayerListener(Contaminate plugin){
    5. this.plugin = plugin;
    6. //Here we have assigned the plugin reference, so now we can create the AirFactory instance and pass a reference that is not null
    7. this.AirF = new AirFactory(plugin);
    8. }
    9.  
    10.  
     
    Orcem12 likes this.
  9. Offline

    Orcem12

    @Ranzdo
    You sir, are Java god! Thank you so much! :) I can't believe how simple some errors really can be...
     
Thread Status:
Not open for further replies.

Share This Page