Skip to content

Commit

Permalink
3.0.1-beta - add mySQL support to save current notify state, add mult…
Browse files Browse the repository at this point in the history
…iproxy support
  • Loading branch information
Lasergott committed Jan 5, 2020
1 parent 52ca89b commit e3ecf74
Show file tree
Hide file tree
Showing 16 changed files with 295 additions and 49 deletions.
9 changes: 9 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions CloudNotify.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: net.md-5:bungeecord-api:1.15-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: net.md-5:bungeecord-chat:1.15-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" name="Maven: net.md-5:bungeecord-config:1.15-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.25" level="project" />
<orderEntry type="library" name="Maven: net.md-5:bungeecord-event:1.15-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: net.md-5:bungeecord-protocol:1.15-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: net.md-5:brigadier:1.0.16-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-codec:4.1.44.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-common:4.1.44.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-buffer:4.1.44.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-transport:4.1.44.Final" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty-resolver:4.1.44.Final" level="project" />
<orderEntry type="library" name="Maven: net.sf.trove4j:core:3.1.0" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:20.0" level="project" />
<orderEntry type="module-library">
<library name="Maven: cloud.timo.TimoCloud:TimoCloud-API:6.0.0">
<CLASSES>
<root url="jar://$MODULE_DIR$/libs/TimoCloud.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.6" level="project" />
</component>
</module>
15 changes: 9 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cloud.timo</groupId>
<artifactId>CloudNotify</artifactId>
<version>3.0.0</version>
<version>3.0.1-beta</version>
<build>
<finalName>${project.name}</finalName>
<resources>
Expand Down Expand Up @@ -54,11 +54,7 @@
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>cloud.timo.TimoCloud</groupId>
<artifactId>TimoCloud-API</artifactId>
<version>5.3.1</version>
</dependency>
<!--BungeeCordAPI-->
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-api</artifactId>
Expand All @@ -73,6 +69,13 @@
<systemPath>${project.basedir}/libs/TimoCloud.jar</systemPath>
<scope>system</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>

</dependencies>

</project>
28 changes: 28 additions & 0 deletions src/main/java/cloud/timo/CloudNotify/CloudNotify.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package cloud.timo.CloudNotify;

import cloud.timo.CloudNotify.commands.CloudNotifyCommand;
import cloud.timo.CloudNotify.listeners.ServerRegisterListener;
import cloud.timo.CloudNotify.listeners.ServerUnregisterListener;
import cloud.timo.CloudNotify.managers.DatabaseManager;
import cloud.timo.CloudNotify.managers.FileManager;
import cloud.timo.CloudNotify.managers.PluginMessageManager;
import cloud.timo.CloudNotify.utils.Helper;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.plugin.Plugin;

