· 7 years ago · Dec 14, 2018, 06:16 PM
1package com.gguurr.fantacore._main;
2
3import java.sql.Connection;
4import java.sql.DriverManager;
5import java.sql.PreparedStatement;
6import java.sql.ResultSet;
7import java.sql.SQLException;
8import java.sql.Statement;
9
10import org.bukkit.Bukkit;
11import org.bukkit.entity.Player;
12import org.bukkit.plugin.RegisteredServiceProvider;
13import org.bukkit.plugin.java.JavaPlugin;
14
15import com.gguurr.fantacore.CMDs.SetRank;
16import com.gguurr.fantacore.CMDs.SugarCMD;
17import com.gguurr.fantacore.events.PlayerJoin;
18import com.gguurr.fantahub._main.Chat;
19import com.gguurr.fantahub._main.Economy;
20import com.gguurr.fantahub._main.Permission;
21import com.google.common.io.ByteArrayDataInput;
22import com.google.common.io.ByteArrayDataOutput;
23import com.google.common.io.ByteStreams;
24
25public class Main extends JavaPlugin {
26
27 public static Main instans;
28
29 private Connection connection;
30 private String host, database, username, password;
31 private String prefix = "fanta_";
32 private int port;
33
34 @Override
35 public void onEnable() {
36 host = "sql.pebblehost.com";
37 database = "customer_10028";
38 username = "customer_10028";
39 password = "4d0bc8e214";
40 port = 3306;
41 instans = this;
42
43 this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
44 this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
45
46 if (!setupEconomy()) {
47 getServer().getPluginManager().disablePlugin(this);
48 return;
49 }
50 setupPermissions();
51 setupChat();
52
53 try {
54 openConnection();
55 Statement statement = connection.createStatement();
56 String q = "CREATE TABLE IF NOT EXISTS " + prefix + "Money(amount INT, uuid CHAR(36));";
57 statement.executeUpdate(q);
58 } catch (ClassNotFoundException e) {
59 e.printStackTrace();
60 } catch (SQLException e) {
61 e.printStackTrace();
62 }
63
64 Bukkit.getPluginManager().registerEvents(new PlayerJoin(), this);
65
66 getCommand("money").setExecutor(new SugarCMD());
67
68 getCommand("rank").setExecutor(new SetRank());
69 }
70
71 private boolean setupEconomy() {
72 if (getServer().getPluginManager().getPlugin("Vault") == null) {
73 return false;
74 }
75 RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
76 if (rsp == null) {
77 return false;
78 }
79 econ = rsp.getProvider();
80 return econ != null;
81 }
82
83 private boolean setupChat() {
84 RegisteredServiceProvider<Chat> rsp = getServer().getServicesManager().getRegistration(Chat.class);
85 chat = rsp.getProvider();
86 return chat != null;
87 }
88
89 private boolean setupPermissions() {
90 RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
91 perms = rsp.getProvider();
92 return perms != null;
93 }
94
95 @Override
96 public void onDisable() {
97 }
98
99 public int getMoney(Player p) {
100 try {
101 Statement statement = connection.createStatement();
102 String q = "SELECT amount FROM " + prefix + "Money WHERE uuid = \"" + p.getUniqueId().toString() + "\";";
103 ResultSet rs = statement.executeQuery(q);
104 int s = -1;
105 while (rs.next()) {
106 s = rs.getInt("amount");
107 }
108 return s;
109 } catch (SQLException e) {
110 // TODO Auto-generated catch block
111 e.printStackTrace();
112 }
113 return -1;
114
115 }
116
117 public boolean haveRecord(Player p) {
118 try {
119 Statement statement = connection.createStatement();
120 String q = "SELECT * FROM " + prefix + "Money WHERE uuid = \"" + p.getUniqueId().toString() + "\";";
121 ResultSet rs = statement.executeQuery(q);
122 while (rs.next()) {
123 return true;
124
125 }
126 } catch (SQLException e) {
127 // TODO Auto-generated catch block
128 e.printStackTrace();
129 }
130 return false;
131 }
132
133 public void addMoney(Player p, int a) {
134 setMoney(p, getMoney(p) + a);
135 }
136
137 public void createMoney(Player p) {
138 try {
139 String q = "INSERT INTO " + prefix + "Money(amount, uuid) VALUES(?,?)";
140 PreparedStatement statement = connection.prepareStatement(q);
141 statement.setInt(1, 0);
142 statement.setString(2, p.getUniqueId().toString());
143 statement.executeUpdate();
144 } catch (SQLException e) {
145 // TODO Auto-generated catch block
146 e.printStackTrace();
147 }
148 }
149
150 public void setMoney(Player p, int a) {
151 try {
152 String q = "UPDATE " + prefix + "Money SET amount = ? WHERE uuid = ?;";
153 PreparedStatement statement = connection.prepareStatement(q);
154 statement.setInt(1, a);
155 statement.setString(2, p.getUniqueId().toString());
156 statement.executeUpdate();
157 } catch (SQLException e) {
158 // TODO Auto-generated catch block
159 e.printStackTrace();
160 }
161 }
162
163 public void openConnection() throws SQLException, ClassNotFoundException {
164 if (connection != null && !connection.isClosed()) {
165 return;
166 }
167
168 synchronized (this) {
169 if (connection != null && !connection.isClosed()) {
170 return;
171 }
172 Class.forName("com.mysql.jdbc.Driver");
173 connection = DriverManager.getConnection(
174 "jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.username, this.password);
175 }
176 }
177
178 @Override
179 public void onPluginMessageReceived(String channel, Player arg1, byte[] message) {
180 if (!channel.equals("BungeeCord")) {
181 return;
182 }
183 ByteArrayDataInput in = ByteStreams.newDataInput(message);
184 String subchannel = in.readUTF();
185 if (subchannel.equals("PlayerCount")) {
186 online = in.readInt();
187 }
188 }
189
190 public void sendPlayer(Player p, String server) {
191 ByteArrayDataOutput out = ByteStreams.newDataOutput();
192 out.writeUTF("Connect");
193 out.writeUTF(server);
194
195 p.sendPluginMessage(this, "BungeeCord", out.toByteArray());
196 }
197
198 public static Economy getEconomy() {
199 return econ;
200 }
201
202 public static Permission getPermissions() {
203 return perms;
204 }
205
206 public static Chat getChat() {
207 return chat;
208 }
209}