Skip to content

Linked Data

Christopher Bishop edited this page Jan 13, 2017 · 12 revisions

Linking Data in DEVIN is another powerful way to do something big with very little code. You can give players nick names or personal spawns with just a few lines of code.

Features

  • Link data to specific Players
  • Link data to specific Worlds
  • Save and Load the data from a YML file.

Linking Data to a Player

The PlayerData class is the most commonly used class for linking data to a player.

Setting Data

The data that is linked to the player is a series of key and value pairs. You can have multiple players with the same key, but each key that player is linked to must be unique. So to set the data you need the player, a key that is what your value represents and the value itself. The value does not have to be a String, it can be any object.

Player p = Bukkit.getPlayers()[0]; // This is a random player we want to link data to.

PlayerData playerData = new PlayerData();
playerData.set(p, "nickname", "GUY");

Getting Data

Now that we can link data to a player it would make sense we would want to retrieve it some other time down the road. This example shows how we can set the display name of the player to what the nickname value.

p.setDisplayName(playerData.get(p, "nickname"));

Saving Data

We have the ability to create values linked to players, and to get those values back and use them. Now we want to make sure that when the server reloads that we can get them back. This is quite simple:

File dataFile = new File(getDataFolder(), "player-data.yml");
playerData.save(dataFile);

Loading Data

Loading the data back into memory has a simalar process as saving it. When you load the data back into the PlayerData instance, it will clear any previous data that is stored and populate it with the data that is inside the file.

File dataFile = new File(getDataFolder(), "player-data.yml");
playerData.load(dataFile);

#Linking Data to a World This is the same process as with PlayerData except you use the WorldData class instead and pass a World variable as the object.