Added icons for each trait and display known traits on item tooltips

This commit is contained in:
Sollace 2021-11-14 01:17:43 +02:00
parent 95378ea50e
commit 60d5d56864
26 changed files with 125 additions and 5 deletions

View file

@ -16,8 +16,11 @@ import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.minelittlepony.unicopia.client.gui.ItemTraitsTooltipRenderer;
import com.minelittlepony.unicopia.util.InventoryUtil; import com.minelittlepony.unicopia.util.InventoryUtil;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.inventory.Inventory; import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item; import net.minecraft.item.Item;
@ -25,7 +28,6 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement; import net.minecraft.nbt.NbtElement;
import net.minecraft.network.PacketByteBuf; import net.minecraft.network.PacketByteBuf;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.MathHelper;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
@ -93,14 +95,12 @@ public final class SpellTraits implements Iterable<Map.Entry<Trait, Float>> {
return MathHelper.clamp(get(trait), min, max); return MathHelper.clamp(get(trait), min, max);
} }
@Environment(EnvType.CLIENT)
public void appendTooltip(List<Text> tooltip) { public void appendTooltip(List<Text> tooltip) {
if (isEmpty()) { if (isEmpty()) {
return; return;
} }
tooltip.add(new LiteralText("Traits:")); tooltip.add(1, new ItemTraitsTooltipRenderer(this));
traits.forEach((trait, amount) -> {
tooltip.add(new LiteralText(trait.name().toLowerCase() + ": " + amount));
});
} }
public NbtCompound toNbt() { public NbtCompound toNbt() {

View file

@ -36,10 +36,12 @@ public enum Trait {
public static final Map<String, Trait> REGISTRY = Arrays.stream(values()).collect(Collectors.toMap(Trait::name, Function.identity())); public static final Map<String, Trait> REGISTRY = Arrays.stream(values()).collect(Collectors.toMap(Trait::name, Function.identity()));
private final Identifier id; private final Identifier id;
private final Identifier sprite;
private final TraitGroup group; private final TraitGroup group;
Trait(TraitGroup group) { Trait(TraitGroup group) {
this.id = new Identifier("unicopia", "spell/trait/" + name().toLowerCase()); this.id = new Identifier("unicopia", "spell/trait/" + name().toLowerCase());
this.sprite = new Identifier("unicopia", "textures/gui/trait/" + name().toLowerCase() + ".png");
this.group = group; this.group = group;
} }
@ -50,4 +52,8 @@ public enum Trait {
public TraitGroup getGroup() { public TraitGroup getGroup() {
return group; return group;
} }
public Identifier getSprite() {
return sprite;
}
} }

View file

@ -12,6 +12,8 @@ import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.util.NbtSerialisable; import com.minelittlepony.unicopia.util.NbtSerialisable;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Items; import net.minecraft.item.Items;
@ -66,6 +68,7 @@ public class TraitDiscovery implements NbtSerialisable {
return items.getOrDefault(Registry.ITEM.getId(item), SpellTraits.EMPTY); return items.getOrDefault(Registry.ITEM.getId(item), SpellTraits.EMPTY);
} }
@Environment(EnvType.CLIENT)
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip) { public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip) {
getKnownTraits(stack.getItem()).appendTooltip(tooltip); getKnownTraits(stack.getItem()).appendTooltip(tooltip);
} }

View file

@ -0,0 +1,91 @@
package com.minelittlepony.unicopia.client.gui;
import java.util.Map.Entry;
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits;
import com.minelittlepony.unicopia.ability.magic.spell.trait.Trait;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawableHelper;
import net.minecraft.client.gui.tooltip.TooltipComponent;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.item.ItemRenderer;
import net.minecraft.client.texture.TextureManager;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.BaseText;
import net.minecraft.text.CharacterVisitor;
import net.minecraft.text.OrderedText;
public class ItemTraitsTooltipRenderer extends BaseText implements OrderedText, TooltipComponent {
private final SpellTraits traits;
public ItemTraitsTooltipRenderer(SpellTraits traits) {
this.traits = traits;
}
@Override
public int getHeight() {
return getRows() * 8 + 2 + 4;
}
@Override
public int getWidth(TextRenderer textRenderer) {
return getColumns() * 8 + 2;
}
private int getColumns() {
return Math.max(2, (int)Math.ceil(Math.sqrt(traits.entries().size() + 1)));
}
private int getRows() {
return (int)Math.ceil((traits.entries().size() + 1) / getColumns());
}
@Override
public void drawItems(TextRenderer textRenderer, int x, int y, MatrixStack matrices, ItemRenderer itemRenderer, int z, TextureManager textureManager) {
int columns = getColumns();
var entries = traits.stream().toList();
for (int i = 0; i < entries.size(); i++) {
int xx = x + (i % columns);
int yy = y + (i / columns);
Entry<Trait, Float> entry = entries.get(i);
RenderSystem.setShaderTexture(0, entry.getKey().getSprite());
DrawableHelper.drawTexture(matrices, xx, yy, 1, 0, 0, 8, 8, 8, 8);
String string = String.valueOf(entry.getValue());
matrices.push();
xx += 19 - 2 - textRenderer.getWidth(string);
yy += 6 + 3;
matrices.translate(xx, yy, itemRenderer.zOffset + 200.0F);
matrices.scale(0.5F, 0.5F, 1);
VertexConsumerProvider.Immediate immediate = VertexConsumerProvider.immediate(Tessellator.getInstance().getBuffer());
textRenderer.draw(string, 0, 0, 16777215, true, matrices.peek().getModel(), immediate, false, 0, 15728880);
immediate.draw();
matrices.pop();
}
}
@Override
public boolean accept(CharacterVisitor visitor) {
return false;
}
@Override
public OrderedText asOrderedText() {
return this;
}
@Override
public BaseText copy() {
return new ItemTraitsTooltipRenderer(traits);
}
}

View file

@ -0,0 +1,19 @@
package com.minelittlepony.unicopia.mixin.client;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import net.minecraft.client.gui.tooltip.TooltipComponent;
import net.minecraft.text.OrderedText;
@Mixin(TooltipComponent.class)
interface MixinTooltipComponent {
@Inject(method = "of", at = @At("HEAD"), cancellable = true)
private static void onOf(OrderedText text, CallbackInfoReturnable<TooltipComponent> info) {
if (text instanceof TooltipComponent) {
info.setReturnValue((TooltipComponent)text);
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 240 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 326 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 223 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 210 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 520 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 431 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 561 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 243 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 404 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 557 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 462 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 501 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 284 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 603 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 226 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 338 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 492 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 485 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 464 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 333 B

View file

@ -43,6 +43,7 @@
"client.MixinKeyboardInput", "client.MixinKeyboardInput",
"client.MixinLightmapTextureManager", "client.MixinLightmapTextureManager",
"client.MixinMouse", "client.MixinMouse",
"client.MixinTooltipComponent",
"client.MixinWorldRenderer" "client.MixinWorldRenderer"
], ],
"injectors": { "injectors": {