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

Commit 3f504ca

Browse files
ItemTooltipEvent (#108)
* ItemTooltipEvent * Commiting before you push is helpful * Remove useless import
1 parent d891443 commit 3f504ca

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.minecraftforge.event.entity.player;
21+
22+
import java.util.List;
23+
24+
import javax.annotation.Nonnull;
25+
import javax.annotation.Nullable;
26+
27+
import net.minecraft.client.item.TooltipContext;
28+
import net.minecraft.entity.player.PlayerEntity;
29+
import net.minecraft.item.ItemStack;
30+
import net.minecraft.text.Text;
31+
32+
public class ItemTooltipEvent extends PlayerEvent {
33+
private final TooltipContext flags;
34+
@Nonnull
35+
private final ItemStack itemStack;
36+
private final List<Text> toolTip;
37+
38+
/**
39+
* 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}.
40+
* Tooltips are also gathered with a null entityPlayer during startup by {@link net.minecraft.client.MinecraftClient#initializeSearchableContainers()}.
41+
*/
42+
public ItemTooltipEvent(@Nonnull ItemStack itemStack, @Nullable PlayerEntity entityPlayer, List<Text> list, TooltipContext flags) {
43+
super(entityPlayer);
44+
this.itemStack = itemStack;
45+
this.toolTip = list;
46+
this.flags = flags;
47+
}
48+
49+
/**
50+
* Use to determine if the advanced information on item tooltips is being shown, toggled by F3+H.
51+
*/
52+
public TooltipContext getFlags() {
53+
return flags;
54+
}
55+
56+
/**
57+
* The {@link ItemStack} with the tooltip.
58+
*/
59+
@Nonnull
60+
public ItemStack getItemStack() {
61+
return itemStack;
62+
}
63+
64+
/**
65+
* The {@link ItemStack} tooltip.
66+
*/
67+
public List<Text> getToolTip() {
68+
return toolTip;
69+
}
70+
71+
/**
72+
* This event is fired with a null player during startup when populating search trees for tooltips.
73+
*/
74+
@Override
75+
@Nullable
76+
public PlayerEntity getPlayer() {
77+
return super.getPlayer();
78+
}
79+
}

patchwork-events-entity/src/main/java/net/patchworkmc/impl/event/entity/EntityEvents.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package net.patchworkmc.impl.event.entity;
2121

22+
import java.util.List;
23+
2224
import net.minecraftforge.common.MinecraftForge;
2325
import net.minecraftforge.common.extensions.IForgeItem;
2426
import net.minecraftforge.event.entity.EntityEvent;
@@ -32,6 +34,7 @@
3234
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent;
3335
import net.minecraftforge.event.entity.living.LivingSpawnEvent;
3436
import net.minecraftforge.event.entity.player.AttackEntityEvent;
37+
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
3538
import net.minecraftforge.event.entity.player.PlayerEvent;
3639
import net.minecraftforge.event.entity.player.PlayerFlyableFallEvent;
3740
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
@@ -40,6 +43,7 @@
4043
import org.apache.logging.log4j.LogManager;
4144
import org.apache.logging.log4j.Logger;
4245

46+
import net.minecraft.client.item.TooltipContext;
4347
import net.minecraft.entity.Entity;
4448
import net.minecraft.entity.EntityDimensions;
4549
import net.minecraft.entity.EntityPose;
@@ -50,6 +54,7 @@
5054
import net.minecraft.entity.player.PlayerEntity;
5155
import net.minecraft.item.ItemStack;
5256
import net.minecraft.server.network.ServerPlayerEntity;
57+
import net.minecraft.text.Text;
5358
import net.minecraft.util.ActionResult;
5459
import net.minecraft.util.Hand;
5560
import net.minecraft.world.IWorld;
@@ -181,6 +186,10 @@ public static boolean attackEntity(PlayerEntity player, Entity target) {
181186
return !item.onLeftClickEntity(stack, player, target);
182187
}
183188

189+
public static void onItemTooltip(ItemStack itemStack, PlayerEntity entityPlayer, List<Text> list, TooltipContext flags) {
190+
MinecraftForge.EVENT_BUS.post(new ItemTooltipEvent(itemStack, entityPlayer, list, flags));
191+
}
192+
184193
@Override
185194
public void onInitialize() {
186195
UseItemCallback.EVENT.register((player, world, hand) -> {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package net.patchworkmc.mixin.event.entity;
21+
22+
import java.util.List;
23+
24+
import org.spongepowered.asm.mixin.Mixin;
25+
import org.spongepowered.asm.mixin.injection.At;
26+
import org.spongepowered.asm.mixin.injection.Inject;
27+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
28+
29+
import net.minecraft.client.item.TooltipContext;
30+
import net.minecraft.entity.player.PlayerEntity;
31+
import net.minecraft.item.ItemStack;
32+
import net.minecraft.text.Text;
33+
34+
import net.patchworkmc.impl.event.entity.EntityEvents;
35+
36+
@Mixin(ItemStack.class)
37+
public class MixinItemStack {
38+
@Inject(method = "getTooltip", at = @At("RETURN"))
39+
private void onGetTooltip(PlayerEntity player, TooltipContext context, CallbackInfoReturnable<List<Text>> cir) {
40+
EntityEvents.onItemTooltip((ItemStack) (Object) this, player, cir.getReturnValue(), context);
41+
}
42+
}

patchwork-events-entity/src/main/resources/patchwork-events-entity.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"client": [
2121
"MixinClientWorld",
2222
"MixinClientPlayerEntity",
23+
"MixinItemStack",
2324
"MixinOtherClientPlayerEntity"
2425
],
2526
"injectors": {

0 commit comments

Comments
 (0)