| 1 | package nicotine.mod.mods.misc;
|
| 2 |
|
| 3 | import ca.weblite.objc.NSObject;
|
| 4 | import ca.weblite.objc.Proxy;
|
| 5 | import ca.weblite.objc.RuntimeUtils;
|
| 6 | import ca.weblite.objc.annotations.Msg;
|
| 7 | import com.sun.jna.Pointer;
|
| 8 | import net.minecraft.resources.Identifier;
|
| 9 | import net.minecraft.world.item.ItemStack;
|
| 10 | import net.minecraft.world.item.Items;
|
| 11 | import net.minecraft.world.level.Level;
|
| 12 | import net.minecraft.world.phys.Vec3;
|
| 13 | import nicotine.command.Command;
|
| 14 | import nicotine.command.CommandManager;
|
| 15 | import nicotine.command.commands.TouchBarCustom;
|
| 16 | import nicotine.events.ClientTickEvent;
|
| 17 | import nicotine.mod.Mod;
|
| 18 | import nicotine.mod.ModCategory;
|
| 19 | import nicotine.mod.ModManager;
|
| 20 | import nicotine.mod.mods.hud.GIF;
|
| 21 | import nicotine.mod.option.SliderOption;
|
| 22 | import nicotine.mod.option.ToggleOption;
|
| 23 | import nicotine.util.EventBus;
|
| 24 | import nicotine.util.render.Render2D;
|
| 25 | import org.joml.Vector2d;
|
| 26 |
|
| 27 | import java.io.*;
|
| 28 | import java.util.*;
|
| 29 |
|
| 30 | import static nicotine.util.Common.*;
|
| 31 |
|
| 32 | public class TouchBar extends Mod {
|
| 33 |
|
| 34 | public static class TouchBarButton {
|
| 35 | public String identifier;
|
| 36 | public Pointer item;
|
| 37 | public boolean visible;
|
| 38 |
|
| 39 | public TouchBarButton(String identifier, Pointer item, boolean visible) {
|
| 40 | this.identifier = identifier;
|
| 41 | this.item = item;
|
| 42 | this.visible = visible;
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | public static final List<TouchBarButton> buttons = new ArrayList<>();
|
| 47 |
|
| 48 | private static final HashMap<String, byte[]> touchBarImages = new HashMap<>();
|
| 49 |
|
| 50 | private static long elapsedTime = System.currentTimeMillis();
|
| 51 |
|
| 52 | private final SliderOption refresh = new SliderOption("Refresh", 5, 1, 20, false);
|
| 53 | private final ToggleOption coords = new ToggleOption("Coords");
|
| 54 | private final ToggleOption totem = new ToggleOption("Totem");
|
| 55 | private final ToggleOption eCrystal = new ToggleOption("ECrystal");
|
| 56 | private final ToggleOption server = new ToggleOption("Server");
|
| 57 | private final ToggleOption customGIF = new ToggleOption("CustomGIF");
|
| 58 | private final ToggleOption customButtons = new ToggleOption("CustomButtons");
|
| 59 |
|
| 60 | public TouchBar() {
|
| 61 | super(ModCategory.Misc, "TouchBar", "Touch bar for Macbook Pro devices");
|
| 62 | this.addOptions(Arrays.asList(refresh, coords, totem, eCrystal, server, customGIF, customButtons));
|
| 63 | }
|
| 64 |
|
| 65 | private static class ButtonClickHandler extends NSObject {
|
| 66 | String executeString;
|
| 67 |
|
| 68 | public ButtonClickHandler(String executeString) {
|
| 69 | super();
|
| 70 | this.init("NSObject");
|
| 71 | this.executeString = executeString;
|
| 72 | }
|
| 73 |
|
| 74 | @Msg(selector = "clicked", signature = "v@:@")
|
| 75 | public void buttonClicked(Proxy sender) {
|
| 76 | Mod mod = ModManager.getMod(executeString);
|
| 77 |
|
| 78 | if (mod != null) {
|
| 79 | mod.toggle();
|
| 80 | } else {
|
| 81 | String[] command = Arrays.stream(executeString.split(" ")).toArray(String[]::new);
|
| 82 |
|
| 83 | for (Command cmd : CommandManager.commands) {
|
| 84 | if (cmd.name.equals(command[0])) {
|
| 85 | cmd.trigger(command);
|
| 86 | break;
|
| 87 | }
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | private static void getCustomButtons(boolean visible) {
|
| 94 | for (String btnName : TouchBarCustom.customTouchBarItems.keySet()) {
|
| 95 | createButton(btnName, btnName, touchBarImages.get("item-redstone.png"), true, false, TouchBarCustom.customTouchBarItems.get(btnName));
|
| 96 | setButtonVisibility(btnName, visible);
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | private static String getCoordsButton() {
|
| 101 | String coordsText = "0 0 0 [0 0]";
|
| 102 |
|
| 103 | if (mc.player != null) {
|
| 104 | Vec3 pos = mc.player.position();
|
| 105 |
|
| 106 | Vector2d otherWorld = new Vector2d(pos.x, pos.z);
|
| 107 | if (!mc.level.dimension().equals(Level.END)) {
|
| 108 |
|
| 109 | if (mc.level.dimension().equals(Level.NETHER)) {
|
| 110 | otherWorld.mul(8);
|
| 111 | } else
|
| 112 | otherWorld.div(8);
|
| 113 | }
|
| 114 |
|
| 115 | coordsText = String.format("%.1f %.1f %.1f [%.1f %.1f]", pos.x, pos.y, pos.z, otherWorld.x, otherWorld.y);
|
| 116 | }
|
| 117 |
|
| 118 | return createButton("pos", coordsText, touchBarImages.get("item-compass_01.png"), true, false, null);
|
| 119 | }
|
| 120 |
|
| 121 | private static String getTotemButton() {
|
| 122 | int totemCount = 0;
|
| 123 |
|
| 124 | if (mc.player != null) {
|
| 125 | for (int i = 0; i <= 45; i++) {
|
| 126 | ItemStack itemStack = mc.player.getInventory().getItem(i);
|
| 127 |
|
| 128 | if (itemStack.getItem() == Items.TOTEM_OF_UNDYING) {
|
| 129 | totemCount++;
|
| 130 | }
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | return createButton("totem", String.valueOf(totemCount), touchBarImages.get("item-totem_of_undying.png"), true, false, null);
|
| 135 | }
|
| 136 |
|
| 137 | private static String getECrystalButton() {
|
| 138 | int eCrystalCount = 0;
|
| 139 |
|
| 140 | if (mc.player != null) {
|
| 141 | for (int i = 0; i <= 45; i++) {
|
| 142 | ItemStack itemStack = mc.player.getInventory().getItem(i);
|
| 143 |
|
| 144 | if (itemStack.getItem() == Items.END_CRYSTAL) {
|
| 145 | eCrystalCount += itemStack.getCount();
|
| 146 | }
|
| 147 | }
|
| 148 | }
|
| 149 |
|
| 150 | return createButton("eCrystal", String.valueOf(eCrystalCount), touchBarImages.get("item-end_crystal.png"), true, false, null);
|
| 151 | }
|
| 152 |
|
| 153 | private static String getServerButton() {
|
| 154 |
|
| 155 | String address = "singleplayer";
|
| 156 | byte[] favicon = new byte[0];
|
| 157 |
|
| 158 | if (mc.player == null)
|
| 159 | address = "main menu";
|
| 160 | else if (mc.hasSingleplayerServer())
|
| 161 | address = "single player";
|
| 162 | else if (currentServer != null) {
|
| 163 | address = currentServer.ip;
|
| 164 | favicon = currentServer.getIconBytes();
|
| 165 | }
|
| 166 |
|
| 167 | return createButton("server", address, favicon, true, true, null);
|
| 168 | }
|
| 169 |
|
| 170 | private static String getCustomGIFButton() {
|
| 171 | if (Render2D.getGIFEntry() == null)
|
| 172 | GIF.images.select();
|
| 173 |
|
| 174 | return createButton("custom", "", Render2D.getGIFEntry().bytes, false, true, null);
|
| 175 | }
|
| 176 |
|
| 177 | private static void setButtonTitle(Pointer button, String title) {
|
| 178 | Pointer titleStr = RuntimeUtils.msgPointer("NSString", "stringWithUTF8String:", title);
|
| 179 | RuntimeUtils.msg(button, "setTitle:", titleStr);
|
| 180 | }
|
| 181 |
|
| 182 | private static void setButtonImage(Pointer button, byte[] img, boolean adjustImgPosition, boolean adjustImageScaling) {
|
| 183 | if (img == null)
|
| 184 | return;
|
| 185 |
|
| 186 | Pointer NSData = RuntimeUtils.cls("NSData");
|
| 187 | Pointer imageData = RuntimeUtils.msgPointer(NSData, "dataWithBytes:length:", img, img.length);
|
| 188 |
|
| 189 | Pointer NSImage = RuntimeUtils.cls("NSImage");
|
| 190 | Pointer allocImage = RuntimeUtils.msgPointer(NSImage, "alloc");
|
| 191 |
|
| 192 | Pointer image = RuntimeUtils.msgPointer(allocImage, "initWithData:", imageData);
|
| 193 | RuntimeUtils.msg(button, "setImage:", image);
|
| 194 |
|
| 195 | if (adjustImgPosition)
|
| 196 | RuntimeUtils.msg(button, "setImagePosition:", 2);
|
| 197 |
|
| 198 | if (adjustImageScaling)
|
| 199 | RuntimeUtils.msg(button, "setImageScaling:", 3);
|
| 200 | }
|
| 201 |
|
| 202 | private static void setButtonVisibility(String identifier, boolean visibility) {
|
| 203 | for (TouchBarButton touchBarButton : buttons) {
|
| 204 | if (identifier.equals(touchBarButton.identifier)) {
|
| 205 | if (touchBarButton.visible == visibility)
|
| 206 | return;
|
| 207 |
|
| 208 | touchBarButton.visible = visibility;
|
| 209 | setTouchBar();
|
| 210 |
|
| 211 | return;
|
| 212 | }
|
| 213 | }
|
| 214 | }
|
| 215 |
|
| 216 | public static String createButton(String identifier, String title, byte[] img, boolean adjustImgPosition, boolean adjustImageScaling, String executeString) {
|
| 217 | for (TouchBarButton touchBarButton : buttons) {
|
| 218 | if (touchBarButton.identifier.equals(identifier)) {
|
| 219 | Pointer button = RuntimeUtils.msgPointer(touchBarButton.item, "view");
|
| 220 | setButtonTitle(button, title);
|
| 221 | setButtonImage(button, img, adjustImgPosition, adjustImageScaling);
|
| 222 | return identifier;
|
| 223 | }
|
| 224 | }
|
| 225 |
|
| 226 | Pointer NSCustomTouchBarItem = RuntimeUtils.cls("NSCustomTouchBarItem");
|
| 227 | Pointer alloc = RuntimeUtils.msgPointer(NSCustomTouchBarItem, "alloc");
|
| 228 | Pointer customItem = RuntimeUtils.msgPointer(alloc, "initWithIdentifier:", RuntimeUtils.str(identifier));
|
| 229 |
|
| 230 | Pointer NSButtonClass = RuntimeUtils.cls("NSButton");
|
| 231 | Pointer allocButton = RuntimeUtils.msgPointer(NSButtonClass, "alloc");
|
| 232 | Pointer button = RuntimeUtils.msgPointer(allocButton, "init");
|
| 233 |
|
| 234 | setButtonTitle(button, title);
|
| 235 | setButtonImage(button, img, adjustImgPosition, adjustImageScaling);
|
| 236 |
|
| 237 | if (executeString != null) {
|
| 238 | ButtonClickHandler handler = new ButtonClickHandler(executeString);
|
| 239 | Pointer target = handler.getPeer();
|
| 240 |
|
| 241 | RuntimeUtils.msg(button, "setTarget:", target);
|
| 242 | RuntimeUtils.msg(button, "setAction:", RuntimeUtils.sel("clicked"));
|
| 243 | }
|
| 244 |
|
| 245 | RuntimeUtils.msg(customItem, "setView:", button);
|
| 246 |
|
| 247 | buttons.add(new TouchBarButton(identifier, customItem, true));
|
| 248 |
|
| 249 | return identifier;
|
| 250 | }
|
| 251 |
|
| 252 | public static void setTouchBar() {
|
| 253 | Pointer NSTouchBarClass = RuntimeUtils.cls("NSTouchBar");
|
| 254 | Pointer alloc = RuntimeUtils.msgPointer(NSTouchBarClass, "alloc");
|
| 255 | Pointer touchBar = RuntimeUtils.msgPointer(alloc, "init");
|
| 256 |
|
| 257 | List<Pointer> identifierPtrs = new ArrayList<>();
|
| 258 | List<Pointer> itemPtrs = new ArrayList<>();
|
| 259 |
|
| 260 | int count = 0;
|
| 261 |
|
| 262 | for (TouchBarButton touchBarButton : buttons) {
|
| 263 | if (!touchBarButton.visible)
|
| 264 | continue;
|
| 265 |
|
| 266 | identifierPtrs.add(RuntimeUtils.str(touchBarButton.identifier));
|
| 267 | itemPtrs.add(touchBarButton.item);
|
| 268 | count++;
|
| 269 | }
|
| 270 |
|
| 271 | Pointer identifiers = RuntimeUtils.msgPointer("NSArray", "arrayWithObjects:count:", identifierPtrs.toArray(Pointer[]::new), count);
|
| 272 | RuntimeUtils.msg(touchBar, "setDefaultItemIdentifiers:", identifiers);
|
| 273 |
|
| 274 | Pointer itemSet = RuntimeUtils.msgPointer("NSSet", "setWithObjects:count:", itemPtrs.toArray(Pointer[]::new), count);
|
| 275 | RuntimeUtils.msg(touchBar, "setTemplateItems:", itemSet);
|
| 276 |
|
| 277 | Pointer NSApp = RuntimeUtils.msgPointer(RuntimeUtils.cls("NSApplication"), "sharedApplication");
|
| 278 | Pointer mainWindow = RuntimeUtils.msgPointer(NSApp, "mainWindow");
|
| 279 | RuntimeUtils.msg(mainWindow, "setTouchBar:", touchBar);
|
| 280 | }
|
| 281 |
|
| 282 | private static void extractTouchBarImagesBytes() {
|
| 283 | List<String> fileNames = Arrays.asList(
|
| 284 | "block-grass_block_side.png",
|
| 285 | "item-compass_01.png",
|
| 286 | "item-redstone.png",
|
| 287 | "item-end_crystal.png",
|
| 288 | "item-totem_of_undying.png"
|
| 289 | );
|
| 290 |
|
| 291 | for (String fileName : fileNames) {
|
| 292 | String path = String.format("minecraft:textures/%s", fileName.replace('-', '/'));
|
| 293 |
|
| 294 | try {
|
| 295 | InputStream inputStream = mc.getResourceManager().getResource(Identifier.parse(path)).get().open();
|
| 296 | touchBarImages.put(fileName, inputStream.readAllBytes());
|
| 297 | } catch (IOException e) {
|
| 298 | e.printStackTrace();
|
| 299 | }
|
| 300 | }
|
| 301 | }
|
| 302 |
|
| 303 | @Override
|
| 304 | public void toggle() {
|
| 305 | this.enabled = !this.enabled;
|
| 306 |
|
| 307 | if (!this.enabled) {
|
| 308 | buttons.clear();
|
| 309 | setTouchBar();
|
| 310 | }
|
| 311 | }
|
| 312 |
|
| 313 | @Override
|
| 314 | protected void init() {
|
| 315 | extractTouchBarImagesBytes();
|
| 316 |
|
| 317 | EventBus.register(ClientTickEvent.class, event -> {
|
| 318 | if (!this.enabled)
|
| 319 | return true;
|
| 320 |
|
| 321 | long curTime = System.currentTimeMillis();
|
| 322 | if (curTime - elapsedTime < refresh.value * 50)
|
| 323 | return true;
|
| 324 | else
|
| 325 | elapsedTime = curTime;
|
| 326 |
|
| 327 | int buttonsSize = buttons.size();
|
| 328 |
|
| 329 | setButtonVisibility(getCoordsButton(), coords.enabled);
|
| 330 | setButtonVisibility(getTotemButton(), totem.enabled);
|
| 331 | setButtonVisibility(getECrystalButton(), eCrystal.enabled);
|
| 332 | setButtonVisibility(getServerButton(), server.enabled);
|
| 333 | setButtonVisibility(getCustomGIFButton(), customGIF.enabled);
|
| 334 |
|
| 335 | getCustomButtons(customButtons.enabled);
|
| 336 |
|
| 337 | if (buttons.size() != buttonsSize) {
|
| 338 | setTouchBar();
|
| 339 | }
|
| 340 |
|
| 341 | return true;
|
| 342 | });
|
| 343 |
|
| 344 | }
|
| 345 | }
|
| 346 |
|