Skip to content

Commit

Permalink
finish region creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nachwahl committed Jan 13, 2023
1 parent c02438f commit f10a8f3
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 15 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@
<id>aikar</id>
<url>https://repo.aikar.co/content/groups/aikar/</url>
</repository>
<repository>
<id>nachwahl-repo</id>
<url>https://maven.nachwahl.dev/</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -123,6 +127,12 @@
<version>2.9.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>6.1.9</version>
<scope>provided</scope>
</dependency>

</dependencies>
</project>
6 changes: 5 additions & 1 deletion src/main/java/dev/nachwahl/btemap/BTEMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ public FileBuilder getDbConfig() {

@Override
public void onDisable() {
// Plugin shutdown logic
if(this.socketIO != null) {
this.socketIO.closeSocket();
}

this.getSqlConnector().disconnect();
}

public SocketIO getSocketIO() {
Expand Down
163 changes: 151 additions & 12 deletions src/main/java/dev/nachwahl/btemap/commands/MapCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,33 @@
import co.aikar.commands.BaseCommand;
import co.aikar.commands.CommandHelp;
import co.aikar.commands.annotation.*;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.function.block.Counter;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.function.visitor.RegionVisitor;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.regions.RegionOperationException;
import dev.nachwahl.btemap.BTEMap;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import javax.xml.transform.Result;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.*;

@CommandAlias("map")
public class MapCommand extends BaseCommand {
Expand All @@ -31,14 +46,19 @@ public void onLink(CommandSender sender) {
public void run() {
int code = new Random().nextInt(900000) + 100000;
try {
PreparedStatement checkLinkedPs = plugin.getSqlConnector().getConnection().prepareStatement("SELECT * FROM user WHERE minecraftUUID = ?");
PreparedStatement checkLinkedPs = plugin.getSqlConnector().getConnection().prepareStatement("SELECT * FROM User WHERE minecraftUUID = ?");
checkLinkedPs.setString(1, ((Player) sender).getUniqueId().toString());
ResultSet checkLinkedRs = checkLinkedPs.executeQuery();
if(checkLinkedRs.next()) {
sender.sendMessage("§b§lBTEG §7» §cThis account is already linked.");
return;
} else {
PreparedStatement ps = plugin.getSqlConnector().getConnection().prepareStatement("INSERT INTO `linkcodes` (`id`, `code`, `playerUUID`, `createdAt`) VALUES (?, ?, ?, ?);");

PreparedStatement deleteCodePs = plugin.getSqlConnector().getConnection().prepareStatement("DELETE FROM LinkCodes WHERE playerUUID = ?");
deleteCodePs.setString(1, ((Player) sender).getUniqueId().toString());
deleteCodePs.execute();

PreparedStatement ps = plugin.getSqlConnector().getConnection().prepareStatement("INSERT INTO `LinkCodes` (`id`, `code`, `playerUUID`, `createdAt`) VALUES (?, ?, ?, ?);");
ps.setString(1, UUID.randomUUID().toString());
ps.setString(2, String.valueOf(code));
ps.setString(3, ((Player) sender).getUniqueId().toString());
Expand All @@ -58,17 +78,136 @@ public void run() {
}

@Default
public static void onDefault(CommandSender sender){
BTEMap.getPlugin(BTEMap.class).sendPluginMessage("nwarp", (Player) sender,sender.getName(),"Map");
}
@Subcommand("create")
@CommandPermission("map.create")
public void onCreate(CommandSender sender){
double lat = 0.0;
double lon = 0.0;
String city = "n/A";
Player player = (Player) sender;
LocalSession sm = WorldEdit.getInstance().getSessionManager().findByName(player.getName());
if(sm == null) {
sender.sendMessage("§b§lBTEG §7» §cPlease select a region via WorldEdit first.");
return;
}
Region region = null;
try {
region = sm.getSelection(WorldEdit.getInstance().getSessionManager().findByName(player.getName()).getSelectionWorld());
} catch (IncompleteRegionException e) {
sender.sendMessage("§b§lBTEG §7» §cPlease select a region via WorldEdit first.");
return;
}
if(region == null) {
sender.sendMessage("§b§lBTEG §7» §cPlease select a region via WorldEdit first.");
return;
}
List<BlockVector2D> poly = null;
try {
poly = region.polygonize(50);
} catch (IllegalArgumentException e) {
sender.sendMessage("§b§lBTEG §7» §cPlease select you region with under 50 points");
return;
}
player.sendMessage("§b§lBTEG §7» One moment please...");



String coords = "[";
for (BlockVector2D vector2D : poly) {
System.out.println(BTEMap.toGeo(vector2D.getBlockX(), vector2D.getBlockZ())[1] + ", " + BTEMap.toGeo(vector2D.getBlockX(), vector2D.getBlockZ())[0]);
System.out.println(Arrays.toString(BTEMap.toGeo(vector2D.getBlockX(), vector2D.getBlockZ())));
lat = BTEMap.toGeo(vector2D.getBlockX(), vector2D.getBlockZ())[1];
lon = BTEMap.toGeo(vector2D.getBlockX(), vector2D.getBlockZ())[0];
coords = coords + "[" + BTEMap.toGeo(vector2D.getBlockX(), vector2D.getBlockZ())[1] + ", " + BTEMap.toGeo(vector2D.getBlockX(), vector2D.getBlockZ())[0] + "],";

}
coords = coords.substring(0, coords.length() - 1);
coords = coords + "]";
System.out.println(coords);
URL url = null;


try {
url = new URL("https://nominatim.openstreetmap.org/reverse.php?osm_type=N&format=json&zoom=18&lon=" + lon + "&accept-language=de&lat=" + lat);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("accept", "application/json");
Reader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String response = "";
for (int i; (i = reader.read()) >= 0;)
response += (char) i;

System.out.println(response);
JsonElement jsonElement = new JsonParser().parse(response);
if(jsonElement.getAsJsonObject().get("address").getAsJsonObject().get("city") != null) {
city = jsonElement.getAsJsonObject().get("address").getAsJsonObject().get("city").getAsString();
} else if (jsonElement.getAsJsonObject().get("address").getAsJsonObject().get("village") != null){
city = jsonElement.getAsJsonObject().get("address").getAsJsonObject().get("village").getAsString();
} else if (jsonElement.getAsJsonObject().get("address").getAsJsonObject().get("town") != null) {
city = jsonElement.getAsJsonObject().get("address").getAsJsonObject().get("town").getAsString();
} else {
city = "n/A";
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

sm.getRegionSelector(WorldEdit.getInstance().getSessionManager().findByName(player.getName()).getSelectionWorld()).clear();

try {
region.contract(new Vector().setY(region.getHeight()-1));
} catch (RegionOperationException e) {
e.printStackTrace();
}

Region finalRegion = region;
String finalCoords = coords;
String finalCity = city;
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
UUID uuid = UUID.randomUUID();
Counter counter = new Counter();
RegionVisitor visitor = new RegionVisitor(finalRegion, counter);
Operations.completeBlindly(visitor);
try {
PreparedStatement checkUserPs = plugin.getSqlConnector().getConnection().prepareStatement("SELECT * FROM User WHERE minecraftUUID = ?");
checkUserPs.setString(1, player.getUniqueId().toString());
ResultSet checkUserRs = checkUserPs.executeQuery();

if(checkUserRs.next()) {
PreparedStatement ps = plugin.getSqlConnector().getConnection().prepareStatement("INSERT INTO Region (createdAt, id, username, userUUID, data, city, area, description, ownerID) VALUES (?, ?, ?, ?, ?, ?, ?, '', ?)");
ps.setTimestamp(1, new Timestamp(new Date().getTime()));
ps.setString(2, uuid.toString());
ps.setString(3, player.getName());
ps.setString(4, player.getUniqueId().toString());
ps.setString(5,finalCoords);
ps.setString(6, finalCity);
ps.setInt(7, counter.getCount());
ps.setString(8, checkUserRs.getString("id"));
ps.executeUpdate();
} else {
PreparedStatement ps = plugin.getSqlConnector().getConnection().prepareStatement("INSERT INTO Region (createdAt, id, username, userUUID, data, city, area, description) VALUES (?, ?, ?, ?, ?, ?, ?, '')");
ps.setTimestamp(1, new Timestamp(new Date().getTime()));
ps.setString(2, uuid.toString());
ps.setString(3, player.getName());
ps.setString(4, player.getUniqueId().toString());
ps.setString(5,finalCoords);
ps.setString(6, finalCity);
ps.setInt(7, counter.getCount());
ps.executeUpdate();
}


@HelpCommand
public static void onHelp(CommandSender sender, CommandHelp help) {
sender.sendMessage("§6BTE Germany Map");
help.showHelp();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
sender.sendMessage("§b§lBTEG §7» Your region was created successfully and is now visible on the map.");
sender.sendMessage("§b§lBTEG §7» You can see your region by clicking on this link: https://map.bte-germany.de/?region=" + uuid.toString() + "&details=true");
});
}





}
2 changes: 0 additions & 2 deletions src/main/java/dev/nachwahl/btemap/utils/SocketIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public SocketIO(String host, int port, String token){
.setAuth(singletonMap("token", token))
.build();


try {
this.socket = IO.socket(host+":"+port, options);
this.socket.connect();
Expand Down Expand Up @@ -73,7 +72,6 @@ public void closeSocket() {
this.socket.close();
}


public void sendPlayerDisconnect(UUID uniqueId) {
this.socket.emit("playerDisconnect", uniqueId.toString());
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
name: BTEMapV2
version: '${project.version}'
main: dev.nachwahl.btemap.BTEMap
depend:
- WorldEdit

0 comments on commit f10a8f3

Please sign in to comment.