diff options
author | Grayray75 <69988482+Grayray75@users.noreply.github.com> | 2023-10-08 16:39:15 +0200 |
---|---|---|
committer | Grayray75 <69988482+Grayray75@users.noreply.github.com> | 2023-10-08 16:40:47 +0200 |
commit | e65f26f096e998025fe4c46602adf4638710a977 (patch) | |
tree | ff1ea8544a923ca41a99e6ee93ba6e8c28a3c295 /src/main/java/me/xmrvizzy/skyblocker/skyblock/item | |
parent | 19c8450f00b1f18725deb8014b642817bb607682 (diff) | |
parent | 0c9381191663a74a9ec3cff9aae1c8b523215f0e (diff) | |
download | Skyblocker-e65f26f096e998025fe4c46602adf4638710a977.tar.gz Skyblocker-e65f26f096e998025fe4c46602adf4638710a977.tar.bz2 Skyblocker-e65f26f096e998025fe4c46602adf4638710a977.zip |
Merge branch 'master' into item-cooldown
Diffstat (limited to 'src/main/java/me/xmrvizzy/skyblocker/skyblock/item')
3 files changed, 213 insertions, 0 deletions
diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/ItemProtection.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/ItemProtection.java new file mode 100644 index 00000000..db671787 --- /dev/null +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/ItemProtection.java @@ -0,0 +1,75 @@ +package me.xmrvizzy.skyblocker.skyblock.item; + +import com.mojang.brigadier.Command; +import com.mojang.brigadier.CommandDispatcher; + +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; +import me.xmrvizzy.skyblocker.config.SkyblockerConfigManager; +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.command.CommandRegistryAccess; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.text.Text; + +public class ItemProtection { + + public static void init() { + ClientCommandRegistrationCallback.EVENT.register(ItemProtection::registerCommand); + } + + public static boolean isItemProtected(ItemStack stack) { + if (stack == null || stack.isEmpty()) return false; + + NbtCompound nbt = stack.getNbt(); + + if (nbt != null && nbt.contains("ExtraAttributes")) { + NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); + String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : ""; + + return SkyblockerConfigManager.get().general.protectedItems.contains(itemUuid); + } + + return false; + } + + private static void registerCommand(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) { + dispatcher.register(ClientCommandManager.literal("skyblocker") + .then(ClientCommandManager.literal("protectItem") + .executes(context -> protectMyItem(context.getSource())))); + } + + private static int protectMyItem(FabricClientCommandSource source) { + ItemStack heldItem = source.getPlayer().getMainHandStack(); + NbtCompound nbt = (heldItem != null) ? heldItem.getNbt() : null; + + if (Utils.isOnSkyblock() && nbt != null && nbt.contains("ExtraAttributes")) { + NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); + String itemUuid = extraAttributes.contains("uuid") ? extraAttributes.getString("uuid") : null; + + if (itemUuid != null) { + ObjectOpenHashSet<String> protectedItems = SkyblockerConfigManager.get().general.protectedItems; + + if (!protectedItems.contains(itemUuid)) { + protectedItems.add(itemUuid); + SkyblockerConfigManager.save(); + + source.sendFeedback(Text.translatable("skyblocker.itemProtection.added", heldItem.getName())); + } else { + protectedItems.remove(itemUuid); + SkyblockerConfigManager.save(); + + source.sendFeedback(Text.translatable("skyblocker.itemProtection.removed", heldItem.getName())); + } + } else { + source.sendFeedback(Text.translatable("skyblocker.itemProtection.noItemUuid")); + } + } else { + source.sendFeedback(Text.translatable("skyblocker.itemProtection.unableToProtect")); + } + + return Command.SINGLE_SUCCESS; + } +} diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/ItemRarityBackgrounds.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/ItemRarityBackgrounds.java new file mode 100644 index 00000000..837c209a --- /dev/null +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/ItemRarityBackgrounds.java @@ -0,0 +1,109 @@ +package me.xmrvizzy.skyblocker.skyblock.item; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.function.Supplier; + +import com.google.common.collect.ImmutableMap; +import com.mojang.blaze3d.systems.RenderSystem; + +import it.unimi.dsi.fastutil.ints.Int2ReferenceOpenHashMap; +import me.xmrvizzy.skyblocker.SkyblockerMod; +import me.xmrvizzy.skyblocker.config.SkyblockerConfigManager; +import me.xmrvizzy.skyblocker.utils.Utils; +import me.xmrvizzy.skyblocker.utils.scheduler.Scheduler; +import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.item.TooltipContext; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.client.texture.Sprite; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; + +public class ItemRarityBackgrounds { + private static final Identifier RARITY_BG_TEX = new Identifier(SkyblockerMod.NAMESPACE, "item_rarity_background"); + private static final Supplier<Sprite> SPRITE = () -> MinecraftClient.getInstance().getGuiAtlasManager().getSprite(RARITY_BG_TEX); + private static final ImmutableMap<String, SkyblockItemRarity> LORE_RARITIES = ImmutableMap.ofEntries( + Map.entry("ADMIN", SkyblockItemRarity.ADMIN), + Map.entry("SPECIAL", SkyblockItemRarity.SPECIAL), //Very special is the same color so this will cover it + Map.entry("DIVINE", SkyblockItemRarity.DIVINE), + Map.entry("MYTHIC", SkyblockItemRarity.MYTHIC), + Map.entry("LEGENDARY", SkyblockItemRarity.LEGENDARY), + Map.entry("LEGENJERRY", SkyblockItemRarity.LEGENDARY), + Map.entry("EPIC", SkyblockItemRarity.EPIC), + Map.entry("RARE", SkyblockItemRarity.RARE), + Map.entry("UNCOMMON", SkyblockItemRarity.UNCOMMON), + Map.entry("COMMON", SkyblockItemRarity.COMMON) + ); + private static final Int2ReferenceOpenHashMap<SkyblockItemRarity> CACHE = new Int2ReferenceOpenHashMap<>(); + + public static void init() { + //Clear the cache every 5 minutes, ints are very compact! + Scheduler.INSTANCE.scheduleCyclic(CACHE::clear, 4800); + + //Clear cache after a screen where items can be upgraded in rarity closes + ScreenEvents.BEFORE_INIT.register((client, screen, scaledWidth, scaledHeight) -> { + String title = screen.getTitle().getString(); + + if (Utils.isOnSkyblock() && (title.equals("The Hex") || title.equals("Craft Item") || title.equals("Anvil") || title.equals("Reforge Anvil"))) { + ScreenEvents.remove(screen).register(screen1 -> CACHE.clear()); + } + }); + } + + public static void tryDraw(ItemStack stack, DrawContext context, int x, int y) { + MinecraftClient client = MinecraftClient.getInstance(); + + if (client.player != null) { + SkyblockItemRarity itemRarity = getItemRarity(stack, client.player); + + if (itemRarity != null) draw(context, x, y, itemRarity); + } + } + + private static SkyblockItemRarity getItemRarity(ItemStack stack, ClientPlayerEntity player) { + if (stack == null || stack.isEmpty()) return null; + + int hashCode = 0; + NbtCompound nbt = stack.getNbt(); + + if (nbt != null && nbt.contains("ExtraAttributes")) { + NbtCompound extraAttributes = nbt.getCompound("ExtraAttributes"); + String itemUuid = extraAttributes.getString("uuid"); + + //If the item has an uuid, then use the hash code of the uuid otherwise use the identity hash code of the stack + hashCode = itemUuid.isEmpty() ? System.identityHashCode(stack) : itemUuid.hashCode(); + } + + if (CACHE.containsKey(hashCode)) return CACHE.get(hashCode); + + List<Text> tooltip = stack.getTooltip(player, TooltipContext.BASIC); + String[] stringifiedTooltip = tooltip.stream().map(Text::getString).toArray(String[]::new); + + for (String rarityString : LORE_RARITIES.keySet()) { + if (Arrays.stream(stringifiedTooltip).anyMatch(line -> line.contains(rarityString))) { + SkyblockItemRarity rarity = LORE_RARITIES.get(rarityString); + + CACHE.put(hashCode, rarity); + return rarity; + } + } + + CACHE.put(hashCode, null); + return null; + } + + private static void draw(DrawContext context, int x, int y, SkyblockItemRarity rarity) { + //Enable blending to handle HUD translucency + RenderSystem.enableBlend(); + RenderSystem.defaultBlendFunc(); + + context.drawSprite(x, y, 0, 16, 16, SPRITE.get(), rarity.r, rarity.g, rarity.b, SkyblockerConfigManager.get().general.itemInfoDisplay.itemRarityBackgroundsOpacity); + + RenderSystem.disableBlend(); + } +} diff --git a/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/SkyblockItemRarity.java b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/SkyblockItemRarity.java new file mode 100644 index 00000000..f7ff1fb9 --- /dev/null +++ b/src/main/java/me/xmrvizzy/skyblocker/skyblock/item/SkyblockItemRarity.java @@ -0,0 +1,29 @@ +package me.xmrvizzy.skyblocker.skyblock.item; + +import net.minecraft.util.Formatting; + +public enum SkyblockItemRarity { + ADMIN(Formatting.DARK_RED), + VERY_SPECIAL(Formatting.RED), + SPECIAL(Formatting.RED), + DIVINE(Formatting.AQUA), + MYTHIC(Formatting.LIGHT_PURPLE), + LEGENDARY(Formatting.GOLD), + EPIC(Formatting.DARK_PURPLE), + RARE(Formatting.BLUE), + UNCOMMON(Formatting.GREEN), + COMMON(Formatting.WHITE); + + public final float r; + public final float g; + public final float b; + + SkyblockItemRarity(Formatting formatting) { + @SuppressWarnings("DataFlowIssue") + int rgb = formatting.getColorValue(); + + this.r = ((rgb >> 16) & 0xFF) / 255f; + this.g = ((rgb >> 8) & 0xFF) / 255f; + this.b = (rgb & 0xFF) / 255f; + } +} |