Merge branch 'development'

This commit is contained in:
Christian Koop 2022-09-05 23:05:50 +02:00
commit b8f5334b45
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
10 changed files with 450 additions and 97 deletions

24
pom.xml
View File

@ -3,7 +3,7 @@
<groupId>com.songoda</groupId>
<artifactId>EpicHeads</artifactId>
<version>3.3.2</version>
<version>3.4.0</version>
<build>
<defaultGoal>clean install</defaultGoal>
@ -48,7 +48,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0</version>
<executions>
<execution>
@ -93,13 +93,6 @@
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>apache.snapshots</id>
<url>https://repository.apache.org/snapshots/</url>
</pluginRepository>
</pluginRepositories>
<repositories>
<repository>
<id>public</id>
@ -114,15 +107,22 @@
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.18</version>
<artifactId>spigot-api</artifactId>
<version>1.19-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.songoda</groupId>
<artifactId>SongodaCore</artifactId>
<version>2.6.13</version>
<version>2.6.16</version>
<scope>compile</scope>
</dependency>
</dependencies>

View File

@ -5,20 +5,16 @@ import com.songoda.core.SongodaPlugin;
import com.songoda.core.commands.CommandManager;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.configuration.Config;
import com.songoda.core.database.DataMigrationManager;
import com.songoda.core.database.DatabaseConnector;
import com.songoda.core.database.SQLiteConnector;
import com.songoda.core.gui.GuiManager;
import com.songoda.core.hooks.EconomyManager;
import com.songoda.core.hooks.PluginHook;
import com.songoda.core.hooks.economies.Economy;
import com.songoda.epicheads.commands.CommandAdd;
import com.songoda.epicheads.commands.CommandBase64;
import com.songoda.epicheads.commands.CommandEpicHeads;
import com.songoda.epicheads.commands.CommandGive;
import com.songoda.epicheads.commands.CommandGiveToken;
import com.songoda.epicheads.commands.CommandHelp;
import com.songoda.epicheads.commands.CommandReload;
import com.songoda.epicheads.commands.CommandSearch;
import com.songoda.epicheads.commands.CommandSettings;
import com.songoda.epicheads.commands.CommandUrl;
import com.songoda.epicheads.commands.*;
import com.songoda.epicheads.database.DataManager;
import com.songoda.epicheads.database.migrations._1_InitialMigration;
import com.songoda.epicheads.head.Category;
import com.songoda.epicheads.head.Head;
import com.songoda.epicheads.head.HeadManager;
@ -32,21 +28,17 @@ import com.songoda.epicheads.utils.storage.Storage;
import com.songoda.epicheads.utils.storage.StorageRow;
import com.songoda.epicheads.utils.storage.types.StorageYaml;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.plugin.PluginManager;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@ -60,7 +52,8 @@ public class EpicHeads extends SongodaPlugin {
private CommandManager commandManager;
private PluginHook itemEconomyHook;
private Storage storage;
private DatabaseConnector databaseConnector;
private DataManager dataManager;
public static EpicHeads getInstance() {
return INSTANCE;
@ -74,8 +67,8 @@ public class EpicHeads extends SongodaPlugin {
@Override
public void onPluginDisable() {
this.storage.closeConnection();
this.saveToFile();
shutdownDataManager(this.dataManager);
this.databaseConnector.closeConnection();
}
@Override
@ -117,8 +110,6 @@ public class EpicHeads extends SongodaPlugin {
new CommandUrl(this)
);
this.storage = new StorageYaml(this);
// Register Listeners
guiManager.init();
PluginManager pluginManager = Bukkit.getPluginManager();
@ -133,32 +124,99 @@ public class EpicHeads extends SongodaPlugin {
loadHeads();
int timeout = Settings.AUTOSAVE.getInt() * 60 * 20;
Bukkit.getScheduler().runTaskTimerAsynchronously(this, this::saveToFile, timeout, timeout);
Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> dataManager.saveAllPlayers(), timeout, timeout);
}
@Override
public void onDataLoad() {
// Adding in favorites.
// Database stuff.
this.databaseConnector = new SQLiteConnector(this);
this.getLogger().info("Data handler connected using SQLite.");
this.dataManager = new DataManager(this.databaseConnector, this);
DataMigrationManager dataMigrationManager = new DataMigrationManager(this.databaseConnector, this.dataManager,
new _1_InitialMigration());
dataMigrationManager.runMigrations();
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
// Legacy data! Yay!
File folder = getDataFolder();
File dataFile = new File(folder, "data.yml");
boolean converted = false;
if (dataFile.exists()) {
converted = true;
Storage storage = new StorageYaml(this);
if (storage.containsGroup("players")) {
console.sendMessage("[" + getDescription().getName() + "] " + ChatColor.RED +
"Conversion process starting. Do NOT turn off your server. " +
"EpicHeads hasn't fully loaded yet, so make sure users don't" +
"interact with the plugin until the conversion process is complete.");
List<EPlayer> players = new ArrayList<>();
for (StorageRow row : storage.getRowsByGroup("players")) {
if (row.get("uuid").asObject() == null) {
continue;
}
EPlayer player = new EPlayer(
players.add(new EPlayer(
UUID.fromString(row.get("uuid").asString()),
(List<String>) row.get("favorites").asObject());
(List<String>) row.get("favorites").asObject()));
}
dataManager.migratePlayers(players);
}
this.playerManager.addPlayer(player);
if (storage.containsGroup("local")) {
for (StorageRow row : storage.getRowsByGroup("local")) {
String tagStr = row.get("category").asString();
Optional<Category> tagOptional = headManager.getCategories().stream()
.filter(t -> t.getName().equalsIgnoreCase(tagStr)).findFirst();
Category category = tagOptional.orElseGet(() -> new Category(tagStr));
Head head = new Head(row.get("id").asInt(),
row.get("name").asString(),
row.get("url").asString(),
category,
true,
null,
(byte) 0);
dataManager.createLocalHead(head);
}
if (storage.containsGroup("disabled")) {
List<Integer> ids = new ArrayList<>();
for (StorageRow row : storage.getRowsByGroup("disabled")) {
ids.add(row.get("id").asInt());
}
dataManager.migrateDisabledHead(ids);
}
}
// Save data initially so that if the person reloads again fast they don't lose all their data.
this.saveToFile();
dataFile.delete();
}
private void saveToFile() {
storage.doSave();
final boolean finalConverted = converted;
dataManager.queueAsync(() -> {
if (finalConverted) {
console.sendMessage("[" + getDescription().getName() + "] " + ChatColor.GREEN + "Conversion complete :)");
}
this.dataManager.getLocalHeads((heads) -> {
this.headManager.addLocalHeads(heads);
getLogger().info("Loaded " + headManager.getHeads().size() + " heads");
});
this.dataManager.getDisabledHeads((ids) -> {
for (int id : ids) {
headManager.disableHead(new Head(id, false));
}
});
}, "create");
});
}
private void downloadHeads() {
@ -212,44 +270,6 @@ public class EpicHeads extends SongodaPlugin {
headManager.addHead(head);
}
if (storage.containsGroup("local")) {
for (StorageRow row : storage.getRowsByGroup("local")) {
String tagStr = row.get("category").asString();
Optional<Category> tagOptional = headManager.getCategories().stream()
.filter(t -> t.getName().equalsIgnoreCase(tagStr)).findFirst();
Category category = tagOptional.orElseGet(() -> new Category(tagStr));
Head head = new Head(row.get("id").asInt(),
row.get("name").asString(),
row.get("url").asString(),
category,
true,
null,
(byte) 0);
if (!tagOptional.isPresent())
headManager.addCategory(category);
headManager.addLocalHead(head);
}
}
if (storage.containsGroup("disabled")) {
for (StorageRow row : storage.getRowsByGroup("disabled")) {
headManager.disableHead(new Head(row.get("id").asInt(), false));
}
}
// convert disabled heads
if (config.contains("Main.Disabled Global Heads")) {
for (int id : config.getIntegerList("Main.Disabled Global Heads")) {
EpicHeads.getInstance().getHeadManager().disableHead(new Head(id, false));
}
config.set("Main.Disabled Global Heads", null);
}
getLogger().info("Loaded " + headManager.getHeads().size() + " heads");
} catch (IOException | ParseException ex) {
getLogger().warning(() -> {
if (ex instanceof ParseException) {
@ -276,12 +296,9 @@ public class EpicHeads extends SongodaPlugin {
@Override
public void onConfigReload() {
saveToFile();
this.setLocale(getConfig().getString("System.Language Mode"), true);
this.locale.reloadMessages();
saveToFile();
downloadHeads();
loadHeads();
}
@ -302,4 +319,12 @@ public class EpicHeads extends SongodaPlugin {
public PlayerManager getPlayerManager() {
return playerManager;
}
public DatabaseConnector getDatabaseConnector() {
return databaseConnector;
}
public DataManager getDataManager() {
return dataManager;
}
}

View File

@ -38,7 +38,9 @@ public class CommandAdd extends AbstractCommand {
Category category = categories.isEmpty() ? new Category(categoryStr) : categories.get(0);
headManager.addLocalHead(new Head(headManager.getNextLocalId(), name, url, category, true, null, (byte) 0));
Head head = new Head(headManager.getNextLocalId(), name, url, category, true, null, (byte) 0);
headManager.addLocalHead(head);
plugin.getDataManager().createLocalHead(head);
plugin.getLocale().getMessage("command.add.success")
.processPlaceholder("name", name).sendPrefixedMessage(sender);

View File

@ -0,0 +1,235 @@
package com.songoda.epicheads.database;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.songoda.core.database.DataManagerAbstract;
import com.songoda.core.database.DatabaseConnector;
import com.songoda.epicheads.EpicHeads;
import com.songoda.epicheads.head.Category;
import com.songoda.epicheads.head.Head;
import com.songoda.epicheads.players.EPlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.sql.*;
import java.util.*;
import java.util.function.Consumer;
public class DataManager extends DataManagerAbstract {
public DataManager(DatabaseConnector connector, Plugin plugin) {
super(connector, plugin);
}
public void updatePlayer(EPlayer ePlayer) {
Gson gson = new Gson();
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String updatePlayer = "UPDATE " + this.getTablePrefix() + "players SET favorites = ? WHERE uuid = ?";
try (PreparedStatement statement = connection.prepareStatement(updatePlayer)) {
statement.setString(1, gson.toJson(ePlayer.getFavorites()));
statement.setString(2, ePlayer.getUuid().toString());
statement.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
});
}
public void getPlayer(Player player, Consumer<EPlayer> callback) {
Gson gson = new Gson();
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String insertPlayer = "REPLACE INTO " + this.getTablePrefix() + "players (uuid, favorites) VALUES (?, ?)";
String selectPlayers = "SELECT * FROM " + this.getTablePrefix() + "players WHERE uuid = ?";
try (PreparedStatement insert = connection.prepareStatement(insertPlayer);
PreparedStatement statement = connection.prepareStatement(selectPlayers)) {
insert.setString(1, player.getUniqueId().toString());
insert.setString(2, gson.toJson(new ArrayList<>()));
insert.execute();
statement.setString(1, player.getUniqueId().toString());
ResultSet result = statement.executeQuery();
if (result.next()) {
UUID uuid = UUID.fromString(result.getString("uuid"));
List<String> favorites = gson.fromJson(result.getString("favorites"), new TypeToken<List<String>>() {
}.getType());
EPlayer ePlayer = new EPlayer(uuid, favorites);
this.sync(() -> callback.accept(ePlayer));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
});
}
public void migratePlayers(List<EPlayer> players) {
Gson gson = new Gson();
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String insertPlayer = "REPLACE INTO " + this.getTablePrefix() + "players (uuid, favorites) VALUES (?, ?)";
try (PreparedStatement insert = connection.prepareStatement(insertPlayer)) {
for (EPlayer player : players) {
insert.setString(1, player.getUuid().toString());
insert.setString(2, gson.toJson(player.getFavorites()));
insert.addBatch();
}
insert.executeBatch();
}
} catch (SQLException e) {
e.printStackTrace();
}
});
}
public void createLocalHead(Head head) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String createHead = "INSERT INTO " + this.getTablePrefix() + "local_heads (category, name, url) VALUES (?, ?, ?)";
try (PreparedStatement statement = connection.prepareStatement(createHead)) {
statement.setString(1, head.getCategory().getName());
statement.setString(2, head.getName());
statement.setString(3, head.getURL());
statement.executeUpdate();
}
int furnaceId = this.lastInsertedId(connection, "local_heads");
head.setId(furnaceId);
} catch (SQLException e) {
e.printStackTrace();
}
});
}
public void getLocalHeads(Consumer<List<Head>> callback) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
List<Head> heads = new ArrayList<>();
try (Statement statement = connection.createStatement()) {
String selectFurnaces = "SELECT * FROM " + this.getTablePrefix() + "local_heads";
ResultSet result = statement.executeQuery(selectFurnaces);
while (result.next()) {
int id = result.getInt("id");
String categoryString = result.getString("category");
Optional<Category> tagOptional = EpicHeads.getInstance().getHeadManager().getCategories().stream()
.filter(t -> t.getName().equalsIgnoreCase(categoryString)).findFirst();
Category category = tagOptional.orElseGet(() -> new Category(categoryString));
String name = result.getString("name");
String url = result.getString("url");
Head head = new Head(id,
name,
url,
category,
true,
null,
(byte) 0);
if (!tagOptional.isPresent())
EpicHeads.getInstance().getHeadManager().addCategory(category);
heads.add(head);
}
}
this.sync(() -> callback.accept(heads));
} catch (SQLException e) {
e.printStackTrace();
}
});
}
public void updateLocalHead(Head head) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String updateHead = "UPDATE " + this.getTablePrefix() + "local_heads SET name = ?, url = ? WHERE id = ?";
try (PreparedStatement statement = connection.prepareStatement(updateHead)) {
statement.setString(1, head.getName());
statement.setString(2, head.getURL());
statement.setInt(3, head.getId());
statement.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
});
}
public void disableHead(Head head) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String updateHead = "INSERT INTO " + this.getTablePrefix() + "disabled_heads (id) VALUES (?)";
try (PreparedStatement statement = connection.prepareStatement(updateHead)) {
statement.setInt(1, head.getId());
statement.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
});
}
public void migrateDisabledHead(List<Integer> heads) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
String updateHead = "INSERT INTO " + this.getTablePrefix() + "disabled_heads (id) VALUES (?)";
try (PreparedStatement statement = connection.prepareStatement(updateHead)) {
for (int head : heads) {
statement.setInt(1, head);
statement.addBatch();
}
statement.executeBatch();
}
} catch (SQLException e) {
e.printStackTrace();
}
});
}
public void getDisabledHeads(Consumer<List<Integer>> callback) {
this.runAsync(() -> {
try (Connection connection = this.databaseConnector.getConnection()) {
List<Integer> heads = new ArrayList<>();
try (Statement statement = connection.createStatement()) {
String selectFurnaces = "SELECT * FROM " + this.getTablePrefix() + "disabled_heads";
ResultSet result = statement.executeQuery(selectFurnaces);
while (result.next()) {
int id = result.getInt("id");
heads.add(id);
}
}
this.sync(() -> callback.accept(heads));
} catch (SQLException e) {
e.printStackTrace();
}
});
}
public void saveAllPlayers() {
Gson gson = new Gson();
try (Connection connection = this.databaseConnector.getConnection()) {
String updatePlayer = "UPDATE " + this.getTablePrefix() + "players SET favorites = ? WHERE uuid = ?";
try (PreparedStatement update = connection.prepareStatement(updatePlayer)) {
for (EPlayer player : EpicHeads.getInstance().getPlayerManager().getPlayers()) {
update.setString(1, gson.toJson(player.getFavorites()));
update.setString(2, player.getUuid().toString());
update.addBatch();
}
update.executeBatch();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,46 @@
package com.songoda.epicheads.database.migrations;
import com.songoda.core.database.DataMigration;
import com.songoda.core.database.MySQLConnector;
import com.songoda.epicheads.EpicHeads;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class _1_InitialMigration extends DataMigration {
public _1_InitialMigration() {
super(1);
}
@Override
public void migrate(Connection connection, String tablePrefix) throws SQLException {
String autoIncrement = EpicHeads.getInstance().getDatabaseConnector() instanceof MySQLConnector ? " AUTO_INCREMENT" : "";
// Create player profiles
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE " + tablePrefix + "players (" +
"uuid VARCHAR(36) PRIMARY KEY, " +
"favorites MEDIUMTEXT NOT NULL" +
")");
}
// Create local heads table
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE " + tablePrefix + "local_heads (" +
"id INTEGER PRIMARY KEY" + autoIncrement + ", " +
"category VARCHAR(48) NOT NULL, " +
"name VARCHAR(64) NOT NULL," +
"url VARCHAR(256) " +
")");
}
// Create disabled heads table
try (Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE " + tablePrefix + "disabled_heads (" +
"id INTEGER PRIMARY KEY" +
")");
}
}
}

View File

@ -189,6 +189,7 @@ public class GUIHeads extends Gui {
setButton(i + 9, item, (event) -> {
if (event.clickType == ClickType.MIDDLE && player.hasPermission("epicheads.delete")) {
plugin.getHeadManager().disableHead(head);
plugin.getDataManager().disableHead(head);
heads.remove(head);
showPage();
return;

View File

@ -14,7 +14,7 @@ import java.util.Objects;
public class Head {
private final int id;
private int id;
private String name = null;
private String URL = null;
private String pack = null;
@ -23,6 +23,15 @@ public class Head {
private Category category;
public Head(String name, String URL, Category category, boolean local, String pack, byte staffPicked) {
this.name = name;
this.URL = URL;
this.category = category;
this.pack = pack;
this.staffPicked = staffPicked;
this.local = local;
}
public Head(int id, String name, String URL, Category category, boolean local, String pack, byte staffPicked) {
this.id = id;
this.name = name;
@ -42,6 +51,10 @@ public class Head {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
@ -50,6 +63,14 @@ public class Head {
return pack;
}
public void setName(String name) {
this.name = name;
}
public void setURL(String URL) {
this.URL = URL;
}
public String getURL() {
if (URL == null)
return "d23eaefbd581159384274cdbbd576ced82eb72423f2ea887124f9ed33a6872c";

View File

@ -4,9 +4,11 @@ import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.utils.ItemUtils;
import com.songoda.epicheads.EpicHeads;
import com.songoda.epicheads.head.Head;
import com.songoda.epicheads.utils.ItemEconomy;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.ItemSpawnEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
@ -53,4 +55,11 @@ public class ItemListeners implements Listener {
}
}
@EventHandler
public void onPlace(BlockPlaceEvent event) {
if (ItemEconomy.isItem(event.getItemInHand())) {
event.setCancelled(true);
}
}
}

View File

@ -8,7 +8,9 @@ import com.songoda.epicheads.head.HeadManager;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.Optional;
@ -49,12 +51,24 @@ public class LoginListeners implements Listener {
if (optional.isPresent()) {
Head head = optional.get();
id = head.getId();
headManager.removeLocalHead(head);
head.setURL(url);
plugin.getDataManager().updateLocalHead(head);
return;
}
headManager.addLocalHeads(new Head(id, player.getName(), url, tag, true, null, (byte) 0));
Head head = new Head(id, player.getName(), url, tag, true, null, (byte) 0);
headManager.addLocalHead(head);
plugin.getDataManager().createLocalHead(head);
}
@EventHandler
public void onJoin(PlayerJoinEvent event) {
plugin.getDataManager().getPlayer(event.getPlayer(), ePlayer -> plugin.getPlayerManager().addPlayer(ePlayer));
}
@EventHandler
public void onQuit(PlayerQuitEvent event) {
plugin.getDataManager().updatePlayer(plugin.getPlayerManager().getPlayer(event.getPlayer()));
}
}

View File

@ -9,7 +9,7 @@ import org.bukkit.inventory.ItemStack;
public class ItemEconomy extends Economy {
public boolean isItem(ItemStack itemStack) {
public static boolean isItem(ItemStack itemStack) {
if (itemStack == null || itemStack.getType() == Material.AIR)
return false;
if (CompatibleMaterial.getMaterial(itemStack) == CompatibleMaterial.PLAYER_HEAD)