Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds //iteminfo command #115

Merged
merged 18 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion resources/lang/deu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ benchmark-result: "#{task}: {name} - {blocks} in {time}s"
#Args: {state} {id} {meta} {name} {x} {y} {z} {java_state}
block-info: "{name} ({id}:{meta}) at {x} {y} {z}\nBlockzustand: {state}\nJava Blockzustand: {java_state}"

item-info: "{count}x {name}§r ({id}:{meta})\nItem data: {nbt}\nJava data: {java_nbt}"

#Args: {schematic} {known}
unknown-schematic: "Unbekannte Struktur \"{schematic}\"\nBekannte Strukturen:{known}"

Expand Down Expand Up @@ -225,4 +227,7 @@ command-thru-usage: "//thru"
command-unstuck-description: "Teleportiere zu einer sicheren Position"
command-unstuck-usage: "//unstuck"
command-up-description: "Teleportiere nach oben"
command-up-usage: "//up <anzahl>"
command-up-usage: "//up <anzahl>"

command-iteminfo-description: "Erhalte Informationen über den gehaltenen Gegenstand"
command-iteminfo-usage: "//iteminfo"
7 changes: 6 additions & 1 deletion resources/lang/eng.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ benchmark-result: "#{task}: {name} - {blocks} in {time}s"
#Args: {state} {id} {meta} {name} {x} {y} {z} {java_state}
block-info: "{name} ({id}:{meta}) at {x} {y} {z}\nBlock state: {state}\nJava state: {java_state}"

item-info: "{count}x {name}§r ({id}:{meta})\nItem data: {nbt}\nJava data: {java_nbt}"
elftausend marked this conversation as resolved.
Show resolved Hide resolved

#Args: {schematic} {known}
unknown-schematic: "Unknown schematic \"{schematic}\"\nKnown schematics: {known}"

Expand Down Expand Up @@ -226,4 +228,7 @@ command-thru-usage: "//thru"
command-unstuck-description: "Teleport to a safe location"
command-unstuck-usage: "//unstuck"
command-up-description: "Teleport up"
command-up-usage: "//up <amount>"
command-up-usage: "//up <amount>"

command-iteminfo-description: "Get information about the item in your hand"
command-iteminfo-usage: "//iteminfo"
elftausend marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 6 additions & 1 deletion resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ block-info: "{name} ({id}:{meta}) at {x} {y} {z}\nBlock state: {state}\nJava sta
#Args: {schematic} {known}
unknown-schematic: "Unknown schematic \"{schematic}\"\nKnown schematics: {known}"

item-info: "{count}x {name}§r ({id}:{meta})\nItem data: {nbt}\nJava data: {java_nbt}"

#Commands
#Note: If command usages contains a short description, it should be spilt with a "-" (e.g. "help - Show this help")

Expand Down Expand Up @@ -227,4 +229,7 @@ command-thru-usage: "//thru"
command-unstuck-description: "Teleport to a safe location"
command-unstuck-usage: "//unstuck"
command-up-description: "Teleport up"
command-up-usage: "//up <amount>"
command-up-usage: "//up <amount>"

command-iteminfo-description: "Get information about the item in your hand"
command-iteminfo-usage: "//iteminfo"
2 changes: 2 additions & 0 deletions src/platz1de/EasyEdit/EasyEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use platz1de\EasyEdit\command\defaults\utility\DrainCommand;
use platz1de\EasyEdit\command\defaults\utility\FillCommand;
use platz1de\EasyEdit\command\defaults\utility\HelpCommand;
use platz1de\EasyEdit\command\defaults\utility\ItemInfoCommand;
use platz1de\EasyEdit\command\defaults\utility\LineCommand;
use platz1de\EasyEdit\command\defaults\utility\PasteStatesCommand;
use platz1de\EasyEdit\command\defaults\utility\StatusCommand;
Expand Down Expand Up @@ -144,6 +145,7 @@ public function onEnable(): void
new BenchmarkCommand(),
new PasteStatesCommand(),
new WandCommand(),
new ItemInfoCommand(),

// Movement
new ThruCommand(),
Expand Down
50 changes: 50 additions & 0 deletions src/platz1de/EasyEdit/command/defaults/utility/ItemInfoCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace platz1de\EasyEdit\command\defaults\utility;

use Generator;
use platz1de\EasyEdit\command\EasyEditCommand;
use platz1de\EasyEdit\command\flags\CommandFlag;
use platz1de\EasyEdit\command\flags\CommandFlagCollection;
use platz1de\EasyEdit\command\KnownPermissions;
use platz1de\EasyEdit\session\Session;
use platz1de\EasyEdit\utils\ItemInfoUtil;

