Skip to content
OliverVsCreeper edited this page May 20, 2015 · 22 revisions

Explaining the GameAPI

NetworkUtilities has a great, build in framework for the development of reliable, fast and modern games. This include automatic logging to Hastebin, debugging and custom events for ease of use.

Beginning our development

All games require a custom class, which extends Game. You'll notice how your IDE automatically implements the getAllStates() and getLobbyLocation() methods. These are required by NetworkUtilities to run a game smoothly.

What are they?

  • getAllStates() - returns a list of GameState extending classes (we'll explain this more later)
  • getLobbyLocation() - quite literally, returns the location for the games lobby. _null _is supported too.

GameState's

Every NetworkUtilities game instance is in a state at all times. Upon initialization, this is null. NetworkUtilities will attempt to set this to the state first in your list specified in getAllStates() one second after class creation.

We won't explain GameState's, as they are quite easy to understand once you extend them on a class and see what is implemented. Just note, you can use (un)registerListener(this) if you want to use events, and gameInstance.nextState() to go to the next state. Take a look at the IdleGameState for a basic example of usage.

Also, make sure you return true generally in your onStateBegin(). If you don't, the state won't be changed. Returning false in your first state will cause nullPointerExceptions, since you will be referring to a false state.

Events

To add/remove players or spectators, you can use MainClass.game.addSpectator(player);, MainClass.game.addPlayer(player);, MainClass.game.removePlayer(player); and MainClass.game.removeSpectator(player);. Several events are triggered by these. There are also other events:

  • GameSwitchStateEvent
  • PlayerDeathInArenaEvent
  • PlayerJoinGameEvent
  • PlayerLeaveGameEvent
  • PlayerStartSpectatingEvent
  • PlayerStopSpectatingEvent

Example Code:

  @EventHandler
   public static void playerDeath(PlayerDeathInArenaEvent e) {
       Player p = e.getPlayer();
       e.setCancelled(true);
   }

Luckily, you can listen to these just like Bukkit events using @EventHandler in a registered Listener class.

And to finish... Extensions

NetworkUtilities aims to be a flexible library, and so we made the decision that we would develop extensions that could be either enabled or disabled. Currently, TeamExtension is the only extension and this has it's own self explanatory methods.

Bonus Feature: I'm personally quite proud of this one! Calling getLogger().getLog() returns a HasteBin URL with a full log of the game!

Example Code:

public class Game {
	public static NULogger logger;
	public static Message prefixedMessage = new Message(Message.INFO);
	public static GameClass game;

	public static void onEnable() {
            game = new GameClass(new NULogger(true));
            logger = new NULogger(true);
            logger.log("Core", "Core initialized, loading Game");
	}
}