HoverEvent .java

net.minecraft.network.chat.HoverEvent (interface): members: EntityTooltipInfo, ShowEntity, ShowItem, ShowText, Action, action, CODEC. strings: "<action ".

net.minecraft.network.chatinterface146 linescommon4.9 KB

API Surface (9)

boolean isAllowedFromServer()
String getSerializedName()
String toString()
List<Component> getTooltipLines()
boolean equals(final Object o)
int hashCode()
HoverEvent.Action action()
HoverEvent.Action action()
HoverEvent.Action action()

Fields & Constants (1)

CODEC

String literals (8)

"action"
"show_text"
"show_item"
"show_entity"
"<action "
"Action not allowed: "
"uuid"
"gui.entity_tooltip.type"

Code Graph: Dependencies (7)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

HoverEvent.java146 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.network.chat;
2
3
import com.mojang.serialization.Codec;
4
import com.mojang.serialization.DataResult;
5
import com.mojang.serialization.Lifecycle;
6
import com.mojang.serialization.MapCodec;
7
import com.mojang.serialization.codecs.RecordCodecBuilder;
8
import java.util.ArrayList;
9
import java.util.List;
10
import java.util.Optional;
11
import java.util.UUID;
12
import net.minecraft.core.UUIDUtil;
13
import net.minecraft.core.registries.BuiltInRegistries;
14
import net.minecraft.util.StringRepresentable;
15
import net.minecraft.world.entity.EntityType;
16
import net.minecraft.world.item.ItemStackTemplate;
17
import org.jspecify.annotations.Nullable;
18
19
public interface HoverEvent {
20
	Codec<HoverEvent> CODEC = HoverEvent.Action.CODEC.dispatch("action", HoverEvent::action, action -> action.codec);
21
22
	HoverEvent.Action action();
23
24
	enum Action implements StringRepresentable {
25
		SHOW_TEXT("show_text", true, HoverEvent.ShowText.CODEC),
26
		SHOW_ITEM("show_item", true, HoverEvent.ShowItem.CODEC),
27
		SHOW_ENTITY("show_entity", true, HoverEvent.ShowEntity.CODEC);
28
29
		public static final Codec<HoverEvent.Action> UNSAFE_CODEC = StringRepresentable.fromValues(HoverEvent.Action::values);
30
		public static final Codec<HoverEvent.Action> CODEC = UNSAFE_CODEC.validate(HoverEvent.Action::filterForSerialization);
31
		private final String name;
32
		private final boolean allowFromServer;
33
		private final MapCodec<? extends HoverEvent> codec;
34
35
		Action(final String name, final boolean allowFromServer, final MapCodec<? extends HoverEvent> codec) {
36
			this.name = name;
37
			this.allowFromServer = allowFromServer;
38
			this.codec = codec;
39
		}
40
41
		public boolean isAllowedFromServer() {
42
			return this.allowFromServer;
43
		}
44
45
		@Override
46
		public String getSerializedName() {
47
			return this.name;
48
		}
49
50
		@Override
51
		public String toString() {
52
			return "<action " + this.name + ">";
53
		}
54
55
		private static DataResult<HoverEvent.Action> filterForSerialization(final HoverEvent.Action action) {
56
			return !action.isAllowedFromServer() ? DataResult.error(() -> "Action not allowed: " + action) : DataResult.success(action, Lifecycle.stable());
57
		}
58
	}
59
60
	class EntityTooltipInfo {
61
		public static final MapCodec<HoverEvent.EntityTooltipInfo> CODEC = RecordCodecBuilder.mapCodec(
62
			i -> i.group(
63
					BuiltInRegistries.ENTITY_TYPE.byNameCodec().fieldOf("id").forGetter(o -> o.type),
64
					UUIDUtil.LENIENT_CODEC.fieldOf("uuid").forGetter(o -> o.uuid),
65
					ComponentSerialization.CODEC.optionalFieldOf("name").forGetter(o -> o.name)
66
				)
67
				.apply(i, HoverEvent.EntityTooltipInfo::new)
68
		);
69
		public final EntityType<?> type;
70
		public final UUID uuid;
71
		public final Optional<Component> name;
72
		private @Nullable List<Component> linesCache;
73
74
		public EntityTooltipInfo(final EntityType<?> type, final UUID uuid, final @Nullable Component name) {
75
			this(type, uuid, Optional.ofNullable(name));
76
		}
77
78
		public EntityTooltipInfo(final EntityType<?> type, final UUID uuid, final Optional<Component> name) {
79
			this.type = type;
80
			this.uuid = uuid;
81
			this.name = name;
82
		}
83
84
		public List<Component> getTooltipLines() {
85
			if (this.linesCache == null) {
86
				this.linesCache = new ArrayList<>();
87
				this.name.ifPresent(this.linesCache::add);
88
				this.linesCache.add(Component.translatable("gui.entity_tooltip.type", this.type.getDescription()));
89
				this.linesCache.add(Component.literal(this.uuid.toString()));
90
			}
91
92
			return this.linesCache;
93
		}
94
95
		@Override
96
		public boolean equals(final Object o) {
97
			if (this == o) {
98
				return true;
99
			} else if (o != null && this.getClass() == o.getClass()) {
100
				HoverEvent.EntityTooltipInfo that = (HoverEvent.EntityTooltipInfo)o;
101
				return this.type.equals(that.type) && this.uuid.equals(that.uuid) && this.name.equals(that.name);
102
			} else {
103
				return false;
104
			}
105
		}
106
107
		@Override
108
		public int hashCode() {
109
			int result = this.type.hashCode();
110
			result = 31 * result + this.uuid.hashCode();
111
			return 31 * result + this.name.hashCode();
112
		}
113
	}
114
115
	record ShowEntity(HoverEvent.EntityTooltipInfo entity) implements HoverEvent {
116
		public static final MapCodec<HoverEvent.ShowEntity> CODEC = RecordCodecBuilder.mapCodec(
117
			i -> i.group(HoverEvent.EntityTooltipInfo.CODEC.forGetter(HoverEvent.ShowEntity::entity)).apply(i, HoverEvent.ShowEntity::new)
118
		);
119
120
		@Override
121
		public HoverEvent.Action action() {
122
			return HoverEvent.Action.SHOW_ENTITY;
123
		}
124
	}
125
126
	record ShowItem(ItemStackTemplate item) implements HoverEvent {
127
		public static final MapCodec<HoverEvent.ShowItem> CODEC = ItemStackTemplate.MAP_CODEC.xmap(HoverEvent.ShowItem::new, HoverEvent.ShowItem::item);
128
129
		@Override
130
		public HoverEvent.Action action() {
131
			return HoverEvent.Action.SHOW_ITEM;
132
		}
133
	}
134
135
	record ShowText(Component value) implements HoverEvent {
136
		public static final MapCodec<HoverEvent.ShowText> CODEC = RecordCodecBuilder.mapCodec(
137
			i -> i.group(ComponentSerialization.CODEC.fieldOf("value").forGetter(HoverEvent.ShowText::value)).apply(i, HoverEvent.ShowText::new)
138
		);
139
140
		@Override
141
		public HoverEvent.Action action() {
142
			return HoverEvent.Action.SHOW_TEXT;
143
		}
144
	}
145
}
146

Decompiled Minecraft 26.3 source of HoverEvent (net/minecraft/network/chat/HoverEvent.java) with Mojang official mappings, stored verbatim on the site.