Skip to content

Commit

Permalink
Merge pull request #1 from Vxrpenter/dev
Browse files Browse the repository at this point in the history
Changes for 1.2
  • Loading branch information
Vxrpenter authored Mar 10, 2024
2 parents 3e480d5 + 057cc32 commit ed4b63b
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = 'dev.vxrp'
version = '1.1'
version = '1.2'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.vxrp.simplemaintenance;

import com.sun.tools.javac.Main;
import dev.vxrp.simplemaintenance.commands.MaintenanceCommand;
import dev.vxrp.simplemaintenance.events.ListPingEvent;
import dev.vxrp.simplemaintenance.events.OnJoinEvent;
Expand Down Expand Up @@ -40,7 +39,7 @@ public void database() {
try {
sqlite = new Sqlite(getDataFolder().getAbsolutePath() + "/data.db");
} catch (SQLException e) {
System.out.println("Connection to database failed!" + e.getMessage());
Bukkit.getConsoleSender().sendMessage("Connection to database failed!" + e.getMessage());
Bukkit.getPluginManager().disablePlugin(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package dev.vxrp.simplemaintenance.commands;

import dev.vxrp.simplemaintenance.SimpleMaintenance;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.title.Title;
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 org.bukkit.scheduler.BukkitScheduler;
import org.jetbrains.annotations.NotNull;

import java.sql.SQLException;
import java.util.Objects;

public class MaintenanceCommand implements CommandExecutor {
private final SimpleMaintenance plugin;
Expand Down Expand Up @@ -42,8 +39,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
+plugin.getConfig().getString("maintenance_information.link")+">support</click>"));
p.sendMessage(mm.deserialize("<newline> <gray>« <red>SM <gray>»"));
}
BukkitScheduler scheduler = Bukkit.getScheduler();
scheduler.runTaskLater(plugin, () -> p.kick(mm.deserialize("<red>" + plugin.getConfig().getString("kick_message"))), time* 20L);
Bukkit.getScheduler().runTaskLater(plugin, () -> p.kick(mm.deserialize("<red>" + plugin.getConfig().getString("kick_message"))), time* 20L);
} else if (p.hasPermission("sm.bypass")) {
p.sendMessage(mm.deserialize("<red>SM <gray>» You won't be disconnected because you are permitted to<newline> be online while maintenance is running"));
p.showTitle(Title.title(mm.deserialize("<gray>Maintenance <green>activated"), mm.deserialize("<red>You won't be disconnected ")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.event.Listener;

import java.sql.SQLException;
import java.util.Objects;

public class ListPingEvent implements Listener {
private final SimpleMaintenance plugin;
Expand All @@ -16,9 +17,10 @@ public ListPingEvent(SimpleMaintenance simpleMaintenance) {
}
@EventHandler
public void onListPing(PaperServerListPingEvent e) throws SQLException {
Component maintenanceMotd = new MOTDManager(plugin.getConfig().getString("maintenance_motd")).build();
Component basicMotd = new MOTDManager(plugin.getConfig().getString("basic_motd")).build();
Component maintenanceMotd = MOTDManager.motd(Objects.requireNonNull(plugin.getConfig().getString("maintenance_motd")));
Component basicMotd = MOTDManager.motd(Objects.requireNonNull(plugin.getConfig().getString("basic_motd")));
if (plugin.getSqlite().getMaintenance()) {
e.setHidePlayers(true);
e.motd(maintenanceMotd);
} else if (!plugin.getSqlite().getMaintenance()) {
e.motd(basicMotd);
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/dev/vxrp/simplemaintenance/events/OnJoinEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import dev.vxrp.simplemaintenance.SimpleMaintenance;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

import org.bukkit.event.player.PlayerQuitEvent;
import java.sql.SQLException;

public class OnJoinEvent implements Listener {
private final SimpleMaintenance plugin;
public OnJoinEvent(SimpleMaintenance simpleMaintenance) {
Expand All @@ -19,7 +21,15 @@ public void onJoin(PlayerJoinEvent e) throws SQLException {
var mm = MiniMessage.miniMessage();
if (plugin.getSqlite().getMaintenance()) {
if (player.hasPermission("sm.bypass")) return;
player.kick(mm.deserialize("<red>" + plugin.getConfig().getString("kick_message")));
e.joinMessage(null);
Bukkit.getScheduler().runTaskLater(plugin, () -> player.kick(mm.deserialize("<red>" + plugin.getConfig().getString("kick_message"))), 1);
}
}
@EventHandler
public void onQuit(PlayerQuitEvent e) throws SQLException {
if (plugin.getSqlite().getMaintenance()) {
if (e.getPlayer().hasPermission("sm.bypass")) return;
e.quitMessage(null);
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/dev/vxrp/simplemaintenance/storage/Sqlite.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package dev.vxrp.simplemaintenance.storage;

import java.sql.*;

public class Sqlite {
private final Connection connection;
public Sqlite(String path) throws SQLException {
connection = DriverManager.getConnection("jdbc:sqlite:" + path);
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE IF NOT EXISTS Maintenance (" +
try (Statement maintenance = connection.createStatement()) {
maintenance.execute("CREATE TABLE IF NOT EXISTS Maintenance (" +
"maintenance BOOLEAN)");
}
}
//Maintenance
public void setMaintenance(boolean state) throws SQLException {
try (PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO Maintenance (maintenance) VALUES (?)")) {
preparedStatement.setBoolean(1, state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;

public class MOTDManager {
private final String motd;

public MOTDManager(String motd) {
this.motd = motd;
}
public Component build() {
public static Component motd(String motd) {
return LegacyComponentSerializer.legacyAmpersand().deserialize(motd.replace("%newline%", "\n"));
}
}

0 comments on commit ed4b63b

Please sign in to comment.