diff --git a/src/main/java/su/nightexpress/nightcore/database/AbstractDataHandler.java b/src/main/java/su/nightexpress/nightcore/database/AbstractDataHandler.java index 0262c87..570f6b3 100644 --- a/src/main/java/su/nightexpress/nightcore/database/AbstractDataHandler.java +++ b/src/main/java/su/nightexpress/nightcore/database/AbstractDataHandler.java @@ -11,6 +11,7 @@ import su.nightexpress.nightcore.database.sql.SQLQueries; import su.nightexpress.nightcore.database.sql.SQLValue; import su.nightexpress.nightcore.database.sql.executor.*; +import su.nightexpress.nightcore.database.sql.query.UpdateQuery; import su.nightexpress.nightcore.manager.AbstractManager; import java.sql.Connection; @@ -135,10 +136,34 @@ public void insert(@NotNull String table, @NotNull List values) { InsertQueryExecutor.builder(table).values(values).execute(this.getConnector()); } + + + + @Deprecated public void update(@NotNull String table, @NotNull List values, @NotNull SQLCondition... conditions) { UpdateQueryExecutor.builder(table).values(values).where(conditions).execute(this.getConnector()); } + @NotNull + public UpdateQuery updateQuery(@NotNull String table, @NotNull List values, @NotNull List conditions) { + return UpdateQuery.create(table, values, conditions); + } + + public void executeUpdate(@NotNull String table, @NotNull List values, @NotNull List conditions) { + this.executeUpdate(this.updateQuery(table, values, conditions)); + } + + public void executeUpdate(@NotNull UpdateQuery query) { + SQLQueries.executeUpdate(this.connector, query); + } + + public void executeUpdates(@NotNull List queries) { + SQLQueries.executeUpdates(this.connector, queries); + } + + + + public void delete(@NotNull String table, @NotNull SQLCondition... conditions) { DeleteQueryExecutor.builder(table).where(conditions).execute(this.getConnector()); } diff --git a/src/main/java/su/nightexpress/nightcore/database/AbstractUserDataHandler.java b/src/main/java/su/nightexpress/nightcore/database/AbstractUserDataHandler.java index 265d002..4de3d34 100644 --- a/src/main/java/su/nightexpress/nightcore/database/AbstractUserDataHandler.java +++ b/src/main/java/su/nightexpress/nightcore/database/AbstractUserDataHandler.java @@ -10,6 +10,8 @@ import su.nightexpress.nightcore.database.sql.SQLValue; import su.nightexpress.nightcore.database.sql.column.ColumnType; import su.nightexpress.nightcore.core.CoreConfig; +import su.nightexpress.nightcore.database.sql.query.UpdateQuery; +import su.nightexpress.nightcore.util.Lists; import su.nightexpress.nightcore.util.TimeUtil; import java.sql.ResultSet; @@ -55,9 +57,7 @@ protected void onShutdown() { @Override public void onSave() { - for (U user : this.plugin.getUserManager().getLoaded()) { - this.saveUser(user); - } + this.executeUpdates(this.plugin.getUserManager().getLoaded().stream().map(this::saveQuery).toList()); } @Override @@ -151,13 +151,30 @@ public boolean isUserExists(@NotNull UUID uuid) { } public void saveUser(@NotNull U user) { + /*List values = new ArrayList<>(); + values.add(COLUMN_USER_NAME.toValue(user.getName())); + values.add(COLUMN_USER_DATE_CREATED.toValue(user.getDateCreated())); + values.add(COLUMN_USER_LAST_ONLINE.toValue(user.getLastOnline())); + values.addAll(this.getSaveColumns(user)); + + this.update(this.tableUsers, values, SQLCondition.equal(COLUMN_USER_ID.toValue(user.getId())));*/ + + this.executeUpdate(this.saveQuery(user)); + } + + @NotNull + public UpdateQuery saveQuery(@NotNull U user) { List values = new ArrayList<>(); values.add(COLUMN_USER_NAME.toValue(user.getName())); values.add(COLUMN_USER_DATE_CREATED.toValue(user.getDateCreated())); values.add(COLUMN_USER_LAST_ONLINE.toValue(user.getLastOnline())); values.addAll(this.getSaveColumns(user)); - this.update(this.tableUsers, values, SQLCondition.equal(COLUMN_USER_ID.toValue(user.getId()))); + List conditions = Lists.newList( + SQLCondition.equal(COLUMN_USER_ID.toValue(user.getId())) + ); + + return this.updateQuery(this.tableUsers, values, conditions); } public void addUser(@NotNull U user) { diff --git a/src/main/java/su/nightexpress/nightcore/database/AbstractUserManager.java b/src/main/java/su/nightexpress/nightcore/database/AbstractUserManager.java index 4f532d1..6a82e33 100644 --- a/src/main/java/su/nightexpress/nightcore/database/AbstractUserManager.java +++ b/src/main/java/su/nightexpress/nightcore/database/AbstractUserManager.java @@ -4,6 +4,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import su.nightexpress.nightcore.NightDataPlugin; +import su.nightexpress.nightcore.database.sql.SQLQueries; +import su.nightexpress.nightcore.database.sql.query.UpdateQuery; import su.nightexpress.nightcore.manager.AbstractManager; import su.nightexpress.nightcore.core.CoreConfig; import su.nightexpress.nightcore.database.listener.UserListener; @@ -42,6 +44,11 @@ protected void onShutdown() { this.scheduledSaves.clear(); } + @NotNull + private AbstractUserDataHandler getDataHandler() { + return this.plugin.getData(); + } + @NotNull public abstract U createUserData(@NotNull UUID uuid, @NotNull String name); @@ -53,8 +60,13 @@ public void loadOnlineUsers() { } public void saveScheduled() { - this.scheduledSaves.forEach(this::save); + List queries = new ArrayList<>(); + this.scheduledSaves.forEach(user -> queries.add(this.getDataHandler().saveQuery(user))); this.scheduledSaves.clear(); + SQLQueries.executeUpdates(this.getDataHandler().getConnector(), queries); + + //this.scheduledSaves.forEach(this::save); + //this.scheduledSaves.clear(); } @NotNull diff --git a/src/main/java/su/nightexpress/nightcore/database/sql/SQLQueries.java b/src/main/java/su/nightexpress/nightcore/database/sql/SQLQueries.java index 81dad55..bee0072 100644 --- a/src/main/java/su/nightexpress/nightcore/database/sql/SQLQueries.java +++ b/src/main/java/su/nightexpress/nightcore/database/sql/SQLQueries.java @@ -2,6 +2,7 @@ import org.jetbrains.annotations.NotNull; import su.nightexpress.nightcore.database.AbstractConnector; +import su.nightexpress.nightcore.database.sql.query.UpdateQuery; import java.sql.*; import java.util.*; @@ -83,6 +84,35 @@ public static void executeStatement(@NotNull AbstractConnector connector, } } + public static void executeUpdate(@NotNull AbstractConnector connector, @NotNull UpdateQuery query) { + executeUpdates(connector, List.of(query)); + } + + public static void executeUpdates(@NotNull AbstractConnector connector, @NotNull List queries) { + try (Connection connection = connector.getConnection()) { + for (UpdateQuery query : queries) { + try (PreparedStatement statement = connection.prepareStatement(query.getSQL())) { + + int count = 1; + for (String columnValue : query.getValues()) { + statement.setString(count++, columnValue); + } + for (String conditionValue : query.getWheres()) { + statement.setString(count++, conditionValue); + } + + statement.executeUpdate(); + } + catch (SQLException exception) { + exception.printStackTrace(); + } + } + } + catch (SQLException exception) { + exception.printStackTrace(); + } + } + @NotNull public static List<@NotNull T> executeQuery(@NotNull AbstractConnector connector, @NotNull String sql, diff --git a/src/main/java/su/nightexpress/nightcore/database/sql/SQLUtils.java b/src/main/java/su/nightexpress/nightcore/database/sql/SQLUtils.java new file mode 100644 index 0000000..7b7d039 --- /dev/null +++ b/src/main/java/su/nightexpress/nightcore/database/sql/SQLUtils.java @@ -0,0 +1,14 @@ +package su.nightexpress.nightcore.database.sql; + +import org.jetbrains.annotations.NotNull; + +public class SQLUtils { + + @NotNull + public static String escape(@NotNull String string) { + if (string.isBlank()) return string; + if (string.charAt(0) == '`') return string; + + return "`" + string + "`"; + } +} diff --git a/src/main/java/su/nightexpress/nightcore/database/sql/executor/UpdateQueryExecutor.java b/src/main/java/su/nightexpress/nightcore/database/sql/executor/UpdateQueryExecutor.java index 0984a4e..44e043c 100644 --- a/src/main/java/su/nightexpress/nightcore/database/sql/executor/UpdateQueryExecutor.java +++ b/src/main/java/su/nightexpress/nightcore/database/sql/executor/UpdateQueryExecutor.java @@ -1,17 +1,20 @@ package su.nightexpress.nightcore.database.sql.executor; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import su.nightexpress.nightcore.database.AbstractConnector; import su.nightexpress.nightcore.database.sql.SQLCondition; import su.nightexpress.nightcore.database.sql.SQLExecutor; import su.nightexpress.nightcore.database.sql.SQLQueries; import su.nightexpress.nightcore.database.sql.SQLValue; +import su.nightexpress.nightcore.database.sql.query.UpdateQuery; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; +@Deprecated public final class UpdateQueryExecutor extends SQLExecutor { private final List values; @@ -52,6 +55,24 @@ public UpdateQueryExecutor where(@NotNull List wheres) { return this; } + /*@Nullable + public UpdateQuery createObject() { + if (this.values.isEmpty()) return null; + + String values = this.values.stream().map(value -> value.getColumn().getNameEscaped() + " = ?") + .collect(Collectors.joining(",")); + + String wheres = this.wheres.stream().map(where -> where.getColumn().getNameEscaped() + " " + where.getType().getOperator() + " ?") + .collect(Collectors.joining(" AND ")); + + String sql = "UPDATE " + this.getTable() + " SET " + values + (wheres.isEmpty() ? "" : " WHERE " + wheres); + + List values2 = this.values.stream().map(SQLValue::getValue).toList(); + List whers2 = this.wheres.stream().map(SQLCondition::getValue).map(SQLValue::getValue).toList(); + + return new UpdateQuery(sql, values2, whers2); + }*/ + @Override @NotNull public Void execute(@NotNull AbstractConnector connector) { diff --git a/src/main/java/su/nightexpress/nightcore/database/sql/query/AbstractQuery.java b/src/main/java/su/nightexpress/nightcore/database/sql/query/AbstractQuery.java new file mode 100644 index 0000000..6ae40de --- /dev/null +++ b/src/main/java/su/nightexpress/nightcore/database/sql/query/AbstractQuery.java @@ -0,0 +1,17 @@ +package su.nightexpress.nightcore.database.sql.query; + +import org.jetbrains.annotations.NotNull; + +public class AbstractQuery { + + private final String sql; + + protected AbstractQuery(@NotNull String sql) { + this.sql = sql; + } + + @NotNull + public String getSQL() { + return sql; + } +} diff --git a/src/main/java/su/nightexpress/nightcore/database/sql/query/UpdateQuery.java b/src/main/java/su/nightexpress/nightcore/database/sql/query/UpdateQuery.java new file mode 100644 index 0000000..efce046 --- /dev/null +++ b/src/main/java/su/nightexpress/nightcore/database/sql/query/UpdateQuery.java @@ -0,0 +1,66 @@ +package su.nightexpress.nightcore.database.sql.query; + +import org.jetbrains.annotations.NotNull; +import su.nightexpress.nightcore.database.sql.SQLCondition; +import su.nightexpress.nightcore.database.sql.SQLUtils; +import su.nightexpress.nightcore.database.sql.SQLValue; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class UpdateQuery extends AbstractQuery { + + private final List values; + private final List wheres; + + private UpdateQuery(@NotNull String sql, @NotNull List values, @NotNull List wheres) { + super(sql); + this.values = values; + this.wheres = wheres; + } + + @NotNull + public static UpdateQuery create(@NotNull String table, @NotNull List values) { + return create(table, values, Collections.emptyList()); + } + + @NotNull + public static UpdateQuery create(@NotNull String table, @NotNull List values, @NotNull List conditions) { + List columnNames = new ArrayList<>(); + List columnValues = new ArrayList<>(); + List whereColumns = new ArrayList<>(); + List whereValues = new ArrayList<>(); + + values.forEach(value -> { + columnNames.add(value.getColumn().getNameEscaped() + " = ?"); + columnValues.add(value.getValue()); + }); + + conditions.forEach(condition -> { + whereColumns.add(condition.getColumn().getNameEscaped() + " " + condition.getType().getOperator() + " ?"); + whereValues.add(condition.getValue().getValue()); + }); + + StringBuilder builder = new StringBuilder(); + builder.append("UPDATE ").append(SQLUtils.escape(table)); + builder.append(" SET ").append(String.join(",", columnNames)); + if (!whereColumns.isEmpty()) { + builder.append(" WHERE ").append(String.join(" AND ", whereColumns)); + } + + String sql = builder.toString(); + + return new UpdateQuery(sql, columnValues, whereValues); + } + + @NotNull + public List getValues() { + return values; + } + + @NotNull + public List getWheres() { + return wheres; + } +} diff --git a/src/main/java/su/nightexpress/nightcore/menu/impl/EditorMenu.java b/src/main/java/su/nightexpress/nightcore/menu/impl/EditorMenu.java index 00a8b98..6466217 100644 --- a/src/main/java/su/nightexpress/nightcore/menu/impl/EditorMenu.java +++ b/src/main/java/su/nightexpress/nightcore/menu/impl/EditorMenu.java @@ -95,7 +95,7 @@ protected MenuItem addExit(int[] slots) { @NotNull protected MenuItem addExit(int slot) { ItemStack item = ItemUtil.getSkinHead("27548362a24c0fa8453e4d93e68c5969ddbde57bf6666c0319c1ed1e84d89065"); - return this.addItem(item, CoreLang.EDITOR_ITEM_CLOSE, slot, (viewer, event, obj) -> ItemHandler.forClose(this)); + return this.addItem(item, CoreLang.EDITOR_ITEM_CLOSE, slot, (viewer, event, obj) -> this.runNextTick(() -> viewer.getPlayer().closeInventory())); } @NotNull diff --git a/src/main/java/su/nightexpress/nightcore/util/Colorizer.java b/src/main/java/su/nightexpress/nightcore/util/Colorizer.java index ec89b0e..3a8c282 100644 --- a/src/main/java/su/nightexpress/nightcore/util/Colorizer.java +++ b/src/main/java/su/nightexpress/nightcore/util/Colorizer.java @@ -154,10 +154,22 @@ public static String tagPlainHex(@NotNull String str) { lastIndex = index + 1; //if (builder.length() < 7) break; - if (index > 0 && builder.charAt(index - 1) == Tag.OPEN_BRACKET) continue; - if (builder.length() < index + 7) break; + int lookup = index + 7; + if (builder.length() < lookup) break; - String sub = builder.substring(index, index + 7); + Character prefix = index > 0 ? builder.charAt(index - 1) : null; + Character postfix = builder.length() > lookup ? builder.charAt(lookup) : null; + + + if (prefix != null && prefix == Tag.OPEN_BRACKET) continue; + if (postfix != null && postfix == Tag.CLOSE_BRACKET) continue; + if (prefix != null && postfix != null && prefix == ':' && postfix == ':') continue; + + //if (index > 0 && builder.charAt(index - 1) == Tag.OPEN_BRACKET) continue; + //if (builder.length() < index + 7) break; + //if (builder.length() > index + 7 && builder.charAt(index + 7) == Tag.CLOSE_BRACKET) continue; + + String sub = builder.substring(index, lookup); try { Integer.decode(sub); } diff --git a/src/main/java/su/nightexpress/nightcore/util/Placeholders.java b/src/main/java/su/nightexpress/nightcore/util/Placeholders.java index d3c36f5..936047d 100644 --- a/src/main/java/su/nightexpress/nightcore/util/Placeholders.java +++ b/src/main/java/su/nightexpress/nightcore/util/Placeholders.java @@ -54,9 +54,9 @@ public class Placeholders { @NotNull public static UnaryOperator forLocation(@NotNull Location location) { return new PlaceholderMap() - .add(LOCATION_X, () -> NumberUtil.format(location.getX())) - .add(LOCATION_Y, () -> NumberUtil.format(location.getY())) - .add(LOCATION_Z, () -> NumberUtil.format(location.getZ())) + .add(LOCATION_X, () -> String.valueOf(location.getBlockX())) + .add(LOCATION_Y, () -> String.valueOf(location.getBlockY())) + .add(LOCATION_Z, () -> String.valueOf(location.getBlockZ())) .add(LOCATION_WORLD, () -> LocationUtil.getWorldName(location)) .replacer(); } diff --git a/src/main/java/su/nightexpress/nightcore/util/text/TextRoot.java b/src/main/java/su/nightexpress/nightcore/util/text/TextRoot.java index 57018fa..cbf04bf 100644 --- a/src/main/java/su/nightexpress/nightcore/util/text/TextRoot.java +++ b/src/main/java/su/nightexpress/nightcore/util/text/TextRoot.java @@ -6,6 +6,7 @@ import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import su.nightexpress.nightcore.core.CoreConfig; import su.nightexpress.nightcore.util.Colorizer; import su.nightexpress.nightcore.util.text.tag.TagPool; import su.nightexpress.nightcore.util.text.tag.Tags; @@ -38,7 +39,9 @@ public TextRoot(@NotNull String string, @NotNull TagPool tagPool) { } public void setString(@NotNull String string) { - string = Colorizer.tagPlainHex(Colorizer.plain(string)); + if (CoreConfig.LEGACY_COLOR_SUPPORT.get()) { + string = Colorizer.tagPlainHex(Colorizer.plain(string)); + } /*if (CoreConfig.LEGACY_COLOR_SUPPORT.get()) { TimedMatcher timedMatcher = TimedMatcher.create(Colorizer.PATTERN_HEX_LEGACY, string); diff --git a/src/main/java/su/nightexpress/nightcore/util/text/tag/impl/HexColorTag.java b/src/main/java/su/nightexpress/nightcore/util/text/tag/impl/HexColorTag.java index 84014e6..36cb0c3 100644 --- a/src/main/java/su/nightexpress/nightcore/util/text/tag/impl/HexColorTag.java +++ b/src/main/java/su/nightexpress/nightcore/util/text/tag/impl/HexColorTag.java @@ -13,7 +13,7 @@ public class HexColorTag extends Tag implements ContentTag { public static final String NAME = "color"; public HexColorTag() { - super(NAME); + super(NAME, new String[]{"c"}); } @Override