Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package <insert your package name>;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import net.risingworld.api.Plugin;
- import net.risingworld.api.Server;
- import net.risingworld.api.database.Database;
- import net.risingworld.api.events.EventMethod;
- import net.risingworld.api.events.Listener;
- import net.risingworld.api.events.player.PlayerSpawnEvent;
- import net.risingworld.api.events.player.world.PlayerDestroyVegetationEvent;
- import net.risingworld.api.objects.Player;
- import net.risingworld.api.utils.Definitions;
- public class <insert your class name> extends Plugin implements Listener
- {
- Database database;
- Server server;
- // Add, remove or change all the attribute keys you want (removing or changing attribute keys will not remove or rename them in the database yet)
- String[] attribute_keys = new String[] { "strength", "agility", "intelligence", "stamina" };
- @Override
- public void onEnable()
- {
- registerEventListener(this);
- server = getServer();
- database = getSQLiteConnection(getPath() + "/database/storage.db");
- database.execute("CREATE TABLE IF NOT EXISTS attributes (uid INTEGER NOT NULL, key VARCHAR(55), value INT)");
- }
- @EventMethod
- public void onPlayerSpawn(PlayerSpawnEvent event)
- {
- //// Don't remove or change anything here unless you know what you are doing
- Player player = event.getPlayer();
- // Creates attribute rows in databse and fills them with 0, if they don't exist yet
- initializeAttributes(player);
- // Load all attributes from Database into RAM
- loadAttributes(player);
- //// Examples (should be used in other functions
- // Changes an attribute in RAM if it exists in the attribute_keys array
- // (Obviously don't call on player spawn or it will reset the attribute every time a player spawns)
- setAttribute(player, "stamina", 25);
- // Increases an attribute in RAM if it exists in the attribute_keys array (can be positive and negative)
- increaseAttribute(player, "strength", 0);
- // Save all attributes from RAM into Database
- storeAttributes(player);
- }
- // Example: When you hit vegetation your strength goes up by 1
- @EventMethod
- public void onVegetationDestroy(PlayerDestroyVegetationEvent event)
- {
- Definitions.PlantDefinition plantDefinition = event.getPlantDefinition();
- if(plantDefinition.isTree())
- {
- server.broadcastTextMessage("You destroyed a tree and gained 1 strength.");
- Player player = event.getPlayer();
- increaseAttribute(player, "strength", 1);
- // TODO: Saving might need a timer, so it doesn't save too often into the database, maybe every 30 or 60 seconds
- storeAttributes(player);
- }
- }
- @Override
- public void onDisable()
- {
- database.close();
- }
- private void initializeAttributes(Player player)
- {
- int errors = 0;
- for (int i = 0; i < attribute_keys.length; i++)
- {
- String query = "SELECT * FROM attributes WHERE uid = " + player.getUID() + " AND key = '" + attribute_keys[i] + "'";
- try (ResultSet result = database.executeQuery(query))
- {
- if (!result.next())
- {
- database.executeUpdate("INSERT INTO attributes (uid, key, value) VALUES (" + player.getUID() + ", '" + attribute_keys[i] + "', 0)");
- }
- }
- catch(SQLException e)
- {
- errors++;
- e.printStackTrace();
- }
- }
- if (errors > 0)
- {
- server.broadcastTextMessage("Initializing the attributes of " + player.getName() + " into the database failed. Please contact an admin.");
- }
- }
- private void loadAttributes(Player player)
- {
- int errors = 0;
- String query = "SELECT * FROM attributes WHERE uid = " + player.getUID();
- try (ResultSet result = database.executeQuery(query))
- {
- while (result.next())
- {
- player.setAttribute(result.getString("key"), result.getInt("value"));
- }
- }
- catch(SQLException e)
- {
- errors++;
- e.printStackTrace();
- }
- if (errors > 0)
- {
- server.broadcastTextMessage("Loading the attributes of " + player.getName() + " from the database failed. Please contact an admin.");
- }
- }
- private void increaseAttribute(Player player, String key, int amount)
- {
- for (int i = 0; i < attribute_keys.length; i++)
- {
- if (attribute_keys[i] == key)
- {
- int value = Integer.parseInt(player.getAttribute(key).toString());
- value += amount;
- player.setAttribute(key, value);
- }
- }
- }
- private void setAttribute(Player player, String key, int value)
- {
- for (int i = 0; i < attribute_keys.length; i++)
- {
- if (attribute_keys[i] == key)
- {
- player.setAttribute(key, value);
- }
- }
- }
- private void storeAttributes(Player player)
- {
- for (int i = 0; i < attribute_keys.length; i++)
- {
- String value = player.getAttribute(attribute_keys[i]).toString();
- database.executeUpdate("UPDATE attributes SET value = " + value + " WHERE uid = " + player.getUID() + " AND key = '" + attribute_keys[i] + "'");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement