Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

ItemTooltipEvent #108

Merged
merged 3 commits into from
Jul 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Text> 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<Text> 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<Text> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Text> list, TooltipContext flags) {
MinecraftForge.EVENT_BUS.post(new ItemTooltipEvent(itemStack, entityPlayer, list, flags));
}

@Override
public void onInitialize() {
UseItemCallback.EVENT.register((player, world, hand) -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<Text>> cir) {
EntityEvents.onItemTooltip((ItemStack) (Object) this, player, cir.getReturnValue(), context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"client": [
"MixinClientWorld",
"MixinClientPlayerEntity",
"MixinItemStack",
"MixinOtherClientPlayerEntity"
],
"injectors": {
Expand Down