| 1 | package com.tangykiwi.kiwiclient.module.client;
|
| 2 |
|
| 3 | import com.google.common.eventbus.Subscribe;
|
| 4 | import com.mojang.serialization.DataResult;
|
| 5 | import com.tangykiwi.kiwiclient.event.ItemStackTooltipEvent;
|
| 6 | import com.tangykiwi.kiwiclient.event.TooltipDataEvent;
|
| 7 | import com.tangykiwi.kiwiclient.mixin.EntityAccessor;
|
| 8 | import com.tangykiwi.kiwiclient.mixin.MobBucketItemAccessor;
|
| 9 | import com.tangykiwi.kiwiclient.module.Category;
|
| 10 | import com.tangykiwi.kiwiclient.module.Module;
|
| 11 | import com.tangykiwi.kiwiclient.module.setting.ToggleSetting;
|
| 12 | import com.tangykiwi.kiwiclient.module.setting.SliderSetting;
|
| 13 | import com.tangykiwi.kiwiclient.util.tooltip.BannerTooltipComponent;
|
| 14 | import com.tangykiwi.kiwiclient.util.tooltip.ContainerTooltipComponent;
|
| 15 | import com.tangykiwi.kiwiclient.util.tooltip.EntityTooltipComponent;
|
| 16 | import com.tangykiwi.kiwiclient.util.tooltip.MapTooltipComponent;
|
| 17 |
|
| 18 | import net.minecraft.ChatFormatting;
|
| 19 | import net.minecraft.core.HolderSet;
|
| 20 | import net.minecraft.core.component.DataComponentMap;
|
| 21 | import net.minecraft.core.component.DataComponents;
|
| 22 | import net.minecraft.nbt.CompoundTag;
|
| 23 | import net.minecraft.nbt.ListTag;
|
| 24 | import net.minecraft.nbt.NbtOps;
|
| 25 | import net.minecraft.network.chat.Component;
|
| 26 | import net.minecraft.network.chat.MutableComponent;
|
| 27 | import net.minecraft.world.ItemStackWithSlot;
|
| 28 | import net.minecraft.world.effect.MobEffectInstance;
|
| 29 | import net.minecraft.world.effect.MobEffectUtil;
|
| 30 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 31 | import net.minecraft.world.entity.EntityType;
|
| 32 | import net.minecraft.world.entity.LivingEntity;
|
| 33 | import net.minecraft.world.entity.Bucketable;
|
| 34 | import net.minecraft.world.item.BannerItem;
|
| 35 | import net.minecraft.world.item.BlockItem;
|
| 36 | import net.minecraft.world.item.DyeColor;
|
| 37 | import net.minecraft.world.item.ItemStack;
|
| 38 | import net.minecraft.world.item.Items;
|
| 39 | import net.minecraft.world.item.MobBucketItem;
|
| 40 | import net.minecraft.world.item.component.Consumable;
|
| 41 | import net.minecraft.world.item.component.CustomData;
|
| 42 | import net.minecraft.world.item.component.SuspiciousStewEffects;
|
| 43 | import net.minecraft.world.item.component.TypedEntityData;
|
| 44 | import net.minecraft.world.item.component.SuspiciousStewEffects.Entry;
|
| 45 | import net.minecraft.world.item.consume_effects.ApplyStatusEffectsConsumeEffect;
|
| 46 | import net.minecraft.world.level.block.Block;
|
| 47 | import net.minecraft.world.level.block.Blocks;
|
| 48 | import net.minecraft.world.level.block.ShulkerBoxBlock;
|
| 49 | import net.minecraft.world.level.block.entity.BannerPattern;
|
| 50 | import net.minecraft.world.level.block.entity.BannerPatternLayers;
|
| 51 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 52 | import net.minecraft.world.level.saveddata.maps.MapId;
|
| 53 |
|
| 54 | import static com.tangykiwi.kiwiclient.KiwiClient.mc;
|
| 55 |
|
| 56 | import java.util.Arrays;
|
| 57 | import java.util.Optional;
|
| 58 |
|
| 59 | public class Tooltips extends Module {
|
| 60 | private static final ItemStack[] PREVIEW = new ItemStack[27];
|
| 61 |
|
| 62 | public Tooltips() {
|
| 63 | super("Tooltips", "Displays advanced tooltips.", Category.CLIENT,
|
| 64 | new ToggleSetting("Status Effects", "Shows effects and duration of status effects in items", true),
|
| 65 | new ToggleSetting("Shulker Boxes", "Shows contents of shulker boxes", true),
|
| 66 | new ToggleSetting("Ender Chest", "Shows contents of your ender chest", true),
|
| 67 | new ToggleSetting("Maps", "Shows a preview of maps in tooltips", true).withChildren(
|
| 68 | new SliderSetting("Scale", "Map preview scale", 0.1, 1, 1, 1)
|
| 69 | ),
|
| 70 | new ToggleSetting("Banners", "Shows a preview of banner patterns in tooltips", true),
|
| 71 | new ToggleSetting("Fish", "Shows mob in water bucket", true)
|
| 72 | );
|
| 73 | }
|
| 74 |
|
| 75 | @Subscribe
|
| 76 | public void appendTooltip(ItemStackTooltipEvent event) {
|
| 77 | if(event.list().isEmpty()) return;
|
| 78 |
|
| 79 | if (getSetting("Status Effects").asToggle().getValue()) {
|
| 80 | if (event.itemStack().getItem() == Items.SUSPICIOUS_STEW) {
|
| 81 | SuspiciousStewEffects stewEffectsComponent = event.itemStack().get(DataComponents.SUSPICIOUS_STEW_EFFECTS);
|
| 82 | if (stewEffectsComponent != null) {
|
| 83 | for (Entry effectTag : stewEffectsComponent.effects()) {
|
| 84 | MobEffectInstance effect = new MobEffectInstance(effectTag.effect(), effectTag.duration(), 0);
|
| 85 | event.appendStart(getStatusText(effect));
|
| 86 | }
|
| 87 | }
|
| 88 | } else {
|
| 89 | Consumable consumable = event.itemStack().get(DataComponents.CONSUMABLE);
|
| 90 | if (consumable != null) {
|
| 91 | consumable.onConsumeEffects().stream()
|
| 92 | .filter(ApplyStatusEffectsConsumeEffect.class::isInstance)
|
| 93 | .map(ApplyStatusEffectsConsumeEffect.class::cast)
|
| 94 | .flatMap(apply -> apply.effects().stream())
|
| 95 | .forEach(effect -> event.appendStart(getStatusText(effect)));
|
| 96 | }
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | private MutableComponent getStatusText(MobEffectInstance effect) {
|
| 102 | MutableComponent text = Component.translatable(effect.getDescriptionId());
|
| 103 | if (effect.getAmplifier() != 0) {
|
| 104 | text.append(String.format(" %d (%s)", effect.getAmplifier() + 1, MobEffectUtil.formatDuration(effect, 1, mc.level.tickRateManager().tickrate()).getString()));
|
| 105 | } else {
|
| 106 | text.append(String.format(" (%s)", MobEffectUtil.formatDuration(effect, 1, mc.level.tickRateManager().tickrate()).getString()));
|
| 107 | }
|
| 108 |
|
| 109 | if (effect.getEffect().value().isBeneficial()) return text.withStyle(ChatFormatting.BLUE);
|
| 110 | return text.withStyle(ChatFormatting.RED);
|
| 111 | }
|
| 112 |
|
| 113 | @Subscribe
|
| 114 | public void appendTooltip(TooltipDataEvent event) {
|
| 115 | if (getSetting("Shulker Boxes").asToggle().getValue() && hasItems(event.itemStack)) {
|
| 116 | getItems(event.itemStack, PREVIEW);
|
| 117 | event.tooltipData = new ContainerTooltipComponent(PREVIEW, getShulkerColor(event.itemStack));
|
| 118 | }
|
| 119 |
|
| 120 | else if (event.itemStack.getItem() == Items.ENDER_CHEST && getSetting("Ender Chest").asToggle().getValue()) {
|
| 121 | // handle echest memory
|
| 122 | }
|
| 123 |
|
| 124 | else if (event.itemStack.getItem() == Items.FILLED_MAP && getSetting("Maps").asToggle().getValue()) {
|
| 125 | MapId mapId = event.itemStack.get(DataComponents.MAP_ID);
|
| 126 | if (mapId != null) event.tooltipData = new MapTooltipComponent(mapId.id());
|
| 127 | }
|
| 128 |
|
| 129 | else if (event.itemStack.getItem() instanceof BannerItem && getSetting("Banners").asToggle().getValue()) {
|
| 130 | event.tooltipData = new BannerTooltipComponent(event.itemStack);
|
| 131 | } else if (event.itemStack.has(DataComponents.PROVIDES_BANNER_PATTERNS) && getSetting("Banners").asToggle().getValue()) {
|
| 132 | event.tooltipData = createBannerFromBannerPatternItem(event.itemStack);
|
| 133 | } else if (event.itemStack.getItem() == Items.SHIELD && getSetting("Banners").asToggle().getValue()) {
|
| 134 | if (!event.itemStack.getOrDefault(DataComponents.BANNER_PATTERNS, BannerPatternLayers.EMPTY).layers().isEmpty()) {
|
| 135 | event.tooltipData = createBannerFromShield(event.itemStack);
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | else if (event.itemStack.getItem() instanceof MobBucketItem bucketItem && getSetting("Fish").asToggle().getValue()) {
|
| 140 | EntityType<?> type = ((MobBucketItemAccessor) bucketItem).getType();
|
| 141 | LivingEntity entity = (LivingEntity) type.create(mc.level, EntitySpawnReason.NATURAL);
|
| 142 |
|
| 143 | if (entity != null) {
|
| 144 | CustomData nbtComponent = event.itemStack.getOrDefault(DataComponents.BUCKET_ENTITY_DATA, null);
|
| 145 | if (nbtComponent == null) {
|
| 146 | return;
|
| 147 | }
|
| 148 |
|
| 149 | entity.applyComponentsFromItemStack(event.itemStack);
|
| 150 | ((Bucketable) entity).loadFromBucketTag(nbtComponent.copyTag());
|
| 151 | ((EntityAccessor) entity).setInWater(true);
|
| 152 | event.tooltipData = new EntityTooltipComponent(entity);
|
| 153 | }
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | public static boolean hasItems(ItemStack itemStack) {
|
| 158 | var container = itemStack.get(DataComponents.CONTAINER);
|
| 159 |
|
| 160 | if (container != null) {
|
| 161 | var items = container.nonEmptyItems().iterator();
|
| 162 | if (items.hasNext()) return true;
|
| 163 | }
|
| 164 |
|
| 165 | TypedEntityData<BlockEntityType<?>> blockEntityData = itemStack.get(DataComponents.BLOCK_ENTITY_DATA);
|
| 166 | return blockEntityData != null && blockEntityData.contains("Items");
|
| 167 | }
|
| 168 |
|
| 169 | public static void getItems(ItemStack itemStack, ItemStack[] items) {
|
| 170 | // handle echest memory
|
| 171 | if (itemStack.getItem() == Items.ENDER_CHEST) {
|
| 172 | for (int i = 0; i < 27; i++) {
|
| 173 |
|
| 174 | }
|
| 175 |
|
| 176 | return;
|
| 177 | }
|
| 178 |
|
| 179 | Arrays.fill(items, ItemStack.EMPTY);
|
| 180 | DataComponentMap components = itemStack.getComponents();
|
| 181 |
|
| 182 | if (components.has(DataComponents.CONTAINER)) {
|
| 183 | var stacks = components.get(DataComponents.CONTAINER).allItemsCopyStream().toList();
|
| 184 |
|
| 185 | for (int i = 0; i < stacks.size(); i++) {
|
| 186 | if (i < items.length) {
|
| 187 | items[i] = stacks.get(i);
|
| 188 | }
|
| 189 | }
|
| 190 | } else if (components.has(DataComponents.BLOCK_ENTITY_DATA)) {
|
| 191 | TypedEntityData<BlockEntityType<?>> blockEntityData = components.get(DataComponents.BLOCK_ENTITY_DATA);
|
| 192 | if (blockEntityData == null) return;
|
| 193 | ListTag nbt3 = blockEntityData.copyTagWithoutId().getListOrEmpty("Items");
|
| 194 |
|
| 195 | for (int i = 0; i < nbt3.size(); i++) {
|
| 196 | Optional<CompoundTag> compound = nbt3.getCompound(i);
|
| 197 | if (compound.isEmpty()) continue;
|
| 198 |
|
| 199 | Optional<Byte> slot = compound.get().getByte("Slot"); // Apparently shulker boxes can store more than 27 items, good job Mojang
|
| 200 | if (slot.isEmpty()) continue;
|
| 201 |
|
| 202 | // now NPEs when mc.world == null
|
| 203 | if (slot.get() >= 0 && slot.get() < items.length) {
|
| 204 | switch (ItemStackWithSlot.CODEC.parse(mc.player.registryAccess().createSerializationContext(NbtOps.INSTANCE), compound.get())) {
|
| 205 | case DataResult.Success<ItemStackWithSlot> success ->
|
| 206 | items[slot.get()] = success.value().stack();
|
| 207 | case DataResult.Error<ItemStackWithSlot> _ -> items[slot.get()] = ItemStack.EMPTY;
|
| 208 | default -> throw new MatchException(null, null);
|
| 209 | }
|
| 210 | }
|
| 211 | }
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | public static int getShulkerColor(ItemStack shulkerItem) {
|
| 216 | if (shulkerItem.getItem() instanceof BlockItem blockItem) {
|
| 217 | Block block = blockItem.getBlock();
|
| 218 | if (block == Blocks.ENDER_CHEST) return 0x003232;
|
| 219 |
|
| 220 | if (block instanceof ShulkerBoxBlock shulkerBlock) {
|
| 221 | DyeColor dye = shulkerBlock.getColor();
|
| 222 | if (dye == null) return 0xFFFFFFFF;
|
| 223 |
|
| 224 | return dye.getTextureDiffuseColor();
|
| 225 | }
|
| 226 | }
|
| 227 |
|
| 228 | return 0xFFFFFF;
|
| 229 | }
|
| 230 |
|
| 231 | private BannerTooltipComponent createBannerFromBannerPatternItem(ItemStack item) {
|
| 232 | HolderSet<BannerPattern> providedPatterns = item.get(DataComponents.PROVIDES_BANNER_PATTERNS);
|
| 233 | if (providedPatterns == null || providedPatterns.size() == 0) {
|
| 234 | return new BannerTooltipComponent(DyeColor.GRAY, BannerPatternLayers.EMPTY);
|
| 235 | }
|
| 236 |
|
| 237 | BannerPatternLayers component = new BannerPatternLayers.Builder().add(providedPatterns.get(0), DyeColor.WHITE).build();
|
| 238 | return new BannerTooltipComponent(DyeColor.GRAY, component);
|
| 239 | }
|
| 240 |
|
| 241 | private BannerTooltipComponent createBannerFromShield(ItemStack shieldItem) {
|
| 242 | DyeColor dyeColor2 = shieldItem.getOrDefault(DataComponents.BASE_COLOR, DyeColor.WHITE);
|
| 243 | BannerPatternLayers bannerPatternsComponent = shieldItem.getOrDefault(DataComponents.BANNER_PATTERNS, BannerPatternLayers.EMPTY);
|
| 244 | return new BannerTooltipComponent(dyeColor2, bannerPatternsComponent);
|
| 245 | }
|
| 246 | }
|
| 247 |
|