Skip to content

Commit

Permalink
Add @nullable and friends (FabricMC#1007)
Browse files Browse the repository at this point in the history
* Boost to Loom 0.5
Add null related annotations

* Rearrange nullable to be overline

* Fix anno sorting for picky

* Add imports

* Address feedback

* Fix import
  • Loading branch information
dexman545 authored Sep 27, 2020
1 parent ca18475 commit 5f10696
Show file tree
Hide file tree
Showing 32 changed files with 118 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package net.fabricmc.fabric.api.block;

import org.jetbrains.annotations.Nullable;

import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
Expand All @@ -24,5 +26,5 @@
import net.minecraft.world.BlockView;

public interface BlockPickInteractionAware {
ItemStack getPickedStack(BlockState state, BlockView view, BlockPos pos, /* nullable */ PlayerEntity player, /* nullable */ HitResult result);
ItemStack getPickedStack(BlockState state, BlockView view, BlockPos pos, @Nullable PlayerEntity player, @Nullable HitResult result);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package net.fabricmc.fabric.api.entity;

import org.jetbrains.annotations.Nullable;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.hit.HitResult;

public interface EntityPickInteractionAware {
ItemStack getPickedStack(/* nullable */ PlayerEntity player, /* nullable */ HitResult result);
ItemStack getPickedStack(@Nullable PlayerEntity player, @Nullable HitResult result);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package net.fabricmc.fabric.api.event.player;

import org.jetbrains.annotations.Nullable;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ActionResult;
Expand Down Expand Up @@ -50,5 +52,5 @@ public interface AttackEntityCallback {
}
);

ActionResult interact(PlayerEntity player, World world, Hand hand, Entity entity, /* Nullable */ EntityHitResult hitResult);
ActionResult interact(PlayerEntity player, World world, Hand hand, Entity entity, @Nullable EntityHitResult hitResult);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package net.fabricmc.fabric.api.event.player;

import org.jetbrains.annotations.Nullable;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ActionResult;
Expand Down Expand Up @@ -50,5 +52,5 @@ public interface UseEntityCallback {
}
);

ActionResult interact(PlayerEntity player, World world, Hand hand, Entity entity, /* Nullable */ EntityHitResult hitResult);
ActionResult interact(PlayerEntity player, World world, Hand hand, Entity entity, @Nullable EntityHitResult hitResult);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import org.jetbrains.annotations.Nullable;

import net.minecraft.server.MinecraftServer;
import net.minecraft.world.GameRules;
Expand Down Expand Up @@ -140,7 +141,7 @@ public static GameRules.Type<GameRules.IntRule> createIntRule(int defaultValue,
* @param changedCallback a callback that is invoked when the value of a game rule has changed
* @return an integer rule type
*/
public static GameRules.Type<GameRules.IntRule> createIntRule(int defaultValue, int minimumValue, int maximumValue, /* @Nullable */ BiConsumer<MinecraftServer, GameRules.IntRule> changedCallback) {
public static GameRules.Type<GameRules.IntRule> createIntRule(int defaultValue, int minimumValue, int maximumValue, @Nullable BiConsumer<MinecraftServer, GameRules.IntRule> changedCallback) {
return new GameRules.Type<>(
() -> IntegerArgumentType.integer(minimumValue, maximumValue),
type -> new BoundedIntRule(type, defaultValue, minimumValue, maximumValue), // Internally use a bounded int rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.mojang.brigadier.context.CommandContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;

import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
Expand Down Expand Up @@ -144,7 +145,7 @@ public boolean supports(E value) {
return this.supportedValues.contains(value);
}

public void set(E value, /* @Nullable */ MinecraftServer server) throws IllegalArgumentException {
public void set(E value, @Nullable MinecraftServer server) throws IllegalArgumentException {
checkNotNull(value);

if (!this.supports(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package net.fabricmc.fabric.impl.gamerule;

import org.jetbrains.annotations.Nullable;

import net.fabricmc.fabric.api.gamerule.v1.CustomGameRuleCategory;

public interface RuleKeyExtensions {
/* @Nullable */
@Nullable
CustomGameRuleCategory fabric_getCustomCategory();

void fabric_setCustomCategory(CustomGameRuleCategory customCategory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package net.fabricmc.fabric.mixin.gamerule;

import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;

Expand All @@ -26,8 +27,8 @@

@Mixin(GameRules.Key.class)
public abstract class RuleKeyMixin implements RuleKeyExtensions {
/* @Nullable */
@Unique
@Nullable
private CustomGameRuleCategory customCategory;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package net.fabricmc.fabric.api.client.model;

import org.jetbrains.annotations.Nullable;

import net.minecraft.client.render.model.UnbakedModel;
import net.minecraft.util.Identifier;

Expand Down Expand Up @@ -46,5 +48,6 @@ public interface ModelResourceProvider {
* @return The loaded UnbakedModel, or null if this ModelResourceProvider doesn't handle a specific Identifier
* (or if there was no error!).
*/
/* @Nullable */ UnbakedModel loadModelResource(Identifier resourceId, ModelProviderContext context) throws ModelProviderException;
@Nullable
UnbakedModel loadModelResource(Identifier resourceId, ModelProviderContext context) throws ModelProviderException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package net.fabricmc.fabric.api.client.model;

import org.jetbrains.annotations.Nullable;

import net.minecraft.client.render.model.UnbakedModel;
import net.minecraft.client.util.ModelIdentifier;

Expand Down Expand Up @@ -43,5 +45,6 @@ public interface ModelVariantProvider {
* @return The loaded UnbakedModel, or null if this ModelVariantProvider doesn't handle a specific Identifier
* (or if there was no error!).
*/
/* @Nullable */ UnbakedModel loadModelVariant(ModelIdentifier modelId, ModelProviderContext context) throws ModelProviderException;
@Nullable
UnbakedModel loadModelVariant(ModelIdentifier modelId, ModelProviderContext context) throws ModelProviderException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.common.collect.Lists;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;

import net.minecraft.client.render.model.ModelLoader;
import net.minecraft.client.render.model.UnbakedModel;
Expand Down Expand Up @@ -139,12 +140,12 @@ private <T> UnbakedModel loadCustomModel(CustomModelItf<T> function, Collection<
}
}

/* @Nullable */
@Nullable
public UnbakedModel loadModelFromResource(Identifier resourceId) {
return loadCustomModel((r) -> r.loadModelResource(resourceId, this), modelResourceProviders, "resource provider");
}

/* @Nullable */
@Nullable
public UnbakedModel loadModelFromVariant(Identifier variantId) {
if (!(variantId instanceof ModelIdentifier)) {
return loadModelFromResource(variantId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkState;

import com.google.common.collect.ImmutableSet;
import org.jetbrains.annotations.Nullable;

import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
Expand Down Expand Up @@ -46,14 +47,15 @@ public final class VillagerProfessionBuilder {
private final ImmutableSet.Builder<Block> secondaryJobSiteBlockBuilder = ImmutableSet.builder();
private Identifier identifier;
private PointOfInterestType pointOfInterestType;
/* @Nullable */
@Nullable
private SoundEvent workSoundEvent;

private VillagerProfessionBuilder() {
}

/**
* Creates a builder instance to allow for creation of a {@link VillagerProfession}.
*
* @return A new builder.
*/
public static VillagerProfessionBuilder create() {
Expand Down Expand Up @@ -140,7 +142,7 @@ public VillagerProfessionBuilder secondaryJobSites(Iterable<Block> blocks) {
* @param workSoundEvent The {@link SoundEvent} to be played.
* @return this builder.
*/
public VillagerProfessionBuilder workSound(/* @Nullable */ SoundEvent workSoundEvent) {
public VillagerProfessionBuilder workSound(@Nullable SoundEvent workSoundEvent) {
this.workSoundEvent = workSoundEvent;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.ArrayList;
import java.util.List;

import org.jetbrains.annotations.Nullable;

import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.tag.Tag;
Expand Down Expand Up @@ -56,7 +58,8 @@ public static void onBuild(Block.Settings settings, Block block) {

public static final class ExtraData {
private final List<MiningLevel> miningLevels = new ArrayList<>();
/* @Nullable */ private Boolean breakByHand;
@Nullable
private Boolean breakByHand;

public ExtraData(Block.Settings settings) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package net.fabricmc.fabric.mixin.object.builder;

import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;
import com.google.common.collect.ImmutableSet;
Expand All @@ -29,7 +30,7 @@
@Mixin(VillagerProfession.class)
public interface VillagerProfessionAccessor {
@Invoker("<init>")
static VillagerProfession create(String id, PointOfInterestType type, ImmutableSet<Item> gatherableItems, ImmutableSet<Block> secondaryJobSites, /* @Nullable */ SoundEvent soundEvent) {
static VillagerProfession create(String id, PointOfInterestType type, ImmutableSet<Item> gatherableItems, ImmutableSet<Block> secondaryJobSites, @Nullable SoundEvent soundEvent) {
throw new AssertionError("Untransformed accessor!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.Packet;
Expand Down Expand Up @@ -105,7 +106,8 @@ public static void receivePacket(PacketContext context, PacketByteBuf buf, boole
* @param activeTag contains the registry ids that were previously read and applied, can be null.
* @return a {@link CompoundTag} to save or sync, null when empty
*/
public static CompoundTag toTag(boolean isClientSync, CompoundTag activeTag) {
@Nullable
public static CompoundTag toTag(boolean isClientSync, @Nullable CompoundTag activeTag) {
CompoundTag mainTag = new CompoundTag();

for (Identifier registryId : Registry.REGISTRIES.getIds()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

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

import org.jetbrains.annotations.Nullable;

import net.minecraft.util.Identifier;

import net.fabricmc.fabric.api.renderer.v1.material.MaterialFinder;
Expand Down Expand Up @@ -50,6 +52,7 @@ public interface Renderer {
* Return a material previously registered via {@link #registerMaterial(Identifier, RenderMaterial)}.
* Will return null if no material was found matching the given identifier.
*/
@Nullable
RenderMaterial materialById(Identifier id);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

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

import org.jetbrains.annotations.Nullable;

import net.fabricmc.fabric.impl.renderer.RendererAccessImpl;

/**
Expand All @@ -37,6 +39,7 @@ public interface RendererAccess {
* Access to the current {@link Renderer} for creating and retrieving model builders
* and materials. Will return null if no render plug in is active.
*/
@Nullable
Renderer getRenderer();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package net.fabricmc.fabric.api.renderer.v1.mesh;

import org.jetbrains.annotations.Nullable;

import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.client.texture.Sprite;
import net.minecraft.client.util.math.Vector3f;
Expand Down Expand Up @@ -115,7 +117,8 @@ public interface MutableQuadView extends QuadView {
* is computed based on face geometry and must be non-null in vanilla quads.
* That computed value is returned by {@link #lightFace()}.
*/
MutableQuadView cullFace(Direction face);
@Nullable
MutableQuadView cullFace(@Nullable Direction face);

/**
* Provides a hint to renderer about the facing of this quad. Not required,
Expand All @@ -130,6 +133,7 @@ public interface MutableQuadView extends QuadView {
* <p>Note: This value is not persisted independently when the quad is encoded.
* When reading encoded quads, this value will always be the same as {@link #lightFace()}.
*/
@Nullable
MutableQuadView nominalFace(Direction face);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package net.fabricmc.fabric.api.renderer.v1.mesh;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.client.texture.Sprite;
Expand Down Expand Up @@ -77,6 +80,7 @@ public interface QuadView {
* calculations and will be the block face to which the quad is most closely aligned. Always
* the same as cull face for quads that are on a block face, but never null.
*/
@NotNull
Direction lightFace();

/**
Expand All @@ -85,7 +89,7 @@ public interface QuadView {
*
* @see MutableQuadView#cullFace(Direction)
*/
Direction cullFace();
@Nullable Direction cullFace();

/**
* See {@link MutableQuadView#nominalFace(Direction)}.
Expand Down Expand Up @@ -135,7 +139,7 @@ default BakedQuad toBakedQuad(int spriteIndex, Sprite sprite, boolean isItem) {
* Pass a non-null target to avoid allocation - will be returned with values.
* Otherwise returns a new instance.
*/
Vector3f copyPos(int vertexIndex, Vector3f target);
Vector3f copyPos(int vertexIndex, @Nullable Vector3f target);

/**
* Convenience: access x, y, z by index 0-2.
Expand Down Expand Up @@ -167,7 +171,8 @@ default BakedQuad toBakedQuad(int spriteIndex, Sprite sprite, boolean isItem) {
* Pass a non-null target to avoid allocation - will be returned with values.
* Otherwise returns a new instance. Returns null if normal not present.
*/
Vector3f copyNormal(int vertexIndex, Vector3f target);
@Nullable
Vector3f copyNormal(int vertexIndex, @Nullable Vector3f target);

/**
* Will return {@link Float#NaN} if normal not present.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;

import com.google.common.collect.ImmutableList;
import org.jetbrains.annotations.Contract;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.model.BakedQuad;
Expand Down Expand Up @@ -56,6 +57,7 @@ public static int toFaceIndex(Direction face) {
* optionally including the null face. (Use &lt; or &lt;= {@link #NULL_FACE_ID}
* to exclude or include the null value, respectively.)
*/
@Contract("null -> null")
public static Direction faceFromIndex(int faceIndex) {
return FACES[faceIndex];
}
Expand Down
Loading

0 comments on commit 5f10696

Please sign in to comment.