Need help with my minigame!

Discussion in 'Plugin Development' started by AnotherRival, Aug 13, 2015.

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

    AnotherRival

    I am creating a mini game, and I have fixed a lot of the bugs (infinate loops and other silly mistakes), but now I am getting stuck because I am no longer getting errors, its just the fact the code isn't working.
    One of the bugs is that my PlayerJoinEvent is not working, when the player joins the game it just gives the normal join message, it does not teleport to the spawn, and Game.setCanStart() is not working at all. Also in my StartCountdown class, when I check if the Game cannot start, the Game never can start because of some other problem. Another Problem I am having is I can't get the GameTime to start. I think if I can get the GameTime to start counting up then my other methods will start working. Also: before you tell me I am in way over my head, and I need to learn java, I am learning java, I have done smaller practice, I would like to finish this game mode as a learning tool.
    This is my PlayerJoin class that listens for the player joining:
    http://hastebin.com/akurufemaq.avrasm
    This is my main class that manages the events and contains other methods:
    http://hastebin.com/reruwetoci.avrasm
    This is my LocationUtilities class which defines the spawn points for the world and game:
    http://hastebin.com/cabekoxiwe.java
    This is my game class where setCanStart is defined:
    http://hastebin.com/doyibogiwo.java
    This is the Class that adds one to the game time every second:
    http://hastebin.com/ukutohimut.java
    If anymore information is needed please let me know!

    Thanks,
    Rival
     
  2. Offline

    CoolDude53

  3. Offline

    SuperOriginal

    Oh.
    Dear.
    God.
    The.
    Static.
     
  4. Offline

    AnotherRival

  5. I can smell TheBcBroz.

    @AnotherRival If you do watch TheBcBroz I and everyone else here will recommend you to stop. He is not a good teacher, he teaches bad practices and people tend not to learn much and just copy him causing them to have many issues and bad practices without the knowledge to fix them.
     
    Synapz likes this.
  6. Offline

    AnotherRival

    I watched him for a few methods yeah.
    As you can see most of the methods aren't from him:
    http://imgur.com/vsLRCtb

    Should I just delete some of the static methods and instead create a new instance of the class and call the methods like that?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  7. Offline

    CraftCreeper6

    @AnotherRival
    Delete the static from everything, privatize all variables and use getters and setters, then use the instance of your class to retrieve information from it.
     
    Synapz likes this.
  8. Offline

    Synapz

    Static methods should only be used as Utils methods, such as Integer#parseInt(String s). As for static finals they are used for constancs, ex. BAN_REASON. For example, static methods are helpful in this way: (it is counting how many jails are created)
    Code:
    public class Jail {
       private static int jails;
    
        public Jail() {
            jailsCount++;
        }
    
    }
    If you did not include the static, a new int of jails would be created every time a jail was created. So each jail would have 1 and never incriment from there.
    Why?
    Because statics are assigned to a class not a object. Object methods and vars are created each time you create an object therefore you can only can use them when you create an object (height, name). Statics are not applies to objects but to classes, which is why they are used all over for Util classes in java (objectCount, parseInt) Why make a Integer object just to parse an it? You dont because you will just be creating unnessary objects. From this you should be able to answer your own question. Hope this helps

    Also here is a good pic I found online:
    http://i.imgur.com/veJm0WF.jpg
    @AnotherRival
     
    Last edited: Aug 14, 2015
  9. Offline

    Hawktasard

    @Synapz
    There's no Math.parseInt?

    @AnotherRival
    You should not do new Game().someMethod() every time you need to call "someMethod" - this is a waste of memory.

    Code:java
    1. public class Something implements Listener {
    2. private Game game;
    3.  
    4. public Something(Game game) {
    5. this.game = game;
    6. }
    7.  
    8. @EventHandler
    9. public void onJoin(PlayerJoinEvent event) {
    10. game.something();
    11. }
    12.  
    13. }
    14.  
    15. public class PluginMainClass extends JavaPlugin {
    16.  
    17. private Game game;
    18.  
    19. @Override
    20. public void onEnable() {
    21. game = new Game();
    22. Bukkit.getPluginManager().registerEvents(new Something(game), this);
    23. }
    24.  
    25. }
     
    Last edited: Aug 14, 2015
  10. Offline

    MisterErwin

    bwfcwalshy likes this.
  11. Offline

    Synapz

    @Hawktasard @MisterErwin
    *face slam* Sorry, I typed this on my ipad and wasnt thinking. I meant Integer.parseInt(). But yes the Math class has tons of statics...
    Math.min
    Math.ceil

    [Edit] Good idea, @bwfcwalshy.
    Bukkit.getOnlinePlayers(), Bukkit.getServer(), Bukkit.broadcast. The whole class is basically statics. Great examples to show you the real purpose of statics
     
    Last edited: Aug 14, 2015
  12. @Synapz @MisterErwin I like how your talking about Java classes and yet this is Bukkit :p Why not talk about the Bukkit class, lots of static methods. Anyway, this is getting offtopic.

    </offtopic>
     
  13. Offline

    AnotherRival

    I have changed all the un needed static classes and I see how dumb it was, I also have replaced making a new instance of a class with just having 1 instance, and I have also gotten rid of the return statement, when I tested, still, the gametime did not start and the game was not set into in game mode

    @CraftCreeper6
    I am getting all kinds of errors when I make the int gameTime private and non static because I made the method
    Code:
        private int gameTime;
       
        public int getGameTime(){
           
            return gameTime;
        }
    whenever I call the method I create a new instance of the class and it asks me to create a constructor when I already have one
    Code:
    public class GameTime extends BukkitRunnable{
       
        NewClass plugin;
        public GameTime(NewClass pl){
            plugin = pl;
           
        }
    
        private int gameTime;
       
        public int getGameTime(){
           
            return gameTime;
        }
       
    
        @Override
        public void run() {
    
            gameTime += 1;
    
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  14. Offline

    CraftCreeper6

    @AnotherRival
    Instance, not Constructor. Is NewClass your main class?
     
  15. Offline

    AnotherRival

    yes

    so what should i do if I want to call the method?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  16. Offline

    Zombie_Striker

    @AnotherRival
    gameTime does not equal anything(null), so you cant add 1 to a null (null does not equal 0/ null is not a number). Set gameTime to a number in some form of initialization method (a method that gets activated only when the class is first created)
     
  17. Offline

    AnotherRival

    Would setting it to 1 work?
     
  18. Offline

    mythbusterma

  19. Offline

    AnotherRival

    It is counting up, if it works if I start it at 1 then im ok with it
     
  20. Offline

    mythbusterma

    @AnotherRival

    You can start at zero as well. If you're counting up.
     
  21. Offline

    AnotherRival

    @mythbusterma this guy just said i cant start at 0
     
  22. Offline

    SuperOriginal

    Nobody said that.
     
  23. Offline

    AnotherRival

  24. Offline

    BrickBoy55

    @AnotherRival Also for your listener, implement the bukkit Listener, not your own. And register your events in your main class.

    EDIT: I saw you registered your events.
     
    Last edited: Aug 15, 2015
  25. Offline

    AnotherRival

    mglistener implements the bukkit listener
     
  26. Online

    timtower Administrator Administrator Moderator

  27. Offline

    AnotherRival

    I created the registerListeners method in the mainclass but it wasnt called anywhere else
     
  28. Offline

    Zombie_Striker

    @AnotherRival
    An initialization method is just a method that gets called once a class is create. It works like the following:
    Code:
    public class StartGame{
    
    int i = 6;
    public StartGame(){
       init();
       System.out.println(i);
    }
    
    public void init(){
       i = 0;
    }
    
    }
    This, when the class is created, will print out the field 'i'. 'init' is the Initiation method that will set i to 0.
     
  29. Online

    timtower Administrator Administrator Moderator

    @AnotherRival Having the method is 1. Now you need to call it in order to function. Where are you doing that?
     
  30. Offline

    AnotherRival

    I did it in onEnable and the listeners are working, now my main problem is this:
    Code:
            if (timeUntilStart == 0) {
                if (!Game.canStart()) {
                    plugin.resetCountDown();
                    ChatUtilities.broadcast(RED + "Cannot start game. Restarting countdown!");
                    return;
                } else if (Game.canStart()) {
                    Game.start();
                    plugin.startGameTime();
                }
            }

    Code:
            int onlineP = 0;
            for (Player pl : Bukkit.getOnlinePlayers()) {
                onlineP += 1;
    
            }
            if (onlineP > 1) {
                plugin.startGameTime();
                Game.setCanStart(true);
            }
    Code:
        public void setCanStart(boolean b){
            canStart = b;
        }
    the if statement at the top wont work!!!
     
Thread Status:
Not open for further replies.

Share This Page