Merge branch 'database' of Etzelia/Mixtape into master

main
Etzelia 2018-10-20 01:45:02 +02:00 committed by Gitea
commit dbd7a73a15
8 changed files with 248 additions and 19 deletions

View File

@ -8,4 +8,5 @@ Changelogs
.. toctree::
:maxdepth: 1
v1.0 <v1.0>
v1.0 <v1.0>
v2.0 <v2.0>

View File

@ -0,0 +1,14 @@
.. include:: ../common.rst
.. _mixtape_v2.0:
Mixtape v2.0
========
Additions
---------
Added H2 database support!
Bug Fixes
---------
None

11
pom.xml
View File

@ -6,7 +6,7 @@
<groupId>xyz.etztech</groupId>
<artifactId>Mixtape</artifactId>
<version>1.0</version>
<version>2.0</version>
<packaging>jar</packaging>
<name>Mixtape</name>
@ -39,9 +39,6 @@
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
@ -72,5 +69,11 @@
<version>1.13.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>xyz.etztech</groupId>
<artifactId>EtzCore</artifactId>
<version>1.0.5</version>
</dependency>
</dependencies>
</project>

View File

@ -2,27 +2,40 @@ package xyz.etztech.mixtape;
public class Alias {
private String alias;
private String command;
private String uuid;
private String from;
private String to;
private Database.AliasType type;
public String getAlias() {
return alias;
public String getUuid() {
return uuid;
}
public void setAlias(String alias) {
this.alias = alias;
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getCommand() {
return command;
public String getFrom() {
return from;
}
public void setCommand(String command) {
this.command = command;
public void setFrom(String from) {
this.from = from;
}
@Override
public String toString() {
return alias + " -> " + command;
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public Database.AliasType getType() {
return type;
}
public void setType(Database.AliasType type) {
this.type = type;
}
}

View File

@ -0,0 +1,122 @@
package xyz.etztech.mixtape;
import org.bukkit.plugin.java.JavaPlugin;
import xyz.etztech.core.db.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class Database extends DataSource {
public enum AliasType {
CHAT, COMMAND
}
public Database(JavaPlugin plugin) {
super(plugin);
create();
}
private void create() {
try {
Connection connection = getConnection();
final String CREATE = "CREATE TABLE IF NOT EXISTS `aliases` ( `id` INT AUTO_INCREMENT PRIMARY KEY, `uuid` VARCHAR(36) NOT NULL, `from` VARCHAR(200) NOT NULL, `to` VARCHAR(200) NOT NULL, `type` VARCHAR(6) NOT NULL );";
PreparedStatement pst = connection.prepareStatement(CREATE);
pst.execute();
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void insert(String uuid, String from, String to, AliasType type) {
try {
int existingId = exists(uuid, from);
if (existingId > 0) {
delete(existingId);
}
Connection connection = getConnection();
final String MERGE = "INSERT INTO `aliases` (`uuid`, `from`, `to`, `type`) VALUES (?, ?, ?, ?);";
PreparedStatement pst = connection.prepareStatement(MERGE);
pst.setString(1, uuid);
pst.setString(2, from.toLowerCase());
pst.setString(3, to);
pst.setString(4, type.name());
pst.executeUpdate();
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void purge(String uuid) {
try {
Connection connection = getConnection();
final String PURGE = "DELETE FROM `aliases` WHERE `uuid` = ?;";
PreparedStatement pst = connection.prepareStatement(PURGE);
pst.setString(1, uuid);
pst.executeUpdate();
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public List<Alias> getAliases(String uuid) {
List<Alias> results = new ArrayList<>();
try {
Connection connection = getConnection();
final String SELECT = "SELECT `uuid`, `from`, `to`, `type` FROM `aliases` WHERE `uuid` = ?;";
PreparedStatement pst = connection.prepareStatement(SELECT);
pst.setString(1, uuid);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
Alias alias = new Alias();
alias.setUuid(uuid);
alias.setFrom(rs.getString("from"));
alias.setTo(rs.getString("to"));
alias.setType(AliasType.valueOf(rs.getString("type")));
results.add(alias);
}
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return results;
}
public int exists(String uuid, String from) {
try {
Connection connection = getConnection();
final String SELECT = "SELECT `id` FROM `aliases` WHERE `uuid` = ? AND `from` = ?;";
PreparedStatement pst = connection.prepareStatement(SELECT);
pst.setString(1, uuid);
pst.setString(2, from.toLowerCase());
ResultSet rs = pst.executeQuery();
if (rs.next()) {
return rs.getInt("id");
}
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return 0;
}
public void delete(int id) {
try {
Connection connection = getConnection();
final String DELETE = "DELETE FROM `aliases` WHERE `id` = ?;";
PreparedStatement pst = connection.prepareStatement(DELETE);
pst.setInt(1, id);
pst.executeUpdate();
connection.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

View File

@ -4,9 +4,10 @@ import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import xyz.etztech.mixtape.commands.CommandAlias;
import xyz.etztech.mixtape.commands.CommandSlashAlias;
import xyz.etztech.mixtape.commands.CommandMixtape;
import xyz.etztech.mixtape.commands.CommandSlashAlias;
import xyz.etztech.mixtape.listeners.CommandPreprocessListener;
import xyz.etztech.mixtape.listeners.PlayerJoinListener;
import java.util.*;
import java.util.logging.Logger;
@ -17,6 +18,7 @@ public final class Mixtape extends JavaPlugin {
private static Mixtape instance;
public static FileConfiguration config;
private static Database database;
private static int loops;
@ -30,12 +32,14 @@ public final class Mixtape extends JavaPlugin {
instance = this;
saveDefaultConfig();
reloadConfig();
database = new Database(this);
this.getCommand("mixtape").setExecutor(new CommandMixtape(this));
this.getCommand("alias").setExecutor(new CommandAlias(this));
this.getCommand("/alias").setExecutor(new CommandSlashAlias(this));
getServer().getPluginManager().registerEvents(new CommandPreprocessListener(this), this);
getServer().getPluginManager().registerEvents(new PlayerJoinListener(this), this);
}
@Override
@ -51,6 +55,7 @@ public final class Mixtape extends JavaPlugin {
validate();
}
public void log(String message) {
log.info( "[Mixtape]: " + message );
}
@ -59,6 +64,26 @@ public final class Mixtape extends JavaPlugin {
return instance;
}
public static Database getDatabase() {
return database;
}
public static void loadAliases(String uuid) {
Map<String, String> chats = new HashMap<>();
Map<String, String> commands = new HashMap<>();
List<Alias> aliases = database.getAliases(uuid);
for (Alias alias : aliases) {
if (alias.getType().equals(Database.AliasType.CHAT)) {
chats.put(alias.getFrom(), alias.getTo());
} else if (alias.getType().equals(Database.AliasType.COMMAND)) {
commands.put(alias.getFrom(), alias.getTo());
}
}
chatAliases.put(uuid, chats);
commandAliases.put(uuid, commands);
Bukkit.getConsoleSender().sendMessage(String.format("Loaded %s aliases for %s", aliases.size(), uuid));
}
public static void setCommandAlias(String uuid, String alias, String command) {
Map<String, String> map = new HashMap<>();
@ -67,6 +92,7 @@ public final class Mixtape extends JavaPlugin {
}
map.put(alias, command);
commandAliases.put(uuid, map);
database.insert(uuid, alias, command, Database.AliasType.COMMAND);
}
public static Map<String, String> getCommandAliases(String uuid) {
@ -81,6 +107,7 @@ public final class Mixtape extends JavaPlugin {
}
map.put(alias, command);
chatAliases.put(uuid, map);
database.insert(uuid, alias, command, Database.AliasType.CHAT);
}
public static Map<String, String> getChatAliases(String uuid) {

View File

@ -1,10 +1,13 @@
package xyz.etztech.mixtape.commands;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import xyz.etztech.mixtape.Mixtape;
import java.util.ArrayList;
@ -28,6 +31,21 @@ public class CommandMixtape implements CommandExecutor {
if ("reload".equalsIgnoreCase(args[0])) {
plugin.reloadConfig();
sender.sendMessage(ChatColor.GREEN + "Mixtape Reloaded.");
} else if ("purge".equalsIgnoreCase(args[0])) {
if (args.length > 1) {
Player player = Bukkit.getPlayerExact(args[1]);
if (player != null) {
Mixtape.getDatabase().purge(player.getUniqueId().toString());
sender.sendMessage(ChatColor.GREEN + "Purged all of " + player.getName() + "'s aliases.");
} else {
sender.sendMessage(ChatColor.RED + "No player found.");
}
} else {
for (OfflinePlayer offline : Bukkit.getBannedPlayers()) {
Mixtape.getDatabase().purge(offline.getUniqueId().toString());
}
sender.sendMessage(ChatColor.GREEN + "Purged all banned players' aliases.");
}
}
}
return true;

View File

@ -0,0 +1,31 @@
package xyz.etztech.mixtape.listeners;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import xyz.etztech.mixtape.Mixtape;
public class PlayerJoinListener implements Listener {
Mixtape plugin;
public PlayerJoinListener(Mixtape plugin) {
this.plugin = plugin;
}
@EventHandler(priority= EventPriority.HIGH, ignoreCancelled=true)
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
Mixtape.loadAliases(player.getUniqueId().toString());
}
}