Skip to content

Commit

Permalink
add mob effects tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Nov 23, 2023
1 parent eb7b73c commit 7db6c3a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import mcp.mobius.waila.plugin.vanilla.provider.ItemEntityProvider;
import mcp.mobius.waila.plugin.vanilla.provider.ItemFrameProvider;
import mcp.mobius.waila.plugin.vanilla.provider.JukeboxProvider;
import mcp.mobius.waila.plugin.vanilla.provider.MobEffectProvider;
import mcp.mobius.waila.plugin.vanilla.provider.MobTimerProvider;
import mcp.mobius.waila.plugin.vanilla.provider.NoteBlockProvider;
import mcp.mobius.waila.plugin.vanilla.provider.PandaProvider;
Expand Down Expand Up @@ -117,15 +118,20 @@ public void register(IRegistrar registrar) {
registrar.addFeatureConfig(Options.ATTRIBUTE_HORSE_SPEED, true);
registrar.addFeatureConfig(Options.ATTRIBUTE_PANDA_GENES, true);
registrar.addFeatureConfig(Options.ATTRIBUTE_BEACON_EFFECTS, false);
registrar.addFeatureConfig(Options.ATTRIBUTE_MOB_EFFECTS, false);
registrar.addSyncedConfig(Options.ATTRIBUTE_HIDDEN_MOB_EFFECTS, false, false);
registrar.addComponent(BlockAttributesProvider.INSTANCE, BODY, Block.class, 950);
registrar.addComponent(EntityAttributesProvider.INSTANCE, HEAD, Entity.class, 950);
registrar.addComponent(EntityAttributesProvider.INSTANCE, BODY, Entity.class, 950);
registrar.addComponent(HorseProvider.INSTANCE, BODY, AbstractHorse.class);
registrar.addComponent(PandaProvider.INSTANCE, BODY, Panda.class);
registrar.addComponent(BeaconProvider.INSTANCE, BODY, BeaconBlockEntity.class);
registrar.addComponent(MobEffectProvider.INSTANCE, BODY, LivingEntity.class);
registrar.addDataType(BeaconProvider.DATA, BeaconProvider.Data.class, BeaconProvider.Data::new);
registrar.addDataType(MobEffectProvider.DATA, MobEffectProvider.Data.class, MobEffectProvider.Data::new);
registrar.addBlockData(BeaconProvider.INSTANCE, BeaconBlockEntity.class);
registrar.addEntityData(EntityAttributesProvider.INSTANCE, Entity.class);
registrar.addEntityData(MobEffectProvider.INSTANCE, LivingEntity.class);

registrar.addFeatureConfig(Options.JUKEBOX_RECORD, false);
registrar.addComponent(JukeboxProvider.INSTANCE, BODY, JukeboxBlockEntity.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public final class Options {
public static final ResourceLocation ATTRIBUTE_HORSE_SPEED = rl("attribute.horse_speed");
public static final ResourceLocation ATTRIBUTE_PANDA_GENES = rl("attribute.panda_genes");
public static final ResourceLocation ATTRIBUTE_BEACON_EFFECTS = rl("attribute.beacon_effects");
public static final ResourceLocation ATTRIBUTE_MOB_EFFECTS = rl("attribute.mob_effects");
public static final ResourceLocation ATTRIBUTE_HIDDEN_MOB_EFFECTS = rl("attribute.hidden_mob_effects");
public static final ResourceLocation BOOK_BOOKSHELF = rl("book.bookshelf");
public static final ResourceLocation BOOK_WRITTEN = rl("book.written");
public static final ResourceLocation BOOK_ENCHANTMENT_DISPLAY_MODE = rl("book.enchantment");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package mcp.mobius.waila.plugin.vanilla.provider;

import java.util.List;
import java.util.Objects;

import mcp.mobius.waila.api.IData;
import mcp.mobius.waila.api.IDataProvider;
import mcp.mobius.waila.api.IDataWriter;
import mcp.mobius.waila.api.IEntityAccessor;
import mcp.mobius.waila.api.IEntityComponentProvider;
import mcp.mobius.waila.api.IPluginConfig;
import mcp.mobius.waila.api.IServerAccessor;
import mcp.mobius.waila.api.ITooltip;
import mcp.mobius.waila.plugin.vanilla.config.Options;
import net.minecraft.ChatFormatting;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;

public enum MobEffectProvider implements IEntityComponentProvider, IDataProvider<LivingEntity> {

INSTANCE;

public static final ResourceLocation DATA = new ResourceLocation("mob_effects");

@Override
public void appendBody(ITooltip tooltip, IEntityAccessor accessor, IPluginConfig config) {
if (!config.getBoolean(Options.ATTRIBUTE_MOB_EFFECTS)) return;

var data = accessor.getData().get(Data.class);
if (data == null) return;

data.list.forEach(it -> {
var text = Component.translatable(it.getDescriptionId());
var amplifier = it.getAmplifier();

if (amplifier > 0) {
if (I18n.exists("potion.potency." + amplifier)) {
text = Component.translatable("potion.withAmplifier", text, Component.translatable("potion.potency." + amplifier));
} else {
text.append(" " + (amplifier + 1));
}
}

if (it.getEffect().getCategory() == MobEffectCategory.HARMFUL) text.withStyle(ChatFormatting.RED);

tooltip.addLine(text);
});
}

@Override
public void appendData(IDataWriter data, IServerAccessor<LivingEntity> accessor, IPluginConfig config) {
if (config.getBoolean(Options.ATTRIBUTE_MOB_EFFECTS)) data.add(Data.class, res -> res.add(new Data(accessor
.getTarget()
.getActiveEffects()
.stream()
.filter(it -> it.isVisible() || config.getBoolean(Options.ATTRIBUTE_HIDDEN_MOB_EFFECTS))
.toList())));
}

public record Data(
List<MobEffectInstance> list
) implements IData {

public Data(FriendlyByteBuf buf) {
this(buf.readList(b -> {
var effect = b.readById(BuiltInRegistries.MOB_EFFECT);
var amplifier = b.readVarInt();
return new MobEffectInstance(Objects.requireNonNull(effect), -1, amplifier);
}));
}

@Override
public void write(FriendlyByteBuf buf) {
buf.writeCollection(list, (b, m) -> {
b.writeId(BuiltInRegistries.MOB_EFFECT, m.getEffect());
b.writeVarInt(m.getAmplifier());
});
}

}

}
2 changes: 2 additions & 0 deletions src/resources/resources/assets/waila/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@
"config.waila.plugin_minecraft.attribute.horse_speed" : "Show Horse Speed",
"config.waila.plugin_minecraft.attribute.panda_genes" : "Show Panda Genes",
"config.waila.plugin_minecraft.attribute.beacon_effects" : "Show Beacon Effects",
"config.waila.plugin_minecraft.attribute.mob_effects" : "Show Mob Effects",
"config.waila.plugin_minecraft.attribute.hidden_mob_effects" : "Show Hidden Mob Effects",
"config.waila.plugin_minecraft.book" : "Book",
"config.waila.plugin_minecraft.book.bookshelf" : "Show Chiseled Bookshelf Books",
"config.waila.plugin_minecraft.book.written" : "Show Written Book Author and Generation",
Expand Down

0 comments on commit 7db6c3a

Please sign in to comment.