Skip to content

Commit

Permalink
🔨 Fixed MySQL for nodes synchronization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo0111 committed Apr 23, 2021
1 parent 9f2ab61 commit 8d18c2e
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,6 @@ buildNumber.properties

# Common working directory
run/

gradle.properties
ignore/
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ You can read the plugin wiki [here](https://docs.rocketplugins.space/rocketplace
- [Issue Tracker][issues]
- [Discord][discord]

---

## Special thanks
[![jetbrains]](https://jb.gg/OpenSource)

[release]: https://img.shields.io/github/v/release/Lorenzo0111/RocketPlaceholders?style=for-the-badge&logo=appveyor
[releaseLink]: https://github.com/Lorenzo0111/RocketPlaceholders/releases/latest

Expand All @@ -24,5 +29,5 @@ You can read the plugin wiki [here](https://docs.rocketplugins.space/rocketplace

[discord]: https://bit.ly/RocketServer_DS
[spigot]: https://www.spigotmc.org/resources/rocketplaceholders-custom-placeholders.82678/

[logo]: https://i.ibb.co/kqTB43K/Rocket-Placeholders-Banner.png
[jetbrains]: https://github.com/Lorenzo0111/RocketPlaceholders/blob/master/media/jetbrains.png?raw=true
[logo]: https://i.ibb.co/kqTB43K/Rocket-Placeholders-Banner.png
8 changes: 5 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ dependencies {
exclude module: 'spigot'
}
implementation 'mysql:mysql-connector-java:8.0.24'
implementation 'com.google.code.gson:gson:2.8.6'
}

shadowJar {
relocate('google.protobuf', 'me.Lorenzo0111.lib.protobuf')
relocate('com.mysql', 'me.Lorenzo0111.lib.mysql')
relocate('com.google.protobuf', 'me.Lorenzo0111.lib.google.protobuf')
relocate('com.google.gson','me.lorenzo0111.lib.gson')
relocate('google.protobuf', 'me.lorenzo0111.lib.protobuf')
relocate('com.mysql', 'me.lorenzo0111.lib.mysql')
relocate('com.google.protobuf', 'me.lorenzo0111.lib.google.protobuf')
}

javadoc {
Expand Down
Binary file added media/jetbrains.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ public final class RocketPlaceholders extends JavaPlugin {
private StorageManager storageManager;
private PluginLoader loader;

/*
Plugin by Lorenzo0111 - https://github.com/Lorenzo0111
*/

@Override
public void onEnable() {

Expand Down Expand Up @@ -74,8 +68,8 @@ public void onEnable() {

@Override
public void onDisable() {
getLogger().info("Plugin disabled! Thanks for using " + this.getDescription().getName() + " v." + this.getDescription().getVersion());
Bukkit.getScheduler().cancelTasks(this);
getLogger().info("Plugin disabled! Thanks for using " + this.getDescription().getName() + " v." + this.getDescription().getVersion());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@
import me.lorenzo0111.rocketplaceholders.RocketPlaceholders;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.Map;

public abstract class Requirement {
protected final RocketPlaceholders plugin;
protected final Map<String,Object> databaseInfo = new HashMap<>();

public Map<String,Object> getDatabaseInfo() {
return this.databaseInfo;
}

/**
* @param plugin Plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class HasGroupCondition extends Requirement {
public HasGroupCondition(RocketPlaceholders plugin,String group) {
super(plugin);
this.group = group;
this.getDatabaseInfo().put("value",group);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.Objects;

public class HasItemCondition extends Requirement {
private final ItemStack item;

Expand All @@ -44,6 +46,9 @@ public String toString() {
public HasItemCondition(ItemStack item, RocketPlaceholders plugin) {
super(plugin);
this.item = item;
this.getDatabaseInfo().put("item_name", Objects.requireNonNull(item.getItemMeta()).getDisplayName());
this.getDatabaseInfo().put("item_lore",Objects.requireNonNull(item.getItemMeta()).getLore());
this.getDatabaseInfo().put("item_material",item.getType().toString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class HasMoneyCondition extends Requirement {
public HasMoneyCondition(RocketPlaceholders plugin, long money) {
super(plugin);
this.money = money;
this.getDatabaseInfo().put("value",String.valueOf(money));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class HasPermissionCondition extends Requirement {
public HasPermissionCondition(RocketPlaceholders plugin, String permission) {
super(plugin);
this.permission = permission;
this.getDatabaseInfo().put("value",permission);
}

@Override
Expand All @@ -46,4 +47,12 @@ public boolean apply(Player player) {
public RequirementType getType() {
return RequirementType.PERMISSION;
}

@Override
public String toString() {
return "{" +
"type=" + getType() + "," +
"permission='" + permission + "'" +
"}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public JSCondition(RocketPlaceholders plugin,String expression) {
this.engine = new ScriptEngineManager().getEngineByName("javascript");
this.engine.put("Server", Bukkit.getServer());
this.expression = expression;
this.getDatabaseInfo().put("value",expression);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,33 @@

package me.lorenzo0111.rocketplaceholders.database;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import me.lorenzo0111.rocketplaceholders.RocketPlaceholders;
import me.lorenzo0111.rocketplaceholders.creator.Placeholder;
import me.lorenzo0111.rocketplaceholders.creator.conditions.ConditionNode;
import me.lorenzo0111.rocketplaceholders.creator.conditions.Requirement;
import me.lorenzo0111.rocketplaceholders.creator.conditions.RequirementType;
import me.lorenzo0111.rocketplaceholders.creator.conditions.engine.Requirements;
import me.lorenzo0111.rocketplaceholders.storage.ConfigManager;
import me.lorenzo0111.rocketplaceholders.storage.Storage;
import me.lorenzo0111.rocketplaceholders.storage.StorageManager;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.scheduler.BukkitRunnable;

import java.lang.reflect.Type;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.concurrent.CompletableFuture;

public class DatabaseManager {
private final RocketPlaceholders plugin;
private final ConfigurationSection mysqlSection;
private Connection connection;
private final Gson gson = new Gson();

public DatabaseManager(RocketPlaceholders plugin) {
this.plugin = plugin;
Expand All @@ -53,14 +62,20 @@ public DatabaseManager(RocketPlaceholders plugin) {
}

try {
Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
this.plugin.getLogger().info("MySQL driver is not installed, please install it to use the mysql function");
return;
}

final String jdbc = mysqlSection.getString("jdbc");

try {
this.connection = DriverManager.getConnection("jdbc:mysql://" + mysqlSection.getString("ip") + ":" + mysqlSection.getInt("port") + "/" + mysqlSection.getString("database"), mysqlSection.getString("username"), mysqlSection.getString("password"));
if (jdbc == null) {
this.connection = DriverManager.getConnection("jdbc:mysql://" + mysqlSection.getString("ip") + ":" + mysqlSection.getInt("port") + "/" + mysqlSection.getString("database"), mysqlSection.getString("username"), mysqlSection.getString("password"));
} else {
this.connection = DriverManager.getConnection(jdbc);
}
} catch (SQLException exception) {
exception.printStackTrace();
}
Expand All @@ -79,14 +94,67 @@ public void run() {
"`text` varchar(255) NOT NULL," +
"PRIMARY KEY (`identifier`)" +
");");

statement.executeUpdate("CREATE TABLE IF NOT EXISTS `rp_nodes` (" +
"`identifier` varchar(255) NOT NULL," +
"`type` varchar(255) NOT NULL," +
"`value` varchar(255)," +
"`item_material` varchar(255)," +
"`item_name` varchar(255)," +
"`item_lore` varchar(255)," +
"`text` varchar(255)" +
");");
} catch (SQLException exception) {
plugin.getLogger().severe("Error while creating tables: " + exception);
}
}
}.runTaskAsynchronously(plugin);
}

public CompletableFuture<Multimap<String, ConditionNode>> getNodes() {
CompletableFuture<Multimap<String,ConditionNode>> completableFuture = new CompletableFuture<>();

new BukkitRunnable() {

@Override
public void run() {

try {
final Statement statement = connection.createStatement();

ResultSet set = statement.executeQuery("SELECT * FROM rp_nodes;");

Multimap<String,ConditionNode> nodes = ArrayListMultimap.create();

while (set.next()) {
List<String> itemLore = new ArrayList<>();
Material material = null;
if (set.getString("item_lore") != null) {
Type listType = new TypeToken<List<String>>() {}.getType();
itemLore = gson.fromJson(set.getString("item_lore"),listType);
}

if (set.getString("item_material") != null) {
material = Material.valueOf(set.getString("item_material"));
}


final Requirement requirement = new Requirements(plugin).createRequirement(RequirementType.valueOf(set.getString("type")),set.getString("value"), material,set.getString("item_name"),itemLore);
nodes.put(set.getString("identifier"), new ConditionNode(requirement, set.getString("text")));
}

completableFuture.complete(nodes);
} catch (SQLException exception) {
exception.printStackTrace();
}

}

}.runTaskAsynchronously(plugin);

return completableFuture;
}


public void sync() {
new BukkitRunnable() {

Expand All @@ -98,12 +166,49 @@ public void run() {

try {
final PreparedStatement statement = connection.prepareStatement("insert into rp_placeholders (`identifier`, `text`) VALUES (?,?);");
final PreparedStatement nodeStatement = connection.prepareStatement("insert into rp_nodes (`identifier`, `type`, `value`, `item_material`, `item_name`,`item_lore`, `text`) VALUES (?,?,?,?,?,?,?);");

hashMap.forEach((s, placeholder) -> {
try {
statement.setString(1, s);
statement.setString(2, placeholder.getText());
statement.executeUpdate();

if (placeholder.hasConditionNodes() && placeholder.getConditionNodes() != null) {

for (ConditionNode node : placeholder.getConditionNodes()) {
nodeStatement.setString(1, s);
nodeStatement.setString(2, node.getRequirement().getType().toString());
nodeStatement.setString(3,null); // Value
nodeStatement.setString(4,null); // Item Material
nodeStatement.setString(5,null); // Item Name
nodeStatement.setString(6,null); // Item Lore
nodeStatement.setString(7, node.getText());

switch (node.getRequirement().getType()) {
case GROUP:
case JAVASCRIPT:
case MONEY:
case PERMISSION:
nodeStatement.setObject(3,node.getRequirement().getDatabaseInfo().get("value"));
break;
case ITEM:
nodeStatement.setObject(4,node.getRequirement().getDatabaseInfo().get("item_material"));
nodeStatement.setObject(5,node.getRequirement().getDatabaseInfo().get("item_name"));

if (node.getRequirement().getDatabaseInfo().containsKey("item_lore") && node.getRequirement().getDatabaseInfo().get("item_lore") != null) {
nodeStatement.setObject(6,
gson.toJson(node.getRequirement().getDatabaseInfo().get("item_lore")));
}
break;
default:
break;
}

nodeStatement.executeUpdate();
}

}
} catch (SQLException exception) {
exception.printStackTrace();
}
Expand All @@ -113,7 +218,7 @@ public void run() {
}



}

}.runTaskAsynchronously(plugin);
Expand All @@ -122,7 +227,8 @@ public void run() {
public CompletableFuture<Map<String, Placeholder>> getFromDatabase() {
CompletableFuture<Map<String, Placeholder>> completableFuture = new CompletableFuture<>();

new BukkitRunnable() {
getNodes().thenAccept(nodes -> new BukkitRunnable() {

@Override
public void run() {
try {
Expand All @@ -132,15 +238,16 @@ public void run() {
Map<String, Placeholder> hashMap = new HashMap<>();

while (resultSet.next()) {
hashMap.put(resultSet.getString("identifier"), new Placeholder(resultSet.getString("identifier"), plugin, resultSet.getString("text")));
hashMap.put(resultSet.getString("identifier"), new Placeholder(resultSet.getString("identifier"), plugin, resultSet.getString("text"), new ArrayList<>(nodes.get(resultSet.getString("identifier")))));
}

completableFuture.complete(hashMap);
}catch (SQLException exception) {
} catch (SQLException exception) {
exception.printStackTrace();
}
}
}.runTaskAsynchronously(plugin);

}.runTaskAsynchronously(plugin));

return completableFuture;
}
Expand All @@ -160,6 +267,7 @@ public void run() {
Statement statement = connection.createStatement();

statement.executeUpdate("DELETE FROM rp_placeholders;");
statement.executeUpdate("DELETE FROM rp_nodes;");

completableFuture.complete(true);
} catch (SQLException exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@

public class JoinListener implements Listener {

/*
Plugin by Lorenzo0111 - https://github.com/Lorenzo0111
*/

private final RocketPlaceholders plugin;
private final UpdateChecker updateChecker;

Expand Down
Loading

0 comments on commit 8d18c2e

Please sign in to comment.