Skip to content

Commit

Permalink
Commands arg fix
Browse files Browse the repository at this point in the history
  • Loading branch information
andiricum2 committed Aug 2, 2023
1 parent e7981e4 commit 7db26f1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ First of all you need to install requirements with:
pip install -r requirements.txt
```

**⚠️ INSTALL [PIERAKNET](https://github.com/PieMC-Dev/PieRakNet) MANUALLY ⚠️**

which if you run `start.cmd` it will automatically install on startup
To run the server, execute the following command in PieMC directory: [See how to get to the directory here](https://PieMC-Dev.github.io/directory)
```bash
Expand Down
18 changes: 8 additions & 10 deletions piemc/commands/piemc.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
from piemc.handlers.command import Command
from piemc.handlers.command import ConsoleCMD


@Command
@ConsoleCMD
def ping(server):
server.logger.info("Pong!")


@Command
@ConsoleCMD
def stop(server):
server.logger.info("Stopping the server...")
server.stop()

@Command
@ConsoleCMD
def setmaxplayers(server, maxplayers):
server.max_players = maxplayers
server.update_server_status()

@Command
def fakeonline(server, fakeonline):
server.players_online = fakeonline
@ConsoleCMD
def fakeonline(server, amount):
server.players_online = amount
server.update_server_status()
20 changes: 15 additions & 5 deletions piemc/handlers/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
registered_commands = {}


def Command(func):
def ConsoleCMD(func):
cmd_name = func.__name__.lower()
registered_commands[cmd_name] = func
return func
Expand All @@ -25,16 +25,26 @@ def handle_command(server, cmd):

func = registered_commands.get(cmd_name)
if func is not None:
if inspect.getfullargspec(func).args:
func(server, *cmd_args[1:])
argspec = inspect.getfullargspec(func)
num_expected_args = len(argspec.args) - 1

if argspec.varargs:
cmd_args = cmd_args[1:]
func(server, *cmd_args)
else:
func(server)
if len(cmd_args) - 1 < num_expected_args:
usage = f"{cmd_name} {' '.join(f'<{arg}>' for arg in argspec.args[1:])}"
print(f"Usage: {usage}")
return
cmd_args = cmd_args[1:num_expected_args + 1]
func(server, *cmd_args)
else:
print(f"Command '{cmd_name}' not found.")


def initialize_commands(self):
command_classes = []

package_path = piemc.commands.__path__
package_name = piemc.commands.__name__ + "."

Expand All @@ -45,4 +55,4 @@ def initialize_commands(self):
)

for command_class in command_classes:
setattr(self, command_class.__name__.lower(), command_class(self.logger, self))
setattr(self, command_class.__name__.lower(), command_class(self.logger, self))
11 changes: 4 additions & 7 deletions piemc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import time

from piemc import config
from piemc.handlers.command import handle_command
from piemc.handlers.command import initialize_commands
from piemc.handlers.command import handle_command, initialize_commands
from piemc.handlers.lang import LangHandler
from piemc.meta.protocol_info import ProtocolInfo
import piemc.commands
from piemc.handlers.logger import create_logger
from piemc.meta.protocol_info import ProtocolInfo
import piemc.commands
from piemc.update import check_for_updates

from pieraknet import Server
Expand All @@ -46,7 +45,6 @@ def __init__(self, hostname, port):
self.logger.info(f"{self.lang['CREATED_PIEUID']}: {str(pieuid)}")
self.server_status = None
self.hostname = hostname
self.port = port
self.edition = "MCPE"
self.protocol_version = 594
self.version_name = "1.20.12"
Expand All @@ -72,7 +70,6 @@ def __init__(self, hostname, port):
self.raknet_server = Server(self.hostname, self.port, create_logger('PieRakNet'))
self.raknet_server.interface = self
self.update_server_status()
self.raknet_server.name = self.server_status
self.raknet_server.protocol_version = self.raknet_version
self.raknet_server.timeout = self.timeout
# self.raknet_server.magic = ''
Expand Down Expand Up @@ -135,7 +132,7 @@ def start(self):
self.logger.error("Error while checking for updates")
while self.running:
cmd = input('>>> ')
self.cmd_handler(self, cmd) # Call self.cmd_handler instead of handle_command
self.cmd_handler(self, cmd)

def stop(self):
self.logger.info(self.lang['STOPPING_WAIT'])
Expand Down

0 comments on commit 7db26f1

Please sign in to comment.