Java plugin in two Class.

Discussion in 'Plugin Development' started by _SavaLLL_, Feb 12, 2014.

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

    _SavaLLL_

    I want create small plugin.
    It`s my main:
    Code:java
    1. package blockspawn;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5.  
    6.  
    7.  
    8. public class Main extends JavaPlugin {
    9.  
    10. ClassBlock respawn;
    11.  
    12. public void onEnable() {
    13. Bukkit.getServer().getPluginManager().registerEvents(this, ClassBlock());
    14. respawn = new ClassBlock(this);
    15. }
    16. }


    It`s my other Class:
    Code:java
    1. package blockspawn;
    2.  
    3. import org.bukkit.Material;
    4. import org.bukkit.block.Block;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.BlockBreakEvent;
    9. import org.bukkit.inventory.ItemStack;
    10. import org.bukkit.inventory.PlayerInventory;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12. import org.bukkit.scheduler.BukkitRunnable;
    13.  
    14. public class ClassBlock extends JavaPlugin implements Listener{
    15.  
    16. Main main;
    17. public ClassBlock(Main main) {
    18. this.main = main;
    19. }
    20.  
    21. @EventHandler
    22. public void onBlockBreak(BlockBreakEvent event){
    23. final Block origin = event.getBlock();
    24. @SuppressWarnings("deprecation")
    25. final int type = origin.getTypeId();
    26. if (type == 15) {
    27. event.setCancelled(true);
    28. origin.setType(Material.COBBLESTONE);
    29. Player p = event.getPlayer();
    30. PlayerInventory pi = p.getInventory();
    31. ItemStack IronOre = new ItemStack(Material.IRON_ORE, 1);
    32. pi.addItem(IronOre);
    33. new BukkitRunnable() {
    34. public void run() {
    35. origin.setType(Material.IRON_ORE);
    36. }
    37. }.runTaskLater(this, 300);
    38. }
    39. }
    40. }

    If you do not import JavaPlugin Event will not work.
    And if you import JavaPlugin Event does not register.
    What to do?
     
  2. Offline

    Timbals

    The solution is simple

    Add this to your constructor in the ClassBlock class
    Code:java
    1. Main main;
    2. public ClassBlock(Main main) {
    3. this.main = main;
    4. Bukkit.getPluginManager().registerEvents(this, main);
    5. }


    And remove that extends JavaPlugin
    Code:java
    1. public class ClassBlock implements Listener {


    And in your onEnable you should change this:
    Code:java
    1. public void onEnable() {
    2. respawn = new ClassBlock(this);
    3. }


    By the way this is really basic stuff about events
     
  3. Offline

    DevRosemberg

    Ewwwwwwwww, why the hell are you doing that, just remove the ClassBlock(Main main) from the classblock class then remove te extends javaplugin and simply register it by doing:

    PluginManager.registerEvents(this, new ClassBlock());
     
  4. Offline

    Tirelessly

    DevRosemberg So that he can access his main class from his listener.
     
  5. Offline

    DevRosemberg

  6. Offline

    Tirelessly

  7. Offline

    DevRosemberg

    Tirelessly just saying but nt everything has to have OOP.
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    Java by design to be an Object-Oriented language. If you are doing almost anything in Java you are in OOP land.
     
    DevRosemberg likes this.
  9. Offline

    mazentheamazin

    Timbals
    Heres another method that I use:
    1. Add a line to your Main class that is:
    Code:java
    1. Main plugin;

    2. Now add this to your onEnable. This will define the variable we just created:
    Code:java
    1. plugin = this;

    3. Now add this to your onDisable:
    Code:java
    1. plugin = null;

    4. Add this method to the class. The name of it doesn't matter, you can use getInstance, getPlugin, or anything!
    Code:java
    1. public static Main getPlugin() {
    2. return plugin;
    3. }

    5. Now you can reference the main class by doing this:
    Code:java
    1. Main.getPlugin()
    2. // If you want to get the config
    3. Main.getPlugin().getConfig()

    Then we're done! You can now use methods from JavaPlugin in other classes!
     
  10. Offline

    Harmings

    mazentheamazin
    Don't forget to set that instance to null when the server disables
     
  11. Offline

    mazentheamazin

Thread Status:
Not open for further replies.

Share This Page