143 lines
5.2 KiB
Java
143 lines
5.2 KiB
Java
package com.josyf.commandbuttons.gui;
|
|
|
|
import com.josyf.commandbuttons.CommandButtons;
|
|
import com.josyf.commandbuttons.ConfigFile;
|
|
import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
|
|
import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription;
|
|
import io.github.cottonmc.cotton.gui.widget.*;
|
|
import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment;
|
|
import net.minecraft.client.MinecraftClient;
|
|
import net.minecraft.client.gui.screen.Screen;
|
|
import net.minecraft.text.Text;
|
|
import org.json.simple.JSONArray;
|
|
import org.json.simple.JSONObject;
|
|
import io.github.cottonmc.cotton.gui.widget.data.Insets;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.stream.Collectors;
|
|
|
|
public class CreateCommandGUI extends LightweightGuiDescription {
|
|
|
|
private final Screen parentScreen;
|
|
private final WGridPanel commandsPanel;
|
|
private final ArrayList<WTextField> commandTextFields = new ArrayList<>();
|
|
private int nextCommandRow = 0;
|
|
|
|
public CreateCommandGUI(Screen parentScreen) {
|
|
this.parentScreen = parentScreen;
|
|
|
|
WGridPanel root = new WGridPanel();
|
|
setRootPanel(root);
|
|
root.setSize(280, 220);
|
|
root.setInsets(new Insets(5));
|
|
|
|
root.setBackgroundPainter(BackgroundPainter.createColorful(0x4D000000));
|
|
|
|
WLabel title = new WLabel(Text.translatable("gui.commandbuttons.create_title"), getTitleColor());
|
|
title.setHorizontalAlignment(HorizontalAlignment.CENTER);
|
|
root.add(title, 0, 0, 16, 1);
|
|
|
|
WLabel nameLabel = new WLabel(Text.translatable("gui.commandbuttons.name_label"));
|
|
root.add(nameLabel, 0, 2);
|
|
WTextField nameField = new WTextField();
|
|
nameField.setSuggestion(Text.translatable("gui.commandbuttons.commands_suggestion_name"));
|
|
root.add(nameField, 0, 3, 16, 1);
|
|
WLabel commandsLabel = new WLabel(Text.translatable("gui.commandbuttons.commands_label"));
|
|
root.add(commandsLabel, 0, 5);
|
|
|
|
commandsPanel = new WGridPanel();
|
|
WScrollPanel scrollPanel = new WScrollPanel(commandsPanel);
|
|
root.add(scrollPanel, 0, 6, 16, 8);
|
|
|
|
addCommandTextField("");
|
|
rebuildCommandsPanel();
|
|
|
|
WButton addCommandButton = new WButton(Text.literal("+"));
|
|
addCommandButton.setOnClick(() -> {
|
|
addCommandTextField("");
|
|
rebuildCommandsPanel();
|
|
});
|
|
root.add(addCommandButton, 16, 6, 1, 1);
|
|
|
|
WButton createButton = new WButton(Text.translatable("gui.commandbuttons.create"));
|
|
createButton.setOnClick(() -> {
|
|
String name = nameField.getText().trim();
|
|
List<String> commands = commandTextFields.stream()
|
|
.map(WTextField::getText)
|
|
.map(String::trim)
|
|
.filter(cmd -> !cmd.isEmpty())
|
|
.collect(Collectors.toList());
|
|
|
|
if (!name.isEmpty() && !commands.isEmpty()) {
|
|
JSONObject newCommand = new JSONObject();
|
|
newCommand.put("name", name);
|
|
|
|
JSONArray commandsArray = new JSONArray();
|
|
commandsArray.addAll(commands);
|
|
newCommand.put("commands", commandsArray);
|
|
|
|
ConfigFile.addObject(newCommand);
|
|
CommandButtons.refreshMasterCommList();
|
|
|
|
MinecraftClient.getInstance().setScreen(this.parentScreen);
|
|
} else {
|
|
System.out.println("名称或命令字段为空!");
|
|
}
|
|
});
|
|
root.add(createButton, 0, 15, 8, 1);
|
|
|
|
WButton cancelButton = new WButton(Text.translatable("gui.commandbuttons.cancel"));
|
|
cancelButton.setOnClick(() -> {
|
|
MinecraftClient.getInstance().setScreen(this.parentScreen);
|
|
});
|
|
|
|
root.add(cancelButton, 8, 15, 8, 1);
|
|
|
|
root.validate(this);
|
|
}
|
|
private void addCommandTextField(String initialText) {
|
|
WTextField textField = new WTextField();
|
|
textField.setText(initialText);
|
|
textField.setSuggestion(Text.translatable("gui.commandbuttons.commands_suggestion_command"));
|
|
commandTextFields.add(textField);
|
|
}
|
|
private void rebuildCommandsPanel() {
|
|
List<WWidget> currentPanelChildren = commandsPanel.streamChildren().collect(Collectors.toList());
|
|
|
|
for (WWidget child : currentPanelChildren) {
|
|
commandsPanel.remove(child);
|
|
}
|
|
nextCommandRow = 0;
|
|
|
|
for (WTextField textField : commandTextFields) {
|
|
|
|
commandsPanel.add(textField, 0, nextCommandRow, 15, 1);
|
|
WButton removeButton = new WButton(Text.literal("-"));
|
|
removeButton.setOnClick(() -> {
|
|
commandTextFields.remove(textField);
|
|
rebuildCommandsPanel();
|
|
});
|
|
commandsPanel.add(removeButton, 15, nextCommandRow, 1, 1);
|
|
|
|
nextCommandRow += 1;
|
|
}
|
|
if (commandTextFields.isEmpty()) {
|
|
addCommandTextField("");
|
|
rebuildCommandsPanel();
|
|
return;
|
|
}
|
|
|
|
commandsPanel.validate(this);
|
|
}
|
|
@Override
|
|
public boolean isTitleVisible() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public int getTitleColor() {
|
|
return 0x404040;
|
|
}
|
|
}
|