Solved use this.plugin inside a static method?

Discussion in 'Plugin Development' started by minepress, Aug 19, 2014.

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

    minepress

    Hi all,

    Getting a bit confused as to how I can execute this.plugin = plugin; inside a static method.

    Code:java
    1. public class Monster {
    2.  
    3. //Include this.plugin
    4. private Minevolve plugin;
    5.  
    6. public Monster(Minevolve plugin) {
    7. this.plugin = plugin; // Store the plugin in situations where you need it.
    8. }
    9.  
    10. public static void monsterLevel(int level, Player p) {
    11.  
    12. Disguise monster1 = new Disguise(Minevolve.dcAPI.newEntityID(),"LEVEL:" + level, DisguiseType.Cow);
    13. Disguise monster2 = new Disguise(Minevolve.dcAPI.newEntityID(),"LEVEL:" + level, DisguiseType.MushroomCow);
    14. Disguise monster3 = new Disguise(Minevolve.dcAPI.newEntityID(),"LEVEL:" + level, DisguiseType.Wither);
    15. Disguise monster4 = new Disguise(Minevolve.dcAPI.newEntityID(),"LEVEL:" + level, DisguiseType.EnderDragon);
    16.  
    17. EntityRegainHealthEvent healthboost = new EntityRegainHealthEvent(p, 20.0, RegainReason.REGEN);
    18. double playercount = Bukkit.getOnlinePlayers().length;
    19. switch (level) {
    20. case 1:
    21. healthboost.setAmount(25 * playercount);
    22. this.plugin.dcAPI.disguisePlayer(p, monster4);
    23. p.sendMessage(ChatColor.AQUA + "Congratulations. You have Evolved." + level);
    24. p.sendMessage(ChatColor.AQUA + "This is your final form. You're now Bigger and as a result slower.");
    25. p.sendMessage(ChatColor.AQUA + "But you are MUCH Stronger.");
    26.  
    27. return;


    This is a SMALL snippet. I'm getting the error on this.plugin.dcAPI.disguisePlayer(p, monster4); Cannot use in static context.

    I would appreciate an explanation :) Thanks guys
     
  2. minepress You can't. I'd recommend you don't use static if you don't know what it does. :)
     
  3. Offline

    minepress


    I know what static means. But I can't think of a logical way to tackle this.

    edit: I had the include as private -.- made public
     
  4. Offline

    Lactem

    Put your monsterLevel() method inside a different class, pass the plugin instance through the constructor, remove static, and then you can just call monsterLevel() with an instance of your new class. If you only want one instance of that class, then just use a singleton instance.
     
    minepress likes this.
  5. Offline

    stormneo7

    Well if you know what static means why don't you make the plugin static?...
     
    KingFaris11 likes this.
  6. Offline

    minepress


    Just turning it static doesn't work. I tried already, listening to the IDE isn't always the solution. Lactem explained it Perfectly.
     
  7. Offline

    Lactem

    Glad I could help! You should mark the thread as "solved" at the top.
     
  8. Offline

    minepress

    Thanks Lactem, Instances was actually one of the first things I learnt when going through tutorials on Oracle. It slipped my mind. Appreciate it.

    Changed to solved.
     
  9. Offline

    _Filip

    minepress Yep, that silly core java concept is always slipping peoples' minds, isn't it?
     
  10. Offline

    minepress

    Yep that cocky waste of a post from an ego driven Java Dev is really appropriate, isn't it?
     
  11. minepress He was just trying to make the point that it's jot really the sort of thing that just 'slips your mind', so perhaps you don't have enough experience and should learn more of the Java basics :)
     
  12. Offline

    _Filip

Thread Status:
Not open for further replies.

Share This Page