Added tooltips to lookups that display item names and enchantments (#67)

This commit is contained in:
Intelli 2022-12-21 19:11:34 -07:00
parent 20710a00de
commit 5d6fd60d40
7 changed files with 90 additions and 56 deletions

View File

@ -1,5 +1,6 @@
package net.coreprotect.command;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.Statement;
import java.text.NumberFormat;
@ -356,14 +357,9 @@ public class LookupCommand {
ConfigHandler.lookupThrottle.put(player2.getName(), new Object[] { true, System.currentTimeMillis() });
if (connection != null) {
Statement statement = connection.createStatement();
String blockdata = ChestTransactionLookup.performLookup(command.getName(), statement, location, player2, p2, finalLimit, false);
if (blockdata.contains("\n")) {
for (String b : blockdata.split("\n")) {
Chat.sendComponent(player2, b);
}
}
else {
Chat.sendComponent(player2, blockdata);
List<String> blockData = ChestTransactionLookup.performLookup(command.getName(), statement, location, player2, p2, finalLimit, false);
for (String data : blockData) {
Chat.sendComponent(player2, data);
}
statement.close();
}
@ -906,7 +902,7 @@ public class LookupCommand {
for (String[] data : lookupList) {
String time = data[0];
String dplayer = data[1];
String dtype = data[5];
int dtype = Integer.parseInt(data[5]);
int ddata = Integer.parseInt(data[6]);
int daction = Integer.parseInt(data[7]);
int amount = Integer.parseInt(data[10]);
@ -916,8 +912,10 @@ public class LookupCommand {
int z = Integer.parseInt(data[4]);
String rbd = ((Integer.parseInt(data[8]) == 2 || Integer.parseInt(data[8]) == 3) ? Color.STRIKETHROUGH : "");
String timeago = Util.getTimeSince(Integer.parseInt(time), unixtimestamp, true);
Material blockType = Util.itemFilter(Util.getType(Integer.parseInt(dtype)), (Integer.parseInt(data[13]) == 0));
Material blockType = Util.itemFilter(Util.getType(dtype), (Integer.parseInt(data[13]) == 0));
String dname = Util.nameFilter(blockType.name().toLowerCase(Locale.ROOT), ddata);
byte[] metadata = data[11] == null ? null : data[11].getBytes(StandardCharsets.ISO_8859_1);
String tooltip = Util.getEnchantments(metadata, dtype, amount);
String selector = Selector.FIRST;
String tag = Color.WHITE + "-";
@ -946,7 +944,7 @@ public class LookupCommand {
tag = (daction == 0 ? Color.GREEN + "+" : Color.RED + "-");
}
Chat.sendComponent(player2, timeago + " " + tag + " " + Phrase.build(Phrase.LOOKUP_CONTAINER, Color.DARK_AQUA + rbd + dplayer + Color.WHITE + rbd, "x" + amount, Color.DARK_AQUA + rbd + dname + Color.WHITE, selector));
Chat.sendComponent(player2, timeago + " " + tag + " " + Phrase.build(Phrase.LOOKUP_CONTAINER, Color.DARK_AQUA + rbd + dplayer + Color.WHITE + rbd, "x" + amount, Util.createTooltip(Color.DARK_AQUA + rbd + dname, tooltip) + Color.WHITE, selector));
PluginChannelListener.getInstance().sendData(player2, Integer.parseInt(time), Phrase.LOOKUP_CONTAINER, selector, dplayer, dname, amount, x, y, z, wid, rbd, true, tag.contains("+"));
}
}
@ -963,7 +961,7 @@ public class LookupCommand {
int x = Integer.parseInt(data[2]);
int y = Integer.parseInt(data[3]);
int z = Integer.parseInt(data[4]);
String dtype = data[5];
int dtype = Integer.parseInt(data[5]);
int ddata = Integer.parseInt(data[6]);
int daction = Integer.parseInt(data[7]);
int wid = Integer.parseInt(data[9]);
@ -983,8 +981,7 @@ public class LookupCommand {
String dname = "";
boolean isPlayer = false;
if (daction == 3 && !finalArgAction.contains(11) && amount == -1) {
int dTypeInt = Integer.parseInt(dtype);
if (dTypeInt == 0) {
if (dtype == 0) {
if (ConfigHandler.playerIdCacheReversed.get(ddata) == null) {
UserStatement.loadName(connection, ddata);
}
@ -992,11 +989,11 @@ public class LookupCommand {
isPlayer = true;
}
else {
dname = Util.getEntityType(dTypeInt).name();
dname = Util.getEntityType(dtype).name();
}
}
else {
dname = Util.getType(Integer.parseInt(dtype)).name().toLowerCase(Locale.ROOT);
dname = Util.getType(dtype).name().toLowerCase(Locale.ROOT);
dname = Util.nameFilter(dname, ddata);
}
if (dname.length() > 0 && !isPlayer) {
@ -1015,6 +1012,9 @@ public class LookupCommand {
String selector = Selector.FIRST;
String action = "a:block";
if (finalArgAction.contains(4) || finalArgAction.contains(5) || finalArgAction.contains(11) || amount > -1) {
byte[] metadata = data[11] == null ? null : data[11].getBytes(StandardCharsets.ISO_8859_1);
String tooltip = Util.getEnchantments(metadata, dtype, amount);
if (daction == 2 || daction == 3) {
phrase = Phrase.LOOKUP_ITEM; // {picked up|dropped}
selector = (daction != 2 ? Selector.FIRST : Selector.SECOND);
@ -1040,7 +1040,7 @@ public class LookupCommand {
action = "a:container";
}
Chat.sendComponent(player2, timeago + " " + tag + " " + Phrase.build(phrase, Color.DARK_AQUA + rbd + dplayer + Color.WHITE + rbd, "x" + amount, Color.DARK_AQUA + rbd + dname + Color.WHITE, selector));
Chat.sendComponent(player2, timeago + " " + tag + " " + Phrase.build(phrase, Color.DARK_AQUA + rbd + dplayer + Color.WHITE + rbd, "x" + amount, Util.createTooltip(Color.DARK_AQUA + rbd + dname, tooltip) + Color.WHITE, selector));
PluginChannelListener.getInstance().sendData(player2, Integer.parseInt(time), phrase, selector, dplayer, dname, (tag.contains("+") ? 1 : -1), x, y, z, wid, rbd, action.contains("container"), tag.contains("+"));
}
else {

View File

@ -1,5 +1,6 @@
package net.coreprotect.database;
import java.nio.charset.StandardCharsets;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
@ -65,6 +66,9 @@ public class Lookup extends Queue {
else if (map[i] instanceof String) {
results[newId] = (String) map[i];
}
else if (map[i] instanceof byte[]) {
results[newId] = new String((byte[]) map[i], StandardCharsets.ISO_8859_1);
}
}
}
catch (Exception e) {

View File

@ -2,6 +2,8 @@ package net.coreprotect.database.lookup;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.bukkit.Location;
@ -18,8 +20,8 @@ import net.coreprotect.utility.Util;
public class ChestTransactionLookup {
public static String performLookup(String command, Statement statement, Location l, CommandSender commandSender, int page, int limit, boolean exact) {
String result = "";
public static List<String> performLookup(String command, Statement statement, Location l, CommandSender commandSender, int page, int limit, boolean exact) {
List<String> result = new ArrayList<>();
try {
if (l == null) {
@ -67,13 +69,11 @@ public class ChestTransactionLookup {
int totalPages = (int) Math.ceil(count / (limit + 0.0));
query = "SELECT time,user,action,type,data,amount,rolled_back FROM " + ConfigHandler.prefix + "container " + Util.getWidIndex("container") + "WHERE wid = '" + worldId + "' AND (x = '" + x + "' OR x = '" + x2 + "') AND (z = '" + z + "' OR z = '" + z2 + "') AND y = '" + y + "' ORDER BY rowid DESC LIMIT " + pageStart + ", " + limit + "";
query = "SELECT time,user,action,type,data,amount,metadata,rolled_back FROM " + ConfigHandler.prefix + "container " + Util.getWidIndex("container") + "WHERE wid = '" + worldId + "' AND (x = '" + x + "' OR x = '" + x2 + "') AND (z = '" + z + "' OR z = '" + z2 + "') AND y = '" + y + "' ORDER BY rowid DESC LIMIT " + pageStart + ", " + limit + "";
if (exact) {
query = "SELECT time,user,action,type,data,amount,rolled_back FROM " + ConfigHandler.prefix + "container " + Util.getWidIndex("container") + "WHERE wid = '" + worldId + "' AND (x = '" + l.getBlockX() + "') AND (z = '" + l.getBlockZ() + "') AND y = '" + y + "' ORDER BY rowid DESC LIMIT " + pageStart + ", " + limit + "";
query = "SELECT time,user,action,type,data,amount,metadata,rolled_back FROM " + ConfigHandler.prefix + "container " + Util.getWidIndex("container") + "WHERE wid = '" + worldId + "' AND (x = '" + l.getBlockX() + "') AND (z = '" + l.getBlockZ() + "') AND y = '" + y + "' ORDER BY rowid DESC LIMIT " + pageStart + ", " + limit + "";
}
results = statement.executeQuery(query);
StringBuilder resultBuilder = new StringBuilder();
while (results.next()) {
int resultUserId = results.getInt("user");
int resultAction = results.getInt("action");
@ -82,6 +82,8 @@ public class ChestTransactionLookup {
long resultTime = results.getLong("time");
int resultAmount = results.getInt("amount");
int resultRolledBack = results.getInt("rolled_back");
byte[] resultMetadata = results.getBytes("metadata");
String tooltip = Util.getEnchantments(resultMetadata, resultType, resultAmount);
if (ConfigHandler.playerIdCacheReversed.get(resultUserId) == null) {
UserStatement.loadName(statement.getConnection(), resultUserId);
@ -91,7 +93,7 @@ public class ChestTransactionLookup {
String timeAgo = Util.getTimeSince(resultTime, time, true);
if (!found) {
resultBuilder = new StringBuilder(Color.WHITE + "----- " + Color.DARK_AQUA + Phrase.build(Phrase.CONTAINER_HEADER) + Color.WHITE + " ----- " + Util.getCoordinates(command, worldId, x, y, z, false, false) + "\n");
result.add(new StringBuilder(Color.WHITE + "----- " + Color.DARK_AQUA + Phrase.build(Phrase.CONTAINER_HEADER) + Color.WHITE + " ----- " + Util.getCoordinates(command, worldId, x, y, z, false, false)).toString());
}
found = true;
@ -117,25 +119,23 @@ public class ChestTransactionLookup {
target = target.split(":")[1];
}
resultBuilder.append(timeAgo + " " + tag + " ").append(Phrase.build(Phrase.LOOKUP_CONTAINER, Color.DARK_AQUA + rbFormat + resultUser + Color.WHITE + rbFormat, "x" + resultAmount, Color.DARK_AQUA + rbFormat + target + Color.WHITE, selector)).append("\n");
result.add(new StringBuilder(timeAgo + " " + tag + " " + Phrase.build(Phrase.LOOKUP_CONTAINER, Color.DARK_AQUA + rbFormat + resultUser + Color.WHITE + rbFormat, "x" + resultAmount, Util.createTooltip(Color.DARK_AQUA + rbFormat + target, tooltip) + Color.WHITE, selector)).toString());
PluginChannelListener.getInstance().sendData(commandSender, resultTime, Phrase.LOOKUP_CONTAINER, selector, resultUser, target, resultAmount, x, y, z, worldId, rbFormat, true, tag.contains("+"));
}
result = resultBuilder.toString();
results.close();
if (found) {
if (count > limit) {
String pageInfo = Color.WHITE + "-----\n";
pageInfo = pageInfo + Util.getPageNavigation(command, page, totalPages) + "\n";
result = result + pageInfo;
result.add(Color.WHITE + "-----");
result.add(Util.getPageNavigation(command, page, totalPages));
}
}
else {
if (rowMax > count && count > 0) {
result = Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.NO_RESULTS_PAGE, Selector.SECOND);
result.add(Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.NO_RESULTS_PAGE, Selector.SECOND));
}
else {
result = Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.NO_DATA_LOCATION, Selector.SECOND);
result.add(Color.DARK_AQUA + "CoreProtect " + Color.WHITE + "- " + Phrase.build(Phrase.NO_DATA_LOCATION, Selector.SECOND));
}
}

View File

@ -60,15 +60,9 @@ public final class ArmorStandManipulateListener extends Queue implements Listene
try (Connection connection = Database.getConnection(true)) {
if (connection != null) {
Statement statement = connection.createStatement();
String blockData = ChestTransactionLookup.performLookup(null, statement, location, finalPlayer, 1, 7, true);
if (blockData.contains("\n")) {
for (String b : blockData.split("\n")) {
Chat.sendComponent(finalPlayer, b);
}
}
else {
Chat.sendComponent(finalPlayer, blockData);
List<String> blockData = ChestTransactionLookup.performLookup(null, statement, location, finalPlayer, 1, 7, true);
for (String data : blockData) {
Chat.sendComponent(finalPlayer, data);
}
statement.close();
}

View File

@ -304,15 +304,9 @@ public final class PlayerInteractListener extends Queue implements Listener {
try (Connection connection = Database.getConnection(true)) {
if (connection != null) {
Statement statement = connection.createStatement();
String blockData = ChestTransactionLookup.performLookup(null, statement, finalLocation, player, 1, 7, false);
if (blockData.contains("\n")) {
for (String splitData : blockData.split("\n")) {
Chat.sendComponent(player, splitData);
}
}
else {
Chat.sendComponent(player, blockData);
List<String> blockData = ChestTransactionLookup.performLookup(null, statement, finalLocation, player, 1, 7, false);
for (String data : blockData) {
Chat.sendComponent(player, data);
}
statement.close();

View File

@ -266,6 +266,51 @@ public class Util extends Queue {
return message.toString();
}
public static String getEnchantments(byte[] metadata, int type, int amount) {
if (metadata == null) {
return "";
}
ItemStack item = new ItemStack(Util.getType(type), amount);
item = (ItemStack) Rollback.populateItemStack(item, metadata)[2];
String displayName = item.hasItemMeta() && item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName() : "";
StringBuilder message = new StringBuilder(Color.ITALIC + displayName + Color.GREY);
List<String> enchantments = ItemMetaHandler.getEnchantments(item, displayName);
for (String enchantment : enchantments) {
if (message.length() > 0) {
message.append("\n");
}
message.append(enchantment);
}
if (!displayName.isEmpty()) {
message.insert(0, enchantments.isEmpty() ? Color.WHITE : Color.AQUA);
}
else if (!enchantments.isEmpty()) {
String name = Util.capitalize(item.getType().name().replace("_", " "), true);
message.insert(0, Color.AQUA + Color.ITALIC + name);
}
return message.toString();
}
public static String createTooltip(String phrase, String tooltip) {
if (tooltip.isEmpty()) {
return phrase;
}
StringBuilder message = new StringBuilder(Chat.COMPONENT_TAG_OPEN + Chat.COMPONENT_POPUP);
// tooltip
message.append("|" + tooltip + "|");
// chat output
message.append(phrase);
return message.append(Chat.COMPONENT_TAG_CLOSE).toString();
}
public static String hoverCommandFilter(String string) {
StringBuilder command = new StringBuilder();

View File

@ -93,21 +93,18 @@ public class ItemMetaHandler {
return itemMeta.getEnchants();
}
public static String getEnchantments(ItemStack item) {
StringBuilder result = new StringBuilder();
public static List<String> getEnchantments(ItemStack item, String displayName) {
List<String> result = new ArrayList<>();
Map<Enchantment, Integer> enchantments = getEnchantments(item.getItemMeta());
for (Entry<Enchantment, Integer> entry : enchantments.entrySet()) {
Enchantment enchantment = entry.getKey();
Integer level = entry.getValue();
if (result.length() > 0) {
result.append("\n");
}
result.append(getEnchantmentName(enchantment, level));
result.add(getEnchantmentName(enchantment, level));
}
return result.toString();
return result;
}
public static List<List<Map<String, Object>>> seralize(ItemStack item, Material type, String faceData, int slot) {