GossipContainer .java

net.minecraft.world.entity.ai.gossip.GossipContainer (class, 236 lines): members: DISCARD_THRESHOLD, getGossipEntries, getCountForType, EntityGossips, add.

net.minecraft.world.entity.ai.gossipclass236 linescommon7.7 KB

API Surface (19)

Map<UUID, Object2IntMap<GossipType>> getGossipEntries()
void decay()
int transferFrom(final GossipContainer source, final RandomSource random, final int maxCount)
int getReputation(final UUID entity, final Predicate<GossipType> types)
long getCountForType(final GossipType type, final DoublePredicate valueTest)
void add(final UUID target, final GossipType type, final int amountToAdd)
void remove(final UUID target, final GossipType type, final int amountToRemove)
void remove(final UUID target, final GossipType type)
void remove(final GossipType type)
void clear()
void putAll(final GossipContainer container)
GossipContainer copy()
int weightedValue(final Predicate<GossipType> types)
Stream<GossipContainer.GossipEntry> unpack(final UUID target)
void decay()
boolean isEmpty()
void makeSureValueIsntTooLowOrTooHigh(final GossipType type)
void remove(final GossipType type)
int weightedValue()

Fields & Constants (3)

CODEC
DISCARD_THRESHOLD
gossips

String literals (1)

"Target"

Same Package (2)

Code Graph: Dependencies (5)

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

Full Source

