Advertisement
Guest User

Attribute Database

a guest
Jul 8th, 2019
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.73 KB | None | 0 0
  1. package <insert your package name>;
  2.  
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import net.risingworld.api.Plugin;
  6. import net.risingworld.api.Server;
  7. import net.risingworld.api.database.Database;
  8. import net.risingworld.api.events.EventMethod;
  9. import net.risingworld.api.events.Listener;
  10. import net.risingworld.api.events.player.PlayerSpawnEvent;
  11. import net.risingworld.api.events.player.world.PlayerDestroyVegetationEvent;
  12. import net.risingworld.api.objects.Player;
  13. import net.risingworld.api.utils.Definitions;
  14.  
  15. public class <insert your class name> extends Plugin implements Listener
  16. {
  17.     Database database;
  18.     Server server;
  19.    
  20.     // 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)
  21.     String[] attribute_keys = new String[] { "strength", "agility", "intelligence", "stamina" };
  22.    
  23.     @Override
  24.     public void onEnable()
  25.     {
  26.         registerEventListener(this);
  27.         server = getServer();
  28.         database = getSQLiteConnection(getPath() + "/database/storage.db");
  29.         database.execute("CREATE TABLE IF NOT EXISTS attributes (uid INTEGER NOT NULL, key VARCHAR(55), value INT)");
  30.     }
  31.    
  32.     @EventMethod
  33.     public void onPlayerSpawn(PlayerSpawnEvent event)
  34.     {
  35.         //// Don't remove or change anything here unless you know what you are doing
  36.        
  37.         Player player = event.getPlayer();
  38.         // Creates attribute rows in databse and fills them with 0, if they don't exist yet
  39.         initializeAttributes(player);
  40.         // Load all attributes from Database into RAM
  41.         loadAttributes(player);
  42.          
  43.         //// Examples (should be used in other functions
  44.        
  45.         // Changes an attribute in RAM if it exists in the attribute_keys array
  46.         // (Obviously don't call on player spawn or it will reset the attribute every time a player spawns)
  47.         setAttribute(player, "stamina", 25);
  48.  
  49.         // Increases an attribute in RAM if it exists in the attribute_keys array (can be positive and negative)
  50.         increaseAttribute(player, "strength", 0);
  51.  
  52.         // Save all attributes from RAM into Database
  53.         storeAttributes(player);
  54.     }
  55.    
  56.     // Example: When you hit vegetation your strength goes up by 1
  57.     @EventMethod
  58.     public void onVegetationDestroy(PlayerDestroyVegetationEvent event)
  59.     {
  60.         Definitions.PlantDefinition plantDefinition = event.getPlantDefinition();
  61.        
  62.         if(plantDefinition.isTree())
  63.         {
  64.             server.broadcastTextMessage("You destroyed a tree and gained 1 strength.");
  65.             Player player = event.getPlayer();
  66.             increaseAttribute(player, "strength", 1);
  67.            
  68.             // TODO: Saving might need a timer, so it doesn't save too often into the database, maybe every 30 or 60 seconds
  69.             storeAttributes(player);
  70.         }
  71.     }
  72.    
  73.     @Override
  74.     public void onDisable()
  75.     {
  76.         database.close();
  77.     }
  78.    
  79.     private void initializeAttributes(Player player)
  80.     {
  81.         int errors = 0;
  82.         for (int i = 0; i < attribute_keys.length; i++)
  83.         {
  84.             String query = "SELECT * FROM attributes WHERE uid = " + player.getUID() + " AND key = '" + attribute_keys[i] + "'";
  85.             try (ResultSet result = database.executeQuery(query))
  86.             {
  87.                 if (!result.next())
  88.                 {
  89.                     database.executeUpdate("INSERT INTO attributes (uid, key, value) VALUES (" + player.getUID() + ", '" + attribute_keys[i] + "', 0)");
  90.                 }
  91.             }
  92.             catch(SQLException e)
  93.             {
  94.                 errors++;
  95.                 e.printStackTrace();
  96.             }
  97.         }
  98.        
  99.         if (errors > 0)
  100.         {
  101.             server.broadcastTextMessage("Initializing the attributes of " + player.getName() + " into the database failed. Please contact an admin.");
  102.         }
  103.     }
  104.    
  105.     private void loadAttributes(Player player)
  106.     {
  107.         int errors = 0;
  108.        
  109.         String query = "SELECT * FROM attributes WHERE uid = " + player.getUID();
  110.         try (ResultSet result = database.executeQuery(query))
  111.         {
  112.             while (result.next())
  113.             {
  114.                 player.setAttribute(result.getString("key"), result.getInt("value"));
  115.             }
  116.         }
  117.         catch(SQLException e)
  118.         {
  119.             errors++;
  120.             e.printStackTrace();
  121.         }
  122.        
  123.         if (errors > 0)
  124.         {
  125.             server.broadcastTextMessage("Loading the attributes of " + player.getName() + " from the database failed. Please contact an admin.");
  126.         }
  127.     }
  128.    
  129.     private void increaseAttribute(Player player, String key, int amount)
  130.     {
  131.         for (int i = 0; i < attribute_keys.length; i++)
  132.         {
  133.             if (attribute_keys[i] == key)
  134.             {
  135.                 int value = Integer.parseInt(player.getAttribute(key).toString());
  136.                 value += amount;
  137.                 player.setAttribute(key, value);
  138.             }
  139.         }
  140.     }
  141.    
  142.     private void setAttribute(Player player, String key, int value)
  143.     {
  144.         for (int i = 0; i < attribute_keys.length; i++)
  145.         {
  146.             if (attribute_keys[i] == key)
  147.             {
  148.                 player.setAttribute(key, value);
  149.             }
  150.         }
  151.     }
  152.    
  153.     private void storeAttributes(Player player)
  154.     {
  155.         for (int i = 0; i < attribute_keys.length; i++)
  156.         {
  157.             String value = player.getAttribute(attribute_keys[i]).toString();
  158.             database.executeUpdate("UPDATE attributes SET value = " + value + " WHERE uid = " + player.getUID() + " AND key = '" + attribute_keys[i] + "'");
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement