diff --git a/README.md b/README.md index e54d05f..91547f1 100644 --- a/README.md +++ b/README.md @@ -11,4 +11,5 @@ If you need help getting started. Or you are stuck. you can go to our [documenta If you don't know how to get started, you should check out some of our [examples](https://github.com/BeeCrew/BeeMineAPI/tree/main/examples) :) # Thanks to: ๐Ÿ‘ -- [barneygale](https://github.com/barneygale) (Creator of [Quarry](https://github.com/barneygale/quarry)) \ No newline at end of file +- [barneygale](https://github.com/barneygale) (For creating [Quarry](https://github.com/barneygale/quarry)) +- [wiki.vg](https://wiki.vg) (For documenting the Minecraft Protocol) \ No newline at end of file diff --git a/beemineapi.py b/beemineapi.py index b3edb9b..a78fbb6 100644 --- a/beemineapi.py +++ b/beemineapi.py @@ -96,44 +96,44 @@ def __init__(self, factory: BeeFactory=None): self.protocol = self.factory.protocol return - def sendMessage(self, message: str, selector: UUID=None): + def sendMessage(self,message: str, selector=None, isActionMsg: bool=false): """ Send a message to someone. Or everyone! """ if not selector: for player in self.factory.players: if player.protocol_version >= 760: #1.19.1+ Uses a boolean for weather to show in action bar - toActionbar = player.buff_type.pack("?", false) + toActionbar = player.buff_type.pack("?", isActionMsg) else: #1.19- uses varint as normal. - toActionbar = player.buff_type.pack_varint(0) + if isActionMsg: + isActionMsg = 1 + else: + isActionMsg = 0 + toActionbar = player.buff_type.pack_varint(isActionMsg) player.send_packet("system_message", player.buff_type.pack_chat(message), toActionbar ) return else: - for player in self.factory.players: - if player.uuid == selector: - if player.protocol_version >= 760: #1.19.1+ Uses a boolean for weather to show in action bar - toActionbar = player.buff_type.pack("?", false) - else: #1.19- uses varint as normal. - toActionbar = player.buff_type.pack_varint(0) - player.send_packet("system_message", - player.buff_type.pack_chat(message), - toActionbar - ) + player = selector + if player.protocol_version >= 760: #1.19.1+ Uses a boolean for weather to show in action bar + toActionbar = player.buff_type.pack("?", false) + else: #1.19- uses varint as normal. + toActionbar = player.buff_type.pack_varint(0) + player.send_packet("system_message", + player.buff_type.pack_chat(message), + toActionbar + ) return return - def loopallPlayers(self, func: Callable, passplayer: bool=true, *args, **kwargs): + def loopallPlayers(self, func: Callable, *args, **kwargs): """ Execute a function for every online player. """ executed = 0 for player in self.factory.players: - if passplayer: - func(player, *args, **kwargs) - else: - fhnc(*args, **kwargs) + func(player, *args, **kwargs) executed += 1 return (executed, func) \ No newline at end of file diff --git a/docs/BeeAPI.md b/docs/BeeAPI.md index 4b68820..11663da 100644 --- a/docs/BeeAPI.md +++ b/docs/BeeAPI.md @@ -19,6 +19,15 @@ Execute a function for every online player. This function executes the specified function for every online player. With the arguments passed. +### kick(reason: str, selector) +```python +""" +Kick a player from the server. +""" +``` + +This function kicks the player that is included in the specified selector. + ## You have reached the end! You have reached the end of this docs page. diff --git a/examples/.gitkeep b/examples/.gitkeep deleted file mode 100644 index 8b13789..0000000 --- a/examples/.gitkeep +++ /dev/null @@ -1 +0,0 @@ - diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..e4e99ad --- /dev/null +++ b/examples/README.md @@ -0,0 +1,11 @@ +# Examples +All of these examples have been written by me (ANDREI12333) includes the downtime one. + +# Vanilla Reimplementation +A non production-ready vanilla reimplementation of a Minecraft Server. + +# Chat Room +A Simple chat room. + +# Downtime +Simply kicks the player with the motd on join. \ No newline at end of file diff --git a/examples/chatroom.py b/examples/chatroom.py index d4650d8..17c2b2a 100644 --- a/examples/chatroom.py +++ b/examples/chatroom.py @@ -8,6 +8,12 @@ true, false = True, False c = "ยง" +def exit(exit_code: int=0): + try: + sys.exit(exit_code) + except: + quit(0) + def chat_message(self, buff): p_text = buff.unpack_string() if not p_text.startswith('.'): @@ -21,6 +27,8 @@ def chat_message(self, buff): class ChatRoomFactory(BeeFactory): #Setup protocol = BeeProtocol + protocol.gamemode = 3 + protocol.prev_gamemode = 3 #Metadata motd = 'Chat Room Example' @@ -36,4 +44,4 @@ class ChatRoomFactory(BeeFactory): factory.listen(*addr) reactor.run() except KeyboardInterrupt: - sys.exit(0) \ No newline at end of file + exit() \ No newline at end of file diff --git a/examples/downtime.py b/examples/downtime.py new file mode 100644 index 0000000..940fb19 --- /dev/null +++ b/examples/downtime.py @@ -0,0 +1,25 @@ +""" +For a downtime server i'd recommand directly using quarry. +BeeMineAPI is not supposed to be used for a down time server. +This is a quarry example. +""" +from twisted.internet import reactor +from quarry.net.server import ServerFactory, ServerProtocol +from quarry.types.uuid import UUID +from quarry.data.data_packs import data_packs, dimension_types +true, false = True, False +c = "ยง" + +class DowntimeProtocol(ServerProtocol): + def player_joined(self): + ServerProtocol.player_joined(self) + self.close(self.factory.motd) + +class DowntimeFactory(ServerFactory): + protocol = DowntimeProtocol + motd = f'{c}4Maintenance' + online_mode = false + +factory = DowntimeFactory() +factory.listen("", 25565) +reactor.run() \ No newline at end of file diff --git a/examples/vanilla_reimplementation.py b/examples/vanilla_reimplementation.py new file mode 100644 index 0000000..71529b3 --- /dev/null +++ b/examples/vanilla_reimplementation.py @@ -0,0 +1,66 @@ +""" +A vanilla reimplementation made using BeeMineAPI. +This should NOT be used in Production. +It is not complete. +""" +from beemineapi import BeeProtocol, BeeFactory, BeeAPI, reactor +import time, sys +true, false = True, False +c = 'ยง' +cmd_prefix = '.' +beeapi = BeeAPI() + +def exit(exit_code: int=0): + try: + sys.exit(exit_code) + except: + quit() + +def packet_chat_message(self, buff): + p_text = buff.unpack_string() + fmt = f"<{self.display_name}> {p_text}" + print(f'[CHAT] {fmt}') + beeapi.sendMessage(fmt) + buff.discard() + +def getHelpMsg(): + return f"""{c}cThis is a placeholder.""" + +def packet_chat_command(self, buff): + command = buff.unpack_string() + commands = command.split() + cmd = commands[0] + args = commands + args.remove(cmd) + print(f'{self.display_name} sent command: {command}') + if cmd == "help": + beeapi.sendMessage(getHelpMsg(), self) + elif cmd == "eval": + beeapi.sendMessage(f'{c}7Executing...', self) + executes = '' + for arg in args: + executes += f' {arg}' + try: + got = eval(executes) + except Exception as e: + exname = str(type(e)).replace(' ', '').replace('<', '>').replace('>', '').replace('class', '').replace('\'', '') + beeapi.sendMessage(f'{c}c{exname}: {e}', self) + else: + beeapi.sendMessage(f'{c}cInvalid Command! Use /help for help.', self) + buff.discard() + +class VanillaFactory(BeeFactory): + protocol = BeeProtocol + motd = 'A Minecraft Server\nBeeMineAPI Vanilla Reimplementation' + protocol.packet_chat_message = packet_chat_message + protocol.packet_chat_command = packet_chat_command + +try: + factory = VanillaFactory() + host, port="", 25565 + addr=(host, port) + beeapi = BeeAPI(factory) + factory.listen(*addr) + reactor.run() +except Exception as e: + exit() \ No newline at end of file