reload services and add ids to notes

This commit is contained in:
Taah 2022-04-06 19:05:29 -07:00
parent 48bf9bdbe5
commit 34d1eafaaf
6 changed files with 75 additions and 27 deletions

View File

@ -20,8 +20,8 @@ public class SQLNotes
{ {
private static final String SELECT = "SELECT * FROM `notes` WHERE uuid=?"; private static final String SELECT = "SELECT * FROM `notes` WHERE uuid=?";
private static final String INSERT = "INSERT INTO `notes` (`uuid`, `written_by`, `note`, `timestamp`) VALUES(?, ?, ?, ?)"; private static final String INSERT = "INSERT INTO `notes` (`id`, `uuid`, `written_by`, `note`, `timestamp`) VALUES(?, ?, ?, ?, ?)";
private static final String DELETE = "DELETE FROM `notes` WHERE uuid=? AND note=?"; private static final String DELETE = "DELETE FROM `notes` WHERE uuid=? AND id=?";
public CompletableFuture<List<Note>> getNotes(UUID uuid) public CompletableFuture<List<Note>> getNotes(UUID uuid)
{ {
@ -41,10 +41,10 @@ public class SQLNotes
UUID.fromString(set.getString("written_by")), UUID.fromString(set.getString("written_by")),
LocalDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("timestamp")), ZoneId.systemDefault()) LocalDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("timestamp")), ZoneId.systemDefault())
); );
note.setId(set.getInt("id"));
notes.add(note); notes.add(note);
} }
} } catch (SQLException e)
catch (SQLException e)
{ {
e.printStackTrace(); e.printStackTrace();
} }
@ -52,23 +52,46 @@ public class SQLNotes
}); });
} }
public CompletableFuture<Void> addNote(Note note) public CompletableFuture<Void> deleteNote(int id, UUID uuid)
{ {
return CompletableFuture.runAsync(() -> return CompletableFuture.runAsync(() ->
{ {
try (Connection con = Plex.get().getSqlConnection().getCon()) try (Connection con = Plex.get().getSqlConnection().getCon())
{ {
PreparedStatement statement = con.prepareStatement(INSERT); PreparedStatement statement = con.prepareStatement(DELETE);
statement.setString(1, note.getUuid().toString()); statement.setString(1, uuid.toString());
statement.setString(2, note.getWrittenBy().toString()); statement.setInt(2, id);
statement.setString(3, note.getNote());
statement.setLong(4, note.getTimestamp().toInstant(ZoneOffset.UTC).toEpochMilli());
statement.execute(); statement.execute();
} } catch (SQLException e)
catch (SQLException e)
{ {
e.printStackTrace(); e.printStackTrace();
} }
}); });
} }
public CompletableFuture<Void> addNote(Note note)
{
return CompletableFuture.runAsync(() ->
{
getNotes(note.getUuid()).whenComplete((notes, throwable) ->
{
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement(INSERT);
statement.setInt(1, notes.size());
statement.setString(2, note.getUuid().toString());
statement.setString(3, note.getWrittenBy().toString());
statement.setString(4, note.getNote());
statement.setLong(5, note.getTimestamp().toInstant(ZoneOffset.UTC).toEpochMilli());
statement.execute();
note.setId(notes.size());
} catch (SQLException e)
{
e.printStackTrace();
}
});
});
}
} }

View File

@ -13,6 +13,8 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import dev.plex.util.PlexLog;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -52,6 +54,9 @@ public class PlexCMD extends PlexCommand
send(sender, "Imported ranks"); send(sender, "Imported ranks");
send(sender, "Plex successfully reloaded."); send(sender, "Plex successfully reloaded.");
plugin.setSystem(plugin.config.getString("system")); plugin.setSystem(plugin.config.getString("system"));
plugin.getServiceManager().endServices();
plugin.getServiceManager().startServices();
PlexLog.debug("Restarted services");
return null; return null;
} }
else if (args[0].equalsIgnoreCase("redis")) else if (args[0].equalsIgnoreCase("redis"))

View File

@ -15,6 +15,8 @@ public class Note
private final UUID writtenBy; private final UUID writtenBy;
private final LocalDateTime timestamp; private final LocalDateTime timestamp;
private int id; // This will be automatically set from addNote
public String toJSON() public String toJSON()
{ {
return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).create().toJson(this); return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).create().toJson(this);

View File

@ -18,4 +18,14 @@ public abstract class AbstractService extends PlexBase implements IService
this.repeating = repeating; this.repeating = repeating;
this.asynchronous = async; this.asynchronous = async;
} }
public void onStart()
{
}
public void onEnd()
{
}
} }

View File

@ -27,22 +27,15 @@ public class ServiceManager
{ {
for (AbstractService service : services) for (AbstractService service : services)
{ {
if (!service.isRepeating()) startService(service);
{
BukkitTask task = Bukkit.getScheduler().runTask(Plex.get(), service::run);
service.setTaskId(task.getTaskId());
} else if (service.isRepeating() && service.isAsynchronous())
{
BukkitTask task = Bukkit.getScheduler().runTaskTimerAsynchronously(Plex.get(), service::run, 0, 20L * service.repeatInSeconds());
service.setTaskId(task.getTaskId());
} else if (service.isRepeating() && !service.isAsynchronous())
{
BukkitTask task = Bukkit.getScheduler().runTaskTimer(Plex.get(), service::run, 0, 20L * service.repeatInSeconds());
service.setTaskId(task.getTaskId());
}
} }
} }
public void endServices()
{
services.forEach(this::endService);
}
public AbstractService getService(Class<? extends AbstractService> clazz) public AbstractService getService(Class<? extends AbstractService> clazz)
{ {
return services.stream().filter(service -> service.getClass().isAssignableFrom(clazz)).findFirst().orElse(null); return services.stream().filter(service -> service.getClass().isAssignableFrom(clazz)).findFirst().orElse(null);
@ -63,11 +56,26 @@ public class ServiceManager
BukkitTask task = Bukkit.getScheduler().runTaskTimer(Plex.get(), service::run, 0, 20L * service.repeatInSeconds()); BukkitTask task = Bukkit.getScheduler().runTaskTimer(Plex.get(), service::run, 0, 20L * service.repeatInSeconds());
service.setTaskId(task.getTaskId()); service.setTaskId(task.getTaskId());
} }
if (!services.contains(service))
{
services.add(service);
}
service.onStart();
}
public void endService(AbstractService service, boolean remove)
{
Bukkit.getScheduler().cancelTask(service.getTaskId());
service.onEnd();
if (remove)
{
services.remove(service);
}
} }
public void endService(AbstractService service) public void endService(AbstractService service)
{ {
Bukkit.getScheduler().cancelTask(service.getTaskId()); endService(service, false);
} }
private void registerService(AbstractService service) private void registerService(AbstractService service)

View File

@ -77,7 +77,7 @@ public class SQLConnection extends PlexBase
"`endDate` BIGINT" + "`endDate` BIGINT" +
");").execute(); ");").execute();
con.prepareStatement("CREATE TABLE IF NOT EXISTS `notes` (" + con.prepareStatement("CREATE TABLE IF NOT EXISTS `notes` (" +
"`id` INT NOT NULL AUTO_INCREMENT, " + "`id` INT NOT NULL, " +
"`uuid` VARCHAR(46) NOT NULL, " + "`uuid` VARCHAR(46) NOT NULL, " +
"`written_by` VARCHAR(16), " + "`written_by` VARCHAR(16), " +
"`note` VARCHAR(2000), " + "`note` VARCHAR(2000), " +