"plugin" is being returned as null

Discussion in 'Plugin Development' started by MavenDisrrpt, Aug 28, 2021.

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

    MavenDisrrpt

    I think the issue is that I am referencing plugin incorrectly. To explain what I mean by plugin, here is an example of what I am trying to reference externally. I have tried to debug this myself but I am stumped, it's probably something obvious but I just need another pair of eyes. Thank you heaps in advanced.

    Code:
    getServer().getPluginManager().registerEvents(new EventThing(), >> this <<) // where "this" is the plugin.
    Here is a overview of the file structure related to this post.

    src/main/java/me/mavendisrrpt/
    -> App.java (main)

    -> Listeners
    --->BlockRegen.java

    -> Tasks
    --->RegenerateBlockTask.java

    Quick context of the project: when mine ore, ore change to bedrock, then after 5 seconds it changes back to the original ore.

    Here is the Listener for when the block is broken.

    Code:
    import me.mavendisrrpt.App; // main file
    import me.mavendisrrpt.Tasks.RegenerateBlockTask;
    
    import a.bunch.of.bukkit.stuff
    
    public final class BlockBreakListener implements Listener {
    
        App plugin;
    
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            this.plugin = plugin;
            Player player = event.getPlayer();
            Block block = event.getBlock();
            Material blockType = block.getType();
    
            switch (blockType) {
                case IRON_ORE:
                    block.setType(Material.BEDROCK);
                    player.getInventory().addItem(new ItemStack(Material.IRON_ORE, 1));
                    event.setCancelled(true);
                    System.out.println(plugin); // returning NULL
                    // BukkitTask task = new RegenerateBlockTask(plugin, block).runTaskLater(plugin, 100L);
                    break;
           
                default:
                    break;
            }
    
        }
    
    }
    Here is the main file

    Code:
    package me.mavendisrrpt;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.mavendisrrpt.Commands.TestCommand;
    import me.mavendisrrpt.Listeners.BlockRegen;
    
    
    public class App extends JavaPlugin {
        @Override
        public void onEnable() {
            getCommand("test").setExecutor(new TestCommand());
            getServer().getPluginManager().registerEvents(new BlockBreakListener(), this);
        }
        @Override
        public void onDisable() {
            // potatoes are tasty
        }
    }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @MavenDisrrpt And where did you initialize plugin in the BlockBreakListener?
    Add a constructor for it.
     
Thread Status:
Not open for further replies.

Share This Page