Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package l2f.gameserver;
  16.  
  17. import java.sql.Connection;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.util.ArrayList;
  21. import java.util.Map;
  22. import java.util.concurrent.ConcurrentHashMap;
  23.  
  24. import l2f.commons.util.Rnd;
  25. import l2f.gameserver.model.Player;
  26. import l2f.gameserver.utils.Location;
  27. import l2f.loginserver.database.L2DatabaseFactory;
  28.  
  29. /**
  30. * @author AccessDenied
  31. */
  32. public class Faction
  33. {
  34.  
  35. public Map<Integer, FactionHolder> factions = new ConcurrentHashMap<>();
  36. public ArrayList<RewardHolder> rewards = new ArrayList<>();
  37.  
  38. public Faction()
  39. {
  40. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  41. {
  42. PreparedStatement statement = con.prepareStatement("SELECT * FROM faction");
  43. ResultSet result = statement.executeQuery();
  44.  
  45. while(result.next())
  46. {
  47. int id = result.getInt("factionId");
  48. String name = result.getString("factionName");
  49. String[] pos = result.getString("factionPos").split(";");
  50. Location loc = new Location(Integer.parseInt(pos[0]), Integer.parseInt(pos[1]), Integer.parseInt(pos[2]));
  51. String color = result.getString("factionColor");
  52.  
  53. factions.put(id, new FactionHolder(id, name, loc, color));
  54. }
  55. result.close();
  56. statement.close();
  57. }
  58. catch (Exception e)
  59. {
  60. System.out.println("Failed to load factions from database: " + e);
  61. return;
  62. }
  63. }
  64.  
  65. public void teleToBase(Player player)
  66. {
  67. player.teleToLocation(factions.get(player.getFactionId()).getFactionLoc(), 0);
  68. }
  69.  
  70. public Location getFactionLocation(Player player)
  71. {
  72. return factions.get(player.getFactionId()).getFactionLoc();
  73. }
  74.  
  75. public void onPlayerEnter(Player player)
  76. {
  77. player.setTitle(factions.get(player.getFactionId()).getFactionName());
  78. player.setNameColor("0x" + factions.get(player.getFactionId()).getFactionColor());
  79. teleToBase(player);
  80. }
  81.  
  82. public void onReward(Player player)
  83. {
  84. if (rewards.isEmpty())
  85. return;
  86.  
  87. for (RewardHolder holder : rewards)
  88. if (Rnd.get(100) <= holder.getChance())
  89. player.getInventory().addItem(holder.getRewardId(), holder.getRewardCount(), "reward");
  90. }
  91.  
  92. public class RewardHolder
  93. {
  94. private int _id;
  95. private int _count;
  96. private int _chance;
  97.  
  98. public RewardHolder(int id, int count, int chance)
  99. {
  100. _id = id;
  101. _count = count;
  102. _chance = chance;
  103. }
  104.  
  105. public int getChance()
  106. {
  107. return _chance;
  108. }
  109.  
  110. public int getRewardId()
  111. {
  112. return _id;
  113. }
  114.  
  115. public int getRewardCount()
  116. {
  117. return _count;
  118. }
  119. }
  120.  
  121. public class FactionHolder
  122. {
  123. private int _id;
  124. private String _name;
  125. private Location _pos;
  126. private String _color;
  127.  
  128. public FactionHolder(int id, String name, Location pos, String color)
  129. {
  130. _id = id;
  131. _name = name;
  132. _pos = pos;
  133. _color = color;
  134. }
  135.  
  136. public String getFactionColor()
  137. {
  138. return _color;
  139. }
  140.  
  141. public int getFactionId()
  142. {
  143. return _id;
  144. }
  145.  
  146. public String getFactionName()
  147. {
  148. return _name;
  149. }
  150.  
  151. public Location getFactionLoc()
  152. {
  153. return _pos;
  154. }
  155. }
  156.  
  157. public static Faction getInstance()
  158. {
  159. return SingletonHolder._instance;
  160. }
  161.  
  162. private static class SingletonHolder
  163. {
  164. protected static final Faction _instance = new Faction();
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement