3

I am trying to make a text-based Minecraft Server, and I'm having trouble in Mines.java. I want to import the integer 'money' into [you'll see the 7-slash comments] in Mines.java, from Main.java. I tried import [package].Main.main.money, but that didn't work.

I am using Mars.1 Eclipse.

Main.java

// NOTE: Please read 'cc.txt' in src folder before modifying

package com.quaggles.main;

import java.util.Scanner;

public class Main {
    // System.out.println(""); (to copy)
    // System objects
    static boolean running = true;
    static Scanner in = new Scanner(System.in);
    static String input;
    static String username;
    // Game variables
    static int coins = 100;
    static int level = 1;
    // Methods that may come in handy
    public static void PETC() {
        System.out.println("Press enter to continue...");
        try {
            System.in.read();
        } catch (Exception e) {
            // Handle any exceptions
        }
    }
    // Doesn't work in java console (sorry Eclipse users) (still testing)
    public static void clearConsole() {
        try {
            final String os = System.getProperty("os.name");

            if (os.contains("Windows")) {
                Runtime.getRuntime().exec("cls");
            } else {
                Runtime.getRuntime().exec("clear");
            }
        } catch (final Exception e) {
            // Handle any exceptions
        }
    }

    public static void main(String[] args) {
        USER: while (running) {
            System.out.println("What is your username:");
            input = in.nextLine();
            if (input.equals("")) {
                System.out.println("You typed nothing! Please try again.");
                PETC();
                clearConsole();
                continue USER;
            } else {
                username = input;
                break USER;
            }
        }
        clearConsole();
        System.out.println("Running on " + System.getProperty("os.name") + "\n");
        System.out.println("Welcome to...");
        System.out.println("   _____                           _____            __ _");
        System.out.println("  / ____|                         / ____|          / _| |");
        System.out.println(" | (___   ___ _ ____   _____ _ __| |     _ __ __ _| |_| |_");
        System.out.println("  \\___ \\ / _ \\ '__\\ \\ / / _ \\ '__| |    | '__/ _` |  _| __|");
        System.out.println("  ____) |  __/ |   \\ V /  __/ |  | |____| | | (_| | | | |_");
        System.out.println(" |_____/ \\___|_|    \\_/ \\___|_|   \\_____|_|  \\__,_|_|  \\__|\n");
        System.out.println("    v0.0.1 beta");
        System.out.println("------------------------------------------");
        System.out.println("ServerCraft is a project made by Quaggle\n" + "that tries to answer the question:\n"
                + "What if a Minecraft server was text based?");
        System.out.println("------------------------------------------\n");
        PETC();
        clearConsole();
        System.out.println("------------------------------------------");
        GAME: while (running) {
            input = in.nextLine().toLowerCase();
            if (input.equals("exit")) {
                break GAME;
            } else if (input.equals("help")) {
                clearConsole();
                System.out.println("HELP");
                System.out.println("------------------------------------------");
                System.out.println("help - shows all* commands");
                System.out.println("stats - shows your stats");
                System.out.println("updates - show any updates on the game");
                System.out.println("mine - go to the mines");
                System.out.println("home");
                System.out.println("exit - quits the game :(\n");
                System.out.println("*not actually all - find them out yourself ;)");
                PETC();
                clearConsole();
                continue GAME;
            } else if (input.equals("stats")) {
                System.out.println("STATISTICS");
                System.out.println("------------------------------------------");
                System.out.println("Coins: " + coins);
                System.out.println("Level " + level);
                PETC();
                clearConsole();
                continue GAME;
            } else if (input.equals("updates")) {
                System.out.println("UPDATES");
                System.out.println("------------------------------------------");
                System.out.println("");
                System.out.println("1-8-16 - PRECIOUS");
                System.out.println("Added mining and a money system\n");
                System.out.println("1-7-16 - Mr. Krabs! I have an idea!");
                System.out.println("I finally started working on the game after countless reconsiderations & regrets!");
            } else if (input.equals("mine")) {
                com.quaggles.main.Mines.mine();
            } else {
                System.out.println("\'" + input + "\' is not a command.");
            }
        }
        System.out.println("Thanks for playing, " + username + "!");
        PETC();
        System.exit(0);
    }
}

Mines.java

// NOTE: Please read 'cc.txt' in src folder before modifying
// WORK IN PROGRESS: I am still working on this, (may be very buggy)

package com.quaggles.main;

import java.util.Scanner;

import com.quaggles.main.Main.*; //unneeded at the moment

public class Mines {
    // System objects
    static boolean running = true;
    static Scanner in = new Scanner(System.in);
    static String input;
    /////// Import 'money' from Main.java
    static int pickaxe = 1;
    static int pickaxeXP = 0;
    static int pickaxeLevelUp = 100;
    // Game variables
    public static void mine() {
        GAME: while (running) {
            System.out.println("Welcome to the...");
            System.out.println("  __  __ _                 ");
            System.out.println(" |  \\/  (_)                ");
            System.out.println(" | \\  / |_ _ __   ___  ___ ");
            System.out.println(" | |\\/| | | '_ \\ / _ \\/ __|");
            System.out.println(" | |  | | | | | |  __/\\__ \\");
            System.out.println(" |_|  |_|_|_| |_|\\___||___/");
            System.out.println("------------------------------------------");
            System.out.println("Type \'mine\' to start mining");
            input = in.nextLine();
            if (input.equals("mine")) {
                /////// money
            }
        }
    }
}

EDIT: I just realized 'money' doesn't exist and I forgot I renamed it to 'coins', that should clear up any confusion.

8
  • 1
    For 11-year old it is impressive indeed. Commented Jan 9, 2016 at 4:49
  • Please expand on "it did not work". What actually happens? Commented Jan 9, 2016 at 4:51
  • 3
    While I understand that you want to 'learn in your own way', it may still help to look through books or take other free courses like udacity.com/course/intro-to-java-programming--cs046 or docs.oracle.com/javase/tutorial . The key thing is that programming uses a "language" and you need to be able to communicate in it. At this point I don't know where the money methods (think doors) to your class (think house) are supposed to connect. I'll now get off my high horse. Props for trying at 11. Commented Jan 9, 2016 at 5:04
  • A text based mine craft server? Good luck lol... I sense much ASCII in your future youngling... Commented Jan 9, 2016 at 5:24
  • 2
    Also I get what you are saying about leaning your own way. I respect that. I'm 14 and I haven't taken any courses. I have been learning through online documentation and tutorials. Also I have a book for beginners Java is by herbert schildt. It's a great book it's 20 dollars but it's worth it and you will learn a ton. Although maybe not since you may know it all now. For graphics in Java2d I have been using the java doc and zetcode.com. Try that website it's great. In general stack overflow, Java doc, tutorialspoint.com, and other online resources are the best. Commented Jan 9, 2016 at 5:27

1 Answer 1

2

I'll answer your specific question as best as possible first. There is no variable named money in the class com.quaggles.main.Main but there is a coins variable so if that's the variable you mean, here's how:

Mines.java:

        if (input.equals("mine")) {
            System.out.println("My coins are: " + Main.coins);
        }

Note 1: classes in the same Java package (i.e. namespace) do not need to import, nor do they need to fully specify the namespace, so you can simply reference Main and Mines (i.e., without the namespace com.quaggles.main).

Note 2: since Main.coins is static there is only a single instance of this coins in the server; presumably, that means the server can only handle one player.

Note 3: Main calls Mines.mines() and Mines then calls Main.coins -- this is called a circular reference. As professionals, we work hard to avoid circular references because they can cause unpredictable behavior. Consider refactoring your code, perhaps to include a list of Accounts, i.e., one for each player which contains the coins or money.

Good luck with your learning, and don't get discouraged by negative comments.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.