From 720045a03ff621913aad0a1c4ba832a58c8c3f40 Mon Sep 17 00:00:00 2001 From: YHDiamond <47502993+YHDiamond@users.noreply.github.com> Date: Fri, 26 Nov 2021 10:49:28 -0500 Subject: [PATCH] Add Advanced Tooltips command (#2632) Co-authored-by: YHDiamond <47502993+yehudahrrs@users.noreply.github.com> --- .../spigot/src/main/resources/plugin.yml | 5 +- .../connector/command/CommandManager.java | 1 + .../defaults/AdvancedTooltipsCommand.java | 57 +++++++++++++++++++ .../network/session/GeyserSession.java | 6 ++ .../translators/item/ItemTranslator.java | 38 +++++++++++++ connector/src/main/resources/languages | 2 +- 6 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/command/defaults/AdvancedTooltipsCommand.java diff --git a/bootstrap/spigot/src/main/resources/plugin.yml b/bootstrap/spigot/src/main/resources/plugin.yml index 7edd14520..adeda4420 100644 --- a/bootstrap/spigot/src/main/resources/plugin.yml +++ b/bootstrap/spigot/src/main/resources/plugin.yml @@ -19,6 +19,9 @@ permissions: geyser.command.advancements: description: Shows the advancements of the player on the server. default: true + geyser.command.tooltips: + description: Toggles showing advanced tooltips on your items. + default: true geyser.command.statistics: description: Shows the statistics of the player on the server. default: true @@ -36,4 +39,4 @@ permissions: default: false geyser.command.version: description: Shows the current Geyser version and checks for updates. - default: op \ No newline at end of file + default: op diff --git a/connector/src/main/java/org/geysermc/connector/command/CommandManager.java b/connector/src/main/java/org/geysermc/connector/command/CommandManager.java index 53cdfb269..2da15c019 100644 --- a/connector/src/main/java/org/geysermc/connector/command/CommandManager.java +++ b/connector/src/main/java/org/geysermc/connector/command/CommandManager.java @@ -54,6 +54,7 @@ public abstract class CommandManager { registerCommand(new SettingsCommand(connector, "settings", "geyser.commands.settings.desc", "geyser.command.settings")); registerCommand(new StatisticsCommand(connector, "statistics", "geyser.commands.statistics.desc", "geyser.command.statistics")); registerCommand(new AdvancementsCommand("advancements", "geyser.commands.advancements.desc", "geyser.command.advancements")); + registerCommand(new AdvancedTooltipsCommand("tooltips", "geyser.commands.advancedtooltips.desc", "geyser.command.tooltips")); if (GeyserConnector.getInstance().getPlatformType() == PlatformType.STANDALONE) { registerCommand(new StopCommand(connector, "stop", "geyser.commands.stop.desc", "geyser.command.stop")); } diff --git a/connector/src/main/java/org/geysermc/connector/command/defaults/AdvancedTooltipsCommand.java b/connector/src/main/java/org/geysermc/connector/command/defaults/AdvancedTooltipsCommand.java new file mode 100644 index 000000000..3c50a3b65 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/command/defaults/AdvancedTooltipsCommand.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2019-2021 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.connector.command.defaults; + +import org.geysermc.connector.command.CommandSender; +import org.geysermc.connector.command.GeyserCommand; +import org.geysermc.connector.network.session.GeyserSession; +import org.geysermc.connector.utils.LocaleUtils; + +public class AdvancedTooltipsCommand extends GeyserCommand { + public AdvancedTooltipsCommand(String name, String description, String permission) { + super(name, description, permission); + } + + @Override + public void execute(GeyserSession session, CommandSender sender, String[] args) { + if (session != null) { + String onOrOff = session.isAdvancedTooltips() ? "off" : "on"; + session.setAdvancedTooltips(!session.isAdvancedTooltips()); + session.sendMessage("§l§e" + LocaleUtils.getLocaleString("debug.prefix", session.getLocale()) + " §r" + LocaleUtils.getLocaleString("debug.advanced_tooltips." + onOrOff, session.getLocale())); + session.getInventoryTranslator().updateInventory(session, session.getPlayerInventory()); + } + } + + @Override + public boolean isExecutableOnConsole() { + return false; + } + + @Override + public boolean isBedrockOnly() { + return true; + } +} diff --git a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java index 3937fa648..25ce005c8 100644 --- a/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java +++ b/connector/src/main/java/org/geysermc/connector/network/session/GeyserSession.java @@ -450,6 +450,12 @@ public class GeyserSession implements CommandSender { private MinecraftProtocol protocol; + /** + * Whether advanced tooltips will be added to the player's items. + */ + @Setter + private boolean advancedTooltips = false; + public GeyserSession(GeyserConnector connector, BedrockServerSession bedrockServerSession, EventLoop eventLoop) { this.connector = connector; this.upstream = new UpstreamSession(bedrockServerSession); diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/item/ItemTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/item/ItemTranslator.java index 9fcf984cd..9d086151c 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/item/ItemTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/item/ItemTranslator.java @@ -155,6 +155,9 @@ public abstract class ItemTranslator { } nbt = translateDisplayProperties(session, nbt, bedrockItem); + if (session.isAdvancedTooltips()) { + nbt = addAdvancedTooltips(nbt, session.getItemMappings().getMapping(stack), session.getLocale()); + } ItemStack itemStack = new ItemStack(stack.getId(), stack.getAmount(), nbt); @@ -184,6 +187,41 @@ public abstract class ItemTranslator { return builder.build(); } + private static CompoundTag addAdvancedTooltips(CompoundTag nbt, ItemMapping mapping, String language) { + CompoundTag newNbt = nbt; + if (newNbt == null) { + newNbt = new CompoundTag("nbt"); + CompoundTag display = new CompoundTag("display"); + display.put(new ListTag("Lore")); + newNbt.put(display); + } + CompoundTag compoundTag = newNbt.get("display"); + if (compoundTag == null) { + compoundTag = new CompoundTag("display"); + } + ListTag listTag = compoundTag.get("Lore"); + + if (listTag == null) { + listTag = new ListTag("Lore"); + } + int maxDurability = mapping.getMaxDamage(); + + if (maxDurability != 0) { + int durability = maxDurability - ((IntTag) newNbt.get("Damage")).getValue(); + if (durability != maxDurability) { + listTag.add(new StringTag("", "§r§f" + String.format(MessageTranslator.convertMessage("item.durability", language), durability, maxDurability))); + } + } + + listTag.add(new StringTag("", "§r§8" + mapping.getJavaIdentifier())); + if (nbt != null) { + listTag.add(new StringTag("", "§r§8" + String.format(MessageTranslator.convertMessage("item.nbt_tags", language), nbt.size()))); + } + compoundTag.put(listTag); + newNbt.put(compoundTag); + return newNbt; + } + /** * Translates the Java NBT of canDestroy and canPlaceOn to its Bedrock counterparts. * In Java, this is treated as normal NBT, but in Bedrock, these arguments are extra parts of the item data itself. diff --git a/connector/src/main/resources/languages b/connector/src/main/resources/languages index d12420771..1a50238c1 160000 --- a/connector/src/main/resources/languages +++ b/connector/src/main/resources/languages @@ -1 +1 @@ -Subproject commit d12420771ea5e13083b3556552298d164767aae9 +Subproject commit 1a50238c1c743579a1dbd93c57d02b5da3be14fa