Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
n1zamu authored May 11, 2024
1 parent d23272f commit 094228b
Show file tree
Hide file tree
Showing 12 changed files with 429 additions and 0 deletions.
14 changes: 14 additions & 0 deletions nCooldowns/nCooldowns.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="FacetManager">
<facet type="minecraft" name="Minecraft">
<configuration>
<autoDetectTypes>
<platformType>PAPER</platformType>
<platformType>ADVENTURE</platformType>
</autoDetectTypes>
<projectReimportVersion>1</projectReimportVersion>
</configuration>
</facet>
</component>
</module>
71 changes: 71 additions & 0 deletions nCooldowns/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>wtf</groupId>
<artifactId>nCooldowns</artifactId>
<version>1</version>
<packaging>jar</packaging>

<name>nCooldowns</name>

<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

<repositories>
<repository>
<id>papermc-repo</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.16.5-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
33 changes: 33 additions & 0 deletions nCooldowns/src/main/java/wtf/n1zamu/NCooldowns.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package wtf.n1zamu;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;
import wtf.n1zamu.commands.ReloadCommand;
import wtf.n1zamu.listeners.*;
import wtf.n1zamu.mysql.SQLite;

public final class NCooldowns extends JavaPlugin {
public static String logo = ChatColor.GOLD + "Задержка" + ChatColor.GRAY + " » ";
@Override
public void onEnable() {
saveDefaultConfig();
getLogger().info("=====NCooldowns successfully loaded=====");
getLogger().info("=====Author: github.com/n1zamu=====");
Bukkit.getPluginManager().registerEvents(new AsyncPlayerChatListener(), this);
Bukkit.getPluginManager().registerEvents(new PlayerJoinListener(), this);
Bukkit.getPluginManager().registerEvents(new CommandsListener(), this);
getCommand("ncooldowns").setExecutor(new ReloadCommand());
SQLite.connect();
SQLite.startChecks();
}

@Override
public void onDisable() {
getLogger().info("=====NCooldowns successfully unloaded=====");
getLogger().info("=====Author: github.com/n1zamu=====");
}
public static NCooldowns getInstance() {
return NCooldowns.getPlugin(NCooldowns.class);
}
}
41 changes: 41 additions & 0 deletions nCooldowns/src/main/java/wtf/n1zamu/commands/ReloadCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package wtf.n1zamu.commands;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;
import wtf.n1zamu.NCooldowns;

public class ReloadCommand implements CommandExecutor {
FileConfiguration config = NCooldowns.getInstance().getConfig();
@Override
public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, @NotNull String[] strings) {
if (strings.length < 1) {
sendMessage(commandSender, config.getString("messages.notEnoughArgumentsMessage"));
return false;
}

if (!commandSender.hasPermission("nCooldowns.reload")) {
sendMessage(commandSender, config.getString("messages.noPermission"));
return false;
}

if (!strings[0].contains("reload")) {
sendMessage(commandSender, config.getString("messages.unknownUsage"));
return false;
}

NCooldowns.getPlugin(NCooldowns.class).reloadConfig();
sendMessage(commandSender,config.getString("messages.successfullyReload"));
return true;
}


