| 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;
|
| 7 |
|
| 8 | import com.mojang.blaze3d.ProjectionType;
|
| 9 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 10 | import com.mojang.serialization.DataResult;
|
| 11 | import it.unimi.dsi.fastutil.objects.*;
|
| 12 | import meteordevelopment.meteorclient.MeteorClient;
|
| 13 | import meteordevelopment.meteorclient.events.world.TickEvent;
|
| 14 | import meteordevelopment.meteorclient.gui.tabs.TabScreen;
|
| 15 | import meteordevelopment.meteorclient.mixin.*;
|
| 16 | import meteordevelopment.meteorclient.mixininterface.IMinecraft;
|
| 17 | import meteordevelopment.meteorclient.settings.StatusEffectAmplifierMapSetting;
|
| 18 | import meteordevelopment.meteorclient.systems.modules.Modules;
|
| 19 | import meteordevelopment.meteorclient.systems.modules.render.BetterTooltips;
|
| 20 | import meteordevelopment.meteorclient.systems.modules.world.Timer;
|
| 21 | import meteordevelopment.meteorclient.utils.misc.Names;
|
| 22 | import meteordevelopment.meteorclient.utils.player.EChestMemory;
|
| 23 | import meteordevelopment.meteorclient.utils.render.PeekScreen;
|
| 24 | import meteordevelopment.meteorclient.utils.render.RenderUtils;
|
| 25 | import meteordevelopment.meteorclient.utils.render.color.Color;
|
| 26 | import meteordevelopment.meteorclient.utils.world.BlockEntityIterator;
|
| 27 | import meteordevelopment.meteorclient.utils.world.ChunkIterator;
|
| 28 | import meteordevelopment.orbit.EventHandler;
|
| 29 | import net.minecraft.client.ResourceLoadStateTracker;
|
| 30 | import net.minecraft.client.gui.screens.Screen;
|
| 31 | import net.minecraft.client.gui.screens.TitleScreen;
|
| 32 | import net.minecraft.client.gui.screens.multiplayer.JoinMultiplayerScreen;
|
| 33 | import net.minecraft.client.gui.screens.worldselection.SelectWorldScreen;
|
| 34 | import net.minecraft.client.renderer.Projection;
|
| 35 | import net.minecraft.client.renderer.ProjectionMatrixBuffer;
|
| 36 | import net.minecraft.core.BlockPos;
|
| 37 | import net.minecraft.core.Holder;
|
| 38 | import net.minecraft.core.component.DataComponentMap;
|
| 39 | import net.minecraft.core.component.DataComponents;
|
| 40 | import net.minecraft.nbt.CompoundTag;
|
| 41 | import net.minecraft.nbt.ListTag;
|
| 42 | import net.minecraft.nbt.NbtOps;
|
| 43 | import net.minecraft.resources.ResourceKey;
|
| 44 | import net.minecraft.util.Mth;
|
| 45 | import net.minecraft.world.ItemStackWithSlot;
|
| 46 | import net.minecraft.world.effect.MobEffect;
|
| 47 | import net.minecraft.world.entity.Entity;
|
| 48 | import net.minecraft.world.item.*;
|
| 49 | import net.minecraft.world.item.component.TypedEntityData;
|
| 50 | import net.minecraft.world.item.enchantment.Enchantment;
|
| 51 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 52 | import net.minecraft.world.item.enchantment.ItemEnchantments;
|
| 53 | import net.minecraft.world.level.block.Block;
|
| 54 | import net.minecraft.world.level.block.Blocks;
|
| 55 | import net.minecraft.world.level.block.ColorCollection;
|
| 56 | import net.minecraft.world.level.block.ShulkerBoxBlock;
|
| 57 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 58 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 59 | import net.minecraft.world.level.chunk.ChunkAccess;
|
| 60 | import net.minecraft.world.phys.Vec3;
|
| 61 | import org.apache.commons.io.IOUtils;
|
| 62 | import org.apache.commons.lang3.StringUtils;
|
| 63 | import org.jetbrains.annotations.Range;
|
| 64 | import org.joml.Matrix4f;
|
| 65 | import org.joml.Vector3d;
|
| 66 |
|
| 67 | import java.io.File;
|
| 68 | import java.io.IOException;
|
| 69 | import java.io.InputStream;
|
| 70 | import java.net.InetSocketAddress;
|
| 71 | import java.util.*;
|
| 72 | import java.util.regex.Pattern;
|
| 73 | import java.util.stream.Collectors;
|
| 74 |
|
| 75 | import static meteordevelopment.meteorclient.MeteorClient.mc;
|
| 76 |
|
| 77 | public class Utils {
|
| 78 | public static final Pattern FILE_NAME_INVALID_CHARS_PATTERN = Pattern.compile("[\\s\\\\/:*?\"<>|]");
|
| 79 | public static final Color WHITE = new Color(255, 255, 255);
|
| 80 |
|
| 81 | private static final Random random = new Random();
|
| 82 | public static boolean isReleasingTrident;
|
| 83 | public static boolean rendering3D = true;
|
| 84 | public static double frameTime;
|
| 85 | public static Screen screenToOpen;
|
| 86 |
|
| 87 | private static final ProjectionMatrixBuffer matrixBuffer = new ProjectionMatrixBuffer("meteor-projection-matrix");
|
| 88 |
|
| 89 | private Utils() {
|
| 90 | }
|
| 91 |
|
| 92 | @PreInit
|
| 93 | public static void init() {
|
| 94 | MeteorClient.EVENT_BUS.subscribe(Utils.class);
|
| 95 | }
|
| 96 |
|
| 97 | @EventHandler
|
| 98 | private static void onTick(TickEvent.Post event) {
|
| 99 | if (screenToOpen != null && mc.gui.screen() == null) {
|
| 100 | mc.gui.setScreen(screenToOpen);
|
| 101 | screenToOpen = null;
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | public static Vec3 getPlayerSpeed() {
|
| 106 | if (mc.player == null) return Vec3.ZERO;
|
| 107 |
|
| 108 | double tX = mc.player.getX() - mc.player.xo;
|
| 109 | double tY = mc.player.getY() - mc.player.yo;
|
| 110 | double tZ = mc.player.getZ() - mc.player.zo;
|
| 111 |
|
| 112 | Timer timer = Modules.get().get(Timer.class);
|
| 113 | if (timer.isActive()) {
|
| 114 | tX *= timer.getMultiplier();
|
| 115 | tY *= timer.getMultiplier();
|
| 116 | tZ *= timer.getMultiplier();
|
| 117 | }
|
| 118 |
|
| 119 | tX *= 20;
|
| 120 | tY *= 20;
|
| 121 | tZ *= 20;
|
| 122 |
|
| 123 | return new Vec3(tX, tY, tZ);
|
| 124 | }
|
| 125 |
|
| 126 | public static String getWorldTime() {
|
| 127 | if (mc.level == null) return "00:00";
|
| 128 |
|
| 129 | int ticks = (int) (mc.level.getGameTime() % 24000);
|
| 130 | ticks += 6000;
|
| 131 | if (ticks > 24000) ticks -= 24000;
|
| 132 |
|
| 133 | return String.format("%02d:%02d", ticks / 1000, (int) (ticks % 1000 / 1000.0 * 60));
|
| 134 | }
|
| 135 |
|
| 136 | public static Iterable<ChunkAccess> chunks(boolean onlyWithLoadedNeighbours) {
|
| 137 | return () -> new ChunkIterator(onlyWithLoadedNeighbours);
|
| 138 | }
|
| 139 |
|
| 140 | public static Iterable<ChunkAccess> chunks() {
|
| 141 | return chunks(false);
|
| 142 | }
|
| 143 |
|
| 144 | public static Iterable<BlockEntity> blockEntities() {
|
| 145 | return BlockEntityIterator::new;
|
| 146 | }
|
| 147 |
|
| 148 | public static void getEnchantments(ItemStack itemStack, Object2IntMap<Holder<Enchantment>> enchantments) {
|
| 149 | enchantments.clear();
|
| 150 |
|
| 151 | if (!itemStack.isEmpty()) {
|
| 152 | Set<Object2IntMap.Entry<Holder<Enchantment>>> itemEnchantments = itemStack.getItem() == Items.ENCHANTED_BOOK
|
| 153 | ? itemStack.getOrDefault(DataComponents.STORED_ENCHANTMENTS, ItemEnchantments.EMPTY).entrySet()
|
| 154 | : itemStack.getEnchantments().entrySet();
|
| 155 |
|
| 156 | for (Object2IntMap.Entry<Holder<Enchantment>> entry : itemEnchantments) {
|
| 157 | enchantments.put(entry.getKey(), entry.getIntValue());
|
| 158 | }
|
| 159 | }
|
| 160 | }
|
| 161 |
|
| 162 | public static int getEnchantmentLevel(ItemStack itemStack, ResourceKey<Enchantment> enchantment) {
|
| 163 | if (itemStack.isEmpty()) return 0;
|
| 164 | Object2IntMap<Holder<Enchantment>> itemEnchantments = new Object2IntArrayMap<>();
|
| 165 | getEnchantments(itemStack, itemEnchantments);
|
| 166 | return getEnchantmentLevel(itemEnchantments, enchantment);
|
| 167 | }
|
| 168 |
|
| 169 | public static int getEnchantmentLevel(Object2IntMap<Holder<Enchantment>> itemEnchantments, ResourceKey<Enchantment> enchantment) {
|
| 170 | for (Object2IntMap.Entry<Holder<Enchantment>> entry : Object2IntMaps.fastIterable(itemEnchantments)) {
|
| 171 | if (entry.getKey().is(enchantment)) return entry.getIntValue();
|
| 172 | }
|
| 173 | return 0;
|
| 174 | }
|
| 175 |
|
| 176 | @SafeVarargs
|
| 177 | public static boolean hasEnchantments(ItemStack itemStack, ResourceKey<Enchantment>... enchantments) {
|
| 178 | if (itemStack.isEmpty()) return false;
|
| 179 | Object2IntMap<Holder<Enchantment>> itemEnchantments = new Object2IntArrayMap<>();
|
| 180 | getEnchantments(itemStack, itemEnchantments);
|
| 181 |
|
| 182 | for (ResourceKey<Enchantment> enchantment : enchantments) {
|
| 183 | if (!hasEnchantment(itemEnchantments, enchantment)) return false;
|
| 184 | }
|
| 185 | return true;
|
| 186 | }
|
| 187 |
|
| 188 | public static boolean hasEnchantment(ItemStack itemStack, ResourceKey<Enchantment> enchantmentKey) {
|
| 189 | if (itemStack.isEmpty()) return false;
|
| 190 | Object2IntMap<Holder<Enchantment>> itemEnchantments = new Object2IntArrayMap<>();
|
| 191 | getEnchantments(itemStack, itemEnchantments);
|
| 192 | return hasEnchantment(itemEnchantments, enchantmentKey);
|
| 193 | }
|
| 194 |
|
| 195 | private static boolean hasEnchantment(Object2IntMap<Holder<Enchantment>> itemEnchantments, ResourceKey<Enchantment> enchantmentKey) {
|
| 196 | for (Holder<Enchantment> enchantment : itemEnchantments.keySet()) {
|
| 197 | if (enchantment.is(enchantmentKey)) return true;
|
| 198 | }
|
| 199 | return false;
|
| 200 | }
|
| 201 |
|
| 202 | public static boolean isFood(ItemStack stack) {
|
| 203 | return isFood(stack.getItem());
|
| 204 | }
|
| 205 |
|
| 206 | // Not every food item in minecraft is consumable for some reason (e.g. buckets of any fish)
|
| 207 | public static boolean isFood(Item item) {
|
| 208 | return item.components().has(DataComponents.FOOD) && item.components().has(DataComponents.CONSUMABLE);
|
| 209 | }
|
| 210 |
|
| 211 | public static int getRenderDistance() {
|
| 212 | return Math.max(mc.options.renderDistance().get(), ((ClientPacketListenerAccessor) mc.getConnection()).meteor$getServerChunkRadius());
|
| 213 | }
|
| 214 |
|
| 215 | public static int getWindowWidth() {
|
| 216 | return mc.getWindow().getWidth();
|
| 217 | }
|
| 218 |
|
| 219 | public static int getWindowHeight() {
|
| 220 | return mc.getWindow().getHeight();
|
| 221 | }
|
| 222 |
|
| 223 | public static void unscaledProjection() {
|
| 224 | float width = mc.getWindow().getWidth();
|
| 225 | float height = mc.getWindow().getHeight();
|
| 226 |
|
| 227 | var proj = new Projection();
|
| 228 | proj.setupOrtho(-10, 100, width, height, true);
|
| 229 | var matrix = proj.getMatrix(new Matrix4f());
|
| 230 |
|
| 231 | RenderSystem.setProjectionMatrix(matrixBuffer.getBuffer(matrix), ProjectionType.ORTHOGRAPHIC);
|
| 232 | RenderUtils.projection.set(matrix);
|
| 233 |
|
| 234 | rendering3D = false;
|
| 235 | }
|
| 236 |
|
| 237 | public static void scaledProjection() {
|
| 238 | float width = (float) (mc.getWindow().getWidth() / mc.getWindow().getGuiScale());
|
| 239 | float height = (float) (mc.getWindow().getHeight() / mc.getWindow().getGuiScale());
|
| 240 |
|
| 241 | var proj = new Projection();
|
| 242 | proj.setupOrtho(-10, 100, width, height, true);
|
| 243 | var matrix = proj.getMatrix(new Matrix4f());
|
| 244 |
|
| 245 | RenderSystem.setProjectionMatrix(matrixBuffer.getBuffer(matrix), ProjectionType.PERSPECTIVE);
|
| 246 | RenderUtils.projection.set(matrix);
|
| 247 |
|
| 248 | rendering3D = true;
|
| 249 | }
|
| 250 |
|
| 251 | public static Vec3 vec3(BlockPos pos) {
|
| 252 | return new Vec3(pos.getX(), pos.getY(), pos.getZ());
|
| 253 | }
|
| 254 |
|
| 255 | public static boolean openContainer(ItemStack itemStack, ItemStack[] contents, boolean pause) {
|
| 256 | if (hasItems(itemStack) || itemStack.getItem() == Items.ENDER_CHEST) {
|
| 257 | Utils.getItemsInContainerItem(itemStack, contents);
|
| 258 | if (pause) screenToOpen = new PeekScreen(itemStack, contents);
|
| 259 | else mc.gui.setScreen(new PeekScreen(itemStack, contents));
|
| 260 | return true;
|
| 261 | }
|
| 262 |
|
| 263 | return false;
|
| 264 | }
|
| 265 |
|
| 266 | public static void getItemsInContainerItem(ItemStack itemStack, ItemStack[] items) {
|
| 267 | if (itemStack.getItem() == Items.ENDER_CHEST) {
|
| 268 | for (int i = 0; i < EChestMemory.ITEMS.size(); i++) {
|
| 269 | items[i] = EChestMemory.ITEMS.get(i);
|
| 270 | }
|
| 271 |
|
| 272 | return;
|
| 273 | }
|
| 274 |
|
| 275 | Arrays.fill(items, ItemStack.EMPTY);
|
| 276 | DataComponentMap components = itemStack.getComponents();
|
| 277 |
|
| 278 | if (components.has(DataComponents.CONTAINER)) {
|
| 279 | var stacks = components.get(DataComponents.CONTAINER).allItemsCopyStream().toList();
|
| 280 |
|
| 281 | for (int i = 0; i < stacks.size(); i++) {
|
| 282 | if (i >= 0 && i < items.length) items[i] = stacks.get(i);
|
| 283 | }
|
| 284 | } else if (components.has(DataComponents.BLOCK_ENTITY_DATA)) {
|
| 285 | TypedEntityData<BlockEntityType<?>> blockEntityData = components.get(DataComponents.BLOCK_ENTITY_DATA);
|
| 286 | if (blockEntityData == null) return;
|
| 287 | ListTag nbt3 = blockEntityData.copyTagWithoutId().getListOrEmpty("Items");
|
| 288 |
|
| 289 | for (int i = 0; i < nbt3.size(); i++) {
|
| 290 | Optional<CompoundTag> compound = nbt3.getCompound(i);
|
| 291 | if (compound.isEmpty()) continue;
|
| 292 |
|
| 293 | Optional<Byte> slot = compound.get().getByte("Slot"); // Apparently shulker boxes can store more than 27 items, good job Mojang
|
| 294 | if (slot.isEmpty()) continue;
|
| 295 |
|
| 296 | // now NPEs when mc.world == null
|
| 297 | if (slot.get() >= 0 && slot.get() < items.length) {
|
| 298 | switch (ItemStackWithSlot.CODEC.parse(mc.player.registryAccess().createSerializationContext(NbtOps.INSTANCE), compound.get())) {
|
| 299 | case DataResult.Success<ItemStackWithSlot> success ->
|
| 300 | items[slot.get()] = success.value().stack();
|
| 301 | case DataResult.Error<ItemStackWithSlot> _ -> items[slot.get()] = ItemStack.EMPTY;
|
| 302 | default -> throw new MatchException(null, null);
|
| 303 | }
|
| 304 | }
|
| 305 | }
|
| 306 | }
|
| 307 | }
|
| 308 |
|
| 309 | public static Color getShulkerColor(ItemStack shulkerItem) {
|
| 310 | if (shulkerItem.getItem() instanceof BlockItem blockItem) {
|
| 311 | Block block = blockItem.getBlock();
|
| 312 | if (block == Blocks.ENDER_CHEST) return BetterTooltips.ECHEST_COLOR;
|
| 313 |
|
| 314 | if (block instanceof ShulkerBoxBlock shulkerBlock) {
|
| 315 | DyeColor dye = shulkerBlock.getColor();
|
| 316 | if (dye == null) return WHITE;
|
| 317 |
|
| 318 | final int color = dye.getTextureDiffuseColor();
|
| 319 | return new Color((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, 255);
|
| 320 | }
|
| 321 | }
|
| 322 |
|
| 323 | return WHITE;
|
| 324 | }
|
| 325 |
|
| 326 | public static boolean hasItems(ItemStack itemStack) {
|
| 327 | var container = itemStack.get(DataComponents.CONTAINER);
|
| 328 |
|
| 329 | if (container != null) {
|
| 330 | var items = container.nonEmptyItems().iterator();
|
| 331 | if (items.hasNext()) return true;
|
| 332 | }
|
| 333 |
|
| 334 | TypedEntityData<BlockEntityType<?>> blockEntityData = itemStack.get(DataComponents.BLOCK_ENTITY_DATA);
|
| 335 | return blockEntityData != null && blockEntityData.contains("Items");
|
| 336 | }
|
| 337 |
|
| 338 | public static Reference2IntMap<MobEffect> createStatusEffectMap() {
|
| 339 | return new Reference2IntArrayMap<>(StatusEffectAmplifierMapSetting.EMPTY_STATUS_EFFECT_MAP);
|
| 340 | }
|
| 341 |
|
| 342 | public static String getEnchantSimpleName(Holder<Enchantment> enchantment, int length) {
|
| 343 | String name = Names.get(enchantment);
|
| 344 | return name.length() > length ? name.substring(0, length) : name;
|
| 345 | }
|
| 346 |
|
| 347 | public static boolean searchTextDefault(String text, String filter, boolean caseSensitive) {
|
| 348 | return searchInWords(text, filter) > 0 || searchLevenshteinDefault(text, filter, caseSensitive) < text.length() / 2;
|
| 349 | }
|
| 350 |
|
| 351 | public static int searchLevenshteinDefault(String text, String filter, boolean caseSensitive) {
|
| 352 | return levenshteinDistance(caseSensitive ? filter : filter.toLowerCase(Locale.ROOT), caseSensitive ? text : text.toLowerCase(Locale.ROOT), 1, 8, 8);
|
| 353 | }
|
| 354 |
|
| 355 | public static int searchInWords(String text, String filter) {
|
| 356 | if (filter.isEmpty()) return 1;
|
| 357 |
|
| 358 | int wordsFound = 0;
|
| 359 | text = text.toLowerCase(Locale.ROOT);
|
| 360 | String[] words = filter.toLowerCase(Locale.ROOT).split(" ");
|
| 361 |
|
| 362 | for (String word : words) {
|
| 363 | if (!text.contains(word)) return 0;
|
| 364 | wordsFound += StringUtils.countMatches(text, word);
|
| 365 | }
|
| 366 |
|
| 367 | return wordsFound;
|
| 368 | }
|
| 369 |
|
| 370 | public static int levenshteinDistance(String from, String to, int insCost, int subCost, int delCost) {
|
| 371 | int textLength = from.length();
|
| 372 | int filterLength = to.length();
|
| 373 |
|
| 374 | if (textLength == 0) return filterLength * insCost;
|
| 375 | if (filterLength == 0) return textLength * delCost;
|
| 376 |
|
| 377 | // Populate matrix
|
| 378 | int[][] d = new int[textLength + 1][filterLength + 1];
|
| 379 |
|
| 380 | for (int i = 0; i <= textLength; i++) {
|
| 381 | d[i][0] = i * delCost;
|
| 382 | }
|
| 383 |
|
| 384 | for (int j = 0; j <= filterLength; j++) {
|
| 385 | d[0][j] = j * insCost;
|
| 386 | }
|
| 387 |
|
| 388 | // Find best route
|
| 389 | for (int i = 1; i <= textLength; i++) {
|
| 390 | for (int j = 1; j <= filterLength; j++) {
|
| 391 | int sCost = d[i - 1][j - 1] + (from.charAt(i - 1) == to.charAt(j - 1) ? 0 : subCost);
|
| 392 | int dCost = d[i - 1][j] + delCost;
|
| 393 | int iCost = d[i][j - 1] + insCost;
|
| 394 | d[i][j] = Math.min(Math.min(dCost, iCost), sCost);
|
| 395 | }
|
| 396 | }
|
| 397 |
|
| 398 | return d[textLength][filterLength];
|
| 399 | }
|
| 400 |
|
| 401 | public static double squaredDistance(double x1, double y1, double z1, double x2, double y2, double z2) {
|
| 402 | double dX = x2 - x1;
|
| 403 | double dY = y2 - y1;
|
| 404 | double dZ = z2 - z1;
|
| 405 | return dX * dX + dY * dY + dZ * dZ;
|
| 406 | }
|
| 407 |
|
| 408 | public static double distance(double x1, double y1, double z1, double x2, double y2, double z2) {
|
| 409 | double dX = x2 - x1;
|
| 410 | double dY = y2 - y1;
|
| 411 | double dZ = z2 - z1;
|
| 412 | return Math.sqrt(dX * dX + dY * dY + dZ * dZ);
|
| 413 | }
|
| 414 |
|
| 415 | public static String getFileWorldName() {
|
| 416 | return FILE_NAME_INVALID_CHARS_PATTERN.matcher(getWorldName()).replaceAll("_");
|
| 417 | }
|
| 418 |
|
| 419 | /**
|
| 420 | * Use {@link Utils#getFileWorldName()} if you are using the world name as a file/directory name
|
| 421 | */
|
| 422 | public static String getWorldName() {
|
| 423 | // Singleplayer
|
| 424 | if (mc.isLocalServer()) {
|
| 425 | if (mc.level == null) return "";
|
| 426 | if (mc.getSingleplayerServer() == null) return "FAILED_BECAUSE_LEFT_WORLD";
|
| 427 |
|
| 428 | File folder = ((MinecraftServerAccessor) mc.getSingleplayerServer()).meteor$getStorageSource().getDimensionPath(mc.level.dimension()).toFile();
|
| 429 | if (folder.toPath().relativize(mc.gameDirectory.toPath()).getNameCount() != 2) {
|
| 430 | folder = folder.getParentFile();
|
| 431 | }
|
| 432 | return folder.getName();
|
| 433 | }
|
| 434 |
|
| 435 | // Multiplayer
|
| 436 | if (mc.getCurrentServer() != null) {
|
| 437 | return mc.getCurrentServer().isRealm() ? "realms" : mc.getCurrentServer().ip;
|
| 438 | }
|
| 439 |
|
| 440 | return "";
|
| 441 | }
|
| 442 |
|
| 443 | public static String nameToTitle(String name) {
|
| 444 | return Arrays.stream(name.split("-")).map(StringUtils::capitalize).collect(Collectors.joining(" "));
|
| 445 | }
|
| 446 |
|
| 447 | public static String titleToName(String title) {
|
| 448 | return title.replace(" ", "-").toLowerCase(Locale.ROOT);
|
| 449 | }
|
| 450 |
|
| 451 | public static byte[] readBytes(InputStream in) {
|
| 452 | try {
|
| 453 | return in.readAllBytes();
|
| 454 | } catch (IOException e) {
|
| 455 | MeteorClient.LOG.error("Error reading from stream.", e);
|
| 456 | return new byte[0];
|
| 457 | } finally {
|
| 458 | IOUtils.closeQuietly(in);
|
| 459 | }
|
| 460 | }
|
| 461 |
|
| 462 | public static boolean canUpdate() {
|
| 463 | return mc != null && mc.level != null && mc.player != null;
|
| 464 | }
|
| 465 |
|
| 466 | public static boolean canOpenGui() {
|
| 467 | if (canUpdate()) return mc.gui.screen() == null;
|
| 468 | return mc.gui.screen() instanceof TitleScreen
|
| 469 | || mc.gui.screen() instanceof JoinMultiplayerScreen
|
| 470 | || mc.gui.screen() instanceof SelectWorldScreen;
|
| 471 | }
|
| 472 |
|
| 473 | public static boolean canCloseGui() {
|
| 474 | return mc.gui.screen() instanceof TabScreen;
|
| 475 | }
|
| 476 |
|
| 477 | public static int random(int min, int max) {
|
| 478 | return random.nextInt(max - min) + min;
|
| 479 | }
|
| 480 |
|
| 481 | public static double random(double min, double max) {
|
| 482 | return min + (max - min) * random.nextDouble();
|
| 483 | }
|
| 484 |
|
| 485 | public static void leftClick() {
|
| 486 | // check if a screen is open
|
| 487 | // see net.minecraft.client.Mouse.lockCursor
|
| 488 | // see net.minecraft.client.MinecraftClient.tick
|
| 489 | int attackCooldown = ((MinecraftAccessor) mc).meteor$getMissTime();
|
| 490 | if (attackCooldown == 10000) {
|
| 491 | ((MinecraftAccessor) mc).meteor$setMissTime(0);
|
| 492 | }
|
| 493 |
|
| 494 | mc.options.keyAttack.setDown(true);
|
| 495 | ((MinecraftAccessor) mc).meteor$leftClick();
|
| 496 | mc.options.keyAttack.setDown(false);
|
| 497 | }
|
| 498 |
|
| 499 | public static void rightClick() {
|
| 500 | ((IMinecraft) mc).meteor$rightClick();
|
| 501 | }
|
| 502 |
|
| 503 | public static boolean isShulker(Item item) {
|
| 504 | return item == Items.SHULKER_BOX || contains(Items.DYED_SHULKER_BOX, item);
|
| 505 | }
|
| 506 |
|
| 507 | public static <T> boolean contains(ColorCollection<T> collection, T value) {
|
| 508 | return collection.white() == value
|
| 509 | || collection.orange() == value
|
| 510 | || collection.magenta() == value
|
| 511 | || collection.lightBlue() == value
|
| 512 | || collection.yellow() == value
|
| 513 | || collection.lime() == value
|
| 514 | || collection.pink() == value
|
| 515 | || collection.gray() == value
|
| 516 | || collection.lightGray() == value
|
| 517 | || collection.cyan() == value
|
| 518 | || collection.purple() == value
|
| 519 | || collection.blue() == value
|
| 520 | || collection.brown() == value
|
| 521 | || collection.green() == value
|
| 522 | || collection.red() == value
|
| 523 | || collection.black() == value;
|
| 524 | }
|
| 525 |
|
| 526 | public static boolean isThrowable(Item item) {
|
| 527 | return item instanceof ExperienceBottleItem || item instanceof BowItem || item instanceof CrossbowItem || item instanceof SnowballItem || item instanceof EggItem || item instanceof EnderpearlItem || item instanceof SplashPotionItem || item instanceof LingeringPotionItem || item instanceof FishingRodItem || item instanceof TridentItem;
|
| 528 | }
|
| 529 |
|
| 530 | public static void addEnchantment(ItemStack itemStack, Holder<Enchantment> enchantment, int level) {
|
| 531 | ItemEnchantments.Mutable b = new ItemEnchantments.Mutable(EnchantmentHelper.getEnchantmentsForCrafting(itemStack));
|
| 532 | b.set(enchantment, level);
|
| 533 |
|
| 534 | EnchantmentHelper.setEnchantments(itemStack, b.toImmutable());
|
| 535 | }
|
| 536 |
|
| 537 | public static void clearEnchantments(ItemStack itemStack) {
|
| 538 | EnchantmentHelper.updateEnchantments(itemStack, components -> components.removeIf(a -> true));
|
| 539 | }
|
| 540 |
|
| 541 | public static void removeEnchantment(ItemStack itemStack, Enchantment enchantment) {
|
| 542 | EnchantmentHelper.updateEnchantments(itemStack, components -> components.removeIf(enchantment1 -> enchantment1.value().equals(enchantment)));
|
| 543 | }
|
| 544 |
|
| 545 | public static Color lerp(Color first, Color second, @Range(from = 0, to = 1) float v) {
|
| 546 | return new Color(
|
| 547 | (int) (first.r * (1 - v) + second.r * v),
|
| 548 | (int) (first.g * (1 - v) + second.g * v),
|
| 549 | (int) (first.b * (1 - v) + second.b * v)
|
| 550 | );
|
| 551 | }
|
| 552 |
|
| 553 | public static boolean isLoading() {
|
| 554 | ResourceLoadStateTracker.ReloadState state = ((ResourceLoadStateTrackerAccessor) ((MinecraftAccessor) mc).meteor$getReloadStateTracker()).meteor$getReloadState();
|
| 555 | return state == null || !((ReloadStateAccessor) state).meteor$isFinished();
|
| 556 | }
|
| 557 |
|
| 558 | public static int parsePort(String full) {
|
| 559 | if (full == null || full.isBlank() || !full.contains(":")) return -1;
|
| 560 |
|
| 561 | int port;
|
| 562 |
|
| 563 | try {
|
| 564 | port = Integer.parseInt(full.substring(full.lastIndexOf(':') + 1, full.length() - 1));
|
| 565 | } catch (NumberFormatException _) {
|
| 566 | port = -1;
|
| 567 | }
|
| 568 |
|
| 569 | return port;
|
| 570 | }
|
| 571 |
|
| 572 | public static String parseAddress(String full) {
|
| 573 | if (full == null || full.isBlank() || !full.contains(":")) return full;
|
| 574 | return full.substring(0, full.lastIndexOf(':'));
|
| 575 | }
|
| 576 |
|
| 577 | public static boolean resolveAddress(String address) {
|
| 578 | if (address == null || address.isBlank()) return false;
|
| 579 |
|
| 580 | int port = parsePort(address);
|
| 581 | if (port == -1) port = 25565;
|
| 582 | else address = parseAddress(address);
|
| 583 |
|
| 584 | return resolveAddress(address, port);
|
| 585 | }
|
| 586 |
|
| 587 | public static boolean resolveAddress(String address, int port) {
|
| 588 | if (port <= 0 || port > 65535 || address == null || address.isBlank()) return false;
|
| 589 | InetSocketAddress socketAddress = new InetSocketAddress(address, port);
|
| 590 | return !socketAddress.isUnresolved();
|
| 591 | }
|
| 592 |
|
| 593 | public static Vector3d set(Vector3d vec, Vec3 v) {
|
| 594 | vec.x = v.x;
|
| 595 | vec.y = v.y;
|
| 596 | vec.z = v.z;
|
| 597 |
|
| 598 | return vec;
|
| 599 | }
|
| 600 |
|
| 601 | public static Vector3d set(Vector3d vec, Entity entity, double tickDelta) {
|
| 602 | vec.x = Mth.lerp(tickDelta, entity.xOld, entity.getX());
|
| 603 | vec.y = Mth.lerp(tickDelta, entity.yOld, entity.getY());
|
| 604 | vec.z = Mth.lerp(tickDelta, entity.zOld, entity.getZ());
|
| 605 |
|
| 606 | return vec;
|
| 607 | }
|
| 608 |
|
| 609 | // Filters
|
| 610 |
|
| 611 | public static boolean nameFilter(String text, char character) {
|
| 612 | return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || (character >= '0' && character <= '9') || character == '_' || character == '-' || character == '.' || character == ' ';
|
| 613 | }
|
| 614 |
|
| 615 | public static boolean fileNameFilter(String text, char character) {
|
| 616 | return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || (character >= '0' && character <= '9') || character == '_' || character == '-' || character == ' ';
|
| 617 | }
|
| 618 |
|
| 619 | public static boolean ipFilter(String text, char character) {
|
| 620 | if (text.contains(":") && character == ':') return false;
|
| 621 | return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || (character >= '0' && character <= '9') || character == '.' || character == '-' || character == ':';
|
| 622 | }
|
| 623 | }
|
| 624 |
|