Ban hourly

This commit is contained in:
Debucquoy Anthony 2024-01-25 22:48:29 +01:00
parent 3e3a6e9a5d
commit 2df9d0c293
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
4 changed files with 71 additions and 6 deletions

View File

@ -0,0 +1,25 @@
package ovh.herisson.thevoidroad;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.AsyncPlayerPreLoginEvent.Result;
import org.bukkit.event.player.PlayerKickEvent.Cause;
import net.kyori.adventure.text.Component;
public class BanHourly implements Listener{
@EventHandler
public void banned(AsyncPlayerPreLoginEvent e){
if(DatabaseManager.getInstance().hasDiedInHour(e.getUniqueId()))
e.disallow(Result.KICK_BANNED, Component.text("Vous etes mort dans la dernière heure... Patientez la prochaine!"));
}
@EventHandler
public void died(PlayerDeathEvent e){
DatabaseManager.getInstance().setDeath(e.getPlayer().getUniqueId());
e.getPlayer().kick(Component.text("Vous etes mort dans la dernière heure... Patientez la prochaine!"), Cause.BANNED);
}
}

View File

@ -35,6 +35,7 @@ public class DatabaseManager {
con = DriverManager.getConnection("jdbc:sqlite:plugins/TheVoidRoad/voids.db");
InitVoids();
InitConfig();
InitDeaths();
}
// Voids table and methods
@ -136,6 +137,45 @@ public class DatabaseManager {
}
}
private void InitDeaths() throws SQLException{
String query = " CREATE TABLE IF NOT EXISTS deaths (\n"
+ "id integer PRIMARY KEY,\n"
+ "uuid text NOT NULL UNIQUE,\n"
+ "lastDeath INTEGER DEFAULT (unixepoch() / (60 * 60)))";
Statement st = con.createStatement();
st.execute(query);
st.close();
}
public void setDeath(UUID uuid) {
try {
Statement st = con.createStatement();
String query = "INSERT OR REPLACE INTO deaths (uuid) VALUES (?)";
PreparedStatement p = con.prepareStatement(query);
p.setString(1, uuid.toString());
p.execute();
st.close();
} catch(Exception e){
e.printStackTrace();
}
}
public boolean hasDiedInHour(UUID uuid){
try {
Statement st = con.createStatement();
String query = "SELECT (unixepoch() / (60 * 60)) - lastDeath <= 0 FROM deaths WHERE uuid = ? LIMIT 1 ;";
PreparedStatement p = con.prepareStatement(query);
p.setString(1, uuid.toString());
ResultSet rs = p.executeQuery();
boolean value = rs.getBoolean(1);
st.close();
return value;
} catch(Exception e){
e.printStackTrace();
}
return false;
}
public void close(){
try {

View File

@ -111,19 +111,18 @@ public class Merchand implements Listener{
@EventHandler
public void OpenInventory(PlayerInteractEntityEvent e){
e.setCancelled(true);
inv.clear();
inv.setItem(4, new ItemStack(current.x, current.y));
if(e.getRightClicked().equals(m)){
e.getPlayer().sendMessage("Open inventory");
e.setCancelled(true);
inv.clear();
inv.setItem(4, new ItemStack(current.x, current.y));
e.getPlayer().openInventory(inv);
}
}
@EventHandler
public void Compass(PlayerInteractEvent e){
if(m.getLocation() != null && e.getItem() != null && e.getItem().getType() == Material.COMPASS){
e.getPlayer().setCompassTarget(m.getLocation());
if(getLocation() != null && e.getItem() != null && e.getItem().getType() == Material.COMPASS){
e.getPlayer().setCompassTarget(getLocation());
}
}

View File

@ -30,6 +30,7 @@ public class TheVoidRoad extends JavaPlugin{
getServer().getPluginManager().registerEvents(new HourEvent(), this); //For HourEvent
getServer().getPluginManager().registerEvents(Merchand.getInstance(), this);
getServer().getPluginManager().registerEvents(new Voids(), this);
getServer().getPluginManager().registerEvents(new BanHourly(), this);
}
@Override