| 1 | /*
|
| 2 | * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
|
| 3 | * Copyright (c) Meteor Development.
|
| 4 | */
|
| 5 |
|
| 6 | package meteordevelopment.meteorclient.utils.misc;
|
| 7 |
|
| 8 | import baritone.api.BaritoneAPI;
|
| 9 | import baritone.api.pathing.goals.Goal;
|
| 10 | import baritone.api.process.IBaritoneProcess;
|
| 11 | import meteordevelopment.meteorclient.MeteorClient;
|
| 12 | import meteordevelopment.meteorclient.mixin.MinecraftAccessor;
|
| 13 | import meteordevelopment.meteorclient.mixin.MultiPlayerGameModeAccessor;
|
| 14 | import meteordevelopment.meteorclient.pathing.BaritoneUtils;
|
| 15 | import meteordevelopment.meteorclient.pathing.PathManagers;
|
| 16 | import meteordevelopment.meteorclient.systems.config.Config;
|
| 17 | import meteordevelopment.meteorclient.systems.modules.Module;
|
| 18 | import meteordevelopment.meteorclient.systems.modules.Modules;
|
| 19 | import meteordevelopment.meteorclient.utils.PreInit;
|
| 20 | import meteordevelopment.meteorclient.utils.Utils;
|
| 21 | import meteordevelopment.meteorclient.utils.player.ChatUtils;
|
| 22 | import meteordevelopment.meteorclient.utils.player.PlayerUtils;
|
| 23 | import meteordevelopment.meteorclient.utils.world.Dimension;
|
| 24 | import meteordevelopment.meteorclient.utils.world.TickRate;
|
| 25 | import net.minecraft.IdentifierException;
|
| 26 | import net.minecraft.SharedConstants;
|
| 27 | import net.minecraft.client.multiplayer.PlayerInfo;
|
| 28 | import net.minecraft.core.BlockPos;
|
| 29 | import net.minecraft.core.Holder;
|
| 30 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 31 | import net.minecraft.core.registries.Registries;
|
| 32 | import net.minecraft.network.protocol.game.ServerboundClientCommandPacket;
|
| 33 | import net.minecraft.resources.Identifier;
|
| 34 | import net.minecraft.stats.Stat;
|
| 35 | import net.minecraft.stats.Stats;
|
| 36 | import net.minecraft.world.effect.MobEffect;
|
| 37 | import net.minecraft.world.effect.MobEffectInstance;
|
| 38 | import net.minecraft.world.entity.Entity;
|
| 39 | import net.minecraft.world.entity.LivingEntity;
|
| 40 | import net.minecraft.world.item.Item;
|
| 41 | import net.minecraft.world.item.ItemStack;
|
| 42 | import net.minecraft.world.item.Items;
|
| 43 | import net.minecraft.world.level.block.state.BlockState;
|
| 44 | import net.minecraft.world.phys.BlockHitResult;
|
| 45 | import net.minecraft.world.phys.EntityHitResult;
|
| 46 | import net.minecraft.world.phys.HitResult;
|
| 47 | import net.minecraft.world.phys.Vec3;
|
| 48 | import org.apache.commons.lang3.StringUtils;
|
| 49 | import org.meteordev.starscript.Script;
|
| 50 | import org.meteordev.starscript.Section;
|
| 51 | import org.meteordev.starscript.StandardLib;
|
| 52 | import org.meteordev.starscript.Starscript;
|
| 53 | import org.meteordev.starscript.compiler.Compiler;
|
| 54 | import org.meteordev.starscript.compiler.Parser;
|
| 55 | import org.meteordev.starscript.utils.Error;
|
| 56 | import org.meteordev.starscript.utils.StarscriptError;
|
| 57 | import org.meteordev.starscript.value.Value;
|
| 58 | import org.meteordev.starscript.value.ValueMap;
|
| 59 |
|
| 60 | import java.time.LocalTime;
|
| 61 | import java.time.format.DateTimeFormatter;
|
| 62 | import java.time.format.FormatStyle;
|
| 63 | import java.util.Arrays;
|
| 64 | import java.util.List;
|
| 65 | import java.util.Optional;
|
| 66 | import java.util.stream.Collectors;
|
| 67 |
|
| 68 | import static meteordevelopment.meteorclient.MeteorClient.mc;
|
| 69 |
|
| 70 | public class MeteorStarscript {
|
| 71 | public static Starscript ss = new Starscript();
|
| 72 |
|
| 73 | private static final BlockPos.MutableBlockPos BP = new BlockPos.MutableBlockPos();
|
| 74 | private static final StringBuilder SB = new StringBuilder();
|
| 75 |
|
| 76 | @PreInit(dependencies = PathManagers.class)
|
| 77 | public static void init() {
|
| 78 | StandardLib.init(ss);
|
| 79 |
|
| 80 | // General
|
| 81 | ss.set("mc_version", SharedConstants.getCurrentVersion().name());
|
| 82 | ss.set("fps", () -> Value.number(MinecraftAccessor.meteor$getFps()));
|
| 83 | ss.set("ping", MeteorStarscript::ping);
|
| 84 | ss.set("time", () -> Value.string(LocalTime.now().format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT))));
|
| 85 | ss.set("cps", () -> Value.number(CPSUtils.getCpsAverage()));
|
| 86 |
|
| 87 | // Meteor
|
| 88 | ss.set("meteor", new ValueMap()
|
| 89 | .set("name", MeteorClient.NAME)
|
| 90 | .set("version", MeteorClient.VERSION != null ? (MeteorClient.BUILD_NUMBER.isEmpty() ? MeteorClient.VERSION.toString() : MeteorClient.VERSION + " " + MeteorClient.BUILD_NUMBER) : "")
|
| 91 | .set("modules", () -> Value.number(Modules.get().getAll().size()))
|
| 92 | .set("active_modules", () -> Value.number(Modules.get().getActive().size()))
|
| 93 | .set("is_module_active", MeteorStarscript::isModuleActive)
|
| 94 | .set("get_module_info", MeteorStarscript::getModuleInfo)
|
| 95 | .set("get_module_setting", MeteorStarscript::getModuleSetting)
|
| 96 | .set("prefix", MeteorStarscript::getMeteorPrefix)
|
| 97 | );
|
| 98 |
|
| 99 | // Baritone
|
| 100 | if (BaritoneUtils.IS_AVAILABLE) {
|
| 101 | ss.set("baritone", new ValueMap()
|
| 102 | .set("is_pathing", () -> Value.bool(BaritoneAPI.getProvider().getPrimaryBaritone().getPathingBehavior().isPathing()))
|
| 103 | .set("distance_to_goal", MeteorStarscript::baritoneDistanceToGoal)
|
| 104 | .set("process", MeteorStarscript::baritoneProcess)
|
| 105 | .set("process_name", MeteorStarscript::baritoneProcessName)
|
| 106 | .set("eta", MeteorStarscript::baritoneETA)
|
| 107 | );
|
| 108 | }
|
| 109 |
|
| 110 | // Camera
|
| 111 | ss.set("camera", new ValueMap()
|
| 112 | .set("pos", new ValueMap()
|
| 113 | .set("_toString", () -> posString(false, true))
|
| 114 | .set("x", () -> Value.number(mc.gameRenderer.mainCamera().position().x))
|
| 115 | .set("y", () -> Value.number(mc.gameRenderer.mainCamera().position().y))
|
| 116 | .set("z", () -> Value.number(mc.gameRenderer.mainCamera().position().z))
|
| 117 | )
|
| 118 | .set("opposite_dim_pos", new ValueMap()
|
| 119 | .set("_toString", () -> posString(true, true))
|
| 120 | .set("x", () -> oppositeX(true))
|
| 121 | .set("y", () -> Value.number(mc.gameRenderer.mainCamera().position().y))
|
| 122 | .set("z", () -> oppositeZ(true))
|
| 123 | )
|
| 124 |
|
| 125 | .set("yaw", () -> yaw(true))
|
| 126 | .set("pitch", () -> pitch(true))
|
| 127 | .set("direction", () -> direction(true))
|
| 128 | );
|
| 129 |
|
| 130 | // Player
|
| 131 | ss.set("player", new ValueMap()
|
| 132 | .set("_toString", () -> Value.string(mc.getUser().getName()))
|
| 133 | .set("health", () -> Value.number(mc.player != null ? mc.player.getHealth() : 0))
|
| 134 | .set("absorption", () -> Value.number(mc.player != null ? mc.player.getAbsorptionAmount() : 0))
|
| 135 | .set("hunger", () -> Value.number(mc.player != null ? mc.player.getFoodData().getFoodLevel() : 0))
|
| 136 | .set("saturation", () -> Value.number(mc.player != null ? mc.player.getFoodData().getSaturationLevel() : 0))
|
| 137 |
|
| 138 | .set("speed", () -> Value.number(Utils.getPlayerSpeed().horizontalDistance()))
|
| 139 | .set("speed_all", new ValueMap()
|
| 140 | .set("_toString", () -> Value.string(mc.player != null ? Utils.getPlayerSpeed().toString() : ""))
|
| 141 | .set("x", () -> Value.number(mc.player != null ? Utils.getPlayerSpeed().x : 0))
|
| 142 | .set("y", () -> Value.number(mc.player != null ? Utils.getPlayerSpeed().y : 0))
|
| 143 | .set("z", () -> Value.number(mc.player != null ? Utils.getPlayerSpeed().z : 0))
|
| 144 | )
|
| 145 |
|
| 146 | .set("breaking_progress", () -> Value.number(mc.gameMode != null ? ((MultiPlayerGameModeAccessor) mc.gameMode).meteor$getBreakingProgress() : 0))
|
| 147 | .set("biome", MeteorStarscript::biome)
|
| 148 |
|
| 149 | .set("dimension", () -> Value.string(PlayerUtils.getDimension().name()))
|
| 150 | .set("opposite_dimension", () -> Value.string(PlayerUtils.getDimension().opposite().name()))
|
| 151 |
|
| 152 | .set("gamemode", () -> PlayerUtils.getGameMode() != null ? Value.string(StringUtils.capitalize(PlayerUtils.getGameMode().getName())) : Value.null_())
|
| 153 |
|
| 154 | .set("pos", new ValueMap()
|
| 155 | .set("_toString", () -> posString(false, false))
|
| 156 | .set("x", () -> Value.number(mc.player != null ? mc.player.getX() : 0))
|
| 157 | .set("y", () -> Value.number(mc.player != null ? mc.player.getY() : 0))
|
| 158 | .set("z", () -> Value.number(mc.player != null ? mc.player.getZ() : 0))
|
| 159 | )
|
| 160 | .set("opposite_dim_pos", new ValueMap()
|
| 161 | .set("_toString", () -> posString(true, false))
|
| 162 | .set("x", () -> oppositeX(false))
|
| 163 | .set("y", () -> Value.number(mc.player != null ? mc.player.getY() : 0))
|
| 164 | .set("z", () -> oppositeZ(false))
|
| 165 | )
|
| 166 |
|
| 167 | .set("yaw", () -> yaw(false))
|
| 168 | .set("pitch", () -> pitch(false))
|
| 169 | .set("direction", () -> direction(false))
|
| 170 |
|
| 171 | .set("hand", () -> mc.player != null ? wrap(mc.player.getMainHandItem()) : Value.null_())
|
| 172 | .set("offhand", () -> mc.player != null ? wrap(mc.player.getOffhandItem()) : Value.null_())
|
| 173 | .set("hand_or_offhand", MeteorStarscript::handOrOffhand)
|
| 174 | .set("get_item", MeteorStarscript::getItem)
|
| 175 | .set("count_items", MeteorStarscript::countItems)
|
| 176 |
|
| 177 | .set("xp", new ValueMap()
|
| 178 | .set("level", () -> Value.number(mc.player != null ? mc.player.experienceLevel : 0))
|
| 179 | .set("progress", () -> Value.number(mc.player != null ? mc.player.experienceProgress : 0))
|
| 180 | .set("total", () -> Value.number(mc.player != null ? mc.player.totalExperience : 0))
|
| 181 | )
|
| 182 |
|
| 183 | .set("has_potion_effect", MeteorStarscript::hasPotionEffect)
|
| 184 | .set("get_potion_effect", MeteorStarscript::getPotionEffect)
|
| 185 |
|
| 186 | .set("get_stat", MeteorStarscript::getStat)
|
| 187 | );
|
| 188 |
|
| 189 | // Crosshair target
|
| 190 | ss.set("crosshair_target", new ValueMap()
|
| 191 | .set("type", MeteorStarscript::crosshairType)
|
| 192 | .set("value", MeteorStarscript::crosshairValue)
|
| 193 | );
|
| 194 |
|
| 195 | // Server
|
| 196 | ss.set("server", new ValueMap()
|
| 197 | .set("_toString", () -> Value.string(Utils.getWorldName()))
|
| 198 | .set("tps", () -> Value.number(TickRate.INSTANCE.getTickRate()))
|
| 199 | .set("time", () -> Value.string(Utils.getWorldTime()))
|
| 200 | .set("weather", MeteorStarscript::weather)
|
| 201 | .set("player_count", () -> Value.number(mc.getConnection() != null ? mc.getConnection().getOnlinePlayers().size() : 0))
|
| 202 | .set("difficulty", () -> Value.string(mc.level != null ? mc.level.getDifficulty().name() : ""))
|
| 203 | );
|
| 204 | }
|
| 205 |
|
| 206 | // Helpers
|
| 207 |
|
| 208 | public static Script compile(String source) {
|
| 209 | Parser.Result result = Parser.parse(source);
|
| 210 |
|
| 211 | if (result.hasErrors()) {
|
| 212 | for (Error error : result.errors) printChatError(error);
|
| 213 | return null;
|
| 214 | }
|
| 215 |
|
| 216 | return Compiler.compile(result);
|
| 217 | }
|
| 218 |
|
| 219 | public static Section runSection(Script script, StringBuilder sb) {
|
| 220 | try {
|
| 221 | return ss.run(script, sb);
|
| 222 | } catch (StarscriptError error) {
|
| 223 | printChatError(error);
|
| 224 | return null;
|
| 225 | }
|
| 226 | }
|
| 227 |
|
| 228 | public static String run(Script script, StringBuilder sb) {
|
| 229 | Section section = runSection(script, sb);
|
| 230 | return section != null ? section.toString() : null;
|
| 231 | }
|
| 232 |
|
| 233 | public static Section runSection(Script script) {
|
| 234 | return runSection(script, new StringBuilder());
|
| 235 | }
|
| 236 |
|
| 237 | public static String run(Script script) {
|
| 238 | return run(script, new StringBuilder());
|
| 239 | }
|
| 240 |
|
| 241 | // Errors
|
| 242 |
|
| 243 | public static void printChatError(int i, Error error) {
|
| 244 | String caller = getCallerName();
|
| 245 |
|
| 246 | if (caller != null) {
|
| 247 | if (i != -1)
|
| 248 | ChatUtils.errorPrefix("Starscript", "%d, %d '%c': %s (from %s)", i, error.character, error.ch, error.message, caller);
|
| 249 | else
|
| 250 | ChatUtils.errorPrefix("Starscript", "%d '%c': %s (from %s)", error.character, error.ch, error.message, caller);
|
| 251 | } else {
|
| 252 | if (i != -1)
|
| 253 | ChatUtils.errorPrefix("Starscript", "%d, %d '%c': %s", i, error.character, error.ch, error.message);
|
| 254 | else ChatUtils.errorPrefix("Starscript", "%d '%c': %s", error.character, error.ch, error.message);
|
| 255 | }
|
| 256 | }
|
| 257 |
|
| 258 | public static void printChatError(Error error) {
|
| 259 | printChatError(-1, error);
|
| 260 | }
|
| 261 |
|
| 262 | public static void printChatError(StarscriptError e) {
|
| 263 | String caller = getCallerName();
|
| 264 |
|
| 265 | if (caller != null) ChatUtils.errorPrefix("Starscript", "%s (from %s)", e.getMessage(), caller);
|
| 266 | else ChatUtils.errorPrefix("Starscript", "%s", e.getMessage());
|
| 267 | }
|
| 268 |
|
| 269 | private static String getCallerName() {
|
| 270 | StackTraceElement[] elements = Thread.currentThread().getStackTrace();
|
| 271 | if (elements.length == 0) return null;
|
| 272 |
|
| 273 | for (int i = 1; i < elements.length; i++) {
|
| 274 | String name = elements[i].getClassName();
|
| 275 |
|
| 276 | if (name.startsWith(Starscript.class.getPackageName())) continue;
|
| 277 | if (name.equals(MeteorStarscript.class.getName())) continue;
|
| 278 |
|
| 279 | return name.substring(name.lastIndexOf('.') + 1);
|
| 280 | }
|
| 281 |
|
| 282 | return null;
|
| 283 | }
|
| 284 |
|
| 285 | // Functions
|
| 286 |
|
| 287 | private static long lastRequestedStatsTime = 0;
|
| 288 |
|
| 289 | private static Value hasPotionEffect(Starscript ss, int argCount) {
|
| 290 | if (argCount < 1) ss.error("player.has_potion_effect() requires 1 argument, got %d.", argCount);
|
| 291 | if (mc.player == null) return Value.bool(false);
|
| 292 |
|
| 293 | Identifier name = popIdentifier(ss, "First argument to player.has_potion_effect() needs to a string.");
|
| 294 |
|
| 295 | Optional<Holder.Reference<MobEffect>> effect = BuiltInRegistries.MOB_EFFECT.get(name);
|
| 296 | if (effect.isEmpty()) return Value.bool(false);
|
| 297 |
|
| 298 | MobEffectInstance effectInstance = mc.player.getEffect(effect.get());
|
| 299 | return Value.bool(effectInstance != null);
|
| 300 | }
|
| 301 |
|
| 302 | private static Value getPotionEffect(Starscript ss, int argCount) {
|
| 303 | if (argCount < 1) ss.error("player.get_potion_effect() requires 1 argument, got %d.", argCount);
|
| 304 | if (mc.player == null) return Value.null_();
|
| 305 |
|
| 306 | Identifier name = popIdentifier(ss, "First argument to player.get_potion_effect() needs to a string.");
|
| 307 |
|
| 308 | Optional<Holder.Reference<MobEffect>> effect = BuiltInRegistries.MOB_EFFECT.get(name);
|
| 309 | if (effect.isEmpty()) return Value.null_();
|
| 310 |
|
| 311 | MobEffectInstance effectInstance = mc.player.getEffect(effect.get());
|
| 312 | if (effectInstance == null) return Value.null_();
|
| 313 |
|
| 314 | return wrap(effectInstance);
|
| 315 | }
|
| 316 |
|
| 317 | private static Value getStat(Starscript ss, int argCount) {
|
| 318 | if (argCount < 1) ss.error("player.get_stat() requires 1 argument, got %d.", argCount);
|
| 319 | if (mc.player == null) return Value.number(0);
|
| 320 |
|
| 321 | long time = System.currentTimeMillis();
|
| 322 | if ((time - lastRequestedStatsTime) / 1000.0 >= 1 && mc.getConnection() != null) {
|
| 323 | mc.getConnection().send(new ServerboundClientCommandPacket(ServerboundClientCommandPacket.Action.REQUEST_STATS));
|
| 324 | lastRequestedStatsTime = time;
|
| 325 | }
|
| 326 |
|
| 327 | String type = argCount > 1 ? ss.popString("First argument to player.get_stat() needs to be a string.") : "custom";
|
| 328 | Identifier name = popIdentifier(ss, (argCount > 1 ? "Second" : "First") + " argument to player.get_stat() needs to be a string.");
|
| 329 |
|
| 330 | Stat<?> stat = switch (type) {
|
| 331 | case "mined" -> Stats.BLOCK_MINED.get(BuiltInRegistries.BLOCK.getValue(name));
|
| 332 | case "crafted" -> Stats.ITEM_CRAFTED.get(BuiltInRegistries.ITEM.getValue(name));
|
| 333 | case "used" -> Stats.ITEM_USED.get(BuiltInRegistries.ITEM.getValue(name));
|
| 334 | case "broken" -> Stats.ITEM_BROKEN.get(BuiltInRegistries.ITEM.getValue(name));
|
| 335 | case "picked_up" -> Stats.ITEM_PICKED_UP.get(BuiltInRegistries.ITEM.getValue(name));
|
| 336 | case "dropped" -> Stats.ITEM_DROPPED.get(BuiltInRegistries.ITEM.getValue(name));
|
| 337 | case "killed" -> Stats.ENTITY_KILLED.get(BuiltInRegistries.ENTITY_TYPE.getValue(name));
|
| 338 | case "killed_by" -> Stats.ENTITY_KILLED_BY.get(BuiltInRegistries.ENTITY_TYPE.getValue(name));
|
| 339 | case "custom" -> {
|
| 340 | name = BuiltInRegistries.CUSTOM_STAT.getValue(name);
|
| 341 | yield name != null ? Stats.CUSTOM.get(name) : null;
|
| 342 | }
|
| 343 | default -> null;
|
| 344 | };
|
| 345 |
|
| 346 | return Value.number(stat != null ? mc.player.getStats().getValue(stat) : 0);
|
| 347 | }
|
| 348 |
|
| 349 | private static Value getModuleInfo(Starscript ss, int argCount) {
|
| 350 | if (argCount != 1) ss.error("meteor.get_module_info() requires 1 argument, got %d.", argCount);
|
| 351 |
|
| 352 | Module module = Modules.get().get(ss.popString("First argument to meteor.get_module_info() needs to be a string."));
|
| 353 | if (module != null && module.isActive()) {
|
| 354 | String info = module.getInfoString();
|
| 355 | return Value.string(info == null ? "" : info);
|
| 356 | }
|
| 357 |
|
| 358 | return Value.string("");
|
| 359 | }
|
| 360 |
|
| 361 | private static Value getModuleSetting(Starscript ss, int argCount) {
|
| 362 | if (argCount != 2) ss.error("meteor.get_module_setting() requires 2 arguments, got %d.", argCount);
|
| 363 |
|
| 364 | var settingName = ss.popString("Second argument to meteor.get_module_setting() needs to be a string.");
|
| 365 | var moduleName = ss.popString("First argument to meteor.get_module_setting() needs to be a string.");
|
| 366 | Module module = Modules.get().get(moduleName);
|
| 367 | if (module == null) {
|
| 368 | ss.error("Unable to get module %s for meteor.get_module_setting()", moduleName);
|
| 369 | }
|
| 370 | var setting = module.settings.get(settingName);
|
| 371 | if (setting == null) {
|
| 372 | ss.error("Unable to get setting %s for module %s for meteor.get_module_setting()", settingName, moduleName);
|
| 373 | }
|
| 374 | var value = setting.get();
|
| 375 | return switch (value) {
|
| 376 | case Double d -> Value.number(d);
|
| 377 | case Integer i -> Value.number(i);
|
| 378 | case Boolean b -> Value.bool(b);
|
| 379 | case List<?> list -> Value.number(list.size());
|
| 380 | case null, default -> Value.string(value.toString());
|
| 381 | };
|
| 382 | }
|
| 383 |
|
| 384 | private static Value isModuleActive(Starscript ss, int argCount) {
|
| 385 | if (argCount != 1) ss.error("meteor.is_module_active() requires 1 argument, got %d.", argCount);
|
| 386 |
|
| 387 | Module module = Modules.get().get(ss.popString("First argument to meteor.is_module_active() needs to be a string."));
|
| 388 | return Value.bool(module != null && module.isActive());
|
| 389 | }
|
| 390 |
|
| 391 | private static Value getItem(Starscript ss, int argCount) {
|
| 392 | if (argCount != 1) ss.error("player.get_item() requires 1 argument, got %d.", argCount);
|
| 393 |
|
| 394 | int i = (int) ss.popNumber("First argument to player.get_item() needs to be a number.");
|
| 395 | if (i < 0) ss.error("First argument to player.get_item() needs to be a non-negative integer.", i);
|
| 396 | return mc.player != null ? wrap(mc.player.getInventory().getItem(i)) : Value.null_();
|
| 397 | }
|
| 398 |
|
| 399 | private static Value countItems(Starscript ss, int argCount) {
|
| 400 | if (argCount != 1) ss.error("player.count_items() requires 1 argument, got %d.", argCount);
|
| 401 |
|
| 402 | String idRaw = ss.popString("First argument to player.count_items() needs to be a string.");
|
| 403 | Identifier id = Identifier.tryParse(idRaw);
|
| 404 | if (id == null) return Value.number(0);
|
| 405 |
|
| 406 | Item item = BuiltInRegistries.ITEM.getValue(id);
|
| 407 | if (item == Items.AIR || mc.player == null) return Value.number(0);
|
| 408 |
|
| 409 | int count = 0;
|
| 410 | for (int i = 0; i < mc.player.getInventory().getContainerSize(); i++) {
|
| 411 | ItemStack itemStack = mc.player.getInventory().getItem(i);
|
| 412 | if (itemStack.getItem() == item) count += itemStack.getCount();
|
| 413 | }
|
| 414 |
|
| 415 | return Value.number(count);
|
| 416 | }
|
| 417 |
|
| 418 | private static Value getMeteorPrefix() {
|
| 419 | if (Config.get() == null) return Value.null_();
|
| 420 | return Value.string(Config.get().prefix.get());
|
| 421 | }
|
| 422 |
|
| 423 | // Other
|
| 424 |
|
| 425 | private static Value baritoneProcess() {
|
| 426 | Optional<IBaritoneProcess> process = BaritoneAPI.getProvider().getPrimaryBaritone().getPathingControlManager().mostRecentInControl();
|
| 427 | return Value.string(process.isEmpty() ? "" : process.get().displayName0());
|
| 428 | }
|
| 429 |
|
| 430 | private static Value baritoneProcessName() {
|
| 431 | Optional<IBaritoneProcess> process = BaritoneAPI.getProvider().getPrimaryBaritone().getPathingControlManager().mostRecentInControl();
|
| 432 | if (process.isEmpty()) return Value.string("");
|
| 433 |
|
| 434 | String className = process.get().getClass().getSimpleName();
|
| 435 | if (className.endsWith("Process")) className = className.substring(0, className.length() - 7);
|
| 436 |
|
| 437 | SB.append(className);
|
| 438 | int i = 0;
|
| 439 | for (int j = 0; j < className.length(); j++) {
|
| 440 | if (j > 0 && Character.isUpperCase(className.charAt(j))) {
|
| 441 | SB.insert(i, ' ');
|
| 442 | i++;
|
| 443 | }
|
| 444 |
|
| 445 | i++;
|
| 446 | }
|
| 447 |
|
| 448 | String name = SB.toString();
|
| 449 | SB.setLength(0);
|
| 450 | return Value.string(name);
|
| 451 | }
|
| 452 |
|
| 453 | // Returns the ETA in seconds
|
| 454 | private static Value baritoneETA() {
|
| 455 | if (mc.player == null) return Value.number(0);
|
| 456 | Optional<Double> ticksTillGoal = BaritoneAPI.getProvider().getPrimaryBaritone().getPathingBehavior().estimatedTicksToGoal();
|
| 457 | return ticksTillGoal.map(aDouble -> Value.number(aDouble / 20)).orElseGet(() -> Value.number(0));
|
| 458 | }
|
| 459 |
|
| 460 | private static Value oppositeX(boolean camera) {
|
| 461 | double x = camera ? mc.gameRenderer.mainCamera().position().x : (mc.player != null ? mc.player.getX() : 0);
|
| 462 | Dimension dimension = PlayerUtils.getDimension();
|
| 463 |
|
| 464 | if (dimension == Dimension.Overworld) x /= 8;
|
| 465 | else if (dimension == Dimension.Nether) x *= 8;
|
| 466 |
|
| 467 | return Value.number(x);
|
| 468 | }
|
| 469 |
|
| 470 | private static Value oppositeZ(boolean camera) {
|
| 471 | double z = camera ? mc.gameRenderer.mainCamera().position().z : (mc.player != null ? mc.player.getZ() : 0);
|
| 472 | Dimension dimension = PlayerUtils.getDimension();
|
| 473 |
|
| 474 | if (dimension == Dimension.Overworld) z /= 8;
|
| 475 | else if (dimension == Dimension.Nether) z *= 8;
|
| 476 |
|
| 477 | return Value.number(z);
|
| 478 | }
|
| 479 |
|
| 480 | private static Value yaw(boolean camera) {
|
| 481 | float yaw;
|
| 482 | if (camera) yaw = mc.gameRenderer.mainCamera().yRot();
|
| 483 | else yaw = mc.player != null ? mc.player.getYRot() : 0;
|
| 484 | yaw %= 360;
|
| 485 |
|
| 486 | if (yaw < 0) yaw += 360;
|
| 487 | if (yaw > 180) yaw -= 360;
|
| 488 |
|
| 489 | return Value.number(yaw);
|
| 490 | }
|
| 491 |
|
| 492 | private static Value pitch(boolean camera) {
|
| 493 | float pitch;
|
| 494 | if (camera) pitch = mc.gameRenderer.mainCamera().xRot();
|
| 495 | else pitch = mc.player != null ? mc.player.getXRot() : 0;
|
| 496 | pitch %= 360;
|
| 497 |
|
| 498 | if (pitch < 0) pitch += 360;
|
| 499 | if (pitch > 180) pitch -= 360;
|
| 500 |
|
| 501 | return Value.number(pitch);
|
| 502 | }
|
| 503 |
|
| 504 | private static Value direction(boolean camera) {
|
| 505 | float yaw;
|
| 506 | if (camera) yaw = mc.gameRenderer.mainCamera().yRot();
|
| 507 | else yaw = mc.player != null ? mc.player.getYRot() : 0;
|
| 508 |
|
| 509 | return wrap(HorizontalDirection.get(yaw));
|
| 510 | }
|
| 511 |
|
| 512 | private static Value biome() {
|
| 513 | if (mc.player == null || mc.level == null) return Value.string("");
|
| 514 |
|
| 515 | BP.set(mc.player.getX(), mc.player.getY(), mc.player.getZ());
|
| 516 | return mc.level.registryAccess().lookup(Registries.BIOME)
|
| 517 | .map(biomeRegistry -> {
|
| 518 | Identifier id = biomeRegistry.getKey(mc.level.getBiome(BP).value());
|
| 519 | if (id == null) return Value.string("Unknown");
|
| 520 | return Value.string(Arrays.stream(id.getPath().split("_")).map(StringUtils::capitalize).collect(Collectors.joining(" ")));
|
| 521 | })
|
| 522 | .orElse(Value.string("Unknown"));
|
| 523 | }
|
| 524 |
|
| 525 | private static Value weather() {
|
| 526 | if (mc.level == null) return Value.string("");
|
| 527 | return Value.string(mc.level.isThundering() ? "Thunder" : mc.level.isRaining() ? "Rain" : "Clear");
|
| 528 | }
|
| 529 |
|
| 530 | private static Value handOrOffhand() {
|
| 531 | if (mc.player == null) return Value.null_();
|
| 532 |
|
| 533 | ItemStack itemStack = mc.player.getMainHandItem();
|
| 534 | if (itemStack.isEmpty()) itemStack = mc.player.getOffhandItem();
|
| 535 |
|
| 536 | return itemStack != null ? wrap(itemStack) : Value.null_();
|
| 537 | }
|
| 538 |
|
| 539 | private static Value ping() {
|
| 540 | if (mc.getConnection() == null || mc.player == null) return Value.number(0);
|
| 541 |
|
| 542 | PlayerInfo playerListEntry = mc.getConnection().getPlayerInfo(mc.player.getUUID());
|
| 543 | return Value.number(playerListEntry != null ? playerListEntry.getLatency() : 0);
|
| 544 | }
|
| 545 |
|
| 546 | private static Value baritoneDistanceToGoal() {
|
| 547 | Goal goal = BaritoneAPI.getProvider().getPrimaryBaritone().getPathingBehavior().getGoal();
|
| 548 | return Value.number((goal != null && mc.player != null) ? goal.heuristic(mc.player.blockPosition()) : 0);
|
| 549 | }
|
| 550 |
|
| 551 | private static Value posString(boolean opposite, boolean camera) {
|
| 552 | Vec3 pos;
|
| 553 | if (camera) pos = mc.gameRenderer.mainCamera().position();
|
| 554 | else pos = mc.player != null ? mc.player.position() : Vec3.ZERO;
|
| 555 |
|
| 556 | double x = pos.x;
|
| 557 | double z = pos.z;
|
| 558 |
|
| 559 | if (opposite) {
|
| 560 | Dimension dimension = PlayerUtils.getDimension();
|
| 561 |
|
| 562 | if (dimension == Dimension.Overworld) {
|
| 563 | x /= 8;
|
| 564 | z /= 8;
|
| 565 | } else if (dimension == Dimension.Nether) {
|
| 566 | x *= 8;
|
| 567 | z *= 8;
|
| 568 | }
|
| 569 | }
|
| 570 |
|
| 571 | return posString(x, pos.y, z);
|
| 572 | }
|
| 573 |
|
| 574 | private static Value posString(double x, double y, double z) {
|
| 575 | return Value.string(String.format("X: %.0f Y: %.0f Z: %.0f", x, y, z));
|
| 576 | }
|
| 577 |
|
| 578 | private static Value crosshairType() {
|
| 579 | if (mc.hitResult == null) return Value.string("miss");
|
| 580 |
|
| 581 | return Value.string(switch (mc.hitResult.getType()) {
|
| 582 | case MISS -> "miss";
|
| 583 | case BLOCK -> "block";
|
| 584 | case ENTITY -> "entity";
|
| 585 | });
|
| 586 | }
|
| 587 |
|
| 588 | private static Value crosshairValue() {
|
| 589 | if (mc.level == null || mc.hitResult == null) return Value.null_();
|
| 590 |
|
| 591 | if (mc.hitResult.getType() == HitResult.Type.MISS) return Value.string("");
|
| 592 | if (mc.hitResult instanceof BlockHitResult hit)
|
| 593 | return wrap(hit.getBlockPos(), mc.level.getBlockState(hit.getBlockPos()));
|
| 594 | return wrap(((EntityHitResult) mc.hitResult).getEntity());
|
| 595 | }
|
| 596 |
|
| 597 | // Utility
|
| 598 |
|
| 599 | public static Identifier popIdentifier(Starscript ss, String errorMessage) {
|
| 600 | try {
|
| 601 | return Identifier.parse(ss.popString(errorMessage));
|
| 602 | } catch (IdentifierException e) {
|
| 603 | ss.error(e.getMessage());
|
| 604 | return null;
|
| 605 | }
|
| 606 | }
|
| 607 |
|
| 608 | // Wrapping
|
| 609 |
|
| 610 | public static Value wrap(ItemStack itemStack) {
|
| 611 | String name = itemStack.isEmpty() ? "" : Names.get(itemStack.getItem());
|
| 612 |
|
| 613 | int durability = 0;
|
| 614 | if (!itemStack.isEmpty() && itemStack.isDamageableItem())
|
| 615 | durability = itemStack.getMaxDamage() - itemStack.getDamageValue();
|
| 616 |
|
| 617 | return Value.map(new ValueMap()
|
| 618 | .set("_toString", Value.string(itemStack.getCount() <= 1 ? name : String.format("%s %dx", name, itemStack.getCount())))
|
| 619 | .set("name", Value.string(name))
|
| 620 | .set("id", Value.string(BuiltInRegistries.ITEM.getKey(itemStack.getItem()).toString()))
|
| 621 | .set("count", Value.number(itemStack.getCount()))
|
| 622 | .set("durability", Value.number(durability))
|
| 623 | .set("max_durability", Value.number(itemStack.getMaxDamage()))
|
| 624 | );
|
| 625 | }
|
| 626 |
|
| 627 | public static Value wrap(BlockPos blockPos, BlockState blockState) {
|
| 628 | return Value.map(new ValueMap()
|
| 629 | .set("_toString", Value.string(Names.get(blockState.getBlock())))
|
| 630 | .set("id", Value.string(BuiltInRegistries.BLOCK.getKey(blockState.getBlock()).toString()))
|
| 631 | .set("pos", Value.map(new ValueMap()
|
| 632 | .set("_toString", posString(blockPos.getX(), blockPos.getY(), blockPos.getZ()))
|
| 633 | .set("x", Value.number(blockPos.getX()))
|
| 634 | .set("y", Value.number(blockPos.getY()))
|
| 635 | .set("z", Value.number(blockPos.getZ()))
|
| 636 | ))
|
| 637 | );
|
| 638 | }
|
| 639 |
|
| 640 | public static Value wrap(Entity entity) {
|
| 641 | return Value.map(new ValueMap()
|
| 642 | .set("_toString", Value.string(entity.getName().getString()))
|
| 643 | .set("id", Value.string(BuiltInRegistries.ENTITY_TYPE.getKey(entity.getType()).toString()))
|
| 644 | .set("health", Value.number(entity instanceof LivingEntity e ? e.getHealth() : 0))
|
| 645 | .set("absorption", Value.number(entity instanceof LivingEntity e ? e.getAbsorptionAmount() : 0))
|
| 646 | .set("pos", Value.map(new ValueMap()
|
| 647 | .set("_toString", posString(entity.getX(), entity.getY(), entity.getZ()))
|
| 648 | .set("x", Value.number(entity.getX()))
|
| 649 | .set("y", Value.number(entity.getY()))
|
| 650 | .set("z", Value.number(entity.getZ()))
|
| 651 | ))
|
| 652 | );
|
| 653 | }
|
| 654 |
|
| 655 | public static Value wrap(HorizontalDirection dir) {
|
| 656 | return Value.map(new ValueMap()
|
| 657 | .set("_toString", Value.string(dir.name + " " + dir.axis))
|
| 658 | .set("name", Value.string(dir.name))
|
| 659 | .set("axis", Value.string(dir.axis))
|
| 660 | );
|
| 661 | }
|
| 662 |
|
| 663 | public static Value wrap(MobEffectInstance effectInstance) {
|
| 664 | return Value.map(new ValueMap()
|
| 665 | .set("duration", effectInstance.getDuration())
|
| 666 | .set("level", effectInstance.getAmplifier() + 1)
|
| 667 | );
|
| 668 | }
|
| 669 | }
|
| 670 |
|