GossipContainer.java236 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.gossip;
2
3
import com.google.common.collect.Maps;
4
import com.google.common.collect.Sets;
5
import com.mojang.serialization.Codec;
6
import com.mojang.serialization.codecs.RecordCodecBuilder;
7
import it.unimi.dsi.fastutil.objects.Object2IntMap;
8
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
9
import it.unimi.dsi.fastutil.objects.ObjectIterator;
10
import it.unimi.dsi.fastutil.objects.Object2IntMap.Entry;
11
import java.util.Arrays;
12
import java.util.Collection;
13
import java.util.Collections;
14
import java.util.HashMap;
15
import java.util.Iterator;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Set;
19
import java.util.UUID;
20
import java.util.function.DoublePredicate;
21
import java.util.function.Predicate;
22
import java.util.stream.Stream;
23
import net.minecraft.core.UUIDUtil;
24
import net.minecraft.util.ExtraCodecs;
25
import net.minecraft.util.RandomSource;
26
import net.minecraft.util.VisibleForDebug;
27
28
public class GossipContainer {
29
	public static final Codec<GossipContainer> CODEC = GossipContainer.GossipEntry.CODEC
30
		.listOf()
31
		.xmap(GossipContainer::new, container -> container.unpack().toList());
32
	public static final int DISCARD_THRESHOLD = 2;
33
	private final Map<UUID, GossipContainer.EntityGossips> gossips = new HashMap<>();
34
35
	public GossipContainer() {
36
	}
37
38
	private GossipContainer(final List<GossipContainer.GossipEntry> entries) {
39
		entries.forEach(e -> this.getOrCreate(e.target).entries.put(e.type, e.value));
40
	}
41
42
	@VisibleForDebug
43
	public Map<UUID, Object2IntMap<GossipType>> getGossipEntries() {
44
		Map<UUID, Object2IntMap<GossipType>> result = Maps.newHashMap();
45
		this.gossips.keySet().forEach(uuid -> {
46
			GossipContainer.EntityGossips entityGossips = this.gossips.get(uuid);
47
			result.put(uuid, entityGossips.entries);
48
		});
49
		return result;
50
	}
51
52
	public void decay() {
53
		Iterator<GossipContainer.EntityGossips> iterator = this.gossips.values().iterator();
54
55
		while (iterator.hasNext()) {
56
			GossipContainer.EntityGossips entityGossips = iterator.next();
57
			entityGossips.decay();
58
			if (entityGossips.isEmpty()) {
59
				iterator.remove();
60
			}
61
		}
62
	}
63
64
	private Stream<GossipContainer.GossipEntry> unpack() {
65
		return this.gossips.entrySet().stream().flatMap(e -> e.getValue().unpack(e.getKey()));
66
	}
67
68
	private Collection<GossipContainer.GossipEntry> selectGossipsForTransfer(final RandomSource random, final int maxCount) {
69
		List<GossipContainer.GossipEntry> entries = this.unpack().toList();
70
		if (entries.isEmpty()) {
71
			return Collections.emptyList();
72
		}
73
74
		int[] ranges = new int[entries.size()];
75
		int rangesEnd = 0;
76
77
		for (int i = 0; i < entries.size(); i++) {
78
			GossipContainer.GossipEntry gossip = entries.get(i);
79
			rangesEnd += Math.abs(gossip.weightedValue());
80
			ranges[i] = rangesEnd - 1;
81
		}
82
83
		Set<GossipContainer.GossipEntry> results = Sets.newIdentityHashSet();
84
85
		for (int i = 0; i < maxCount; i++) {
86
			int choice = random.nextInt(rangesEnd);
87
			int selectedIndex = Arrays.binarySearch(ranges, choice);
88
			results.add(entries.get(selectedIndex < 0 ? -selectedIndex - 1 : selectedIndex));
89
		}
90
91
		return results;
92
	}
93
94
	private GossipContainer.EntityGossips getOrCreate(final UUID target) {
95
		return this.gossips.computeIfAbsent(target, uuid -> new GossipContainer.EntityGossips());
96
	}
97
98
	public int transferFrom(final GossipContainer source, final RandomSource random, final int maxCount) {
99
		Collection<GossipContainer.GossipEntry> newGossips = source.selectGossipsForTransfer(random, maxCount);
100
		newGossips.forEach(newGossip -> {
101
			int decayedValue = newGossip.value - newGossip.type.decayPerTransfer;
102
			if (decayedValue >= 2) {
103
				this.getOrCreate(newGossip.target).entries.mergeInt(newGossip.type, decayedValue, GossipContainer::mergeValuesForTransfer);
104
			}
105
		});
106
		return newGossips.size();
107
	}
108
109
	public int getReputation(final UUID entity, final Predicate<GossipType> types) {
110
		GossipContainer.EntityGossips entry = this.gossips.get(entity);
111
		return entry != null ? entry.weightedValue(types) : 0;
112
	}
113
114
	public long getCountForType(final GossipType type, final DoublePredicate valueTest) {
115
		return this.gossips.values().stream().filter(e -> valueTest.test(e.entries.getOrDefault(type, 0) * type.weight)).count();
116
	}
117
118
	public void add(final UUID target, final GossipType type, final int amountToAdd) {
119
		GossipContainer.EntityGossips entityGossips = this.getOrCreate(target);
120
		entityGossips.entries.mergeInt(type, amountToAdd, (o, n) -> this.mergeValuesForAddition(type, o, n));
121
		entityGossips.makeSureValueIsntTooLowOrTooHigh(type);
122
		if (entityGossips.isEmpty()) {
123
			this.gossips.remove(target);
124
		}
125
	}
126
127
	public void remove(final UUID target, final GossipType type, final int amountToRemove) {
128
		this.add(target, type, -amountToRemove);
129
	}
130
131
	public void remove(final UUID target, final GossipType type) {
132
		GossipContainer.EntityGossips entityGossips = this.gossips.get(target);
133
		if (entityGossips != null) {
134
			entityGossips.remove(type);
135
			if (entityGossips.isEmpty()) {
136
				this.gossips.remove(target);
137
			}
138
		}
139
	}
140
141
	public void remove(final GossipType type) {
142
		Iterator<GossipContainer.EntityGossips> iterator = this.gossips.values().iterator();
143
144
		while (iterator.hasNext()) {
145
			GossipContainer.EntityGossips entityGossips = iterator.next();
146
			entityGossips.remove(type);
147
			if (entityGossips.isEmpty()) {
148
				iterator.remove();
149
			}
150
		}
151
	}
152
153
	public void clear() {
154
		this.gossips.clear();
155
	}
156
157
	public void putAll(final GossipContainer container) {
158
		container.gossips.forEach((target, gossips) -> this.getOrCreate(target).entries.putAll(gossips.entries));
159
	}
160
161
	private static int mergeValuesForTransfer(final int oldValue, final int newValue) {
162
		return Math.max(oldValue, newValue);
163
	}
164
165
	private int mergeValuesForAddition(final GossipType type, final int oldValue, final int newValue) {
166
		int sum = oldValue + newValue;
167
		return sum > type.max ? Math.max(type.max, oldValue) : sum;
168
	}
169
170
	public GossipContainer copy() {
171
		GossipContainer container = new GossipContainer();
172
		container.putAll(this);
173
		return container;
174
	}
175
176
	private static class EntityGossips {
177
		private final Object2IntMap<GossipType> entries = new Object2IntOpenHashMap<>();
178
179
		public int weightedValue(final Predicate<GossipType> types) {
180
			return this.entries.object2IntEntrySet().stream().filter(e -> types.test(e.getKey())).mapToInt(e -> e.getIntValue() * e.getKey().weight).sum();
181
		}
182
183
		public Stream<GossipContainer.GossipEntry> unpack(final UUID target) {
184
			return this.entries.object2IntEntrySet().stream().map(e -> new GossipContainer.GossipEntry(target, e.getKey(), e.getIntValue()));
185
		}
186
187
		public void decay() {
188
			ObjectIterator<Entry<GossipType>> it = this.entries.object2IntEntrySet().iterator();
189
190
			while (it.hasNext()) {
191
				Entry<GossipType> gossip = it.next();
192
				int newValue = gossip.getIntValue() - gossip.getKey().decayPerDay;
193
				if (newValue < 2) {
194
					it.remove();
195
				} else {
196
					gossip.setValue(newValue);
197
				}
198
			}
199
		}
200
201
		public boolean isEmpty() {
202
			return this.entries.isEmpty();
203
		}
204
205
		public void makeSureValueIsntTooLowOrTooHigh(final GossipType type) {
206
			int value = this.entries.getInt(type);
207
			if (value > type.max) {
208
				this.entries.put(type, type.max);
209
			}
210
211
			if (value < 2) {
212
				this.remove(type);
213
			}
214
		}
215
216
		public void remove(final GossipType type) {
217
			this.entries.removeInt(type);
218
		}
219
	}
220
221
	private record GossipEntry(UUID target, GossipType type, int value) {
222
		public static final Codec<GossipContainer.GossipEntry> CODEC = RecordCodecBuilder.create(
223
			i -> i.group(
224
					UUIDUtil.CODEC.fieldOf("Target").forGetter(GossipContainer.GossipEntry::target),
225
					GossipType.CODEC.fieldOf("Type").forGetter(GossipContainer.GossipEntry::type),
226
					ExtraCodecs.POSITIVE_INT.fieldOf("Value").forGetter(GossipContainer.GossipEntry::value)
227
				)
228
				.apply(i, GossipContainer.GossipEntry::new)
229
		);
230
231
		public int weightedValue() {
232
			return this.value * this.type.weight;
233
		}
234
	}
235
}
236

Decompiled Minecraft 26.3 source of GossipContainer (net/minecraft/world/entity/ai/gossip/GossipContainer.java) with Mojang official mappings, stored verbatim on the site.