public class CloudNotify extends Plugin {
private static CloudNotify instance;
private FileManager fileManager;
private DatabaseManager databaseManager;
private CloudNotifyCommand cloudNotifyCommand;
private PluginMessageManager pluginMessageManager;
private Helper helper;
private String prefix;

Expand All @@ -23,8 +29,11 @@ public void onEnable() {
private void makeInstances() {
setInstance(this);
setFileManager(new FileManager());
setDatabaseManager(new DatabaseManager());
setHelper(new Helper());
setPluginMessageManager(new PluginMessageManager());
setPrefix(ChatColor.translateAlternateColorCodes('&', fileManager.getConfig().getString("Prefix") + " "));
setCloudNotifyCommand(new CloudNotifyCommand("cloudNotify", "cloudNotify.command.notify"));
}

@Override
Expand Down Expand Up @@ -69,4 +78,23 @@ public void setPrefix(String prefix) {
this.prefix = prefix;
}

public DatabaseManager getDatabaseManager() {
return databaseManager;
}

public void setCloudNotifyCommand(CloudNotifyCommand cloudNotifyCommand) {
this.cloudNotifyCommand = cloudNotifyCommand;
}

public void setDatabaseManager(DatabaseManager databaseManager) {
this.databaseManager = databaseManager;
}

public PluginMessageManager getPluginMessageManager() {
return pluginMessageManager;
}

public void setPluginMessageManager(PluginMessageManager pluginMessageManager) {
this.pluginMessageManager = pluginMessageManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cloud.timo.CloudNotify.commands;

import cloud.timo.CloudNotify.CloudNotify;
import cloud.timo.CloudNotify.managers.MessageManager;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;

public class CloudNotifyCommand extends Command {

public CloudNotifyCommand(String name, String permissions) {
super(name, permissions);
ProxyServer.getInstance().getPluginManager().registerCommand(CloudNotify.getInstance(), this);
}

@Override
public void execute(CommandSender commandSender, String[] args) {
if (!(commandSender instanceof ProxiedPlayer)) {
MessageManager.sendMessage(commandSender, CloudNotify.getInstance().getFileManager().getMessages().getString("onlyPlayer"));
return;
}
ProxiedPlayer proxiedPlayer = (ProxiedPlayer) commandSender;

if (args.length == 0) {
boolean currentState = CloudNotify.getInstance().getDatabaseManager().getNotifyState(proxiedPlayer.getUniqueId());
currentState = !currentState;

CloudNotify.getInstance().getDatabaseManager().setNotifyState(proxiedPlayer.getUniqueId(), currentState);
MessageManager.sendMessage(commandSender, CloudNotify.getInstance().getFileManager().getMessages().getString("changeStateCommand").replace("{0}", (currentState) ? CloudNotify.getInstance().getFileManager().getMessages().getString("enabled") :CloudNotify.getInstance().getFileManager().getMessages().getString("disabled")));
}
}

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package cloud.timo.CloudNotify.listeners;

import cloud.timo.CloudNotify.utils.NotifyType;
import cloud.timo.CloudNotify.CloudNotify;
import cloud.timo.TimoCloud.api.TimoCloudAPI;
import cloud.timo.TimoCloud.api.events.EventHandler;
import cloud.timo.TimoCloud.api.events.Listener;
import cloud.timo.CloudNotify.CloudNotify;
import cloud.timo.TimoCloud.api.events.server.ServerRegisterEvent;

public class ServerRegisterListener implements Listener {

public ServerRegisterListener(){
TimoCloudAPI.getEventImplementation().registerListener(this);
public ServerRegisterListener() {
TimoCloudAPI.getEventAPI().registerListener(this);
}

@EventHandler
public void onServerRegister(ServerRegisterEvent event) {
CloudNotify.getInstance().getHelper().notify(NotifyType.REGISTER, event.getServer());
CloudNotify.getInstance().getHelper().initiatingNotification(event, event.getServer());
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cloud.timo.CloudNotify.listeners;

import cloud.timo.CloudNotify.utils.NotifyType;
import cloud.timo.TimoCloud.api.TimoCloudAPI;
import cloud.timo.TimoCloud.api.events.EventHandler;
import cloud.timo.TimoCloud.api.events.Listener;
Expand All @@ -9,13 +8,13 @@

public class ServerUnregisterListener implements Listener {

public ServerUnregisterListener(){
TimoCloudAPI.getEventImplementation().registerListener(this);
public ServerUnregisterListener() {
TimoCloudAPI.getEventAPI().registerListener(this);
}

@EventHandler
public void onServerUnregister(ServerUnregisterEvent event) {
CloudNotify.getInstance().getHelper().notify(NotifyType.UNREGISTER, event.getServer());
CloudNotify.getInstance().getHelper().initiatingNotification(event, event.getServer());
}

}
77 changes: 77 additions & 0 deletions src/main/java/cloud/timo/CloudNotify/managers/DatabaseManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cloud.timo.CloudNotify.managers;

import cloud.timo.CloudNotify.CloudNotify;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.UUID;

public class DatabaseManager extends Thread {
public MysqlDataSource dataSource;

public DatabaseManager() {
start();
dataSource = new MysqlDataSource();
dataSource.setUseSSL(CloudNotify.getInstance().getFileManager().getConfig().getBoolean("MySQL.useSSL"));
dataSource.setUser(CloudNotify.getInstance().getFileManager().getConfig().getString("MySQL.user"));
dataSource.setPassword(CloudNotify.getInstance().getFileManager().getConfig().getString("MySQL.password"));
dataSource.setDatabaseName(CloudNotify.getInstance().getFileManager().getConfig().getString("MySQL.databaseName"));
dataSource.setServerName(CloudNotify.getInstance().getFileManager().getConfig().getString("MySQL.host"));
dataSource.setPort(CloudNotify.getInstance().getFileManager().getConfig().getInt("MySQL.port"));
execute("CREATE TABLE IF NOT EXISTS cloudNotify (uuid VARCHAR(128) NOT NULL, state BOOLEAN NOT NULL, PRIMARY KEY(uuid))");
}

private void execute(String request) {
try {
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
statement.executeUpdate(request);

statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public boolean notNull(UUID uuid) {
try {
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM cloudNotify WHERE uuid='" + uuid.toString() + "'");
while (resultSet.next()) return true;

resultSet.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

public void setNotifyState(UUID uuid, boolean b) {
execute("REPLACE INTO cloudNotify (uuid, state) VALUES ('" + uuid.toString() + "', " + b + ")");
}

public boolean getNotifyState(UUID uuid) {
boolean state = false;

try {
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT state FROM cloudNotify WHERE uuid='" + uuid.toString() + "'");
resultSet.next();
state = resultSet.getBoolean("state");
resultSet.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
return state;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public class FileManager {
private String pluginsDirectory = "plugins/CloudNotify/";
private String configsDirectory = pluginsDirectory + "bungeecord/";
private String configsDirectory = pluginsDirectory;
private File configFile;
private Configuration config;
private File messagesFile;
Expand Down Expand Up @@ -40,7 +40,7 @@ public void load() {
config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);

//Load messagesFile
messagesFile = new File(configsDirectory, "config.yml");
messagesFile = new File(configsDirectory, "messages.yml");
if (!messagesFile.exists()) {
messagesFile.createNewFile();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package cloud.timo.CloudNotify.managers;

import cloud.timo.CloudNotify.CloudNotify;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.TextComponent;

public class MessageManager {

public static void sendMessageToTeam(String message) {
ProxyServer.getInstance().getPlayers().forEach(proxiedPlayer -> {
if (proxiedPlayer.hasPermission("cloudnotify.notify"))
proxiedPlayer.sendMessage(new TextComponent(CloudNotify.getInstance().getPrefix() + message));
});
public static void sendMessage(CommandSender proxiedPlayer, String message) {
proxiedPlayer.sendMessage(TextComponent.fromLegacyText(ChatColor.translateAlternateColorCodes('&', CloudNotify.getInstance().getPrefix() + message)));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cloud.timo.CloudNotify.managers;

import cloud.timo.CloudNotify.CloudNotify;
import cloud.timo.TimoCloud.api.TimoCloudAPI;
import cloud.timo.TimoCloud.api.messages.listeners.MessageListener;
import cloud.timo.TimoCloud.api.messages.objects.AddressedPluginMessage;

public class PluginMessageManager implements MessageListener {

public PluginMessageManager(){
TimoCloudAPI.getMessageAPI().registerMessageListener(this::onPluginMessage, "CLOUDNOTIFY_NOTIFICATION");
}

@Override
public void onPluginMessage(AddressedPluginMessage addressedPluginMessage) {
CloudNotify.getInstance().getHelper().notify(addressedPluginMessage.getMessage());
}
}
Loading

0 comments on commit e3ecf74

Please sign in to comment.