Advertisement
Guest User

Untitled

a guest
Jun 18th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. package com.gmail.candanatak97.selfmade.gasstation;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10.  
  11. import net.gtaun.shoebill.data.Location;
  12. import net.gtaun.shoebill.data.Radius;
  13. import net.gtaun.shoebill.pickup.DynamicPickup;
  14. import net.gtaun.shoebill.pickup.DynamicPickupImpl;
  15.  
  16. public class GasStationManager {
  17. private static Connection connection;
  18.  
  19. private static ArrayList<DynamicPickup> pickups;
  20. private static Map<Integer, GasStation> gasStations;
  21.  
  22. public static void initialize(Connection connection) {
  23. GasStationManager.connection = connection;
  24.  
  25. reload();
  26. }
  27.  
  28. public static void reload() {
  29. gasStations = new HashMap<Integer, GasStation>();
  30. pickups = new ArrayList<DynamicPickup>();
  31.  
  32. try {
  33. PreparedStatement stmt;
  34. stmt = connection.prepareStatement("SELECT * FROM `samp_gasstations`");
  35.  
  36. ResultSet result = stmt.executeQuery();
  37.  
  38. GasStation gasStation = null;
  39. Location location = new Location(0, 0, 0);
  40. float radius = 0;
  41.  
  42. while(result.next()) {
  43. location = new Location(Float.parseFloat(result.getString("pos_x")), Float.parseFloat(result.getString("pos_y")), Float.parseFloat(result.getString("pos_z")));
  44. radius = Float.parseFloat(result.getString("radius"));
  45.  
  46. gasStation = new GasStation(result.getInt("id"), result.getString("name"), location, radius, result.getInt("pickup_id"), result.getInt("price_liter"));
  47. if(gasStations.containsKey(gasStation.getId())) {
  48. gasStations.replace(gasStation.getId(), gasStation);
  49. } else {
  50. gasStations.put(gasStation.getId(), gasStation);
  51. }
  52. }
  53. } catch (SQLException e) {
  54. e.printStackTrace();
  55. }
  56.  
  57. for(GasStation gasStation : GasStationManager.getGasStations().values()) {
  58. pickups.add(new DynamicPickupImpl(gasStation.getPickupId(), 0, gasStation.getLocation(), 30f));
  59. }
  60. }
  61.  
  62. public static Map<Integer, GasStation> getGasStations() {
  63. return gasStations;
  64. }
  65.  
  66. public static GasStation getGasStation(int id) {
  67. for(GasStation gasStation : getGasStations().values()) {
  68. if(gasStation.getId() == id) {
  69. return gasStation;
  70. }
  71. }
  72.  
  73. return null;
  74. }
  75.  
  76. public static void remove(int id) {
  77. if(gasStations.containsKey(id)) {
  78. gasStations.remove(id);
  79. }
  80. }
  81.  
  82. public static void remove(GasStation gasStation) {
  83. remove(gasStation.getId());
  84. }
  85.  
  86. public static boolean add(GasStation gasStation) {
  87. try {
  88. String SQL = "INSERT INTO `samp_gasstations` (pos_x, pos_y, pos_z, pickup_id, radius) VALUES (?, ?, ?, ?, ?);";
  89. PreparedStatement preparedStatement = connection.prepareStatement(SQL);
  90. preparedStatement.setFloat(1, gasStation.getLocation().getX());
  91. preparedStatement.setFloat(2, gasStation.getLocation().getY());
  92. preparedStatement.setFloat(3, gasStation.getLocation().getZ());
  93. preparedStatement.setInt(4, gasStation.getPickupId());
  94. preparedStatement.setFloat(5, gasStation.getRadius());
  95.  
  96. if(preparedStatement.executeUpdate() > 0) {
  97. reload();
  98. return true;
  99. }
  100. } catch (SQLException e) {
  101. e.printStackTrace();
  102. }
  103.  
  104. return false;
  105. }
  106.  
  107. public static GasStation getNearestGasStation(Location location) {
  108. for(GasStation gasStation : getGasStations().values()) {
  109. Radius radius = new Radius(gasStation.getLocation(), gasStation.getRadius());
  110.  
  111. if(radius.isInRange(location)) {
  112. return gasStation;
  113. }
  114. }
  115.  
  116. return null;
  117. }
  118.  
  119. public static boolean isDestroyed() {
  120. if(pickups != null) {
  121. for(DynamicPickup pickup : pickups) {
  122. if(!pickup.isDestroyed())
  123. return false;
  124. }
  125. }
  126.  
  127. return true;
  128. }
  129.  
  130. public static void destroy() {
  131. if(isDestroyed())
  132. return;
  133.  
  134. if(pickups != null) {
  135. for(DynamicPickup pickup : pickups) {
  136. pickup.destroy();
  137. }
  138. }
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement