quilt-loader icon indicating copy to clipboard operation
quilt-loader copied to clipboard

Add quilt's environment annotations, and deprecate fabric's.

Open AlexIIL opened this issue 3 years ago • 2 comments

Implements #53.

This uses specialized annotations for each side (since there are only two this shouldn't be a problem) rather than using the EnvType enum. This also means the names are shorter:

@Environment(EnvType.CLIENT)
@Environment(EnvType.SERVER)
@EnvironmentInterface(value = EnvType.CLIENT, itf = IRenderable.class)

now becomes:

@ClientOnly
@DedicatedServerOnly
@ClientOnlyInterface(IRenderable.class)

Both @ClientOnly and @DedicatedServerOnly also have a stripLambdas property (defaulting to true), which controls whether or not to also remove methods referenced in only those methods via lambdas. (This allows mods to disable lambda stripping if they don't want it for a particular method, although I'm not sure why).

This leaves only a single non-deprecated class in net.fabricmc.api: EnvType, which this PR doesn't touch. Fabric's annotations are not processed by the new lambda-stripping functionality, to preserve compatibility.

AlexIIL avatar Apr 15 '22 04:04 AlexIIL

How would you implement side-specific behavior with this?

The old code would have been something like this:

switch (FabricLoader.getInstance().getEnvironment()) {
	EnvType.CLIENT -> LOGGER.info("We're on the client");
	EnvType.SERVER -> LOGGER.info("Hello server");
}

halotroop2288 avatar Jun 24 '22 17:06 halotroop2288

@halotroop2288 As mentioned in the OP, EnvType is not touched. So your example would be:

switch (MinecraftQuiltLoader.getEnvironmentType()) {
	EnvType.CLIENT -> LOGGER.info("We're on the client");
	EnvType.SERVER -> LOGGER.info("Hello server");
}

as it is currently and will still be after this PR.

triphora avatar Jun 24 '22 17:06 triphora

Leo brought up a useful suggestion on discord:

Leo, Agent of Cursed — Today at 10:33 man, it's too bad TYPE_USE annotations are weird we could've had public class ChestBlockEntity implements @ClientOnly ChestAnimationProgress

So, I'm going to add them since it makes a lot more sense to be able to do that than declare the interface twice. I'm not sure if this means I should remove @ClientOnlyInterface as well.

AlexIIL avatar Oct 13 '22 10:10 AlexIIL

LambdAurora brought up another useful suggestion:

Question then If we have a client only package Does annotating the package will strip all its classes?

Loader doesn't do this yet - but it probably should, so I'm going to try adding them to this PR too.

AlexIIL avatar Oct 13 '22 10:10 AlexIIL

As of https://github.com/QuiltMC/quilt-loader/pull/63/commits/a2e988759e4fb29ed6e7a5bc56fdf9889e756af8 this now strips annotated interfaces. For example:

@ClientOnlyInterface(IRenderable.class)
public class CentrifugeBlockEntity implements IRenderable {

}

can now be written as:

public class CentrifugeBlockEntity implements @ClientOnly IRenderable {

}

AlexIIL avatar Oct 13 '22 10:10 AlexIIL

Okay, annotating a package-info class with @ClientOnly or @DedicatedServerOnly now works.

I'm not sure if I should remove @ClientOnlyInterface, @ClientOnlyInterfaces, @DedicatedServerOnlyInterface and @DedicatedServerOnlyInterfaces, since those just add clutter at this point?

AlexIIL avatar Oct 13 '22 11:10 AlexIIL