Skip to content

Commit 6bfddb3

Browse files
authored
Merge pull request #4 from Zhou-Shilin/main
Add GUI support and fix some bugs
2 parents 138af0d + 1dcc83f commit 6bfddb3

File tree

7 files changed

+144
-25
lines changed

7 files changed

+144
-25
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
projects/*
2-
!projects/template/
1+
generated/*
32

43
logs/*
54
test.py

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
"></a>
88
<!-- <a href="https://crowdin.com/project/bukkitgpt"><img src="https://img.shields.io/badge/i18n-Crowdin-darkblue"></a> -->
99
<!-- <p>English | <a href="https://github.com/CubeGPT/BukkitGPT/blob/master/README-zh_cn.md">简体中文</a></p> -->
10+
<br>
11+
<a href="https://discord.gg/kTZtXw8s7r">Join our discord</a>
1012
<br/>
1113
</div>
1214

@@ -40,9 +42,8 @@ After the user enters a requirement, the program causes `gpt-4-preview` to gener
4042
```json
4143
{
4244
"materials": [
43-
"A": "minecraft:air",
44-
"S": "minecraft:stone",
45-
"G": "minecraft:glass"
45+
"A: \"minecraft:air\"",
46+
"S: \"minecraft:stone\""
4647
],
4748
"structures": [
4849
{

config.yaml

+16-17
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,38 @@ SYS_GEN: |
1616
You are a minecraft structure builder bot. You should design a building or a structure based on user's instructions.
1717
Response in json like this:
1818
{
19-
\"materials\": [
20-
\"A\": \"minecraft:air\",
21-
\"S\": \"minecraft:stone\",
22-
\"G\": \"minecraft:glass\"
19+
"materials": [
20+
"A: \"minecraft:air\"",
21+
"S: \"minecraft:stone\""
2322
],
24-
\"structures\": [
23+
"structures": [
2524
{
26-
\"floor\": 0,
27-
\"structure\": \"SSSSSSSS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSSSSSSSS\"
25+
"floor": 0,
26+
"structure": "SSSSSSSS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSSSSSSSS"
2827
},
2928
{
30-
\"floor\": 1,
31-
\"structure\": \"SSGGGGSS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSSSSSSSS\"
29+
"floor": 1,
30+
"structure": "SSGGGGSS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSSSSSSSS"
3231
},
3332
{
34-
\"floor\": 2,
35-
\"structure\": \"SSGGGGSS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSSSSSSSS\"
33+
"floor": 2,
34+
"structure": "SSGGGGSS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSSSSSSSS"
3635
},
3736
{
38-
\"floor\": 3,
39-
\"structure\": \"SSSSSSSS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSSSSSSSS\"
37+
"floor": 3,
38+
"structure": "SSSSSSSS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSAAAAAAS\nSSSSSSSS"
4039
},
4140
{
42-
\"floor\": 4,
43-
\"structure\": \"SSSSSSSS\nSSSSSSSS\nSSSSSSSS\nSSSSSSSS\nSSSSSSSS\nSSSSSSSS\nSSSSSSSS\nSSSSSSSS\nSSSSSSSS\nSSSSSSSS\n\"
41+
"floor": 4,
42+
"structure": "SSSSSSSS\nSSSSSSSS\nSSSSSSSS\nSSSSSSSS\nSSSSSSSS\nSSSSSSSS\nSSSSSSSS\nSSSSSSSS\nSSSSSSSS\nSSSSSSSS\n"
4443
}
4544
]
4645
}
47-
Never response anything else. Do not design a building which is too large (more than 10 floors). Never use markdown format. Use \n for line feed. And for example , if there's a "t" before \", use \\t.
46+
Never response anything else. Do not design a building which is too large (more than 10 floors). Never use markdown format. Use \n for line feed.
4847
4948
USR_GEN: |
5049
%DESCRIPTION%
5150
5251
# Developer Settings #
53-
DEBUG_MODE: False
52+
DEBUG_MODE: True
5453
VERSION_NUMBER: "Alpha-1.0" #NEVER EDIT THIS IF YOU DON'T KNOW WHAT ARE YOU DOING

console.py

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ def generate_plugin(description):
5858

5959
logger(f"console: Saving {name}.schem to generated/ folder.")
6060
version_tag = core.input_version_to_mcs_tag(version)
61+
62+
# Check if the version is valid. If not, ask the user to retype the version number.
63+
while version_tag is None:
64+
print("Error: Invalid version number. Please retype the version number.")
65+
version = input("[re-0/0] What's your minecraft version? (eg. 1.20.1): ")
66+
version_tag = core.input_version_to_mcs_tag(version)
67+
6168
schem.save("generated", name, version_tag)
6269

6370
print("Generated. Get your schem file in folder generated.")

core.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ def text_to_schem(text: str):
9797
return schematic
9898

9999
except (json.decoder.JSONDecodeError, KeyError, TypeError, ValueError, AttributeError, IndexError) as e:
100-
logger(f"text_to_command: failed to load JSON data. Error: {e}")
100+
logger(f"text_to_command: failed to load JSON data. Error message: {e}")
101+
102+
if config.DEBUG_MODE:
103+
raise e
104+
101105
return None
102106

103107
def input_version_to_mcs_tag(input_version):
@@ -114,8 +118,13 @@ def input_version_to_mcs_tag(input_version):
114118
>>> input_version_to_mcs_tag("1.20.1")
115119
'JE_1_20_1'
116120
"""
117-
version = input_version.split(".")
118-
return getattr(mcschematic.Version, f"JE_{version[0]}_{version[1]}_{version[2]}")
121+
try:
122+
version = input_version.split(".")
123+
result = getattr(mcschematic.Version, f"JE_{version[0]}_{version[1]}_{version[2]}")
124+
except (AttributeError, IndexError) as e:
125+
logger(f"input_version_to_mcs_tag: failed to convert version {input_version}; {e}")
126+
return None
127+
return result
119128

120129
if __name__ == "__main__":
121130
print("This script is not meant to be run directly. Please run console.py instead.")

logo.png

260 KB
Loading

ui.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import sys
2+
import tkinter as tk
3+
import tkinter.messagebox as msgbox
4+
5+
from log_writer import logger
6+
import core
7+
import config
8+
9+
def get_schematic(description):
10+
"""
11+
Generates a schematic based on the given description.
12+
13+
Args:
14+
description (str): The description of the schematic.
15+
16+
Returns:
17+
str: The generated schematic.
18+
19+
Raises:
20+
SystemExit: If the schematic generation fails.
21+
"""
22+
response = core.askgpt(config.SYS_GEN, config.USR_GEN.replace("%DESCRIPTION%", description), config.GENERATE_MODEL)
23+
24+
schem = core.text_to_schem(response)
25+
26+
if schem is None:
27+
msgbox.showerror("Error", "Failed to generate the schematic. We recommend you to change the generating model to gpt-4-turbo-preview or other smarter models.")
28+
sys.exit(1)
29+
30+
return schem
31+
32+
def generate_schematic():
33+
"""
34+
Generates a schematic file based on user input.
35+
36+
This function retrieves the version, name, and description from the user interface,
37+
initializes the core functionality, and generates a plugin based on the provided description.
38+
It then saves the generated schematic file in the 'generated' folder.
39+
40+
Returns:
41+
None
42+
"""
43+
generate_button.config(state=tk.DISABLED, text="Generating...")
44+
45+
version = version_entry.get()
46+
name = name_entry.get()
47+
description = description_entry.get()
48+
49+
logger(f"console: input version {version}")
50+
logger(f"console: input name {name}")
51+
logger(f"console: input description {description}")
52+
53+
schem = get_schematic(description)
54+
55+
logger(f"console: Saving {name}.schem to generated/ folder.")
56+
version_tag = core.input_version_to_mcs_tag(version)
57+
58+
while version_tag is None:
59+
msgbox.showerror("Error", "Invalid version number. Please retype the version number.")
60+
version = version_entry.get()
61+
version_tag = core.input_version_to_mcs_tag(version)
62+
63+
schem.save("generated", name, version_tag)
64+
65+
msgbox.showinfo("Success", "Generated. Get your schem file in folder generated.")
66+
67+
generate_button.config(state=tk.NORMAL, text="Generate")
68+
69+
def Application():
70+
global version_entry, name_entry, description_entry, generate_button
71+
72+
window = tk.Tk()
73+
window.title("BuilderGPT")
74+
75+
logo = tk.PhotoImage(file="logo.png")
76+
logo = logo.subsample(4)
77+
logo_label = tk.Label(window, image=logo)
78+
logo_label.pack()
79+
80+
version_label = tk.Label(window, text="What's your minecraft version? (eg. 1.20.1):")
81+
version_label.pack()
82+
version_entry = tk.Entry(window)
83+
version_entry.pack()
84+
85+
name_label = tk.Label(window, text="What's the name of your structure? It will be the name of the generated *.schem file:")
86+
name_label.pack()
87+
name_entry = tk.Entry(window)
88+
name_entry.pack()
89+
90+
description_label = tk.Label(window, text="What kind of structure would you like to generate? Describe as clear as possible:")
91+
description_label.pack()
92+
description_entry = tk.Entry(window)
93+
description_entry.pack()
94+
95+
generate_button = tk.Button(window, text="Generate", command=generate_schematic)
96+
generate_button.pack()
97+
98+
window.mainloop()
99+
100+
if __name__ == "__main__":
101+
core.initialize()
102+
Application()
103+
else:
104+
print("Error: Please run ui.py as the main program instead of importing it from another program.")

0 commit comments

Comments
 (0)