Static methods using a non-static abstract object's method?

Discussion in 'Plugin Development' started by unenergizer, Apr 4, 2014.

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

    unenergizer

    Hello BukkitDev!

    My name is Andrew and I have been programming for a little more than 15 months. I am starting to really grasp the basics and create little programs that work and look polished. However I have ran into something new today that I can't find much on google about.

    I took 2 weeks to learn about a few different design patterens after creating this thread:
    http://forums.bukkit.org/threads/re...amework-not-intended-for-live-servers.245115/

    I rewrote all the code from scratch again and I remade this version 100% event driven and I am also using the singleton pattern.

    Everything was going great, I have a fully featured lobby system that works great, but when I started to create mini-games is where things went south. I created an absctract minigame class. The subclass has methods and variables that setup the games and the kit contents. That code works fine, however I can Not create an object of my minigames in my instance classes.

    Example:
    Code:java
    1.  
    2. package com.minepile.mpmg;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.entity.Player;
    6.  
    7. public class Arena {
    8.  
    9. static Arena arenaInstance = new Arena();
    10. @SuppressWarnings("unused")
    11. private MPMG plugin;
    12.  
    13. public static Arena getInstance() {
    14. return arenaInstance;
    15. }
    16.  
    17. public void setup(MPMG plugin) {
    18. this.plugin = plugin;
    19. }
    20.  
    21. public static void startGameSetup() {
    22. //Code goes here that selects the game to play
    23.  
    24. //Now begin game setup
    25. MiniGame miniGame = new TeamDeathMatch();
    26. miniGame.setupGame();
    27.  
    28. }
    29.  
    30. }
    31.  


    In the above code, the class can not use the following code:


    Code:java
    1. MiniGame miniGame = new TeamDeathMatch();
    2. miniGame.setupGame();


    How can I get around this? I googled and someone mentioned recursion. I know there has to be a better way of fixing this problem. If you have a better way logically to create the minigame classes then please share. I do not need code, just logic. Thank you for your help and insight.

    -une
     
  2. Offline

    Garris0n

    I'm not really sure what you're getting at by this without being able to see the minigame and teamdeathmatch classes.
     
  3. Offline

    RawCode

    You did something wrong inside your minigame classes, static methods disallow to invoke nonstatic methods only of same class, there is no limit for other classes.
     
  4. Offline

    unenergizer


    The classes that actually make up game code, are non-static.
    The classes that handle tracking, events, and universal data, are static.
    My arena class, which is static, refuses to make a non-static object.

    What are my options to get around this restriction?

    My fear right now, is I am not seeing a way to dynamically load in minigames. As I was writing the code for it, I was not aware of this restriction. My original plan does not work, because I can not use a method from a non-static class. I only want to load mini-game code into memory when the game is actually needed. This includes the maps for the games aswell. I don't want to keep them loaded into memory.

    Hopefully this helps anyone understand what is going on. Thanks for the help.
     
  5. Offline

    unenergizer

    Okay I was able to solve this by not making my parent class abstract. I forgot that you could extend a class that wasn't abstract. I always manage to forget the little things, lol :D Thanks anyways guys.
     
Thread Status:
Not open for further replies.

Share This Page