diff options
Diffstat (limited to 'src/main/java/me/xmrvizzy/skyblocker/skyblock')
4 files changed, 222 insertions, 34 deletions
diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/dungeon/DungeonBlaze.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/dungeon/DungeonBlaze.java index 219f4258..d0dcf1e1 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/dungeon/DungeonBlaze.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/dungeon/DungeonBlaze.java @@ -1,79 +1,111 @@ package me.xmrvizzy.skyblocker.skyblock.dungeon; +import it.unimi.dsi.fastutil.objects.ObjectIntPair; import me.xmrvizzy.skyblocker.config.SkyblockerConfig; +import me.xmrvizzy.skyblocker.utils.RenderHelper; +import me.xmrvizzy.skyblocker.utils.RenderUtils; import me.xmrvizzy.skyblocker.utils.Utils; import me.xmrvizzy.skyblocker.utils.color.QuadColor; -import me.xmrvizzy.skyblocker.utils.RenderUtils; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents; import net.minecraft.client.MinecraftClient; import net.minecraft.client.world.ClientWorld; import net.minecraft.entity.Entity; import net.minecraft.util.math.Box; +import net.minecraft.util.math.Vec3d; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + public class DungeonBlaze { private static final Logger LOGGER = LoggerFactory.getLogger(DungeonBlaze.class.getName()); + private static final float[] WHITE_COLOR_COMPONENTS = { 1.0f, 1.0f, 1.0f }; static Entity highestBlaze = null; static Entity lowestBlaze = null; + static Entity nextHighestBlaze = null; + static Entity nextLowestBlaze = null; static boolean renderHooked = false; public static void update() { ClientWorld world = MinecraftClient.getInstance().world; if (world == null || !Utils.isInDungeons()) return; - if(!renderHooked){ + if (!renderHooked){ - WorldRenderEvents.END.register(DungeonBlaze::blazeRenderer); + WorldRenderEvents.BEFORE_DEBUG_RENDER.register(DungeonBlaze::blazeRenderer); renderHooked = true; } Iterable<Entity> entities = world.getEntities(); - int highestHealth = 0; - int lowestHealth = 99999999; + List<ObjectIntPair<Entity>> blazes = new ArrayList<>(); for (Entity entity : entities) { - if (entity.getName().getString().contains("Blaze") && entity.getName().getString().contains("/")) { - - String blazeName = entity.getName().getString(); + String blazeName = entity.getName().getString(); + + if (blazeName.contains("Blaze") && blazeName.contains("/")) { try { - int health = Integer.parseInt(blazeName.substring(blazeName.indexOf("/") + 1, blazeName.length() - 1)); - - if (health > highestHealth) { - highestHealth = health; - - highestBlaze = entity; - - } - if (health < lowestHealth) { - lowestHealth = health; - lowestBlaze = entity; - } + + blazes.add(ObjectIntPair.of(entity, health)); } catch (NumberFormatException ex) { ex.printStackTrace(); } } } + + // Order the blazes in the list from the lowest health to the highest health + blazes.sort(Comparator.comparingInt(ObjectIntPair::rightInt)); + + // Ensure that there are blazes in the list + if (!blazes.isEmpty()) { + lowestBlaze = blazes.get(0).left(); + + int highestIndex = blazes.size() - 1; + highestBlaze = blazes.get(highestIndex).left(); + + // If there's more than 1 blaze + if (blazes.size() > 1) { + nextLowestBlaze = blazes.get(1).left(); + nextHighestBlaze = blazes.get(highestIndex - 1).left(); + } + } + } public static void blazeRenderer(WorldRenderContext wrc) { QuadColor outlineColorRed = QuadColor.single( 0.0F, 1.0F, 0.0F, 1f); QuadColor outlineColorGreen = QuadColor.single(1.0F, 0.0F, 0.0F, 1f); + QuadColor outlineColorWhite = QuadColor.single(1.0f, 1.0f, 1.0f, 1.0f); + try { - if(highestBlaze != null && lowestBlaze != null && highestBlaze.isAlive() && lowestBlaze.isAlive() && SkyblockerConfig.get().locations.dungeons.blazesolver){ + if (highestBlaze != null && lowestBlaze != null && highestBlaze.isAlive() && lowestBlaze.isAlive() && SkyblockerConfig.get().locations.dungeons.blazesolver){ /* Outline */ - if(highestBlaze.getY() <69) { + if (highestBlaze.getY() < 69) { Box blaze = highestBlaze.getBoundingBox().expand(0.3, 0.9, 0.3).offset(0, -1.1, 0); RenderUtils.drawBoxOutline(blaze, outlineColorRed, 5f); + + if (nextHighestBlaze != null && nextHighestBlaze.isAlive() && nextHighestBlaze != highestBlaze) { + Box nextBlaze = nextHighestBlaze.getBoundingBox().expand(0.3, 0.9, 0.3).offset(0, -1.1, 0); + RenderUtils.drawBoxOutline(nextBlaze, outlineColorWhite, 5f); + RenderHelper.renderLinesFromPoints(wrc, new Vec3d[] { blaze.getCenter(), nextBlaze.getCenter() }, WHITE_COLOR_COMPONENTS, 1f, 5f); + } } /* Outline */ - if(lowestBlaze.getY() >69) { + if (lowestBlaze.getY() > 69) { Box blaze = lowestBlaze.getBoundingBox().expand(0.3, 0.9, 0.3).offset(0, -1.1, 0); RenderUtils.drawBoxOutline(blaze, outlineColorRed, 5f); + + if (nextLowestBlaze != null && nextLowestBlaze.isAlive() && nextLowestBlaze != lowestBlaze) { + Box nextBlaze = nextLowestBlaze.getBoundingBox().expand(0.3, 0.9, 0.3).offset(0, -1.1, 0); + RenderUtils.drawBoxOutline(nextBlaze, outlineColorWhite, 5f); + RenderHelper.renderLinesFromPoints(wrc, new Vec3d[] { blaze.getCenter(), nextBlaze.getCenter() }, WHITE_COLOR_COMPONENTS, 1f, 5f); + } } } - }catch(Exception e) { + } catch (Exception e) { LOGGER.warn("[Skyblocker BlazeRenderer] " + e); + e.printStackTrace(); } } }
\ No newline at end of file diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomArmorDyeColors.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomArmorDyeColors.java index 9385b4a1..7c4ed423 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomArmorDyeColors.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomArmorDyeColors.java @@ -10,7 +10,7 @@ import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.minecraft.command.CommandRegistryAccess; -import net.minecraft.item.DyeableArmorItem; +import net.minecraft.item.DyeableItem; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.text.Text; @@ -22,10 +22,11 @@ public class CustomArmorDyeColors { private static void registerCommands(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) { dispatcher.register(ClientCommandManager.literal("skyblocker") - .then(ClientCommandManager.literal("dyeColor") - .executes(context -> customizeDyeColor(context.getSource(), null)) - .then(ClientCommandManager.argument("hexCode", StringArgumentType.string()) - .executes(context -> customizeDyeColor(context.getSource(), StringArgumentType.getString(context, "hexCode")))))); + .then(ClientCommandManager.literal("custom") + .then(ClientCommandManager.literal("dyeColor") + .executes(context -> customizeDyeColor(context.getSource(), null)) + .then(ClientCommandManager.argument("hexCode", StringArgumentType.string()) + .executes(context -> customizeDyeColor(context.getSource(), StringArgumentType.getString(context, "hexCode"))))))); } @SuppressWarnings("SameReturnValue") @@ -39,7 +40,7 @@ public class CustomArmorDyeColors { } if (Utils.isOnSkyblock() && heldItem != null) { - if (heldItem.getItem() instanceof DyeableArmorItem) { + if (heldItem.getItem() instanceof DyeableItem) { if (nbt != null && nbt.contains("ExtraAttributes")) { NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : null; diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomArmorTrims.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomArmorTrims.java new file mode 100644 index 00000000..ffc3f67d --- /dev/null +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomArmorTrims.java @@ -0,0 +1,147 @@ +package me.xmrvizzy.skyblocker.skyblock.item; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.suggestion.SuggestionProvider; +import it.unimi.dsi.fastutil.Pair; +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; +import me.xmrvizzy.skyblocker.config.SkyblockerConfig; +import me.xmrvizzy.skyblocker.utils.SkyblockEvents; +import me.xmrvizzy.skyblocker.utils.Utils; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.command.CommandRegistryAccess; +import net.minecraft.command.CommandSource; +import net.minecraft.command.argument.IdentifierArgumentType; +import net.minecraft.item.ArmorItem; +import net.minecraft.item.ItemStack; +import net.minecraft.item.trim.ArmorTrim; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtOps; +import net.minecraft.registry.*; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Optional; + +public class CustomArmorTrims { + private static final Logger LOGGER = LoggerFactory.getLogger(CustomArmorTrims.class); + public static final Object2ObjectOpenHashMap<ArmorTrimId, Optional<ArmorTrim>> TRIMS_CACHE = new Object2ObjectOpenHashMap<>(); + private static boolean trimsInitialized = false; + + public static void init() { + SkyblockEvents.JOIN.register(CustomArmorTrims::initializeTrimCache); + ClientCommandRegistrationCallback.EVENT.register(CustomArmorTrims::registerCommand); + } + + private static void initializeTrimCache() { + ClientPlayerEntity player = MinecraftClient.getInstance().player; + if (!trimsInitialized && player != null) { + TRIMS_CACHE.clear(); + DynamicRegistryManager registryManager = player.networkHandler.getRegistryManager(); + for (Identifier material : registryManager.get(RegistryKeys.TRIM_MATERIAL).getIds()) { + for (Identifier pattern : registryManager.get(RegistryKeys.TRIM_PATTERN).getIds()) { + NbtCompound compound = new NbtCompound(); + compound.putString("material", material.toString()); + compound.putString("pattern", pattern.toString()); + + ArmorTrim trim = ArmorTrim.CODEC.parse(RegistryOps.of(NbtOps.INSTANCE, registryManager), compound).resultOrPartial(LOGGER::error).orElse(null); + + // Something went terribly wrong + if (trim == null) throw new IllegalStateException("Trim shouldn't be null! [" + "\"" + material + "\",\"" + pattern + "\"]"); + + TRIMS_CACHE.put(new ArmorTrimId(material, pattern), Optional.of(trim)); + } + } + + LOGGER.info("[Skyblocker] Successfully cached all armor trims!"); + trimsInitialized = true; + } + } + + private static void registerCommand(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) { + dispatcher.register(ClientCommandManager.literal("skyblocker") + .then(ClientCommandManager.literal("custom") + .then(ClientCommandManager.literal("armorTrim") + .executes(context -> customizeTrim(context.getSource(), null, null)) + .then(ClientCommandManager.argument("material", IdentifierArgumentType.identifier()) + .suggests(getIdSuggestionProvider(RegistryKeys.TRIM_MATERIAL)) + .executes(context -> customizeTrim(context.getSource(), context.getArgument("material", Identifier.class), null)) + .then(ClientCommandManager.argument("pattern", IdentifierArgumentType.identifier()) + .suggests(getIdSuggestionProvider(RegistryKeys.TRIM_PATTERN)) + .executes(context -> customizeTrim(context.getSource(), context.getArgument("material", Identifier.class), context.getArgument("pattern", Identifier.class)))))))); + } + + @NotNull + private static SuggestionProvider<FabricClientCommandSource> getIdSuggestionProvider(RegistryKey<? extends Registry<?>> registryKey) { + return (context, builder) -> context.getSource().listIdSuggestions(registryKey, CommandSource.SuggestedIdType.ELEMENTS, builder, context); + } + + @SuppressWarnings("SameReturnValue") + private static int customizeTrim(FabricClientCommandSource source, Identifier material, Identifier pattern) { + ItemStack heldItem = source.getPlayer().getMainHandStack(); + NbtCompound nbt = (heldItem != null) ? heldItem.getNbt() : null; + + if (Utils.isOnSkyblock() && heldItem != null) { + if (heldItem.getItem() instanceof ArmorItem) { + if (nbt != null && nbt.contains("ExtraAttributes")) { + NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); + String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : null; + + if (itemUuid != null) { + Object2ObjectOpenHashMap<String, ArmorTrimId> customArmorTrims = SkyblockerConfig.get().general.customArmorTrims; + + if (material == null && pattern == null) { + if (customArmorTrims.containsKey(itemUuid)) { + customArmorTrims.remove(itemUuid); + SkyblockerConfig.save(); + source.sendFeedback(Text.translatable("skyblocker.customArmorTrims.removed")); + } else { + source.sendFeedback(Text.translatable("skyblocker.customArmorTrims.neverHad")); + } + } else { + // Ensure that the material & trim are valid + ArmorTrimId trimId = new ArmorTrimId(material, pattern); + if (TRIMS_CACHE.get(trimId) == null) { + source.sendError(Text.translatable("skyblocker.customArmorTrims.invalidMaterialOrPattern")); + + return Command.SINGLE_SUCCESS; + } + + customArmorTrims.put(itemUuid, trimId); + SkyblockerConfig.save(); + source.sendFeedback(Text.translatable("skyblocker.customArmorTrims.added")); + } + } else { + source.sendError(Text.translatable("skyblocker.customArmorTrims.noItemUuid")); + } + } + } else { + source.sendError(Text.translatable("skyblocker.customArmorTrims.notAnArmorPiece")); + return Command.SINGLE_SUCCESS; + } + } else { + source.sendError(Text.translatable("skyblocker.customArmorTrims.unableToSetTrim")); + } + + return Command.SINGLE_SUCCESS; + } + + public record ArmorTrimId(Identifier material, Identifier pattern) implements Pair<Identifier, Identifier> { + @Override + public Identifier left() { + return material(); + } + + @Override + public Identifier right() { + return pattern(); + } + } +} diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomItemNames.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomItemNames.java index 5d410947..c744144a 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomItemNames.java +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/CustomItemNames.java @@ -12,6 +12,8 @@ import net.minecraft.command.CommandRegistryAccess; import net.minecraft.command.argument.TextArgumentType; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; +import net.minecraft.text.MutableText; +import net.minecraft.text.Style; import net.minecraft.text.Text; public class CustomItemNames { @@ -21,10 +23,11 @@ public class CustomItemNames { private static void registerCommands(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) { dispatcher.register(ClientCommandManager.literal("skyblocker") - .then(ClientCommandManager.literal("renameItem") - .executes(context -> renameItem(context.getSource(), null)) - .then(ClientCommandManager.argument("textComponent", TextArgumentType.text()) - .executes(context -> renameItem(context.getSource(), context.getArgument("textComponent", Text.class)))))); + .then(ClientCommandManager.literal("custom") + .then(ClientCommandManager.literal("renameItem") + .executes(context -> renameItem(context.getSource(), null)) + .then(ClientCommandManager.argument("textComponent", TextArgumentType.text()) + .executes(context -> renameItem(context.getSource(), context.getArgument("textComponent", Text.class))))))); } @SuppressWarnings("SameReturnValue") @@ -50,6 +53,11 @@ public class CustomItemNames { } } else { //If the text is provided then set the item's custom name to it + + //Set italic to false if it hasn't been changed (or was already false) + Style currentStyle = text.getStyle(); + ((MutableText) text).setStyle(currentStyle.withItalic((currentStyle.isItalic() ? true : false))); + customItemNames.put(itemUuid, text); SkyblockerConfig.save(); source.sendFeedback(Text.translatable("skyblocker.customItemNames.added")); |