awa
This commit is contained in:
parent
0de2bcd15a
commit
731ad3eff0
BIN
libs/awa (1).png
Normal file
BIN
libs/awa (1).png
Normal file
Binary file not shown.
After Width: | Height: | Size: 219 KiB |
BIN
libs/awa (2).png
Normal file
BIN
libs/awa (2).png
Normal file
Binary file not shown.
After Width: | Height: | Size: 275 KiB |
@ -1,72 +0,0 @@
|
||||
package com.josyf.commandbuttons;
|
||||
|
||||
|
||||
import com.josyf.commandbuttons.gui.ButtonGUI;
|
||||
import com.josyf.commandbuttons.gui.ButtonGUIScreen;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
||||
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.option.KeyBinding;
|
||||
import net.minecraft.client.util.InputUtil;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.lwjgl.glfw.GLFW;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CommandButtons implements ModInitializer {
|
||||
|
||||
public static final String MOD_ID = "mgbuttons";
|
||||
private static ArrayList<JSONObject> masterCommList;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
assignGuiToKey();
|
||||
initArray();
|
||||
}
|
||||
|
||||
|
||||
private void assignGuiToKey() {
|
||||
// Currently assigns to the G key
|
||||
KeyBinding keyBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding(
|
||||
"key.commandbuttons.opengui", // The translation key of the keybinding's name
|
||||
InputUtil.Type.KEYSYM, // The type of the keybinding, KEYSYM for keyboard, MOUSE for mouse.
|
||||
GLFW.GLFW_KEY_G, // The keycode of the key
|
||||
"gui.commandbuttons.mgbuttons" // The translation key of the keybinding's category.
|
||||
));
|
||||
|
||||
ClientTickEvents.END_CLIENT_TICK.register(client -> {
|
||||
while (keyBinding.wasPressed()) {
|
||||
MinecraftClient.getInstance().setScreen(new ButtonGUIScreen(new ButtonGUI()));
|
||||
//client.player.closeScreen();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void runCommand(String command) {
|
||||
MinecraftClient.getInstance().player.sendChatMessage(command);
|
||||
}
|
||||
|
||||
// Assign masterCommList to JSONArray<objects> (from commands.json). Runs once.
|
||||
static void initArray() {
|
||||
masterCommList = ConfigFile.getArrayFromJsonFile();
|
||||
// If commands.json doesn't exist yet, start a global list variable for future creation
|
||||
if (masterCommList == null) {
|
||||
setMasterCommList(new ArrayList<>());
|
||||
}
|
||||
}
|
||||
|
||||
public static ArrayList<JSONObject> getMasterCommList() {
|
||||
return masterCommList;
|
||||
}
|
||||
|
||||
public static void setMasterCommList(ArrayList<JSONObject> commList) {
|
||||
masterCommList = commList;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,105 +0,0 @@
|
||||
package com.josyf.commandbuttons;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.cedarsoftware.util.io.JsonWriter;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ConfigFile {
|
||||
|
||||
private static final JSONParser parser = new JSONParser();
|
||||
private static FileWriter fileWriter;
|
||||
|
||||
// Read commands.json, convert it to an array, and append A JSON OBJECT
|
||||
public static void appendToFile(JSONObject jsonObject) {
|
||||
try {
|
||||
Object obj = parser.parse(new FileReader("commands.json"));
|
||||
JSONArray array = (JSONArray) obj;
|
||||
array.add(jsonObject);
|
||||
writeToFile(array);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Commands.json 不存在。正在创建...");
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
jsonArray.add(jsonObject);
|
||||
writeToFile(jsonArray);
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeFromFile(JSONObject jsonObject) {
|
||||
try {
|
||||
Object obj = parser.parse(new FileReader("commands.json"));
|
||||
JSONArray array = (JSONArray) obj;
|
||||
array.remove(jsonObject);
|
||||
writeToFile(array);
|
||||
} catch (IOException | ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// overwrites current commands.json w/ updated jsonArray
|
||||
private static void writeToFile(JSONArray jsonArray) {
|
||||
try {
|
||||
fileWriter = new FileWriter("commands.json");
|
||||
String jArrayToString = JSON.toJSONString(jsonArray);
|
||||
String formattedJson = JsonWriter.formatJson(jArrayToString);
|
||||
fileWriter.write(formattedJson);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
fileWriter.flush();
|
||||
fileWriter.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// parses commands.json into array, loop through array and add JSONObjects. returns array
|
||||
public static ArrayList<JSONObject> getArrayFromJsonFile() {
|
||||
ArrayList<JSONObject> commandObjects = new ArrayList<>();
|
||||
try {
|
||||
// assign array to JSONArray using our commands.json as an object
|
||||
Object obj = parser.parse(new FileReader("commands.json"));
|
||||
JSONArray array = (JSONArray) obj;
|
||||
// so "array" is now a JSONArray full of JSONObjects
|
||||
// now we will iterate through the array and add COs to our local CO array
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JSONObject childObject = (JSONObject)array.get(i);
|
||||
commandObjects.add(childObject);
|
||||
if (i >= 19) break;
|
||||
}
|
||||
return commandObjects;
|
||||
} catch (IOException | ParseException e) {
|
||||
System.out.println("Commands.json尚未初始化!");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void addObjectToCommList(JSONObject jsonObject) {
|
||||
ArrayList<JSONObject> commListCopy = CommandButtons.getMasterCommList();
|
||||
commListCopy.add(jsonObject);
|
||||
CommandButtons.setMasterCommList(commListCopy);
|
||||
}
|
||||
|
||||
public static void removeObject(JSONObject objToRemove) {
|
||||
// get masterCommList and remove object from list
|
||||
ArrayList<JSONObject> commListCopy = CommandButtons.getMasterCommList();
|
||||
commListCopy.remove(objToRemove);
|
||||
|
||||
// get commands.json
|
||||
// remove corresponding button from json
|
||||
removeFromFile(objToRemove);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -1,166 +0,0 @@
|
||||
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 net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.text.TranslatableText;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ButtonGUI extends LightweightGuiDescription {
|
||||
|
||||
int xValue = 0;
|
||||
int yValue = 1;
|
||||
|
||||
public ButtonGUI() {
|
||||
|
||||
// initialize root panel of GUI
|
||||
WGridPanel root = new WGridPanel();
|
||||
setupBackground(root);
|
||||
addCloseButton(root);
|
||||
|
||||
// Add delete toggle button
|
||||
WToggleButton delToggle = new WToggleButton(new TranslatableText("Delete"));
|
||||
root.add(delToggle, 0, 11, 3, 1);
|
||||
|
||||
addSavedButtons(root, delToggle);
|
||||
addCommandSection(root, delToggle);
|
||||
root.validate(this);
|
||||
}
|
||||
|
||||
private void addCloseButton(WGridPanel root) {
|
||||
WButton escButton = new WButton(new TranslatableText("x"));
|
||||
escButton.setOnClick(() -> MinecraftClient.getInstance().player.closeScreen());
|
||||
root.add(escButton, 17, 1, 2, 2);
|
||||
}
|
||||
|
||||
private void addCommandSection(WGridPanel root, WToggleButton toggle) {
|
||||
// Add text field for command NAME entry
|
||||
WTextField nameTextField = new WTextField();
|
||||
nameTextField.setMaxLength(11);
|
||||
nameTextField.setSuggestion("Name");
|
||||
root.add(nameTextField, 0, 12, 6, 1);
|
||||
|
||||
// Add text field for command / entry
|
||||
WTextField commandTextField = new WTextField();
|
||||
commandTextField.setSuggestion("/command");
|
||||
commandTextField.setMaxLength(300);
|
||||
root.add(commandTextField, 6, 12, 11, 1);
|
||||
|
||||
// Add button for command entry
|
||||
WButton addCmdBtn = new WButton(new TranslatableText("+"));
|
||||
addCmdBtn.setOnClick(() -> addGUIButton(root, nameTextField, commandTextField, toggle));
|
||||
root.add(addCmdBtn, 18, 12, 1, 1);
|
||||
}
|
||||
|
||||
// Function to save newly added buttons to commands.json
|
||||
private void addGUIButton(WGridPanel root, WTextField name, WTextField command, WToggleButton isDeleteToggled) {
|
||||
// Only add the button if there are contents in both
|
||||
if (!name.getText().equals("") && !command.getText().equals("")) {
|
||||
// Create a new Json object & append to masterCommList
|
||||
JSONObject newJsonObject = new JSONObject();
|
||||
newJsonObject.put("name", name.getText());
|
||||
newJsonObject.put("command", command.getText());
|
||||
|
||||
if (!isListTooLong()) {
|
||||
String commandString = command.getText();
|
||||
WButton button = new WButton(new TranslatableText(name.getText()));
|
||||
button.setOnClick(() -> {
|
||||
if (isDeleteToggled.getToggle()) {
|
||||
ConfigFile.removeObject(newJsonObject);
|
||||
root.remove(button);
|
||||
} else {
|
||||
CommandButtons.runCommand(commandString);
|
||||
}
|
||||
|
||||
});
|
||||
root.add(button, xValue, yValue, 4, 1);
|
||||
|
||||
// append the buttons to masterList for future loading
|
||||
ConfigFile.addObjectToCommList(newJsonObject);
|
||||
ConfigFile.appendToFile(newJsonObject);
|
||||
|
||||
adjustBounds();
|
||||
}
|
||||
|
||||
name.setText("");
|
||||
command.setText("");
|
||||
|
||||
root.validate(this);
|
||||
|
||||
} else {
|
||||
System.out.println("未输入名称和数值!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// function to load buttons from commands.json
|
||||
private void addGUIButton(WGridPanel root, String name, String command, WToggleButton isDeleteToggled, JSONObject object) {
|
||||
if (!name.equals("") && !command.equals("")) {
|
||||
WButton button = new WButton(new TranslatableText(name));
|
||||
button.setOnClick(() -> {
|
||||
if (isDeleteToggled.getToggle()) {
|
||||
ConfigFile.removeObject(object);
|
||||
root.remove(button);
|
||||
} else {
|
||||
CommandButtons.runCommand(command);
|
||||
}
|
||||
|
||||
});
|
||||
root.add(button, xValue, yValue, 4, 1);
|
||||
adjustBounds();
|
||||
root.validate(this);
|
||||
} else {
|
||||
System.out.println("未输入名称和数值!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Array will contain String class types. Convert these to objects.
|
||||
private void addSavedButtons(WGridPanel root, WToggleButton toggle) {
|
||||
ArrayList<JSONObject> commListCopy = CommandButtons.getMasterCommList();
|
||||
// Then convert the objects to buttons
|
||||
if (commListCopy != null) {
|
||||
for (int i = 0; i < commListCopy.size(); i++) {
|
||||
JSONObject object = commListCopy.get(i);
|
||||
String name = commListCopy.get(i).get("name").toString();
|
||||
String command = commListCopy.get(i).get("command").toString();
|
||||
addGUIButton(root, name, command, toggle, object);
|
||||
if (i >= 19) break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void adjustBounds() {
|
||||
if (xValue % 12 == 0 && xValue != 0) {
|
||||
yValue += 2;
|
||||
xValue = 0;
|
||||
} else {
|
||||
xValue += 4;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isListTooLong() {
|
||||
return CommandButtons.getMasterCommList().size() > 19;
|
||||
}
|
||||
|
||||
// Change background panel color to transparent black
|
||||
@Override
|
||||
public void addPainters() {
|
||||
super.addPainters();
|
||||
this.rootPanel.setBackgroundPainter(BackgroundPainter.createColorful(0x4D000000));
|
||||
}
|
||||
|
||||
private void setupBackground(WGridPanel root) {
|
||||
setRootPanel(root);
|
||||
root.setSize(350, 240);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
package com.josyf.commandbuttons.gui;
|
||||
|
||||
import io.github.cottonmc.cotton.gui.GuiDescription;
|
||||
import io.github.cottonmc.cotton.gui.client.CottonClientScreen;
|
||||
|
||||
public class ButtonGUIScreen extends CottonClientScreen {
|
||||
public ButtonGUIScreen(GuiDescription description) {
|
||||
super(description);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user