Garde-Havoc/bot/cogs/minecraft.py

186 lines
5.7 KiB
Python
Raw Normal View History

2024-07-24 14:46:46 +02:00
import discord
from discord.ext import commands
from mcrcon import MCRcon
import enum
from statemachine import StateMachine, State
from statemachine.states import States
class BorderWarsSession(StateMachine):
"A workflow maschine for managing InGame States"
Nothing = State(initial=True)
Initialization = State()
Safe = State()
Fight = State()
SuddenDeath = State()
End = State()
init_game = Nothing.to(Initialization)
start_game = Initialization.to(Safe)
start_fight = Safe.to(Fight)
start_last_round = Fight.to(SuddenDeath)
end_game = Fight.to(End) | SuddenDeath.to(End)
abort = Initialization.to(Nothing) | Safe.to(Nothing) | Fight.to(Nothing) | SuddenDeath.to(Nothing) | End.to(Nothing) | Nothing.to(Nothing)
reset = End.to(Initialization)
@Initialization.enter
def initialization(self) -> list:
@Safe.enter
def safe(self) -> list:
# Chat countdown
return [
'''/title @a subtitle ["",{"text":"bei ","color":"blue"},{"text":"BORDER WARS!","bold":true,"color":"red"}]''',
'''/title @a title {"text":"Viel Glück!","bold":true,"color":"blue"}''',
"playsound minecraft:entity.wither.spawn ambient @a 0 64 080",
"/worldborder set 1000",
"/gamerule keepInventory true"
]
@Fight.enter
def fight(self) -> list:
# Timer Starten
return [
'''/title @a subtitle {"text":"ÜBERLEBEN!","bold":true,"color":"red"}''',
'''/title @a title {"text":"Möge der beste","color":"blue"}''',
"/playsound minecraft:item.totem.use ambient @a 0 64 0 80",
"/worldborder set 75 3600",
"/gamerule keepInventory false"
]
@SuddenDeath.enter
def death(self) -> list:
# Timer Starten
return [
'''/title @a title ["",{"text":"Sudden ","color":"dark_blue"},{"text":"DEATH!","bold":true,"color":"red"}]''',
"/playsound minecraft:entity.ender_dragon.growl ambient @a 0 64 0 80",
"/worldborder set 5 600"
]
@End.enter
def end(self, playername: str) -> list:
return [
"/worldborder center 0 0",
"/worldborder set 75",
"/gamerule keepInventory true",
'''/title @a subtitle ["",{"text":"''' + playername + '''","bold":true,"color":"red"},{"text":" gewinnt","color":"dark_blue"}]''',
'''/title @a title {"text":"ENDE!","color":"dark_blue"}''',
"/playsound minecraft:entity.ender_dragon_death ambient @a 0 64 0 80",
]
class Whitelist(StateMachine):
"A workflow machine for managing Whitelist states"
On = State(initial=True)
Off = State()
toggle = On.to(Off) | Off.to(On)
class RCON(MCRcon):
def __init__(self, ip: str, secret: str, port: int = 31066):
super().__init__(ip, secret, port)
self.connect()
def whitelist(self):
self.whitelist.toggle()
cmds = "/whitelist {}".format(self.whitelist.current_state.id)
print(cmds)
def _sendcmd(self, cmds: str | list) -> None:
if isinstance(cmds, str):
return self.server.command(str)
if isinstance(cmds, list):
return [self.server.commands(cmd) for cmd in cmds]
def __del__(self):
self.disconnect()
class Minecraft(commands.Cog):
def __init__(self, bot: commands.Bot, ip: str, secret: str, port: int = 31066):
self.bot = bot
self.server = RCON(ip, secret, port)
self.session = BorderWarsSession()
self.whitelist = Whitelist()
@commands.hybrid_command(name='whitelist')
async def whitelist(self, ctx: commands.Context):
"""
Toggles Servers Whitelist
Parameters
----------
ctx: commands.Context
The context of the command invocation
"""
await self.whitelist.activate_initial_state()
await ctx.send("Whitelist")
@commands.hybrid_command(name='start')
async def start(self, ctx: commands.Context):
"""
Starts a Border Wars Session
Parameters
----------
ctx: commands.Context
The context of the command invocation
"""
cmds =
await ctx.send("Start")
@commands.hybrid_command(name='init')
async def init(self, ctx: commands.Context):
"""
Initialize a Border Wars session
Parameters
----------
ctx: commands.Context
The context of the command invocation
"""
cmds = [
"/worldborder center 0 0",
"/worldborder set 5",
"/whitelist off"
]
await self.session.activate_initial_state()
await self.session.init_game()
await ctx.send(self.session.current_state)
@commands.hybrid_command(name='end')
async def end(self, ctx: commands.Context):
"""
Ends a Border Wars session
Parameters
----------
ctx: commands.Context
The context of the command invocation
"""
await ctx.send("End")
@commands.hybrid_command(name='rules')
async def rules(self, ctx: commands.Context):
"""
Displays the Border Wars rules
Parameters
----------
ctx: commands.Context
The context of the command invocation
"""
await ctx.send("Rules")
@commands.hybrid_command(name='custom')
async def custom(self, ctx: commands.Context):
"""
Register a custom command
Parameters
----------
ctx: commands.Context
The context of the command invocation
"""
await ctx.send("Custom")