Skip to content

Commit 38ef774

Browse files
committed
Auto-generate command parameter types
I'm not sure why I didn't do this much sooner...
1 parent 482a852 commit 38ef774

File tree

3 files changed

+167
-19
lines changed

3 files changed

+167
-19
lines changed

src/AvailableCommandsPacket.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use pocketmine\network\mcpe\protocol\types\command\CommandEnumConstraint;
2323
use pocketmine\network\mcpe\protocol\types\command\CommandOverload;
2424
use pocketmine\network\mcpe\protocol\types\command\CommandParameter;
25+
use pocketmine\network\mcpe\protocol\types\command\CommandParameterTypes as ArgTypes;
2526
use pocketmine\utils\BinaryDataException;
2627
use function array_search;
2728
use function count;
@@ -40,35 +41,35 @@ class AvailableCommandsPacket extends DataPacket implements ClientboundPacket{
4041
* Basic parameter types. These must be combined with the ARG_FLAG_VALID constant.
4142
* ARG_FLAG_VALID | (type const)
4243
*/
43-
public const ARG_TYPE_INT = 1;
44-
public const ARG_TYPE_FLOAT = 3;
45-
public const ARG_TYPE_VALUE = 4;
46-
public const ARG_TYPE_WILDCARD_INT = 5;
47-
public const ARG_TYPE_OPERATOR = 6;
48-
public const ARG_TYPE_COMPARE_OPERATOR = 7;
49-
public const ARG_TYPE_TARGET = 8;
44+
public const ARG_TYPE_INT = ArgTypes::INT;
45+
public const ARG_TYPE_FLOAT = ArgTypes::VAL;
46+
public const ARG_TYPE_VALUE = ArgTypes::RVAL;
47+
public const ARG_TYPE_WILDCARD_INT = ArgTypes::WILDCARDINT;
48+
public const ARG_TYPE_OPERATOR = ArgTypes::OPERATOR;
49+
public const ARG_TYPE_COMPARE_OPERATOR = ArgTypes::COMPAREOPERATOR;
50+
public const ARG_TYPE_TARGET = ArgTypes::SELECTION;
5051

51-
public const ARG_TYPE_WILDCARD_TARGET = 10;
52+
public const ARG_TYPE_WILDCARD_TARGET = ArgTypes::WILDCARDSELECTION;
5253

53-
public const ARG_TYPE_FILEPATH = 17;
54+
public const ARG_TYPE_FILEPATH = ArgTypes::PATHCOMMAND;
5455

55-
public const ARG_TYPE_FULL_INTEGER_RANGE = 23;
56+
public const ARG_TYPE_FULL_INTEGER_RANGE = ArgTypes::FULLINTEGERRANGE;
5657

57-
public const ARG_TYPE_EQUIPMENT_SLOT = 47;
58-
public const ARG_TYPE_STRING = 48;
58+
public const ARG_TYPE_EQUIPMENT_SLOT = ArgTypes::EQUIPMENTSLOTENUM;
59+
public const ARG_TYPE_STRING = ArgTypes::ID;
5960

60-
public const ARG_TYPE_INT_POSITION = 64;
61-
public const ARG_TYPE_POSITION = 65;
61+
public const ARG_TYPE_INT_POSITION = ArgTypes::POSITION;
62+
public const ARG_TYPE_POSITION = ArgTypes::POSITION_FLOAT;
6263

63-
public const ARG_TYPE_MESSAGE = 67;
64+
public const ARG_TYPE_MESSAGE = ArgTypes::MESSAGE_ROOT;
6465

65-
public const ARG_TYPE_RAWTEXT = 70;
66+
public const ARG_TYPE_RAWTEXT = ArgTypes::RAWTEXT;
6667

67-
public const ARG_TYPE_JSON = 74;
68+
public const ARG_TYPE_JSON = ArgTypes::JSON_OBJECT;
6869

69-
public const ARG_TYPE_BLOCK_STATES = 84;
70+
public const ARG_TYPE_BLOCK_STATES = ArgTypes::BLOCK_STATE_ARRAY;
7071

71-
public const ARG_TYPE_COMMAND = 87;
72+
public const ARG_TYPE_COMMAND = ArgTypes::SLASHCOMMAND;
7273

7374
/**
7475
* Enums are a little different: they are composed as follows:
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of BedrockProtocol.
5+
* Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6+
*
7+
* BedrockProtocol is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*/
12+
13+
declare(strict_types=1);
14+
15+
namespace pocketmine\network\mcpe\protocol\types\command;
16+
17+
/**
18+
* This file is automatically generated; do NOT edit it by hand.
19+
* Regenerate it by running tools/generate-command-parameter-types.php
20+
*/
21+
final class CommandParameterTypes{
22+
23+
private function __construct(){
24+
//NOOP
25+
}
26+
27+
public const INT = 1; // int
28+
public const VAL = 3; // float
29+
public const RVAL = 4; // value
30+
public const WILDCARDINT = 5; // wildcard int
31+
public const OPERATOR = 6; // operator
32+
public const COMPAREOPERATOR = 7; // compare operator
33+
public const SELECTION = 8; // target
34+
public const WILDCARDSELECTION = 10; // target
35+
public const PATHCOMMAND = 17; // filepath
36+
public const FULLINTEGERRANGE = 23; // integer range
37+
public const EQUIPMENTSLOTENUM = 47; // equipment slots
38+
public const ID = 56; // string
39+
public const POSITION = 64; // x y z
40+
public const POSITION_FLOAT = 65; // x y z
41+
public const MESSAGE_ROOT = 68; // message
42+
public const RAWTEXT = 70; // text
43+
public const JSON_OBJECT = 74; // json
44+
public const BLOCK_STATE_ARRAY = 84; // block states
45+
public const SLASHCOMMAND = 87; // command
46+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
/*
4+
*
5+
* ____ _ _ __ __ _ __ __ ____
6+
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7+
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8+
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9+
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Lesser General Public License as published by
13+
* the Free Software Foundation, either version 3 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* @author PocketMine Team
17+
* @link http://www.pocketmine.net/
18+
*
19+
*
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace pocketmine\network\mcpe\protocol\tools\generate_command_parameter_types;
25+
26+
use function count;
27+
use function file_get_contents;
28+
use function fwrite;
29+
use function strtoupper;
30+
use function uasort;
31+
32+
if(count($argv) !== 2){
33+
echo "Usage: php generate-command-parameter-types.php <input-file> \n";
34+
echo "Hint: Input file is a JSON file like command_arg_types.json in pmmp/BedrockData\n";
35+
exit(1);
36+
}
37+
38+
$jsonRaw = file_get_contents($argv[1]);
39+
if($jsonRaw === false){
40+
echo "Failed to read input file $argv[1]\n";
41+
exit(1);
42+
}
43+
44+
$list = json_decode($jsonRaw, true, flags: JSON_THROW_ON_ERROR);
45+
if(!is_array($list)){
46+
echo "Failed to decode input file $argv[1], expected a JSON object\n";
47+
exit(1);
48+
}
49+
50+
$output = fopen(dirname(__DIR__) . "/src/types/command/CommandParameterTypes.php", "wb");
51+
if($output === false){
52+
throw new \RuntimeException("Failed to open output file");
53+
}
54+
55+
fwrite($output, <<<'CODE'
56+
<?php
57+
58+
/*
59+
* This file is part of BedrockProtocol.
60+
* Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
61+
*
62+
* BedrockProtocol is free software: you can redistribute it and/or modify
63+
* it under the terms of the GNU Lesser General Public License as published by
64+
* the Free Software Foundation, either version 3 of the License, or
65+
* (at your option) any later version.
66+
*/
67+
68+
declare(strict_types=1);
69+
70+
namespace pocketmine\network\mcpe\protocol\types\command;
71+
72+
/**
73+
* This file is automatically generated; do NOT edit it by hand.
74+
* Regenerate it by running tools/generate-command-parameter-types.php
75+
*/
76+
final class CommandParameterTypes{
77+
78+
private function __construct(){
79+
//NOOP
80+
}
81+
82+
83+
CODE);
84+
85+
uasort($list, function(array $left, array $right) : int{
86+
return $left["id"] <=> $right["id"];
87+
});
88+
89+
foreach($list as $name => $properties){
90+
if($properties["description"] === "unknown"){
91+
echo "Skipping $name - description unknown, assuming internal type\n";
92+
continue;
93+
}
94+
95+
fwrite($output, "\tpublic const " . strtoupper($name) . " = " . $properties["id"] . "; // " . $properties["description"] . "\n");
96+
}
97+
98+
fwrite($output, "}\n");
99+
fclose($output);
100+
101+
echo "Done. Don't forget to run CS fixer after generating code.\n";

0 commit comments

Comments
 (0)