private void sendMessage(CommandSender player, String text) {
player.sendMessage(NCooldowns.logo +
ChatColor.translateAlternateColorCodes('&', text));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package wtf.n1zamu.listeners;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;

import wtf.n1zamu.NCooldowns;
import wtf.n1zamu.mysql.SQLite;
import wtf.n1zamu.utils.TimeUtil;
public class AsyncPlayerChatListener implements Listener {
@EventHandler
public void onChat(AsyncPlayerChatEvent e) {
Player player = e.getPlayer();
if (SQLite.isBlocked(player)) {
if (!player.hasPermission("nCooldown.bypass")) {
int time = SQLite.getTime(player);
if (time > 0) {
e.setCancelled(true);
TimeUtil.sendNotification(player, NCooldowns.getInstance().getConfig().getString("messages.blockMessage"));
}
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package wtf.n1zamu.listeners;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import wtf.n1zamu.NCooldowns;
import wtf.n1zamu.mysql.SQLite;
import wtf.n1zamu.utils.TimeUtil;

public class CommandsListener implements Listener {
FileConfiguration config = NCooldowns.getInstance().getConfig();
@EventHandler
public void onCommand(PlayerCommandPreprocessEvent event) {
Player player = event.getPlayer();
String command = event.getMessage();
if (!config.getBoolean("doubleDotCommands"))
return;
if (player.hasPermission("nCooldowns.doubleDot"))
return;
if (command.contains(":") && SQLite.isBlocked(player)) {
event.setCancelled(true);
TimeUtil.sendNotification(player, config.getString("messages.doubleDotMessage"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package wtf.n1zamu.listeners;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

import wtf.n1zamu.NCooldowns;
import wtf.n1zamu.mysql.SQLite;
import wtf.n1zamu.utils.TimeUtil;

public class PlayerJoinListener implements Listener {
FileConfiguration config = NCooldowns.getInstance().getConfig();
@EventHandler
public void onJoin(PlayerJoinEvent e) {
Player player = e.getPlayer();
if (!SQLite.firstJoin(player)) {
SQLite.setBlock(player, config.getInt("cooldown"));
TimeUtil.sendNotification(player, config.getString("messages.joinMessage"));
}
}
}

116 changes: 116 additions & 0 deletions nCooldowns/src/main/java/wtf/n1zamu/mysql/SQLite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package wtf.n1zamu.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class SQLite {
private static Statement statement;

public static void connect() {
try {
Class.forName("org.sqlite.JDBC");
Connection connection = DriverManager.getConnection("jdbc:sqlite:plugins/nCooldowns/database.db");
String sql = "CREATE TABLE IF NOT EXISTS cooldowns (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,time INTEGER,block INTEGER);";
statement = connection.createStatement();
statement.executeUpdate(sql);
} catch (Exception var1) {
System.err.println(var1.getClass().getName() + ": " + var1.getMessage());
}

}

private static Set<String> players() {
Set<String> set = new HashSet<>();

try {
ResultSet rs = statement.executeQuery("SELECT * FROM cooldowns;");

while (rs.next()) {
String name = rs.getString("name");
set.add(name);
}
} catch (SQLException var3) {
System.err.println(var3.getClass().getName() + ": " + var3.getMessage());
}

return set;
}

public static void setBlock(Player p, int time) {
try {
statement.executeUpdate("INSERT INTO cooldowns (name,time,block) VALUES ('" + p.getName() + "'," + time + "," + 1 + ");");
} catch (SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}

public static boolean isBlocked(Player p) {
try {
ResultSet rs = statement.executeQuery("SELECT * FROM cooldowns;");

while (rs.next()) {
String name = rs.getString("name");
int b = rs.getInt("block");
if (name.equals(p.getName())) {
return b == 1;
}
}
} catch (SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}

return false;
}

public static int getTime(Player p) {
try {
ResultSet rs = statement.executeQuery("SELECT * FROM cooldowns;");

while (rs.next()) {
String name = rs.getString("name");
int time = rs.getInt("time");
if (name.equals(p.getName())) {
return time;
}
}
} catch (SQLException e) {
e.printStackTrace();
}

return 0;
}

public static void startChecks() {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
for (Player p : Bukkit.getOnlinePlayers()) {
try {
if (SQLite.isBlocked(p)) {
int time = SQLite.getTime(p);
if (time > 0) {
SQLite.statement.executeUpdate("UPDATE cooldowns SET time = " + (time - 1) + " WHERE name='" + p.getName() + "';");
} else {
SQLite.statement.executeUpdate("UPDATE cooldowns SET block = 0 WHERE name='" + p.getName() + "';");
}
}
} catch (SQLException e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}
}
}, 1L, 1000L);
}

public static boolean firstJoin(Player p) {
return players().contains(p.getName());
}
}
30 changes: 30 additions & 0 deletions nCooldowns/src/main/java/wtf/n1zamu/utils/TimeUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package wtf.n1zamu.utils;

import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import wtf.n1zamu.NCooldowns;
import wtf.n1zamu.mysql.SQLite;

public class TimeUtil {
public static String formattedTime(int time) {
String cooldown = time / 60 + 1 + " минут";
if (time / 60 + 1 >= 60) cooldown = time / 60 / 60 + " часов";
if (time / 60 + 1 <= 1) cooldown = time + " секунд";

return cooldown;
}

public static void sendNotification(Player player, String message) {
if (NCooldowns.getInstance().getConfig().getBoolean("notification.sound")) {
player.playSound(player.getLocation(), Sound.valueOf(NCooldowns.getInstance().getConfig().getString("notification.soundType")), 1, 1);
}
if (NCooldowns.getInstance().getConfig().getBoolean("notification.message")) {
player.sendMessage(NCooldowns.logo + ChatColor.translateAlternateColorCodes('&', message.replace("%time%", TimeUtil.formattedTime(SQLite.getTime(player)))));
}
if (NCooldowns.getInstance().getConfig().getBoolean("notification.message")) {
player.sendTitle(ChatColor.GOLD + "Задержка", ChatColor.translateAlternateColorCodes('&', message.replace("%time%", TimeUtil.formattedTime(SQLite.getTime(player)))));
}
}
}
Loading

0 comments on commit 094228b

Please sign in to comment.