Skip to content

Commit

Permalink
Added Examples and completed code.
Browse files Browse the repository at this point in the history
  • Loading branch information
ANDREI12333 committed Jan 22, 2023
1 parent 03393b0 commit 323e492
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 21 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
- [barneygale](https://github.com/barneygale) (For creating [Quarry](https://github.com/barneygale/quarry))
- [wiki.vg](https://wiki.vg) (For documenting the Minecraft Protocol)
36 changes: 18 additions & 18 deletions beemineapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 9 additions & 0 deletions docs/BeeAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
1 change: 0 additions & 1 deletion examples/.gitkeep

This file was deleted.

11 changes: 11 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 9 additions & 1 deletion examples/chatroom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('.'):
Expand All @@ -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'
Expand All @@ -36,4 +44,4 @@ class ChatRoomFactory(BeeFactory):
factory.listen(*addr)
reactor.run()
except KeyboardInterrupt:
sys.exit(0)
exit()
25 changes: 25 additions & 0 deletions examples/downtime.py
Original file line number Diff line number Diff line change
@@ -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()
66 changes: 66 additions & 0 deletions examples/vanilla_reimplementation.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 323e492

Please sign in to comment.