added tasks: unpunish
This commit is contained in:
6 files changed
+166
No files matched your search
@@ -26,6 +26,7 @@ repositories {
|
||||
|
||||
|
||||
dependencies {
|
||||
implementation 'com.google.code.gson:gson:2.10.1'
|
||||
api 'commons-io:commons-io:2.11.0'
|
||||
api 'org.apache.commons:commons-pool2:2.11.1'
|
||||
api 'org.mariadb.jdbc:mariadb-java-client:3.1.3'
|
||||
|
||||
@@ -8,6 +8,7 @@ import uk.hotten.staffog.commands.*;
|
||||
import uk.hotten.staffog.data.DatabaseManager;
|
||||
import uk.hotten.staffog.punish.PunishManager;
|
||||
import uk.hotten.staffog.security.SecurityManager;
|
||||
import uk.hotten.staffog.tasks.TaskManager;
|
||||
import uk.hotten.staffog.utils.Console;
|
||||
|
||||
public class StaffOGPlugin extends JavaPlugin {
|
||||
@@ -28,6 +29,7 @@ public class StaffOGPlugin extends JavaPlugin {
|
||||
DatabaseManager databaseManager = new DatabaseManager(this);
|
||||
PunishManager punishManager = new PunishManager(this);
|
||||
SecurityManager securityManager = new SecurityManager(this);
|
||||
TaskManager taskManager = new TaskManager(this);
|
||||
|
||||
getCommand("stafflink").setExecutor(new StaffLinkCommand());
|
||||
|
||||
|
||||
@@ -140,6 +140,42 @@ public class PunishManager {
|
||||
}
|
||||
}
|
||||
|
||||
public PunishEntry getPunishment(PunishType type, int id) {
|
||||
try (Connection connection = DatabaseManager.getInstance().createConnection()) {
|
||||
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM `" + type.getTable() + "` WHERE `id`=?");
|
||||
statement.setLong(1, id);
|
||||
|
||||
ResultSet rs = statement.executeQuery();
|
||||
|
||||
if (!rs.next())
|
||||
return null;
|
||||
|
||||
PunishEntry entry = new PunishEntry(type);
|
||||
entry.setId(rs.getLong("id"));
|
||||
entry.setUuid(UUID.fromString(rs.getString("uuid")));
|
||||
entry.setReason(rs.getString("reason"));
|
||||
entry.setByUuid(rs.getString("by_uuid"));
|
||||
entry.setByName(rs.getString("by_name"));
|
||||
entry.setTime(rs.getLong("time"));
|
||||
entry.setUntil(rs.getLong("until"));
|
||||
entry.setActive(rs.getInt("active") == 1);
|
||||
|
||||
if (entry.checkDurationOver()) {
|
||||
expirePunishment(entry);
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry;
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
Console.error("Failed to get punishment info for id " + id);
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public PunishEntry checkActivePunishment(PunishType type, UUID uuid) {
|
||||
try (Connection connection = DatabaseManager.getInstance().createConnection()) {
|
||||
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package uk.hotten.staffog.tasks;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import uk.hotten.staffog.data.DatabaseManager;
|
||||
import uk.hotten.staffog.punish.PunishManager;
|
||||
import uk.hotten.staffog.punish.data.PunishEntry;
|
||||
import uk.hotten.staffog.tasks.data.TaskEntry;
|
||||
import uk.hotten.staffog.tasks.data.UnpunishTask;
|
||||
import uk.hotten.staffog.utils.Console;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class TaskManager {
|
||||
|
||||
@Getter private JavaPlugin plugin;
|
||||
|
||||
public TaskManager(JavaPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
|
||||
plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, () -> {
|
||||
for (TaskEntry entry : checkForTasks()) {
|
||||
switch (entry.getTask()) {
|
||||
case "unpunish": {
|
||||
processUnpunish(entry);
|
||||
deleteTask(entry.getId());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}, 0, 1200);
|
||||
}
|
||||
|
||||
private ArrayList<TaskEntry> checkForTasks() {
|
||||
try (Connection connection = DatabaseManager.getInstance().createConnection()){
|
||||
PreparedStatement statement = connection.prepareStatement("SELECT * FROM `staffog_task`;");
|
||||
|
||||
ResultSet rs = statement.executeQuery();
|
||||
|
||||
ArrayList<TaskEntry> toReturn = new ArrayList<>();
|
||||
|
||||
while (rs.next()) {
|
||||
TaskEntry entry = new TaskEntry();
|
||||
entry.setId(rs.getInt("id"));
|
||||
entry.setTask(rs.getString("task"));
|
||||
entry.setData(rs.getString("data"));
|
||||
toReturn.add(entry);
|
||||
}
|
||||
|
||||
return toReturn;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
private void processUnpunish(TaskEntry entry) {
|
||||
if (!entry.getTask().equals("unpunish"))
|
||||
return;
|
||||
|
||||
Gson gson = new Gson();
|
||||
UnpunishTask task = gson.fromJson(entry.getData(), UnpunishTask.class);
|
||||
PunishEntry punishEntry = PunishManager.getInstance().getPunishment(task.getType(), task.getId());
|
||||
punishEntry.setRemovedUuid(task.getRemovedUuid().toString());
|
||||
punishEntry.setRemovedName(task.getRemovedName());
|
||||
punishEntry.setRemovedReason(task.getRemovedReason());
|
||||
punishEntry.setRemovedTime(task.getRemovedTime());
|
||||
punishEntry.setActive(false);
|
||||
PunishManager.getInstance().removePunishment(punishEntry);
|
||||
}
|
||||
|
||||
private void deleteTask(int id) {
|
||||
try (Connection connection = DatabaseManager.getInstance().createConnection()){
|
||||
PreparedStatement statement = connection.prepareStatement("DELETE FROM `staffog_task` WHERE `id`=?");
|
||||
statement.setInt(1, id);
|
||||
|
||||
statement.executeQuery();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package uk.hotten.staffog.tasks.data;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
public class TaskEntry {
|
||||
|
||||
@Getter @Setter private int id;
|
||||
@Getter @Setter private String task;
|
||||
@Getter @Setter private String data;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package uk.hotten.staffog.tasks.data;
|
||||
|
||||
import lombok.Getter;
|
||||
import uk.hotten.staffog.punish.data.PunishType;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class UnpunishTask {
|
||||
|
||||
@Getter private PunishType type;
|
||||
@Getter private int id;
|
||||
@Getter private UUID removedUuid;
|
||||
@Getter private String removedName;
|
||||
@Getter private String removedReason;
|
||||
@Getter private long removedTime;
|
||||
|
||||
public UnpunishTask(String type, int id, String removedUuid, String removedName, String removedReason, long removedTime) {
|
||||
this.type = PunishType.valueOf(type);
|
||||
this.id = id;
|
||||
this.removedUuid = UUID.fromString(removedUuid);
|
||||
this.removedName = removedName;
|
||||
this.removedReason = removedReason;
|
||||
this.removedTime = removedTime;
|
||||
}
|
||||
|
||||
public UnpunishTask() { }
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user