FilterMask .java

net.minecraft.network.chat.FilterMask (class): members: applyWithFormatting, FILTERED_STYLE, FULLY_FILTERED, PASS_THROUGH, setFiltered, HASH, mask, Type.

net.minecraft.network.chatclass168 linescommon5.3 KB

API Surface (6)

void setFiltered(final int index)
boolean isEmpty()
boolean isFullyFiltered()
boolean equals(final Object o)
int hashCode()
String getSerializedName()

Fields & Constants (8)

CODEC
STREAM_CODEC
FULLY_FILTERED
PASS_THROUGH
FILTERED_STYLE
HASH
mask
type

String literals (4)

"chat.filtered"
"pass_through"
"fully_filtered"
"partially_filtered"

Full Source

FilterMask.java168 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.network.chat;
2
3
import com.google.common.base.Suppliers;
4
import com.mojang.serialization.Codec;
5
import com.mojang.serialization.MapCodec;
6
import io.netty.buffer.ByteBuf;
7
import java.util.BitSet;
8
import java.util.function.IntFunction;
9
import java.util.function.Supplier;
10
import net.minecraft.ChatFormatting;
11
import net.minecraft.network.codec.ByteBufCodecs;
12
import net.minecraft.network.codec.StreamCodec;
13
import net.minecraft.util.ByIdMap;
14
import net.minecraft.util.ExtraCodecs;
15
import net.minecraft.util.StringRepresentable;
16
import org.apache.commons.lang3.StringUtils;
17
import org.jspecify.annotations.Nullable;
18
19
public class FilterMask {
20
	public static final Codec<FilterMask> CODEC = StringRepresentable.fromEnum(FilterMask.Type::values).dispatch(FilterMask::type, FilterMask.Type::codec);
21
	public static final StreamCodec<ByteBuf, FilterMask> STREAM_CODEC = FilterMask.Type.STREAM_CODEC.dispatch(FilterMask::type, FilterMask.Type::streamCodec);
22
	public static final FilterMask FULLY_FILTERED = new FilterMask(new BitSet(0), FilterMask.Type.FULLY_FILTERED);
23
	public static final FilterMask PASS_THROUGH = new FilterMask(new BitSet(0), FilterMask.Type.PASS_THROUGH);
24
	public static final Style FILTERED_STYLE = Style.EMPTY
25
		.withColor(ChatFormatting.DARK_GRAY)
26
		.withHoverEvent(new HoverEvent.ShowText(Component.translatable("chat.filtered")));
27
	private static final char HASH = '#';
28
	private final BitSet mask;
29
	private final FilterMask.Type type;
30
31
	private FilterMask(final BitSet mask, final FilterMask.Type type) {
32
		this.mask = mask;
33
		this.type = type;
34
	}
35
36
	private FilterMask(final BitSet mask) {
37
		this.mask = mask;
38
		this.type = FilterMask.Type.PARTIALLY_FILTERED;
39
	}
40
41
	public FilterMask(final int length) {
42
		this(new BitSet(length), FilterMask.Type.PARTIALLY_FILTERED);
43
	}
44
45
	private FilterMask.Type type() {
46
		return this.type;
47
	}
48
49
	private BitSet mask() {
50
		return this.mask;
51
	}
52
53
	public void setFiltered(final int index) {
54
		this.mask.set(index);
55
	}
56
57
	public @Nullable String apply(final String text) {
58
		return switch (this.type) {
59
			case PASS_THROUGH -> text;
60
			case FULLY_FILTERED -> null;
61
			case PARTIALLY_FILTERED -> {
62
				char[] chars = text.toCharArray();
63
64
				for (int i = 0; i < chars.length && i < this.mask.length(); i++) {
65
					if (this.mask.get(i)) {
66
						chars[i] = '#';
67
					}
68
				}
69
70
				yield new String(chars);
71
			}
72
		};
73
	}
74
75
	public @Nullable Component applyWithFormatting(final String text) {
76
		return switch (this.type) {
77
			case PASS_THROUGH -> Component.literal(text);
78
			case FULLY_FILTERED -> null;
79
			case PARTIALLY_FILTERED -> {
80
				MutableComponent result = Component.empty();
81
				int previousIndex = 0;
82
				boolean filtered = this.mask.get(0);
83
84
				while (true) {
85
					int nextIndex = filtered ? this.mask.nextClearBit(previousIndex) : this.mask.nextSetBit(previousIndex);
86
					nextIndex = nextIndex < 0 ? text.length() : nextIndex;
87
					if (nextIndex == previousIndex) {
88
						yield result;
89
					}
90
91
					if (filtered) {
92
						result.append(Component.literal(StringUtils.repeat('#', nextIndex - previousIndex)).withStyle(FILTERED_STYLE));
93
					} else {
94
						result.append(text.substring(previousIndex, nextIndex));
95
					}
96
97
					filtered = !filtered;
98
					previousIndex = nextIndex;
99
				}
100
			}
101
		};
102
	}
103
104
	public boolean isEmpty() {
105
		return this.type == FilterMask.Type.PASS_THROUGH;
106
	}
107
108
	public boolean isFullyFiltered() {
109
		return this.type == FilterMask.Type.FULLY_FILTERED;
110
	}
111
112
	@Override
113
	public boolean equals(final Object o) {
114
		if (this == o) {
115
			return true;
116
		} else if (o != null && this.getClass() == o.getClass()) {
117
			FilterMask that = (FilterMask)o;
118
			return this.mask.equals(that.mask) && this.type == that.type;
119
		} else {
120
			return false;
121
		}
122
	}
123
124
	@Override
125
	public int hashCode() {
126
		int result = this.mask.hashCode();
127
		return 31 * result + this.type.hashCode();
128
	}
129
130
	private enum Type implements StringRepresentable {
131
		PASS_THROUGH(0, "pass_through", () -> MapCodec.unit(FilterMask.PASS_THROUGH), () -> StreamCodec.unit(FilterMask.PASS_THROUGH)),
132
		FULLY_FILTERED(1, "fully_filtered", () -> MapCodec.unit(FilterMask.FULLY_FILTERED), () -> StreamCodec.unit(FilterMask.FULLY_FILTERED)),
133
		PARTIALLY_FILTERED(
134
			2,
135
			"partially_filtered",
136
			() -> ExtraCodecs.BIT_SET.xmap(FilterMask::new, FilterMask::mask).fieldOf("value"),
137
			() -> ByteBufCodecs.BIT_SET.map(FilterMask::new, FilterMask::mask)
138
		);
139
140
		private static final IntFunction<FilterMask.Type> ID_MAP = ByIdMap.continuous(t -> t.id, values(), ByIdMap.OutOfBoundsStrategy.ZERO);
141
		public static final StreamCodec<ByteBuf, FilterMask.Type> STREAM_CODEC = ByteBufCodecs.idMapper(ID_MAP, t -> t.id);
142
		private final int id;
143
		private final String serializedName;
144
		private final Supplier<MapCodec<FilterMask>> codec;
145
		private final Supplier<StreamCodec<ByteBuf, FilterMask>> streamCodec;
146
147
		Type(final int id, final String serializedName, final Supplier<MapCodec<FilterMask>> codec, final Supplier<StreamCodec<ByteBuf, FilterMask>> streamCodec) {
148
			this.id = id;
149
			this.serializedName = serializedName;
150
			this.codec = Suppliers.memoize(codec::get);
151
			this.streamCodec = Suppliers.memoize(streamCodec::get);
152
		}
153
154
		@Override
155
		public String getSerializedName() {
156
			return this.serializedName;
157
		}
158
159
		private MapCodec<FilterMask> codec() {
160
			return this.codec.get();
161
		}
162
163
		private StreamCodec<ByteBuf, FilterMask> streamCodec() {
164
			return this.streamCodec.get();
165
		}
166
	}
167
}
168

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