Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions fabric-debug-api-v1/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version = getSubprojectVersion(project)

moduleDependencies(project, [
'fabric-api-base'
])

testDependencies(project, [
])

loom {
accessWidenerPath = file("src/main/resources/fabric-debug-api-v1.accesswidener")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.api.debug.v1.client;

import java.util.Objects;

import net.minecraft.util.debug.DebugSubscription;

import net.fabricmc.fabric.impl.debug.client.ClientDebugSubscriptionRegistryImpl;

/// A registry for [debug subscriptions][DebugSubscription] on the client,
/// allowing listening to registered debug subscriptions on the client.
public final class ClientDebugSubscriptionRegistry {
/// Registers a [DebugSubscription] on the client.
///
/// **Note:** this will register **outside development environments** if it
/// is not checked. Surround calls to this method with
/// [net.fabricmc.loader.api.FabricLoader#isDevelopmentEnvironment] if you
/// do not intend for a debug feature to be present in production.
///
/// @param <T> the inner type of the [DebugSubscription].
/// @param debugSubscription the [DebugSubscription] to register.
public static <T> void register(DebugSubscription<T> debugSubscription) {
Objects.requireNonNull(debugSubscription);
ClientDebugSubscriptionRegistryImpl.register(debugSubscription);
}

/// Registers a [DebugSubscription] on the client if the `isEnabledFlag`
/// parameter is `true`.
///
/// **Note:** this will register **outside development environments** if it
/// is not checked. Surround calls to this method with
/// [net.fabricmc.loader.api.FabricLoader#isDevelopmentEnvironment] if you
/// do not intend for a debug feature to be present in production.
///
/// @param <T> the inner type of the [DebugSubscription].
/// @param debugSubscription the [DebugSubscription] to register.
/// @param isEnabledFlag the flag determining whether to register this
/// [DebugSubscription].
public static <T> void register(
DebugSubscription<T> debugSubscription,
boolean isEnabledFlag
) {
if (isEnabledFlag) {
register(debugSubscription);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.api.debug.v1.client.renderer;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.debug.DebugRenderer;

/// A constructor for a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I would never call or describe an interface as "a constructor", this word has already a very specific meaning in Java, and we should stick to it.
This is a factory interface, and should be described as such.

We could instead say A factory to create a [debug subscription][...] renderer. instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to fix this when I get time (probably this Saturday EST) as I have university starting tomorrow.

/// [debug subscription][net.minecraft.util.debug.DebugSubscription] renderer.
@FunctionalInterface
public interface DebugRendererFactory {
/// @return a new renderer for a
/// [debug subscription][net.minecraft.util.debug.DebugSubscription].
DebugRenderer.SimpleDebugRenderer create(Minecraft minecraft);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.api.debug.v1.client.renderer;

import java.util.Objects;

import net.minecraft.util.debug.DebugSubscription;

import net.fabricmc.fabric.impl.debug.client.renderer.DebugRendererRegistryImpl;

/// Registry for custom
/// [debug renderers][net.minecraft.client.renderer.debug.DebugRenderer.SimpleDebugRenderer].
public final class DebugRendererRegistry {
/// Registers a debug renderer for the given [DebugSubscription].
///
/// @param <T> the inner type of the [DebugSubscription].
/// @param debugSubscription the [DebugSubscription].
/// @param rendererFactory the factory/constructor for the debug renderer.
public static <T> void register(
DebugSubscription<T> debugSubscription,
DebugRendererFactory rendererFactory
) {
Objects.requireNonNull(debugSubscription);
DebugRendererRegistryImpl.register(debugSubscription, rendererFactory);
}

/// Registers a debug renderer for the given [DebugSubscription] if
/// `isEnabledFlag` is `true`.
///
/// @param <T> the inner type of the [DebugSubscription].
/// @param debugSubscription the [DebugSubscription].
/// @param rendererFactory the factory/constructor for the debug renderer.
/// @param isEnabledFlag the flag determining whether to register this debug
/// renderer.
public static <T> void register(
DebugSubscription<T> debugSubscription,
DebugRendererFactory rendererFactory,
boolean isEnabledFlag
) {
if (isEnabledFlag) {
register(debugSubscription, rendererFactory);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.impl.debug.client;

import java.util.HashSet;
import java.util.Set;

import net.minecraft.util.debug.DebugSubscription;

public final class ClientDebugSubscriptionRegistryImpl {
public static final Set<DebugSubscription<?>> DEBUG_SUBSCRIPTIONS = new HashSet<>();

public static <T> void register(DebugSubscription<T> debugSubscription) {
DEBUG_SUBSCRIPTIONS.add(debugSubscription);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.impl.debug.client.renderer;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import net.minecraft.util.debug.DebugSubscription;

import net.fabricmc.fabric.api.debug.v1.client.renderer.DebugRendererFactory;

public final class DebugRendererRegistryImpl {
public static final Set<Entry> RENDERERS = new HashSet<>();

public static <T> void register(
DebugSubscription<T> debugSubscription,
DebugRendererFactory rendererFactory
) {
RENDERERS.add(new Entry(debugSubscription, rendererFactory));
}

public record Entry(
DebugSubscription<?> debugSubscription,
DebugRendererFactory rendererFactory
) {
// Ensure DebugSubscriptions with different values don't both
// get into the Set, causing undesirable behavior

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Entry entry = (Entry) o;
return Objects.equals(
debugSubscription,
entry.debugSubscription
);
}

@Override
public int hashCode() {
return Objects.hashCode(debugSubscription);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.mixin.debug.client;

import java.util.Set;

import com.llamalad7.mixinextras.sugar.Local;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import net.minecraft.client.multiplayer.ClientDebugSubscriber;
import net.minecraft.util.debug.DebugSubscription;

import net.fabricmc.fabric.impl.debug.client.ClientDebugSubscriptionRegistryImpl;

@Mixin(ClientDebugSubscriber.class)
public abstract class ClientDebugSubscriberMixin {
@Inject(
method = "requestedSubscriptions",
at = @At("RETURN")
)
private void addSubscribers(
CallbackInfoReturnable<Set<DebugSubscription<?>>> cir,
@Local(name = "subscriptions") Set<DebugSubscription<?>> subscriptions
) {
subscriptions.addAll(ClientDebugSubscriptionRegistryImpl.DEBUG_SUBSCRIPTIONS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.mixin.debug.client;

import java.util.List;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.debug.DebugRenderer;

import net.fabricmc.fabric.impl.debug.client.renderer.DebugRendererRegistryImpl;

@Mixin(DebugRenderer.class)
public abstract class DebugRendererMixin {
@Shadow
@Final
private List<DebugRenderer.SimpleDebugRenderer> renderers;

private DebugRendererMixin() {
}

@Inject(method = "refreshRendererList", at = @At("RETURN"))
private void registerRenderers(CallbackInfo ci) {
Minecraft minecraft = Minecraft.getInstance();

for (DebugRendererRegistryImpl.Entry entry : DebugRendererRegistryImpl.RENDERERS) {
// a Stream#map would make the most sense here, but they're banned
// so you have to suffer with me now
renderers.add(entry.rendererFactory().create(minecraft));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"required": true,
"package": "net.fabricmc.fabric.mixin.debug.client",
"compatibilityLevel": "JAVA_25",
"client": [
"ClientDebugSubscriberMixin",
"DebugRendererMixin"
],
"injectors": {
"defaultRequire": 1
},
"overwrites": {
"requireAnnotations": true
},
"mixinextras": {
"minVersion": "0.5.0"
}
}
Loading
Loading