-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminecraft_command_generator.py
More file actions
350 lines (285 loc) · 13.2 KB
/
minecraft_command_generator.py
File metadata and controls
350 lines (285 loc) · 13.2 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import customtkinter as ctk
from data.minecraft_data import VERSIONS, EFFECT_CATEGORIES, ITEM_CATEGORIES, COMMAND_TYPES
from commands import GiveCommand, EffectCommand, GamemodeCommand, TeleportCommand
from functools import partial
import json
import os
class MinecraftCommandGenerator(ctk.CTk):
"""
Main application class for the Minecraft Command Generator.
This class creates the GUI for generating Minecraft commands and provides
functionality for selecting command types, versions, and parameters.
"""
FAVORITES_FILE = "data/favorites.json" # File to store favorites
def __init__(self):
"""
Initialize the application.
"""
super().__init__()
self.title("Minecraft Command Generator")
self.geometry("1200x800") # Set the window size
# Create the main frame to hold all widgets
self.main_frame = ctk.CTkFrame(self)
self.main_frame.pack(fill="both", expand=True, padx=10, pady=10)
# Create a dictionary to store favorite commands
self.favorite_commands = set()
# Ensure the directory for the favorites file exists
os.makedirs(os.path.dirname(self.FAVORITES_FILE), exist_ok=True)
# Load favorites from file
self.favorite_commands = self.load_favorites()
# Update the top bar layout
self.top_bar = ctk.CTkFrame(self.main_frame, height=40)
self.top_bar.pack(fill="x", padx=5, pady=5)
self.home_button = ctk.CTkButton(
self.top_bar, text="Home", width=80, height=30, command=self.show_home_page
)
self.home_button.pack(side="left", padx=5)
# Create a horizontally scrollable frame for command buttons
self.command_slider_frame = ctk.CTkScrollableFrame(
self.top_bar, height=40, orientation="horizontal"
)
self.command_slider_frame.pack(side="left", fill="x", expand=True, padx=5)
# Bind mouse wheel events for horizontal scrolling
self.command_slider_frame._parent_canvas.bind("<Enter>", self.bind_horizontal_scroll)
self.command_slider_frame._parent_canvas.bind("<Leave>", self.unbind_horizontal_scroll)
# Add command buttons to the slider
self.add_command_buttons()
self.settings_button = ctk.CTkButton(
self.top_bar, text="⚙️ Settings", width=80, height=30, command=self.open_settings
)
self.settings_button.pack(side="right", padx=5)
# Add command buttons to the slider
self.add_command_buttons()
# Create the command type selector
self.command_type_frame = ctk.CTkFrame(self.main_frame)
self.command_type_frame.pack(fill="x", padx=5, pady=5)
self.command_type_label = ctk.CTkLabel(self.command_type_frame, text="Command Type:")
self.command_type_label.pack(side="left", padx=5)
self.command_type_var = ctk.StringVar(value="")
self.command_type_dropdown = ctk.CTkOptionMenu(
self.command_type_frame,
values=[""] + list(COMMAND_TYPES.keys()),
variable=self.command_type_var,
command=self.on_command_change
)
self.command_type_dropdown.pack(side="left", padx=5)
# Bind the command type variable to actively update the UI
self.command_type_var.trace_add("write", self.on_command_type_change)
# Create the version selector
self.version_frame = ctk.CTkFrame(self.main_frame)
self.version_frame.pack(fill="x", padx=5, pady=5)
self.version_label = ctk.CTkLabel(self.version_frame, text="Minecraft Version:")
self.version_label.pack(side="left", padx=5)
self.version_var = ctk.StringVar(value=VERSIONS[0])
self.version_var.trace_add("write", self.on_version_change)
self.version_dropdown = ctk.CTkOptionMenu(
self.version_frame,
values=VERSIONS,
variable=self.version_var,
command=self.on_version_change
)
self.version_dropdown.pack(side="left", padx=5)
# Create the parameters frame for command-specific inputs
self.parameters_frame = ctk.CTkFrame(self.main_frame)
self.parameters_frame.pack(fill="both", expand=True, padx=5, pady=5)
# Create the command output frame
self.command_frame = ctk.CTkFrame(self.main_frame)
self.command_frame.pack(fill="x", padx=5, pady=5)
self.command_label = ctk.CTkLabel(self.command_frame, text="Command:")
self.command_label.pack(side="left", padx=5)
self.command_text = ctk.CTkTextbox(self.command_frame, height=50)
self.command_text.pack(side="left", fill="x", expand=True, padx=5)
# Create the feedback frame for additional information
self.feedback_frame = ctk.CTkFrame(self.main_frame)
self.feedback_frame.pack(fill="both", expand=True, padx=5, pady=5)
self.feedback_label = ctk.CTkLabel(self.feedback_frame, text="Feedback:")
self.feedback_label.pack(side="left", padx=5)
self.feedback_text = ctk.CTkTextbox(self.feedback_frame, height=50) # Increased height
self.feedback_text.pack(side="left", fill="both", expand=True, padx=5)
# Show the home page initially
self.show_home_page()
# Initialize the current command instance
self.current_command = None
# Preload all items and effects for suggestions
self.all_items = []
for category_items in ITEM_CATEGORIES.values():
self.all_items.extend(category_items)
self.all_items.sort()
self.all_formatted_items = [item.replace("_", " ").title() for item in self.all_items]
self.all_effects = []
for category_effects in EFFECT_CATEGORIES.values():
self.all_effects.extend(category_effects)
self.all_effects.sort()
self.all_formatted_effects = [effect.replace("_", " ").title() for effect in self.all_effects]
# Ensure the directory for the favorites file exists
os.makedirs(os.path.dirname(self.FAVORITES_FILE), exist_ok=True)
# Load favorites from file
self.favorite_commands = self.load_favorites()
def bind_horizontal_scroll(self, event):
"""
Bind the mouse wheel event to enable horizontal scrolling.
"""
self.command_slider_frame._parent_canvas.bind_all("<MouseWheel>", self.scroll_horizontally)
def unbind_horizontal_scroll(self, event):
"""
Unbind the mouse wheel event when the cursor leaves the frame.
"""
self.command_slider_frame._parent_canvas.unbind_all("<MouseWheel>")
def scroll_horizontally(self, event):
"""
Scroll the command slider horizontally based on mouse wheel movement.
"""
scroll_speed = 20 # Increased scroll speed multiplier
self.command_slider_frame._parent_canvas.xview_scroll(-int(event.delta / 120 * scroll_speed), "units")
def add_command_buttons(self):
"""
Add command buttons to the slider, sorted alphabetically, with favorite functionality.
"""
# Clear existing buttons
for widget in self.command_slider_frame.winfo_children():
widget.destroy()
# Sort commands alphabetically, with favorites first
sorted_commands = sorted(COMMAND_TYPES.keys(), key=lambda c: (c not in self.favorite_commands, c.lower()))
for command_type in sorted_commands:
frame = ctk.CTkFrame(self.command_slider_frame, height=30, width=130)
frame.pack(side="left", padx=5, pady=5)
# Create the main command button
button = ctk.CTkButton(
frame,
text=command_type.capitalize(),
width=100,
height=30,
command=partial(self.on_command_change, command_type),
)
button.pack(side="left")
# Create the favorite button with the correct star icon
favorite_button = ctk.CTkButton(
frame,
text="⭐" if command_type in self.favorite_commands else "☆",
width=30,
height=30,
command=partial(self.toggle_favorite, command_type),
)
favorite_button.pack(side="left")
def toggle_favorite(self, command_type: str):
"""
Toggle the favorite status of a command.
"""
if command_type in self.favorite_commands:
self.favorite_commands.remove(command_type)
else:
self.favorite_commands.add(command_type)
# Save favorites to file
self.save_favorites()
# Refresh the command buttons to reflect the updated favorites
self.add_command_buttons()
def open_settings(self):
"""
Open the settings page (placeholder for now).
"""
print("Settings button clicked!")
def show_home_page(self):
"""
Display the home page with all command options as buttons.
"""
# Clear previous UI
for widget in self.parameters_frame.winfo_children():
widget.destroy()
# Clear the command output and feedback
self.command_text.delete("1.0", "end")
self.feedback_text.delete("1.0", "end")
self.add_command_buttons()
def on_command_type_change(self, *args):
"""
Handle changes to the command type input box and update the UI.
"""
selected_command = self.command_type_var.get()
if selected_command:
self.on_command_change(selected_command)
else:
self.show_home_page()
def on_command_change(self, command_type: str) -> None:
"""
Handle changes to the selected command type.
Args:
command_type (str): The selected command type.
"""
self.command_type_var.set(command_type) # Update the dropdown value
# Clear previous command UI
for widget in self.parameters_frame.winfo_children():
widget.destroy()
# Create new command UI based on the selected type
if command_type:
command_data = COMMAND_TYPES[command_type]
if command_type == "give":
self.current_command = GiveCommand(self.parameters_frame, command_data, self.all_formatted_items, self.version_var)
elif command_type == "effect":
self.current_command = EffectCommand(self.parameters_frame, command_data, self.all_formatted_effects)
elif command_type == "gamemode":
self.current_command = GamemodeCommand(self.parameters_frame, command_data)
elif command_type == "tp":
self.current_command = TeleportCommand(self.parameters_frame, command_data)
# Set up command update callback
if self.current_command:
self.current_command.on_parameter_change = self.update_command
else:
self.current_command = None
# Update the command output
self.update_command()
def on_version_change(self, *args):
"""
Handle changes to the selected Minecraft version.
Args:
version (str): The selected Minecraft version.
"""
if hasattr(self, "current_command") and isinstance(self.current_command, GiveCommand):
self.current_command.on_version_change(self.version_var.get())
# Update the command if needed
if self.current_command:
self.update_command()
def update_command(self, event=None) -> None:
"""
Update the command output and feedback based on the current parameters.
"""
if not self.current_command:
self.command_text.delete("1.0", "end")
self.feedback_text.delete("1.0", "end")
return
# Update the command text
command = self.current_command.update_command()
self.command_text.delete("1.0", "end")
self.command_text.insert("1.0", command)
# Update the feedback text
feedback = self.current_command.get_feedback()
self.feedback_text.delete("1.0", "end")
self.feedback_text.insert("1.0", "\n".join(feedback))
def load_favorites(self) -> set:
"""
Load favorite commands from a JSON file.
If the file doesn't exist, create it with an empty set.
Returns:
set: A set of favorite commands.
"""
if not os.path.exists(self.FAVORITES_FILE):
# Create an empty JSON file if it doesn't exist
with open(self.FAVORITES_FILE, "w") as file:
json.dump([], file)
return set()
try:
with open(self.FAVORITES_FILE, "r") as file:
return set(json.load(file))
except (json.JSONDecodeError, IOError):
print("Failed to load favorites. Starting with an empty set.")
return set()
def save_favorites(self):
"""
Save favorite commands to a JSON file.
"""
try:
with open(self.FAVORITES_FILE, "w") as file:
json.dump(list(self.favorite_commands), file)
except IOError:
print("Failed to save favorites.")
if __name__ == "__main__":
app = MinecraftCommandGenerator()
app.mainloop()