File size: 4,376 Bytes
b309cf2
 
 
42a1498
297c842
b309cf2
5338510
42a1498
b309cf2
 
5338510
 
 
b309cf2
5338510
b309cf2
5338510
b309cf2
42a1498
5338510
b309cf2
5338510
b309cf2
5338510
b309cf2
 
42a1498
b309cf2
42a1498
b309cf2
 
 
5338510
b309cf2
 
 
 
 
5338510
 
b309cf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5338510
b309cf2
0f3e1ac
b309cf2
 
 
5338510
b309cf2
 
 
297c842
 
 
0f3e1ac
 
 
297c842
b309cf2
 
 
 
 
 
5338510
 
b309cf2
 
 
 
 
5338510
b309cf2
 
 
 
 
 
0f3e1ac
b309cf2
 
 
 
0f3e1ac
 
b309cf2
 
 
297c842
 
0f3e1ac
297c842
 
 
425f643
0f3e1ac
297c842
b309cf2
42a1498
b309cf2
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import gradio as gr
import subprocess
import os
import requests
import shutil

versions = requests.get("https://api.purpurmc.org/v2/purpur/", timeout=10).json()['versions']

process = None

server_path = "server"
os.makedirs(server_path, exist_ok=True)

def download_playit():
    os.makedirs(f"{server_path}/plugins", exist_ok=True)
    url = "https://github.com/playit-cloud/playit-minecraft-plugin/releases/latest/download/playit-minecraft-plugin.jar"
    os.system(f"curl -L -o {server_path}/plugins/playit-minecraft-plugin.jar {url}")

def download_server(version_id: str):
    if not os.path.exists(f"{server_path}/server.jar"):
        url = f"https://api.purpurmc.org/v2/purpur/{version_id}/latest/download"
        os.system(f"curl -L -o {server_path}/server.jar {url}")
    # Принимаем EULA автоматически
    with open(f"{server_path}/eula.txt", "w") as f:
        f.write("eula=true")

def start_mc(ram, version: str):
    global process
    download_server(version)
    download_playit()

    process = subprocess.Popen(
        ["java", f"-Xmx{ram}M", "-jar", f"server.jar", "nogui"],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        text=True,
        bufsize=1,
        universal_newlines=True,
        cwd=server_path
    )
    
    log_output = ""
    for line in iter(process.stdout.readline, ""):
        log_output += line 
        yield log_output

def send_command(command):
    global process
    if process and process.poll() is None:
        process.stdin.write(command + "\n")
        process.stdin.flush()
        return f"✅ Команда \"{command}\" отправлена"
    return "Сервер не запущен"

def load_properties():
    if not os.path.exists(f"{server_path}/server.properties"):
        return "# Файл еще не создан, запустите сервер один раз или введите настройки вручную."
    with open(f"{server_path}/server.properties", "r") as f:
        return f.read()

def save_properties(content):
    with open(f"{server_path}/server.properties", "w") as f:
        f.write(content)
    return "✅ Файл server.properties сохранен!"


def download_server_folder():
    if not os.path.exists(server_path):
        return None
    archive_path = shutil.make_archive("server", 'zip', server_path)
    return archive_path

with gr.Blocks() as demo:
    gr.Markdown("# HFcraft")
    
    with gr.Tabs():
        with gr.Tab("Консоль"):
            with gr.Row():
                version_dropdown = gr.Dropdown(choices=versions, value=versions[-1], label="Версия Minecraft (Purpur)")
                ram_slider = gr.Slider(512, 12288, value=4096, step=1, label="ОЗУ (МБ)")
                start_btn = gr.Button("Запустить", variant="primary")
            
            logs = gr.Textbox(label="Логи", lines=15, interactive=False)
            
            with gr.Row():
                cmd_input = gr.Textbox(label="Команда")
                send_btn = gr.Button("Отправить", scale=0)
            
            status_msg = gr.Markdown("")

        with gr.Tab("Настройки"):
            prop_editor = gr.Textbox(
                label="Редактор server.properties", 
                lines=20, 
                elem_id="prop-text"
            )
            with gr.Row():
                refresh_btn = gr.Button("Загрузить server.properties")
                save_btn = gr.Button("Сохранить server.properties", variant="primary")
            
            prop_status = gr.Markdown("")

            with gr.Row():
                export_btn = gr.Button("Скачать сервер", variant="secondary")
                file_output = gr.File(label="архив")


    export_btn.click(
        fn=download_server_folder, 
        outputs=[file_output]
    )
    # Логика Консоли
    start_btn.click(start_mc, inputs=[ram_slider, version_dropdown], outputs=[logs])
    send_btn.click(send_command, inputs=[cmd_input], outputs=[status_msg])
    send_btn.click(lambda: "", None, cmd_input)

    # Логика Редактора
    refresh_btn.click(load_properties, outputs=[prop_editor])
    save_btn.click(save_properties, inputs=[prop_editor], outputs=[prop_status])

demo.launch()