· 8 years ago · Aug 05, 2018, 04:47 AM
1package cn.blockmc.Zao_hon;
2
3import java.util.HashMap;
4import java.util.List;
5
6import org.bukkit.configuration.ConfigurationSection;
7import org.bukkit.plugin.Plugin;
8import org.bukkit.plugin.java.JavaPlugin;
9
10import cn.BlockMC.Zao_hon.AnvilLogin;
11
12public class Signin extends JavaPlugin {
13 private HashMap<String, Reward> totalrewards = new HashMap<String, Reward>();
14 private HashMap<String, Reward> continuousrewards = new HashMap<String, Reward>();
15 private AnvilLogin anvillogin;
16 private SqlManager sqlmanager;
17
18 @Override
19 public void onEnable() {
20 this.saveDefaultConfig();
21 this.reloadConfig();
22 this.getLogger().info("SignIn Started");
23
24//the 23 line
25 this.sqlmanager = new SqlManager(this);
26
27///
28 this.getCommand("Signin").setExecutor(new Commands(this));
29 this.getServer().getPluginManager().registerEvents(new EventListener(this), this);
30 this.loadDepends();
31 this.loadRewards();
32 }
33 @Override
34 public void onDisable(){
35 sqlmanager.close();
36 }
37
38 private void loadRewards() {
39 ConfigurationSection totalsec = this.getConfig().getConfigurationSection("TotalRewards");
40 if (totalsec != null) {
41 totalsec.getKeys(false).forEach(key -> {
42 int day = totalsec.getInt(key + ".Day");
43 String displayname = totalsec.getString(key + ".DisplayName");
44 List<String> lore = totalsec.getStringList(key + ".Lore");
45 String command = totalsec.getString(key + ".Command", "");
46 int vault = totalsec.getInt(key + ".Vault", 0);
47 int patch = totalsec.getInt(key+".Patch",0);
48 Reward r = new Reward(day, displayname, lore, command, vault,patch);
49 totalrewards.put(key, r);
50 });
51 }
52 ConfigurationSection continuoussec = this.getConfig().getConfigurationSection("ContinuousRewards");
53 if (continuoussec != null) {
54 continuoussec.getKeys(false).forEach(key -> {
55 int day = continuoussec.getInt(key + ".Day");
56 String displayname = continuoussec.getString(key + ".DisplayName");
57 List<String> lore = continuoussec.getStringList(key + ".Lore");
58 String command = continuoussec.getString(key + ".Command", "");
59 int vault = continuoussec.getInt(key + ".Vault", 0);
60 int patch = continuoussec.getInt(key+".Patch",0);
61 Reward r = new Reward(day, displayname, lore, command, vault,patch);
62 continuousrewards.put(key, r);
63 });
64 }
65 }
66
67 private void loadDepends() {
68 Plugin anv = this.getServer().getPluginManager().getPlugin("AnvilLogin");
69 if (anv == null) {
70 this.getLogger().info("没有找到AnvilLogin");
71 } else {
72 this.getLogger().info("å·²åŠ è½½ä¾èµ–æ’ä»¶AnvilLogin");
73 anvillogin = (AnvilLogin) anv;
74 this.getServer().getPluginManager().registerEvents(new AnvilLoginEventListener(this), this);
75 }
76 }
77 public void reload(){
78 reloadConfig();
79 sqlmanager.close();
80 sqlmanager = new SqlManager(this);
81 totalrewards.clear();
82 continuousrewards.clear();
83 loadRewards();
84 loadDepends();
85 }
86
87 public SqlManager getSql() {
88 return sqlmanager;
89 }
90
91 public AnvilLogin getAnvilLogin() {
92 return anvillogin;
93 }
94
95 public HashMap<String, Reward> getTotalRewards() {
96 return totalrewards;
97 }
98 public HashMap<String, Reward> getContinuousRewards() {
99 return continuousrewards;
100 }
101
102}
103package cn.blockmc.Zao_hon;
104
105import java.sql.Connection;
106import java.sql.DriverManager;
107import java.sql.PreparedStatement;
108import java.sql.ResultSet;
109import java.sql.SQLException;
110import java.util.ArrayList;
111import java.util.Calendar;
112import java.util.List;
113
114import org.bukkit.entity.Player;
115
116public class SqlManager {
117
118 private static final String CREATE_SIGNIN_TABLE = "CREATE TABLE IF NOT EXISTS signin (Name VARCHAR(30),UUID VARCHAR(40),Date VARCHAR(30),isLate BOOLEAN)";
119 private static final String CREATE_PATCH_TABLE = "CREATE TABLE IF NOT EXISTS patch (Name VARCHAR(30),UUID VARCHAR(40),Patch INTEGER)";
120 private static final String CREATE_REWARDS_TABLE = "CREATE TABLE IF NOT EXISTS rewards (Name VARCHAR(30),UUID VARCHAR(40),Reward VARCHAR(30))";
121 private static final String INSERT_PLAYER_SIGNIN = "INSERT INTO signin VALUES(?,?,?,?)";
122 private static final String INSERT_PLAYER_REWARD = "INSERT INTO rewards VALUES(?,?,?)";
123 private static final String INSERT_NEW_PLAYER_PATCH = "INSERT INTO patch (Name,UUID,Patch) SELECT ?,?,0 FROM DUAL WHERE NOT EXISTS (SELECT * FROM patch WHERE UUID = ?) ";
124 private static final String SELECT_PLAYER_REWARDS = "SELECT Reward FROM rewards WHERE UUID = ?";
125 private static final String SELECT_PLAYER_SIGNIN = "SELECT Date FROM signin WHERE UUID = ? ORDER BY Date";
126 private static final String SELECT_ISSIGNIN_TODAY = "SELECT * FROM signin WHERE UUID = ? AND Date = ?";
127 private static final String SELECT_PLAYER_PATCH = "SELECT Patch FROM patch WHERE UUID = ?";
128 private static final String UPDATE_SET_PLAYER_PATCH = "UPDATE patch SET Patch = ? WHERE UUID = ?";
129 private static final String UPDATE_ADD_PLAYER_PATCH = "UPDATE patch SET Patch = Patch + ? where UUID = ?";
130 private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
131 private Connection conn;
132
133 public SqlManager(Signin plugin) {
134 boolean b = plugin.getConfig().getBoolean("Mysql.Enable");
135 if(b){
136 try {
137 Class.forName(JDBC_DRIVER);
138 String host = plugin.getConfig().getString("MYSQL.Host");
139 String port = plugin.getConfig().getString("MYSQL.Port");
140 String database = plugin.getConfig().getString("MYSQL.DatabaseName");
141 String name = plugin.getConfig().getString("MYSQL.UserName");
142 String password = plugin.getConfig().getString("MYSQL.Password");
143 conn = DriverManager.getConnection(
144 "jdbc:mysql://" + host + ":" + port + "/" + database + "?characterEncoding=utf8&useSSL=true", name,
145 password);
146 } catch (ClassNotFoundException e) {
147 plugin.getLogger().info("JDBCé©±åŠ¨åŠ è½½å¤±è´¥");
148 } catch (SQLException e) {
149 e.printStackTrace();
150 plugin.getLogger().info("åŠ è½½MYSQLæ•°æ®åº“失败..");
151 }
152 }else{
153 try {
154 Class.forName("org.sqlite.JDBC");
155 conn = DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder() + "/Signin.db");
156 } catch (ClassNotFoundException e) {
157 plugin.getLogger().info("SQLITEæ•°æ®åº“驱动未找到!");
158 } catch (SQLException e) {
159 plugin.getLogger().info("连接SQLITEæ•°æ®åº“失败!");
160 }
161 }
162 try {
163 conn.prepareStatement(CREATE_SIGNIN_TABLE).execute();
164 conn.prepareStatement(CREATE_REWARDS_TABLE).execute();
165 conn.prepareStatement(CREATE_PATCH_TABLE).execute();
166 conn.prepareStatement(INSERT_PLAYER_SIGNIN);
167 conn.prepareStatement(INSERT_NEW_PLAYER_PATCH);
168 conn.prepareStatement(SELECT_ISSIGNIN_TODAY);
169 conn.prepareStatement(SELECT_PLAYER_SIGNIN);
170 conn.prepareStatement(INSERT_PLAYER_REWARD);
171 conn.prepareStatement(SELECT_PLAYER_REWARDS);
172 conn.prepareStatement(SELECT_PLAYER_PATCH);
173 conn.prepareStatement(UPDATE_SET_PLAYER_PATCH);
174 conn.prepareStatement(UPDATE_ADD_PLAYER_PATCH);
175 } catch (SQLException e) {
176 e.printStackTrace();
177 }
178
179
180
181 }
182 public int getPlayerContinuousSignin(Player p){
183 int i =0;
184 List<String> list = new ArrayList<String>();
185 list.forEach(k->p.sendMessage(k));
186 return i;
187 }
188
189 public int getPlayerPatch(Player p) {
190 int i = 0;
191 try {
192 PreparedStatement s = conn.prepareStatement(SELECT_PLAYER_PATCH);
193 s.setString(1, p.getUniqueId().toString());
194 ResultSet rs = s.executeQuery();
195 if(rs.next()){
196 i = rs.getInt(1);
197 }
198 } catch (SQLException e) {
199 e.printStackTrace();
200 }
201 return i;
202 }
203 public void addPlayerPatch(Player p,int i){
204 try {
205 PreparedStatement s = conn.prepareStatement(UPDATE_ADD_PLAYER_PATCH);
206 s.setInt(1, i);
207 s.setString(2, p.getUniqueId().toString());
208 s.execute();
209 } catch (SQLException e) {
210 e.printStackTrace();
211 }
212 }
213
214 public void setPlayerPatch(Player p, int i) {
215 try {
216 PreparedStatement s = conn.prepareStatement(UPDATE_SET_PLAYER_PATCH);
217 s.setInt(1, i);
218 s.setString(2, p.getUniqueId().toString());
219 } catch (SQLException e) {
220 e.printStackTrace();
221 }
222 }
223
224 public void addNewPlayerPatch(Player p) {
225 try {
226 PreparedStatement s = conn.prepareStatement(INSERT_NEW_PLAYER_PATCH);
227 s.setString(1, p.getName());
228 s.setString(2, p.getUniqueId().toString());
229 s.setString(3, p.getUniqueId().toString());
230 s.execute();
231 } catch (SQLException e) {
232 e.printStackTrace();
233 }
234 }
235
236 public List<String> getPlayerRewards(Player p) {
237 List<String> list = new ArrayList<String>();
238 try {
239 PreparedStatement s = conn.prepareStatement(SELECT_PLAYER_REWARDS);
240 s.setString(1, p.getUniqueId().toString());
241 ResultSet rs = s.executeQuery();
242 while (rs.next()) {
243 list.add(rs.getString(1));
244 }
245 } catch (SQLException e) {
246 e.printStackTrace();
247 }
248 return list;
249 }
250
251 public void addPlayerReward(Player p, String reward) {
252 try {
253 PreparedStatement s = conn.prepareStatement(INSERT_PLAYER_REWARD);
254 s.setString(1, p.getName());
255 s.setString(2, p.getUniqueId().toString());
256 s.setString(3, reward);
257 s.execute();
258 } catch (SQLException e) {
259 e.printStackTrace();
260 }
261 }
262
263 public int getPlayerTotalSigninNumber(Player p) {
264 int i = 0;
265 try {
266 PreparedStatement s = conn.prepareStatement(SELECT_PLAYER_SIGNIN);
267 s.setString(1, p.getUniqueId().toString());
268 ResultSet rs = s.executeQuery();
269 rs.last();
270 i = rs.getRow();
271 } catch (SQLException e) {
272 e.printStackTrace();
273 }
274 return i;
275 }
276
277 public boolean isSignINToday(Player player) {
278 boolean b = false;
279 try {
280 PreparedStatement p = conn.prepareStatement(SELECT_ISSIGNIN_TODAY);
281 Calendar c = Calendar.getInstance();
282 String date = c.get(Calendar.YEAR) + "." + (c.get(Calendar.MONTH) + 1) + "." + c.get(Calendar.DAY_OF_MONTH);
283 p.setString(1, player.getUniqueId().toString());
284 p.setString(2, date);
285 ResultSet rs = p.executeQuery();
286 if (rs.next()) {
287 b = true;
288 }
289 } catch (SQLException e) {
290 e.printStackTrace();
291 }
292 return b;
293 }
294
295 public List<String> getAllPlayerSignin(Player p) {
296 List<String> list = new ArrayList<String>();
297 try {
298 PreparedStatement s = conn.prepareStatement(SELECT_PLAYER_SIGNIN);
299 s.setString(1, p.getUniqueId().toString());
300 ResultSet rs = s.executeQuery();
301 while (rs.next()) {
302 list.add(rs.getString(1));
303 }
304 } catch (SQLException e) {
305 e.printStackTrace();
306 }
307 return list;
308 }
309
310 public void inserctPlayerSignin(Player p, String date, Boolean islate) {
311 try {
312 PreparedStatement s = conn.prepareStatement(INSERT_PLAYER_SIGNIN);
313 s.setString(1, p.getName());
314 s.setString(2, p.getUniqueId().toString());
315 s.setString(3, date);
316 s.setBoolean(4, islate);
317 s.execute();
318 } catch (SQLException e) {
319 }
320 }
321
322 public void close(){
323 try {
324 if(conn!=null&&!conn.isClosed()){
325 conn.close();
326 }
327 } catch (SQLException e) {
328 e.printStackTrace();
329 }
330 }
331}