class ItemInfoCommand extends EasyEditCommand
{
public function __construct()
{
parent::__construct("/iteminfo", [KnownPermissions::PERMISSION_UTIL]);
}


/**
* @param Session $session
* @param CommandFlagCollection $flags
*/
public function process(Session $session, CommandFlagCollection $flags): void
{
$itemInHand = $session->asPlayer()->getInventory()->getItemInHand();
$session->sendMessage("item-info", ItemInfoUtil::createItemInfo($session, $itemInHand));
elftausend marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param Session $session
* @return CommandFlag[]
*/
public function getKnownFlags(Session $session): array
{
return [];
}

/**
* @param CommandFlagCollection $flags
* @param Session $session
* @param string[] $args
* @return Generator<CommandFlag>
*/
public function parseArguments(CommandFlagCollection $flags, Session $session, array $args): Generator
{
yield from [];
}
}
22 changes: 21 additions & 1 deletion src/platz1de/EasyEdit/convert/ItemConvertor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use platz1de\EasyEdit\utils\RepoManager;
use pocketmine\data\bedrock\item\SavedItemData;
use pocketmine\nbt\tag\CompoundTag;
use platz1de\EasyEdit\utils\MixedUtils;
use platz1de\EasyEdit\thread\output\NbtConversionData;
use Throwable;

class ItemConvertor
Expand All @@ -31,16 +33,19 @@ class ItemConvertor

public static function load(): void
{
$rawConversionMap = "{}";
try {
/**
* @var string $java
* @var array{name: string, damage: string|int} $bedrock
*/
foreach (RepoManager::getJson("item-conversion-map", 3) as $java => $bedrock) {
foreach ($converionMap = RepoManager::getJson("item-conversion-map", 3) as $java => $bedrock) {
elftausend marked this conversation as resolved.
Show resolved Hide resolved
self::$itemTranslationBedrock[$java] = [$bedrock["name"], (int) $bedrock["damage"]];
self::$itemTranslationJava[$bedrock["name"]][(int) $bedrock["damage"]] = $java;
}

$rawConversionMap = json_encode($converionMap, JSON_THROW_ON_ERROR);

/**
* TODO: Add more convertors
* Bucket of Aquatic Mob
Expand All @@ -67,6 +72,21 @@ public static function load(): void
EditThread::getInstance()->getLogger()->error("Failed to parse conversion data, Item conversion is not available");
EditThread::getInstance()->getLogger()->debug($e->getMessage());
}
EditThread::getInstance()->sendOutput(new NbtConversionData($rawConversionMap));
elftausend marked this conversation as resolved.
Show resolved Hide resolved
}

public static function loadResourceData(string $rawConversionMap): void {
elftausend marked this conversation as resolved.
Show resolved Hide resolved
try {
$conversionMap = MixedUtils::decodeJson($rawConversionMap, 3);
} catch (Throwable $e) {
EditThread::getInstance()->getLogger()->error("Failed to parse java and bedrock conversion map data");
elftausend marked this conversation as resolved.
Show resolved Hide resolved
EditThread::getInstance()->getLogger()->debug($e->getMessage());
return;
}
foreach ($conversionMap as $java => $bedrock) {
self::$itemTranslationBedrock[$java] = [$bedrock["name"], (int) $bedrock["damage"]];
self::$itemTranslationJava[$bedrock["name"]][(int) $bedrock["damage"]] = $java;
}
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/platz1de/EasyEdit/thread/output/NbtConversionData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace platz1de\EasyEdit\thread\output;

use platz1de\EasyEdit\convert\ItemConvertor;
use platz1de\EasyEdit\utils\ExtendedBinaryStream;

class NbtConversionData extends OutputData
{
public function __construct(private string $rawConversionMap) { }

public function handle(): void { }

public function putData(ExtendedBinaryStream $stream): void
{
$stream->putString($this->rawConversionMap);
}

public function parseData(ExtendedBinaryStream $stream): void
{
ItemConvertor::loadResourceData($stream->getString());
}
}
71 changes: 71 additions & 0 deletions src/platz1de/EasyEdit/utils/ItemInfoUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace platz1de\EasyEdit\utils;

use platz1de\EasyEdit\session\Session;
use platz1de\EasyEdit\convert\ItemConvertor;
use pocketmine\item\Item;
use pocketmine\nbt\tag\ByteArrayTag;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\ImmutableTag;
use pocketmine\nbt\tag\IntArrayTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\world\format\io\GlobalItemDataHandlers;
use platz1de\EasyEdit\thread\EditThread;

class ItemInfoUtil
{

public static function convertNbtToPrettyString(CompoundTag | ListTag $nbt): string
{
$isCompoundTag = $nbt instanceof CompoundTag;
$openingChar = $isCompoundTag ? "{" : "[";
$stringified = $openingChar;

$idx = 0;
foreach($nbt->getValue() as $name => $tag) {
$value = null;
if ($tag instanceof CompoundTag || $tag instanceof ListTag) {
$value = self::convertNbtToPrettyString($tag);
} else {
$tagAsString = $tag->toString();
$value = substr($tagAsString, strpos($tagAsString,"=") + 1);
}

$valueColor = "§r";
elftausend marked this conversation as resolved.
Show resolved Hide resolved
if ($tag instanceof StringTag) {
$valueColor = "§a";
} else if (!($tag instanceof IntArrayTag || $tag instanceof ByteArrayTag)
&& $tag instanceof ImmutableTag)
{
$valueColor = "§6";
}

$hasNextTagChar = ($idx < count($nbt->getValue())-1) ? ", " : "";
$lhs = $isCompoundTag ? "§b" . $name . "§r: " : "";
$stringified .= $lhs . $valueColor . $value . "§r" . $hasNextTagChar;

$idx++;
}

$closingChar = $isCompoundTag ? "}" : "]";
return $stringified . $closingChar;
}

public static function createItemInfo(Session $session, Item $item): array
{
$itemData = GlobalItemDataHandlers::getSerializer()->serializeType($item);
$javaNbt = ItemConvertor::convertItemJava($itemData->toNbt());
$baseInfo = [
"{name}" => $item->getName(),
"{id}" => $itemData->getName(),
"{count}" => $item->getCount(),
"{meta}" => $itemData->getMeta(),
"{nbt}" => self::convertNbtToPrettyString($itemData->toNbt()),
"{java_nbt}" => $javaNbt === null ? "-" : self::convertNbtToPrettyString($javaNbt)
];

return $baseInfo;
}
}
elftausend marked this conversation as resolved.
Show resolved Hide resolved