OMGPIDocs

OMGPI

Source

JavaDocs

SpigotMC

Coding Skywars

A walkthrough of Skywars example game written on OMGPI.

After reading through the docs, it is time to create your own games! As an example, we will do Skywars. For that create a project Skywars and set it up like you usually setup workspace for spigot plugins.

After workspace setup, lets create main class for our Skywars plugin, we call it OMGPI_SW:

public class OMGPI_SW extends Game {
  public void onEnable() {
    name = "Skywars";
    super.onEnable();
  }
}

This code is our code setup for the game. We first name the plugin "Skywars" for the OMGPI systems and then load OMGPI systems with super.onEnable(). Next, we want our game to have no discovery, to have loots and players to lose after they die (give one life only), for that we use settings:

public class OMGPI_SW extends Game {
  public void onEnable() {
    name = "Skywars";
    super.onEnable();
    settings.hasDiscovery = false;
    settings.isLootingOn = true;
    settings.oneLife = true;
  }
}

Great! Our game is setup. It will work, but we better do more things. We need to do player spawns. To do that, we first should tell OMGPI to set defaults for spawns in mapfig. For that, we use event_preMapfigSave():

public void event_preMapfigSave(Mapfig m) {
  m.setUnpresent("spawns", new OMGList(){{
    for (int i = 0; i < m.getInt("players"); i++) add("0,0,0");
  }});
}

This creates a new list to save spawns in, like so:

... #mapfig values that are already in
spawns:
- '0,0,0'
- '0,0,0'
- # ...repeated "players" amount of times.

When players spawn, we need to tell them where to spawn. We should first create a game variable that counts players. For that, before onEnable() we can do:

public class OMGPI_SW extends Game {
  public static int counter = 0;

This will be our player counter. Then we tell OMGPI to use mapfig to teleport players on spawn, for that there is player_spawnLocation() we need to override:

public Location player_spawnLocation(OMGPlayer p) {
  double[] coords = Coordinates.parse(loadedMap.mapfig.getStringList("spawns").get(counter++), Coordinates.CoordinateType.ROTATION);
  return new Location(OMGPI.gameworld.bukkit, coords[0], coords[1], coords[2], (float) (coords.length > 3 ? coords[3] : 0), (float) (coords.length > 3 ? coords[4] : 0));
}

First we get spawns list from loaded map's mapfig: loadedMap.mapfig.getStringList("spawns"). Next - get a spawn from it - we use our counter we did before to do this: loadedMap.mapfig.getStringList("spawns").get(counter++). Then we parse our coordinates we got with string "x,y,z,yaw,pitch" into double array {x, y, z, yaw, pitch}: Coordinates.parse(loadedMap.mapfig.getStringList("spawns").get(counter++), Coordinates.CoordinateType.ROTATION). Rotation is for yaw and pitch.

Next we return a location where players will spawn - Game world OMGPI.gameworld.bukkit, coordinates we have, which are in array. Notice that yaw and pitch must be floats, though we used doubles, so we cast, and map can be setup like "x,y,z" instead of "x,y,z,yaw,pitch". For that, use ?: statement - if there are yaw and pitch (not just 3 coords - x,y,z), get them, else use 0. Very efficient and compact.

We are almost done. If you run this, you will have a problem that we can't use anything. To fix this, we need to make teams with id default have survival game mode. For that, use event_team_creation():

public void event_team_creation(OMGTeam t) {
  if (t.id.equals("default")) t.gameMode = GameMode.SURVIVAL;
}

Then we can add a cosmetic thing: scoreboard. For that we should override game_checkForEnd(), and if game is running (this is because of OMGPI design, it calls the method on join and other cases and it is easier to check it inside the method), we set ObjectiveBuffer.createPlayerBuffer() for each player:

public void game_checkForEnd() {
  if (state == GameState.INGAME)
    OMGPlayer.link.values().forEach(p -> ObjectiveBuffer.createPlayerBuffer().loadInto(p.displayObjective));
  super.game_checkForEnd();
}

ObjectiveBuffer.createPlayerBuffer() creates a list of names of players that have not lost. This is also why we use game_checkForEnd(): it is run after each death. After setting scoreboard we should call super so it checks for game end.

That is all for the game! Set your plugin.yml like you usually do and you are ready to put it into /plugins/OMGPI/games/ and setup. Optionally you can save some things in SQL with overriding according methods.