Skip to content

Commit

Permalink
added PastePlotsCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Nachwahl committed May 6, 2021
1 parent 7c596a1 commit 65f3287
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 51 deletions.
11 changes: 10 additions & 1 deletion src/main/java/dev/nachwahl/plottools/PlotTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import dev.arantes.inventorymenulib.listeners.InventoryListener;
import dev.nachwahl.plottools.commands.CreatePlotCommand;
import dev.nachwahl.plottools.commands.PastePlotsCommand;
import dev.nachwahl.plottools.utils.FileBuilder;
import dev.nachwahl.plottools.utils.MySQL;
import org.bukkit.plugin.java.JavaPlugin;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

public final class PlotTools extends JavaPlugin {

private static PlotTools plugin;
Expand All @@ -16,9 +21,12 @@ public void onEnable() {
// Register commands

this.getCommand("createplot").setExecutor(new CreatePlotCommand(this));
this.getCommand("pasteplots").setExecutor(new PastePlotsCommand(this));

new InventoryListener(this);

List example = new ArrayList<Integer>();
example.add(1);
example.add(2);
new FileBuilder("plugins/PlotTools", "mysql.yml")
.addDefault("mysql.host", "localhost")
.addDefault("mysql.port", "3306")
Expand All @@ -31,6 +39,7 @@ public void onEnable() {
.addDefault("sftp.username", "root")
.addDefault("sftp.password", "foo")
.addDefault("sftp.location", "/home/mc/server/plugins/AlpsBTE-PlotSystem/plots/")
.addDefault("citiesOnTheServer", example)
.copyDefaults(true).save();
if(!MySQL.isConnected()) {
MySQL.connect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,26 +242,7 @@ public void createPlot(Player player, int cityID) {
channelSftp.put(filePath, plotID + ".schematic");
channelSftp.disconnect();
jschSession.disconnect();

/*SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.addHostKeyVerifier("c1:94:01:00:56:2e:c8:35:5a:87:16:c4:6a:8c:82:c5");
ssh.connect(fb.getString("sftp.host"));
try {
ssh.authPassword(fb.getString("sftp.username"), fb.getString("sftp.password"));
SFTPClient sftp = ssh.newSFTPClient();
try {
sftp.mkdirs(schematic.getParentFile().getName());
System.out.println(filePath);
System.out.println("to");
System.out.println( Paths.get(fb.getString("sftp.location"), String.valueOf(cityID), plotID + ".schematic").toString());
sftp.put(new FileSystemFile(filePath), Paths.get(fb.getString("sftp.location"), String.valueOf(cityID), plotID + ".schematic").toString());
} finally {
sftp.close();
}
} finally {
ssh.disconnect();
}*/



} catch (Exception ex) {
Expand Down
151 changes: 151 additions & 0 deletions src/main/java/dev/nachwahl/plottools/commands/PastePlotsCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package dev.nachwahl.plottools.commands;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.sk89q.worldedit.CuboidClipboard;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.schematic.SchematicFormat;
import com.sk89q.worldedit.world.DataException;
import dev.nachwahl.plottools.PlotTools;
import dev.nachwahl.plottools.utils.FileBuilder;
import dev.nachwahl.plottools.utils.MySQL;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.sql.*;
import java.util.logging.Level;
import java.util.regex.Pattern;

public class PastePlotsCommand implements CommandExecutor {
PlotTools plugin;

public PastePlotsCommand(PlotTools plugin) {
this.plugin = plugin;
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
System.out.println("This command can only be executed by a player.");
return false;
}
Player player = (Player) sender;


if (!player.hasPermission("plottools.createplot")) {
player.sendMessage("§7§l>> §cYou have no permission to execute this command.");
return false;
}

player.sendMessage("§7§l>> §aOne moment please...");
FileBuilder fb = new FileBuilder("plugins/PlotTools", "config.yml");
JSch jsch = new JSch();
Session jschSession;
ChannelSftp channelSftp;

try (Connection connection = MySQL.getConnection()) {



jsch.setKnownHosts(Paths.get(System.getProperty("user.dir"), "known_hosts").toString());
jschSession = jsch.getSession(fb.getString("sftp.username"), fb.getString("sftp.host"));
jschSession.setPassword(fb.getString("sftp.password"));
jschSession.connect();
channelSftp = (ChannelSftp) jschSession.openChannel("sftp");
channelSftp.connect();



for(Integer city : fb.getIntegerList("citiesOnTheServer")) {
uploadPlot(1, player, channelSftp);
}



channelSftp.disconnect();
jschSession.disconnect();
} catch (Exception exception) {
exception.printStackTrace();
}
return false;
}
public static void pastePlotSchematic(int plotID, int cityID, Vector mcCoordinates) throws IOException, DataException, MaxChangedBlocksException {
File file = Paths.get(System.getProperty("user.dir"), "schem", "finishedPlots", String.valueOf(cityID), plotID + ".schematic").toFile();

EditSession editSession = new EditSession(new BukkitWorld(Bukkit.getWorld("world")), -1);
editSession.enableQueue();

SchematicFormat schematicFormat = SchematicFormat.getFormat(file);
CuboidClipboard clipboard = schematicFormat.load(file);

clipboard.paste(editSession, mcCoordinates, true);
editSession.flushQueue();

try (Connection connection = MySQL.getConnection()){
PreparedStatement ps = connection.prepareStatement("UPDATE plots SET isPasted = '1' WHERE idplot = '" + plotID + "'");
ps.executeUpdate();
} catch (SQLException ex) {
Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex);
}
}


public Boolean uploadPlot(Integer city, Player player, ChannelSftp channelSftp) throws SQLException, SftpException, DataException, IOException, MaxChangedBlocksException {
FileBuilder fb = new FileBuilder("plugins/PlotTools", "config.yml");

PreparedStatement ps;
ps = MySQL.getConnection().prepareStatement("SELECT COUNT(*) AS count FROM plots WHERE idcity = ? AND isPasted = 0 AND `status` = 'complete';");
ps.setInt(1, city);
ResultSet rs = ps.executeQuery();
Integer count = 0;
while (rs.next()) {
count = rs.getInt("count");
}

if(count == 0) {
player.sendMessage("§7§l>> §cThere are no plots to be pasted.");
return false;
}
player.sendMessage("§7§l>> §aStart copying §6" + count + " §aplots.");

ps = MySQL.getConnection().prepareStatement("SELECT * FROM plots WHERE idcity = ? AND isPasted = 0 AND status = 'complete';");
ps.setInt(1, city);
rs = ps.executeQuery();

while (rs.next()) {
channelSftp.cd(fb.getString("sftp.location"));
channelSftp.cd("./finishedPlots");
channelSftp.cd("./" + city);
String filePath = Paths.get(System.getProperty("user.dir"), "schem", "finishedPlots", String.valueOf(city), rs.getInt("idplot") + ".schematic").toString();
File schematic = new File(filePath);
boolean createdDirectory = schematic.getParentFile().mkdirs();
channelSftp.get(String.valueOf(rs.getInt("idplot")) + ".schematic", filePath);
player.sendMessage("§7§l>> §aSuccessfully copied plot §6#" + rs.getInt("idplot") + "§a!");
String[] splitCoordinates = rs.getString("mcCoordinates").split(",");
if (splitCoordinates.length == 3) {
Vector mcCoordinates = Vector.toBlockPoint(
Float.parseFloat(splitCoordinates[0]),
Float.parseFloat(splitCoordinates[1]),
Float.parseFloat(splitCoordinates[2])
);
pastePlotSchematic(rs.getInt("idplot"), city, mcCoordinates);
} else {
player.sendMessage("§7§l>> §cThis plot doesn't have a Y coordinate!");
}

}
return true;
}

}
31 changes: 1 addition & 30 deletions src/main/java/dev/nachwahl/plottools/utils/MySQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,6 @@ public static void connect() {
}
}
}
public static void connectDiscordUserDB() {
FileBuilder fb = new FileBuilder("plugins/BuildEvent", "mysql.yml");
String host = fb.getString("user.host");
String port = fb.getString("user.port");
String database = fb.getString("user.database");
String user = fb.getString("user.user");
String password = fb.getString("user.password");
if (!isDiscordDBConnected()) {
try {
DCDBcon = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?useSSL=false", user, password);
System.out.println("[PlotTools]" + ANSI_GREEN + " Discord user DB MySQL connection ok!" + ANSI_RESET);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("[PlotTools]" + ANSI_RED + " Discord user DB MySQL connection error" + ANSI_RESET);
}
}
}

public static void disconnect() {
if (isConnected()) {
Expand All @@ -72,25 +55,13 @@ public static boolean isConnected() {
return false;
}
}
public static boolean isDiscordDBConnected() {
try {
return (DCDBcon != null && (!DCDBcon.isClosed()));
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}


public static Connection getConnection() {
if (!isConnected())
connect();
return con;
}
public static Connection getDCDBConnection() {
if (!isConnected())
connect();
return DCDBcon;
}

public static ResultSet getCities() throws SQLException {
PreparedStatement ps = getConnection().prepareStatement("SELECT * FROM cityProjects");
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ depend:
- WorldEdit
commands:
createplot:
pasteplots:

0 comments on commit 65f3287

Please sign in to comment.