Skip to content

Commit

Permalink
check all nullables
Browse files Browse the repository at this point in the history
closes #273

(cherry picked from commit 66102f2)

(cherry picked from commit 4af0959)

(cherry picked from commit 75948cb)
  • Loading branch information
deirn committed Jul 21, 2024
1 parent 3ff00e5 commit 1f2d4fa
Show file tree
Hide file tree
Showing 30 changed files with 65 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
public class ForgeApiService extends ApiService {

@Override
@SuppressWarnings("DataFlowIssue")
public IModInfo getModInfo(ItemStack stack) {
return ModInfo.get(stack.getItem().getCreatorModId(stack));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected void gatherPlugins() {

var satisfied = true;
for (var dep : required) {
satisfied &= ModList.get().isLoaded(dep);
satisfied = satisfied && ModList.get().isLoaded(dep);
}

if (side == IPluginInfo.Side.CLIENT && FMLLoader.getDist() != Dist.CLIENT) {
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/mcp/mobius/waila/access/ClientAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Nullable;

public enum ClientAccessor implements ICommonAccessor, IBlockAccessor, IEntityAccessor {

Expand All @@ -34,8 +35,8 @@ public enum ClientAccessor implements ICommonAccessor, IBlockAccessor, IEntityAc
private BlockState state = Blocks.AIR.defaultBlockState();
private BlockPos pos = BlockPos.ZERO;
private ResourceLocation blockRegistryName = Registry.ITEM.getDefaultKey();
private BlockEntity blockEntity;
private Entity entity;
private @Nullable BlockEntity blockEntity;
private @Nullable Entity entity;
private long timeLastUpdate = System.currentTimeMillis();
private ItemStack stack = ItemStack.EMPTY;
private int updateId;
Expand Down Expand Up @@ -77,7 +78,7 @@ public <T extends Entity> T getEntity() {
return (T) this.entity;
}

public void setEntity(Entity entity) {
public void setEntity(@Nullable Entity entity) {
this.entity = entity;
}

Expand Down Expand Up @@ -138,7 +139,7 @@ public double getPartialFrame() {
}

@Override
public Direction getSide() {
public @Nullable Direction getSide() {
return hitResult == null ? null : hitResult.getType() == HitResult.Type.ENTITY ? null : ((BlockHitResult) hitResult).getDirection();
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mcp/mobius/waila/config/ConfigEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ConfigEntry<T> {

private final Type<T> type;

private T serverValue;
private @Nullable T serverValue;
private T localValue;

private ConfigEntry(IPluginInfo origin, ResourceLocation id, T defaultValue, T clientOnlyValue, boolean serverRequired, boolean merged, Type<T> type) {
Expand Down Expand Up @@ -72,7 +72,7 @@ public T getLocalValue() {
return localValue;
}

public T getServerValue() {
public @Nullable T getServerValue() {
return serverValue;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/mcp/mobius/waila/config/PluginConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public String getString(ResourceLocation key) {
}

@Override
@SuppressWarnings("DataFlowIssue")
public <T extends Enum<T>> T getEnum(ResourceLocation key) {
return getValue(key, null);
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/mcp/mobius/waila/gui/hud/ComponentHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mcp.mobius.waila.gui.hud;

import java.util.Objects;

import io.netty.buffer.Unpooled;
import lol.bai.badpackets.api.PacketSender;
import mcp.mobius.waila.Waila;
Expand All @@ -17,11 +19,11 @@
import net.minecraft.client.Minecraft;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import org.jetbrains.annotations.Nullable;

public class ComponentHandler {

Expand All @@ -38,7 +40,7 @@ public static void requestBlockData(ClientAccessor accessor) {
accessor.resetTimer();
accessor.setDataAccess(false);
DataWriter.CLIENT.reset();
var player = Minecraft.getInstance().player;
var player = accessor.getPlayer();

for (var entry : registrar.blockDataCtx.get(block)) {
DataWriter.CLIENT.tryAppend(player, entry.instance(), accessor, PluginConfig.CLIENT, IBlockComponentProvider::appendDataContext);
Expand All @@ -62,7 +64,7 @@ public static void gatherBlock(ClientAccessor accessor, Tooltip tooltip, Tooltip
var blockEntity = accessor.getBlockEntity();

handleBlock(accessor, tooltip, block, position);
handleBlock(accessor, tooltip, blockEntity, position);
if (blockEntity != null) handleBlock(accessor, tooltip, blockEntity, position);
}

@SuppressWarnings("DuplicatedCode")
Expand Down Expand Up @@ -95,7 +97,7 @@ public static void requestEntityData(Entity entity, ClientAccessor accessor) {
accessor.resetTimer();
accessor.setDataAccess(false);
DataWriter.CLIENT.reset();
var player = Minecraft.getInstance().player;
var player = accessor.getPlayer();

for (var entry : registrar.entityDataCtx.get(entity)) {
DataWriter.CLIENT.tryAppend(player, entry.instance(), accessor, PluginConfig.CLIENT, IEntityComponentProvider::appendDataContext);
Expand Down Expand Up @@ -181,7 +183,7 @@ public static ITooltipComponent getIcon(HitResult target) {
return EmptyComponent.INSTANCE;
}

public static Entity getOverrideEntity(HitResult target) {
public static @Nullable Entity getOverrideEntity(HitResult target) {
if (target == null || target.getType() != HitResult.Type.ENTITY) {
return null;
}
Expand All @@ -203,9 +205,7 @@ public static Entity getOverrideEntity(HitResult target) {
public static BlockState getOverrideBlock(HitResult target) {
var registrar = Registrar.get();

Level world = Minecraft.getInstance().level;
if (world == null) return null;

var world = Objects.requireNonNull(Minecraft.getInstance().level);
var pos = ((BlockHitResult) target).getBlockPos();
final var state = world.getBlockState(pos);

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/mcp/mobius/waila/gui/hud/TooltipHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ private static ProcessResult redirectTarget(HitResult target, TargetRedirector r

private static ProcessResult processTarget(HitResult target, Minecraft client, Player player, Vec3 castOrigin, Vec3 castDirection, float pickRange, WailaConfig.General config) {
var accessor = ClientAccessor.INSTANCE;

//noinspection DataFlowIssue
accessor.set(client.level, player, target, client.cameraEntity, castOrigin, castDirection, pickRange, client.getFrameTime());

TooltipRenderer.beginBuild(STATE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ public static Rectangle endBuild() {
return RECT.get();
}

@SuppressWarnings("DataFlowIssue")
public static void resetState() {
state = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
import mcp.mobius.waila.registry.Registrar;
import mcp.mobius.waila.util.TypeUtil;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

public final class ThemeDefinition<T extends ITheme> {

private static Map<ResourceLocation, ThemeDefinition<?>> all;
private static @Nullable Map<ResourceLocation, ThemeDefinition<?>> all;

public final ResourceLocation id;
public final ThemeType<T> type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ResourceLocation getId() {
return Objects.requireNonNull(Registrar.get().themeTypes.inverse().get(this));
}

private <V, C> ThemeType<T> property(String name, Class<V> type, V exampleValue, C context) {
private <V, C> ThemeType<T> property(String name, Class<V> type, V exampleValue, @Nullable C context) {
Preconditions.checkState(!built);
Preconditions.checkArgument(!INVALID_NAMES.contains(name), INVALID_NAME_ERROR_MSG);
properties.put(name, new Property<>(name, type, exampleValue, context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected void init() {
super.init();

try {
var credits = new Gson().fromJson(minecraft.getResourceManager().getResource(Waila.id("credits.json")).get().openAsReader(), CreditMap.class);
var credits = new Gson().fromJson(minecraft.getResourceManager().getResource(Waila.id("credits.json")).orElseThrow().openAsReader(), CreditMap.class);
var listWidget = new ListWidget(minecraft, width, height, 32, height - 32, minecraft.font.lineHeight + 6);

credits.forEach((key, category) -> {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/mcp/mobius/waila/gui/screen/HomeScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class HomeScreen extends YesIAmSureTheClientInstanceIsPresentByTheTimeIUseItScreen {

private final Screen parent;
private final @Nullable Screen parent;

public HomeScreen(Screen parent) {
public HomeScreen(@Nullable Screen parent) {
super(Component.literal(WailaConstants.MOD_NAME));

this.parent = parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public ConfigListWidget getOptions() {
initialValues.put(id, enabled);
updatedValues.put(id, enabled);

var toggle = new BooleanValue(null, enabled, null, val -> updatedValues.put(id, val.booleanValue())) {
var toggle = new BooleanValue("", enabled, null, val -> updatedValues.put(id, val.booleanValue())) {
@Override
public MutableComponent getTitle() {
return Component.literal(plugin.getPluginId().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ private void addTypeProperties(ConfigListWidget options) {
ConfigValue<?> value;

if (propType == int.class) {
//noinspection DataFlowIssue
value = new IntInputValue(prop.getTlKey(), TypeUtil.uncheckedCast(templateValue), null, val -> attr.put(key, val), TypeUtil.uncheckedCast(prop.context));
} else if (propType == boolean.class) {
value = new BooleanValue(prop.getTlKey(), TypeUtil.uncheckedCast(templateValue), null, val -> attr.put(key, val));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class WailaConfigScreen extends ConfigScreen {

private ThemeValue themeIdVal;

private KeyBindValue selectedKeyBind;
private @Nullable KeyBindValue selectedKeyBind;

public WailaConfigScreen(Screen parent) {
super(parent, Component.translatable(Tl.Gui.CONFIGURATION, WailaConstants.MOD_NAME), Waila.CONFIG::save, Waila.CONFIG::invalidate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public class ConfigListWidget extends ContainerObjectSelectionList<ConfigListWid
private @Nullable EditBox searchBox;

public @Nullable String filter = null;
public @Nullable String[] splitFilter = null;

// the fuck? apparently @Nullable String[] means a non-null array containing nullable string
public String @Nullable [] splitFilter = null;

public ConfigListWidget(ConfigScreen owner, Minecraft client, int width, int height, int top, int bottom, int itemHeight, @Nullable Runnable diskWriter) {
super(client, width, height, top, bottom, itemHeight - 4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.util.FormattedCharSequence;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public abstract class ConfigValue<T> extends ConfigListWidget.Entry {
Expand Down Expand Up @@ -113,7 +114,7 @@ public boolean isValueValid() {

@Override
protected void buildSearchKey(StringBuilder sb) {
sb.append(title.getString());
sb.append(getTitle().getString());
var desc = getDescription();
if (desc != null) sb.append(" ").append(desc.getString());
}
Expand Down Expand Up @@ -160,14 +161,15 @@ public int getX() {
return x;
}

public final T getValue() {
public final @NotNull T getValue() {
return value;
}

public void setValue(T value) {
this.value = value;
}

@SuppressWarnings("DataFlowIssue")
protected void resetValue() {
setValue(defaultValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public GuiEventListener getListener() {
}

@Override
@SuppressWarnings("DataFlowIssue")
protected void resetValue() {
textField.setValue(serializer.serialize(defaultValue));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectLists;
import mcp.mobius.waila.api.IInstanceRegistry;
import org.jetbrains.annotations.Nullable;

public class InstanceRegistry<T> implements IInstanceRegistry<T> {

Expand All @@ -29,7 +30,7 @@ public void add(Class<?> key, T instance, int priority) {
}

@Override
public List<Entry<T>> get(Object target) {
public List<Entry<T>> get(@Nullable Object target) {
if (target == null) {
return ObjectLists.emptyList();
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/mcp/mobius/waila/registry/Registrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public void attach(@Nullable IPluginInfo plugin) {
this.plugin = plugin;
}

@SuppressWarnings("DataFlowIssue")
private <T> void addConfig(ResourceLocation key, T defaultValue, T clientOnlyValue, boolean serverRequired, boolean merged, ConfigEntry.Type<T> type) {
assertLock();
PluginConfig.addConfig(type.create(plugin, key, defaultValue, clientOnlyValue, serverRequired, merged));
Expand Down Expand Up @@ -454,6 +455,7 @@ private void assertPlugin() {
Preconditions.checkNotNull(plugin, "Tried to register things outside the register method");
}

@SuppressWarnings("DataFlowIssue")
private boolean skip() {
assertPlugin();
return !plugin.isEnabled();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/mcp/mobius/waila/util/CachedSupplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import java.util.function.Supplier;

import org.jetbrains.annotations.Nullable;

public class CachedSupplier<T> implements Supplier<T> {

private final Supplier<T> supplier;
private T value;
private @Nullable T value;

public CachedSupplier(Supplier<T> supplier) {
this.supplier = supplier;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/mcp/mobius/waila/util/ExceptionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jetbrains.annotations.Nullable;

public final class ExceptionUtil {

private static final Log LOG = Log.create();
private static final Set<String> ERRORS = new HashSet<>();

public static boolean dump(Throwable e, String errorName, ITooltip tooltip) {
public static boolean dump(Throwable e, String errorName, @Nullable ITooltip tooltip) {
var log = ERRORS.add(errorName);

if (log) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ protected void implAdd(Fluid fluid, @Nullable CompoundTag nbt, double stored, do
}

@Override
@SuppressWarnings("DataFlowIssue")
protected PlatformTranslator<Object> translator() {
return proxy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ public void appendBody(ITooltip tooltip, IBlockAccessor accessor, IPluginConfig
var blockEntityType = Objects.<BlockEntity>requireNonNull(accessor.getBlockEntity()).getType();
if (blacklistConfig.get().getView().blockEntityFilter.matches(blockEntityType)) return;

appendBody(tooltip, accessor.getData(), config, Registry.BLOCK_ENTITY_TYPE.getKey(blockEntityType));
var blockEntityId = Objects.requireNonNull(Registry.BLOCK_ENTITY_TYPE.getKey(blockEntityType));
appendBody(tooltip, accessor.getData(), config, blockEntityId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public enum BoatProvider implements IEntityComponentProvider {

@Override
public void appendHead(ITooltip tooltip, IEntityAccessor accessor, IPluginConfig config) {
stack = accessor.getEntity().getPickResult();
//noinspection DataFlowIssue
stack = accessor.<Boat>getEntity().getPickResult();

var formatter = IWailaConfig.get().getFormatter();
tooltip.setLine(WailaConstants.OBJECT_NAME_TAG, formatter.entityName(stack.getHoverName().getString()));

Expand Down
Loading

0 comments on commit 1f2d4fa

Please sign in to comment.