Skip to content

Commit 867ad61

Browse files
committed
implements fabric-debug-api-v1 api
1 parent 448f73d commit 867ad61

4 files changed

Lines changed: 198 additions & 1 deletion

File tree

tenet-server/minecraft-patches/sources/net/minecraft/world/entity/Entity.java.patch

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
--- a/net/minecraft/world/entity/Entity.java
22
+++ b/net/minecraft/world/entity/Entity.java
3-
@@ -29,6 +_,8 @@
3+
@@ -29,6 +_,9 @@
44
import java.util.function.Predicate;
55
import java.util.stream.Stream;
66
import javax.annotation.Nullable;
77
+
88
+import net.fabricmc.fabric.api.entity.event.v1.ServerEntityLevelChangeEvents;
9+
+import net.fabricmc.fabric.impl.debug.EntityDebugSubscriptionRegistryImpl;
910
import net.minecraft.BlockUtil;
1011
import net.minecraft.CrashReport;
1112
import net.minecraft.CrashReportCategory;
@@ -22,3 +23,11 @@
2223
} else {
2324
return null;
2425
}
26+
@@ -5320,6 +_,7 @@
27+
28+
@Override
29+
public void registerDebugValues(ServerLevel level, DebugValueSource.Registration registrar) {
30+
+ EntityDebugSubscriptionRegistryImpl.addDebugValues(this, registrar);
31+
}
32+
33+
record EntityPathElement(Entity entity) implements ProblemReporter.PathElement {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.fabricmc.fabric.api.debug.v1;
18+
19+
/// A factory that creates a debug value of type [T] using data of type [D].
20+
///
21+
/// @param <D> the data passed for construction
22+
/// (e.g. [net.minecraft.world.entity.Entity]).
23+
/// @param <T> the debug value being constructed.
24+
@FunctionalInterface
25+
public interface DebugValueFactory<D, T> {
26+
T create(D data);
27+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.fabricmc.fabric.api.debug.v1;
18+
19+
import java.util.Objects;
20+
import java.util.function.Predicate;
21+
22+
import net.minecraft.util.debug.DebugSubscription;
23+
import net.minecraft.world.entity.Entity;
24+
25+
import net.fabricmc.fabric.impl.debug.EntityDebugSubscriptionRegistryImpl;
26+
27+
/// A server-side registry for [debug subscriptions][DebugSubscription] specific
28+
/// to entities or properties of entities.
29+
public final class EntityDebugSubscriptionRegistry {
30+
/// Registers a [DebugSubscription] based on a given [Entity] and
31+
/// [Predicate].
32+
///
33+
/// @param <T> the inner type of the [DebugSubscription].
34+
/// @param <E> the type of [Entity] to check against.
35+
/// @param debugSubscription the [DebugSubscription].
36+
/// @param shouldSubscribe whether an [Entity] should subscribe to this
37+
/// [DebugSubscription].
38+
/// @param valueFactory the factory for the value of type [T].
39+
public static <T, E extends Entity> void register(
40+
DebugSubscription<T> debugSubscription,
41+
Predicate<Entity> shouldSubscribe,
42+
DebugValueFactory<E, T> valueFactory
43+
) {
44+
Objects.requireNonNull(debugSubscription);
45+
EntityDebugSubscriptionRegistryImpl.register(
46+
debugSubscription,
47+
shouldSubscribe,
48+
valueFactory
49+
);
50+
}
51+
52+
/// Registers a [DebugSubscription] based on a given [Entity] and
53+
/// [Predicate] if `isEnabledFlag` is `true`.
54+
///
55+
/// @param <T> the inner type of the [DebugSubscription].
56+
/// @param <E> the type of [Entity] to check against.
57+
/// @param debugSubscription the [DebugSubscription].
58+
/// @param shouldSubscribe whether an [Entity] should subscribe to this
59+
/// [DebugSubscription].
60+
/// @param valueFactory the factory for the value of type [T].
61+
/// @param isEnabledFlag the flag determining whether to register this
62+
/// [DebugSubscription].
63+
public static <T, E extends Entity> void register(
64+
DebugSubscription<T> debugSubscription,
65+
Predicate<Entity> shouldSubscribe,
66+
DebugValueFactory<E, T> valueFactory,
67+
boolean isEnabledFlag
68+
) {
69+
if (isEnabledFlag) {
70+
register(
71+
debugSubscription,
72+
shouldSubscribe,
73+
valueFactory
74+
);
75+
}
76+
}
77+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.fabricmc.fabric.impl.debug;
18+
19+
import java.util.HashSet;
20+
import java.util.Objects;
21+
import java.util.Set;
22+
import java.util.function.Predicate;
23+
24+
import net.minecraft.util.debug.DebugSubscription;
25+
import net.minecraft.util.debug.DebugValueSource;
26+
import net.minecraft.world.entity.Entity;
27+
28+
import net.fabricmc.fabric.api.debug.v1.DebugValueFactory;
29+
30+
public final class EntityDebugSubscriptionRegistryImpl {
31+
public static final Set<Entry> ENTITY_DEBUG_SUBSCRIPTIONS =
32+
new HashSet<>();
33+
34+
public static <T, E extends Entity> void register(
35+
DebugSubscription<T> debugSubscription,
36+
Predicate<Entity> shouldSubscribe,
37+
DebugValueFactory<E, T> valueFactory
38+
) {
39+
//noinspection unchecked // Casts to super-type
40+
ENTITY_DEBUG_SUBSCRIPTIONS.add(new Entry(
41+
(DebugSubscription<Object>) debugSubscription,
42+
shouldSubscribe,
43+
(DebugValueFactory<Entity, T>) valueFactory
44+
));
45+
}
46+
47+
public static void addDebugValues(
48+
Object entity,
49+
DebugValueSource.Registration registration
50+
) {
51+
for (Entry entry : ENTITY_DEBUG_SUBSCRIPTIONS) {
52+
if (entry.shouldSubscribe().test((Entity) entity)) {
53+
registration.register(
54+
entry.debugSubscription(),
55+
() -> entry.valueFactory().create((Entity) entity)
56+
);
57+
}
58+
}
59+
}
60+
61+
public record Entry(
62+
DebugSubscription<Object> debugSubscription,
63+
Predicate<Entity> shouldSubscribe,
64+
DebugValueFactory<Entity, ?> valueFactory
65+
) {
66+
// Ensure DebugSubscriptions with different values don't both
67+
// get into the Set, causing undesirable behavior
68+
69+
@Override
70+
public boolean equals(Object o) {
71+
if (o == null || getClass() != o.getClass()) return false;
72+
Entry entry = (Entry) o;
73+
return Objects.equals(
74+
debugSubscription,
75+
entry.debugSubscription
76+
);
77+
}
78+
79+
@Override
80+
public int hashCode() {
81+
return Objects.hash(debugSubscription);
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)