· 8 years ago · Apr 11, 2018, 09:36 AM
1package cubes.EssemCSH.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.Location;
12import org.bukkit.World;
13import org.bukkit.block.Block;
14import org.bukkit.configuration.file.FileConfiguration;
15import org.bukkit.entity.Player;
16import org.bukkit.event.EventHandler;
17import org.bukkit.event.Listener;
18import org.bukkit.event.player.PlayerJoinEvent;
19import org.bukkit.plugin.PluginManager;
20import org.bukkit.plugin.java.JavaPlugin;
21
22import net.md_5.bungee.api.ChatColor;
23
24public class Main extends JavaPlugin implements Listener {
25
26 // VARIABLES
27
28 String cubeType;
29 String str;
30
31
32
33
34
35
36
37
38 private Connection connection;
39 private String host, database, username, password;
40 private int port;
41 Statement statement;
42 ResultSet res;
43
44
45 public static Main main;
46
47
48 static FileConfiguration config = main.config;
49
50 // WHEN PLUGIN IS ENABLED
51 @Override
52 public void onEnable() {
53
54 PluginManager pm = Bukkit.getPluginManager();
55 pm.registerEvents(this, this);
56
57 host = getConfig().getString("db.hostname");
58 port = getConfig().getInt("db.port");
59 database = getConfig().getString("db.database");
60 username = getConfig().getString("db.username");
61 password = getConfig().getString("db.password");
62
63 try {
64 openConnection();
65 statement = connection.createStatement();
66 } catch (ClassNotFoundException e) {
67 e.printStackTrace();
68 } catch (SQLException e) {
69 // TODO Auto-generated catch block
70 e.printStackTrace();
71 }
72
73 getConfig().options().copyDefaults(true);
74 saveConfig();
75
76 getLogger().info("Plugin Successfully Enabled!");
77
78 cube();
79
80 }
81
82 public void cube() {
83 if (!getConfig().contains("cube.worker")) {
84 /*for (Player p : Bukkit.getServer().getOnlinePlayers()) {
85 if (p.isOp()) {
86 getConfig().addDefault("cube.worker.x", p.getLocation().);
87 getConfig().addDefault("cube.worker.x", 300.5);
88 getConfig().addDefault("cube.worker.x", 100);
89 }
90 }*/
91 cubeType = "worker";
92 int x = -1;
93 int y = 0;
94 int z = 0;
95
96 String sql = "CREATE TABLE IF NOT EXISTS cubetype(cube varchar(64));";
97 try {
98 PreparedStatement stmt = connection.prepareStatement(sql);
99 // I use executeUpdate() to update the databases table.
100 stmt.executeUpdate();
101 } catch (SQLException e) {
102 e.printStackTrace();
103 }
104 for (int i = 0 ; i < 8001; i++) {
105 x++;
106 if(x>20) {
107 x=1;
108 z++;
109 }
110 if (z>20) {
111 z=0;
112 y++;
113 }
114 World w = (World) getConfig().get("cube." + cubeType + ".w");
115 double x1 = getConfig().getDouble("cube." + cubeType + ".x");
116 double y1 = getConfig().getDouble("cube." + cubeType + ".x");
117 double z1 = getConfig().getDouble("cube." + cubeType + ".x");
118
119 Location loc = new Location(Bukkit.getWorld("world"),x,y,z);
120 int b = loc.getBlock().getTypeId();
121 str = "ALTER TABLE cubetype ADD '" + i + "' INT(11)";
122 try {
123 PreparedStatement stmt = connection.prepareStatement(str);
124 ResultSet results = stmt.executeQuery();
125 } catch (SQLException e) {
126 // TODO Auto-generated catch block
127 e.printStackTrace();
128 }
129
130 str = "INSERT INTO cubetype('" + i + "') VALUES('?');";
131 try {
132 PreparedStatement stmt = connection.prepareStatement(str);
133 stmt.setInt(1, b);
134 stmt.executeUpdate();
135 } catch (SQLException e) {
136 // TODO Auto-generated catch block
137 e.printStackTrace();
138 }
139
140 }
141 saveConfig();
142 }
143 }
144
145 public void openConnection() throws SQLException, ClassNotFoundException {
146 if (connection != null && !connection.isClosed()) {
147 return;
148 }
149
150 synchronized (this) {
151 if (connection != null && !connection.isClosed()) {
152 return;
153 }
154 Class.forName("com.mysql.jdbc.Driver");
155 connection = DriverManager.getConnection("jdbc:mysql://" + this.host+ ":" + this.port + "/" + this.database, this.username, this.password);
156 }
157 }
158
159 // WHEN PLAYER JOINS THE GAME
160 @EventHandler
161 public void onPlayerJoin(PlayerJoinEvent event) {
162
163 Player p = event.getPlayer();
164
165 // IF PLAYER HASN'T PLAYED BEFORE
166 if (!p.hasPlayedBefore() || !getConfig().contains(p.getDisplayName())) {
167 for (int i = 0 ; i < 8001; i++) {
168
169 getConfig().addDefault(p.getDisplayName() + ".cube." + i, "hi");
170
171 }
172 saveConfig();
173 }
174 // IF PLAYER HAS PLAYED BEFORE
175 else {
176
177 }
178
179 }
180
181 // WHEN PLUGIN IS DISABLED
182 @Override
183 public void onDisable() {
184
185 }
186}