diff --git a/patchwork-events-entity/src/main/java/net/minecraftforge/event/entity/player/ItemTooltipEvent.java b/patchwork-events-entity/src/main/java/net/minecraftforge/event/entity/player/ItemTooltipEvent.java new file mode 100644 index 00000000..bac5a6c4 --- /dev/null +++ b/patchwork-events-entity/src/main/java/net/minecraftforge/event/entity/player/ItemTooltipEvent.java @@ -0,0 +1,79 @@ +/* + * Minecraft Forge, Patchwork Project + * Copyright (c) 2016-2020, 2019-2020 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.event.entity.player; + +import java.util.List; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import net.minecraft.client.item.TooltipContext; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.text.Text; + +public class ItemTooltipEvent extends PlayerEvent { + private final TooltipContext flags; + @Nonnull + private final ItemStack itemStack; + private final List toolTip; + + /** + * This event is fired in {@link ItemStack#getTooltip(PlayerEntity, TooltipContext)}, which in turn is called from it's respective {@link net.minecraft.client.gui.screen.ingame.ContainerScreen}. + * Tooltips are also gathered with a null entityPlayer during startup by {@link net.minecraft.client.MinecraftClient#initializeSearchableContainers()}. + */ + public ItemTooltipEvent(@Nonnull ItemStack itemStack, @Nullable PlayerEntity entityPlayer, List list, TooltipContext flags) { + super(entityPlayer); + this.itemStack = itemStack; + this.toolTip = list; + this.flags = flags; + } + + /** + * Use to determine if the advanced information on item tooltips is being shown, toggled by F3+H. + */ + public TooltipContext getFlags() { + return flags; + } + + /** + * The {@link ItemStack} with the tooltip. + */ + @Nonnull + public ItemStack getItemStack() { + return itemStack; + } + + /** + * The {@link ItemStack} tooltip. + */ + public List getToolTip() { + return toolTip; + } + + /** + * This event is fired with a null player during startup when populating search trees for tooltips. + */ + @Override + @Nullable + public PlayerEntity getPlayer() { + return super.getPlayer(); + } +} diff --git a/patchwork-events-entity/src/main/java/net/patchworkmc/impl/event/entity/EntityEvents.java b/patchwork-events-entity/src/main/java/net/patchworkmc/impl/event/entity/EntityEvents.java index 3c753fd4..9355e90f 100644 --- a/patchwork-events-entity/src/main/java/net/patchworkmc/impl/event/entity/EntityEvents.java +++ b/patchwork-events-entity/src/main/java/net/patchworkmc/impl/event/entity/EntityEvents.java @@ -19,6 +19,8 @@ package net.patchworkmc.impl.event.entity; +import java.util.List; + import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.extensions.IForgeItem; import net.minecraftforge.event.entity.EntityEvent; @@ -32,6 +34,7 @@ import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent; import net.minecraftforge.event.entity.living.LivingSpawnEvent; import net.minecraftforge.event.entity.player.AttackEntityEvent; +import net.minecraftforge.event.entity.player.ItemTooltipEvent; import net.minecraftforge.event.entity.player.PlayerEvent; import net.minecraftforge.event.entity.player.PlayerFlyableFallEvent; import net.minecraftforge.event.entity.player.PlayerInteractEvent; @@ -40,6 +43,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import net.minecraft.client.item.TooltipContext; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityDimensions; import net.minecraft.entity.EntityPose; @@ -50,6 +54,7 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; import net.minecraft.world.IWorld; @@ -181,6 +186,10 @@ public static boolean attackEntity(PlayerEntity player, Entity target) { return !item.onLeftClickEntity(stack, player, target); } + public static void onItemTooltip(ItemStack itemStack, PlayerEntity entityPlayer, List list, TooltipContext flags) { + MinecraftForge.EVENT_BUS.post(new ItemTooltipEvent(itemStack, entityPlayer, list, flags)); + } + @Override public void onInitialize() { UseItemCallback.EVENT.register((player, world, hand) -> { diff --git a/patchwork-events-entity/src/main/java/net/patchworkmc/mixin/event/entity/MixinItemStack.java b/patchwork-events-entity/src/main/java/net/patchworkmc/mixin/event/entity/MixinItemStack.java new file mode 100644 index 00000000..6389a5de --- /dev/null +++ b/patchwork-events-entity/src/main/java/net/patchworkmc/mixin/event/entity/MixinItemStack.java @@ -0,0 +1,42 @@ +/* + * Minecraft Forge, Patchwork Project + * Copyright (c) 2016-2020, 2019-2020 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.patchworkmc.mixin.event.entity; + +import java.util.List; + +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.item.TooltipContext; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.text.Text; + +import net.patchworkmc.impl.event.entity.EntityEvents; + +@Mixin(ItemStack.class) +public class MixinItemStack { + @Inject(method = "getTooltip", at = @At("RETURN")) + private void onGetTooltip(PlayerEntity player, TooltipContext context, CallbackInfoReturnable> cir) { + EntityEvents.onItemTooltip((ItemStack) (Object) this, player, cir.getReturnValue(), context); + } +} diff --git a/patchwork-events-entity/src/main/resources/patchwork-events-entity.mixins.json b/patchwork-events-entity/src/main/resources/patchwork-events-entity.mixins.json index 7b7c0180..081b587e 100644 --- a/patchwork-events-entity/src/main/resources/patchwork-events-entity.mixins.json +++ b/patchwork-events-entity/src/main/resources/patchwork-events-entity.mixins.json @@ -20,6 +20,7 @@ "client": [ "MixinClientWorld", "MixinClientPlayerEntity", + "MixinItemStack", "MixinOtherClientPlayerEntity" ], "injectors": {