Skip to content

Commit

Permalink
remove usages of NonNullList (GregTechCEu#2581)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechLord22 authored Aug 10, 2024
1 parent 4c6a43a commit fbc0f91
Show file tree
Hide file tree
Showing 30 changed files with 74 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public void breakBlock(@NotNull World worldIn, @NotNull BlockPos pos, @NotNull I
MetaTileEntity metaTileEntity = getMetaTileEntity(worldIn, pos);
if (metaTileEntity != null) {
if (!metaTileEntity.keepsInventory()) {
NonNullList<ItemStack> inventoryContents = NonNullList.create();
List<ItemStack> inventoryContents = new ArrayList<>();
metaTileEntity.clearMachineInventory(inventoryContents);
for (ItemStack itemStack : inventoryContents) {
Block.spawnAsEntity(worldIn, pos, itemStack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.Constants;
Expand Down Expand Up @@ -71,7 +70,7 @@ public abstract class AbstractRecipeLogic extends MTETrait implements IWorkable,
protected int maxProgressTime;
protected long recipeEUt;
protected List<FluidStack> fluidOutputs;
protected NonNullList<ItemStack> itemOutputs;
protected List<ItemStack> itemOutputs;

protected boolean isActive;
protected boolean workingEnabled = true;
Expand Down Expand Up @@ -1227,7 +1226,7 @@ public void deserializeNBT(@NotNull NBTTagCompound compound) {
this.maxProgressTime = compound.getInteger("MaxProgress");
this.recipeEUt = compound.getLong("RecipeEUt");
NBTTagList itemOutputsList = compound.getTagList("ItemOutputs", Constants.NBT.TAG_COMPOUND);
this.itemOutputs = NonNullList.create();
this.itemOutputs = new ArrayList<>(itemOutputsList.tagCount());
for (int i = 0; i < itemOutputsList.tagCount(); i++) {
this.itemOutputs.add(new ItemStack(itemOutputsList.getCompoundTagAt(i)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraft.util.NonNullList;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
Expand All @@ -30,7 +29,6 @@
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;

import static gregtech.api.capability.GregtechDataCodes.BOILER_HEAT;
import static gregtech.api.capability.GregtechDataCodes.BOILER_LAST_TICK_STEAM;
Expand All @@ -51,7 +49,7 @@ public class BoilerRecipeLogic extends AbstractRecipeLogic implements ICategoryO
public BoilerRecipeLogic(MetaTileEntityLargeBoiler tileEntity) {
super(tileEntity, null);
this.fluidOutputs = Collections.emptyList();
this.itemOutputs = NonNullList.create();
this.itemOutputs = Collections.emptyList();
}

@Override
Expand Down Expand Up @@ -80,15 +78,14 @@ protected void trySearchNewRecipe() {
// can optimize with an override of checkPreviousRecipe() and a check here

IMultipleTankHandler importFluids = boiler.getImportFluids();
List<ItemStack> dummyList = NonNullList.create();
boolean didStartRecipe = false;

for (IFluidTank fluidTank : importFluids.getFluidTanks()) {
FluidStack fuelStack = fluidTank.drain(Integer.MAX_VALUE, false);
if (fuelStack == null || CommonFluidFilters.BOILER_FLUID.test(fuelStack)) continue;

Recipe dieselRecipe = RecipeMaps.COMBUSTION_GENERATOR_FUELS.findRecipe(
GTValues.V[GTValues.MAX], dummyList, Collections.singletonList(fuelStack));
GTValues.V[GTValues.MAX], Collections.emptyList(), Collections.singletonList(fuelStack));
// run only if it can apply a certain amount of "parallel", this is to mitigate int division
if (dieselRecipe != null &&
fuelStack.amount >= dieselRecipe.getFluidInputs().get(0).getAmount() * FLUID_DRAIN_MULTIPLIER) {
Expand All @@ -102,7 +99,7 @@ protected void trySearchNewRecipe() {
}

Recipe denseFuelRecipe = RecipeMaps.SEMI_FLUID_GENERATOR_FUELS.findRecipe(
GTValues.V[GTValues.MAX], dummyList, Collections.singletonList(fuelStack));
GTValues.V[GTValues.MAX], Collections.emptyList(), Collections.singletonList(fuelStack));
// run only if it can apply a certain amount of "parallel", this is to mitigate int division
if (denseFuelRecipe != null &&
fuelStack.amount >= denseFuelRecipe.getFluidInputs().get(0).getAmount() * FLUID_DRAIN_MULTIPLIER) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public void performMining() {
blockState = metaTileEntity.getWorld().getBlockState(blocksToMine.getFirst());
}
// When we are here we have an ore to mine! I'm glad we aren't threaded
if (!blocksToMine.isEmpty() & GTUtility.isOre(GTUtility.toItem(blockState))) {
if (!blocksToMine.isEmpty() && GTUtility.isOre(GTUtility.toItem(blockState))) {
// get the small ore drops, if a small ore
getSmallOreBlockDrops(blockDrops, world, blocksToMine.getFirst(), blockState);
// get the block's drops.
Expand Down Expand Up @@ -280,7 +280,7 @@ protected void getRegularBlockDrops(NonNullList<ItemStack> blockDrops, WorldServ
* @param blockDrops the List of items to insert
* @param world the {@link WorldServer} the miner is in
*/
private void mineAndInsertItems(NonNullList<ItemStack> blockDrops, WorldServer world) {
private void mineAndInsertItems(List<ItemStack> blockDrops, WorldServer world) {
// If the block's drops can fit in the inventory, move the previously mined position to the block
// replace the ore block with cobblestone instead of breaking it to prevent mob spawning
// remove the ore block's position from the mining queue
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/gregtech/api/cover/CoverBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.NonNullList;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.IItemHandlerModifiable;
Expand All @@ -21,6 +20,9 @@
import codechicken.lib.vec.Matrix4;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public abstract class CoverBase implements Cover {

private final CoverDefinition definition;
Expand Down Expand Up @@ -55,7 +57,7 @@ public CoverBase(@NotNull CoverDefinition definition, @NotNull CoverableView cov
* @param inventory the inventory to clear
*/
protected void dropInventoryContents(@NotNull IItemHandlerModifiable inventory) {
NonNullList<ItemStack> drops = NonNullList.create();
List<ItemStack> drops = new ArrayList<>();
MetaTileEntity.clearInventory(drops, inventory);
for (ItemStack itemStack : drops) {
Block.spawnAsEntity(getWorld(), getPos(), itemStack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.NonNullList;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;

public abstract class FakeModularGuiContainer implements WidgetUIAccess {

protected final NonNullList<ItemStack> inventoryItemStacks = NonNullList.create();
public final List<Slot> inventorySlots = Lists.newArrayList();
protected final List<ItemStack> inventoryItemStacks = new ArrayList<>();
public final List<Slot> inventorySlots = new ArrayList<>();
public final ModularUI modularUI;
protected int windowId;

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/gregtech/api/metatileentity/MetaTileEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ private void updateSound() {
* @param dropsList list of meta tile entity drops
* @param harvester harvester of this meta tile entity, or null
*/
public void getDrops(NonNullList<ItemStack> dropsList, @Nullable EntityPlayer harvester) {}
public void getDrops(@NotNull List<@NotNull ItemStack> dropsList, @Nullable EntityPlayer harvester) {}

public final ItemStack getPickItem(CuboidRayTraceResult result, EntityPlayer player) {
IndexedCuboid6 hitCuboid = result.cuboid6;
Expand Down Expand Up @@ -1325,12 +1325,13 @@ public boolean isValid() {
return getHolder() != null && getHolder().isValid();
}

public void clearMachineInventory(NonNullList<ItemStack> itemBuffer) {
public void clearMachineInventory(@NotNull List<@NotNull ItemStack> itemBuffer) {
clearInventory(itemBuffer, importItems);
clearInventory(itemBuffer, exportItems);
}

public static void clearInventory(NonNullList<ItemStack> itemBuffer, IItemHandlerModifiable inventory) {
public static void clearInventory(@NotNull List<@NotNull ItemStack> itemBuffer,
@NotNull IItemHandlerModifiable inventory) {
for (int i = 0; i < inventory.getSlots(); i++) {
ItemStack stackInSlot = inventory.getStackInSlot(i);
if (!stackInSlot.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
Expand Down Expand Up @@ -475,7 +474,7 @@ public boolean isAllowInputFromOutputSideFluids() {
}

@Override
public void clearMachineInventory(NonNullList<ItemStack> itemBuffer) {
public void clearMachineInventory(@NotNull List<@NotNull ItemStack> itemBuffer) {
super.clearMachineInventory(itemBuffer);
clearInventory(itemBuffer, chargerInventory);
}
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/gregtech/api/recipes/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import gregtech.integration.groovy.GroovyScriptModule;

import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemHandlerHelper;
Expand Down Expand Up @@ -59,8 +58,6 @@
*/
public class Recipe {

private static final NonNullList<ItemStack> EMPTY = NonNullList.create();

/**
* This method was deprecated in 2.8 and will be removed in 2.9
*
Expand All @@ -73,7 +70,7 @@ public static int getMaxChancedValue() {
}

private final List<GTRecipeInput> inputs;
private final NonNullList<ItemStack> outputs;
private final List<ItemStack> outputs;

/**
* A chance of 10000 equals 100%
Expand Down Expand Up @@ -122,10 +119,9 @@ public Recipe(@NotNull List<GTRecipeInput> inputs,
recipePropertyStorage;
this.inputs = GTRecipeInputCache.deduplicateInputs(inputs);
if (outputs.isEmpty()) {
this.outputs = EMPTY;
this.outputs = Collections.emptyList();
} else {
this.outputs = NonNullList.create();
this.outputs.addAll(outputs);
this.outputs = new ArrayList<>(outputs);
}
this.chancedOutputs = chancedOutputs;
this.chancedFluidOutputs = chancedFluidOutputs;
Expand Down Expand Up @@ -457,7 +453,7 @@ public List<GTRecipeInput> getInputs() {
return inputs;
}

public NonNullList<ItemStack> getOutputs() {
public List<ItemStack> getOutputs() {
return outputs;
}

Expand All @@ -475,7 +471,7 @@ public NonNullList<ItemStack> getOutputs() {
* @return A list of all resulting ItemStacks from the recipe, after chance has been applied to any chanced outputs
*/
public List<ItemStack> getResultItemOutputs(int recipeTier, int machineTier, RecipeMap<?> recipeMap) {
List<ItemStack> outputs = new ArrayList<>(GTUtility.copyStackList(getOutputs()));
List<ItemStack> outputs = new ArrayList<>(getOutputs());
ChanceBoostFunction function = recipeMap.getChanceFunction();
List<ChancedItemOutput> chancedOutputsList = getChancedOutputs().roll(function, recipeTier, machineTier);

Expand Down
17 changes: 6 additions & 11 deletions src/main/java/gregtech/api/recipes/RecipeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.common.Optional;
Expand Down Expand Up @@ -89,8 +88,8 @@ public class RecipeBuilder<R extends RecipeBuilder<R>> {
protected boolean recipePropertyStorageErrored = false;

protected RecipeBuilder() {
this.inputs = NonNullList.create();
this.outputs = NonNullList.create();
this.inputs = new ArrayList<>();
this.outputs = new ArrayList<>();
this.chancedOutputs = new ArrayList<>();
this.fluidInputs = new ArrayList<>();
this.fluidOutputs = new ArrayList<>();
Expand All @@ -99,10 +98,8 @@ protected RecipeBuilder() {

public RecipeBuilder(Recipe recipe, RecipeMap<R> recipeMap) {
this.recipeMap = recipeMap;
this.inputs = NonNullList.create();
this.inputs.addAll(recipe.getInputs());
this.outputs = NonNullList.create();
this.outputs.addAll(GTUtility.copyStackList(recipe.getOutputs()));
this.inputs = new ArrayList<>(recipe.getInputs());
this.outputs = new ArrayList<>(recipe.getOutputs());
this.chancedOutputs = new ArrayList<>(recipe.getChancedOutputs().getChancedEntries());
this.fluidInputs = new ArrayList<>(recipe.getFluidInputs());
this.fluidOutputs = GTUtility.copyFluidList(recipe.getFluidOutputs());
Expand All @@ -120,10 +117,8 @@ public RecipeBuilder(Recipe recipe, RecipeMap<R> recipeMap) {
@SuppressWarnings("CopyConstructorMissesField")
protected RecipeBuilder(RecipeBuilder<R> recipeBuilder) {
this.recipeMap = recipeBuilder.recipeMap;
this.inputs = NonNullList.create();
this.inputs.addAll(recipeBuilder.getInputs());
this.outputs = NonNullList.create();
this.outputs.addAll(GTUtility.copyStackList(recipeBuilder.getOutputs()));
this.inputs = new ArrayList<>(recipeBuilder.getInputs());
this.outputs = new ArrayList<>(recipeBuilder.getOutputs());
this.chancedOutputs = new ArrayList<>(recipeBuilder.chancedOutputs);
this.fluidInputs = new ArrayList<>(recipeBuilder.getFluidInputs());
this.fluidOutputs = GTUtility.copyFluidList(recipeBuilder.getFluidOutputs());
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/gregtech/api/unification/OreDictUnifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.oredict.OreDictionary;
Expand Down Expand Up @@ -87,8 +86,7 @@ public static void registerOre(ItemStack itemStack, String oreDict) {

public static void init() {
for (String registeredOreName : OreDictionary.getOreNames()) {
NonNullList<ItemStack> theseOres = OreDictionary.getOres(registeredOreName);
for (ItemStack itemStack : theseOres) {
for (ItemStack itemStack : OreDictionary.getOres(registeredOreName)) {
onItemRegistration(new OreRegisterEvent(registeredOreName, itemStack));
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/gregtech/api/util/BlockUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ public BlockWrapper() {
super(Material.AIR);
}

@NotNull
@Override
public NonNullList<ItemStack> captureDrops(boolean start) {
public @NotNull NonNullList<ItemStack> captureDrops(boolean start) {
return super.captureDrops(start);
}
}
Expand All @@ -58,7 +57,7 @@ public static void startCaptureDrops() {
WRAPPER.captureDrops(true);
}

public static NonNullList<ItemStack> stopCaptureDrops() {
public static @NotNull NonNullList<ItemStack> stopCaptureDrops() {
return WRAPPER.captureDrops(false);
}

Expand Down
29 changes: 14 additions & 15 deletions src/main/java/gregtech/api/util/GTUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;

import com.google.common.collect.Lists;
import com.google.common.util.concurrent.AtomicDouble;
import it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet;
import org.apache.commons.lang3.ArrayUtils;
Expand All @@ -64,6 +63,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -446,18 +446,20 @@ public static NBTTagCompound getOrCreateNbtCompound(ItemStack stack) {
return compound;
}

public static NonNullList<ItemStack> copyStackList(List<ItemStack> itemStacks) {
ItemStack[] stacks = new ItemStack[itemStacks.size()];
for (int i = 0; i < itemStacks.size(); i++) {
stacks[i] = copy(itemStacks.get(i));
public static @NotNull List<@NotNull ItemStack> copyStackList(@NotNull List<@NotNull ItemStack> itemStacks) {
List<ItemStack> list = new ArrayList<>(itemStacks.size());
for (ItemStack itemStack : itemStacks) {
list.add(copy(itemStack));
}
return NonNullList.from(ItemStack.EMPTY, stacks);
return list;
}

public static List<FluidStack> copyFluidList(List<FluidStack> fluidStacks) {
FluidStack[] stacks = new FluidStack[fluidStacks.size()];
for (int i = 0; i < fluidStacks.size(); i++) stacks[i] = fluidStacks.get(i).copy();
return Lists.newArrayList(stacks);
public static @NotNull List<@NotNull FluidStack> copyFluidList(@NotNull List<@NotNull FluidStack> fluidStacks) {
List<FluidStack> list = new ArrayList<>(fluidStacks.size());
for (FluidStack stack : fluidStacks) {
list.add(stack.copy());
}
return list;
}

/**
Expand All @@ -466,8 +468,7 @@ public static List<FluidStack> copyFluidList(List<FluidStack> fluidStacks) {
* @param stack item stack for copying
* @return a copy of ItemStack, or {@link ItemStack#EMPTY} if the stack is empty
*/
@NotNull
public static ItemStack copy(@NotNull ItemStack stack) {
public static @NotNull ItemStack copy(@NotNull ItemStack stack) {
return stack.isEmpty() ? ItemStack.EMPTY : stack.copy();
}

Expand Down Expand Up @@ -820,9 +821,7 @@ public static Set<ItemStack> getAllSubItems(@NotNull Item item) {
if (tab == null || tab == CreativeTabs.SEARCH) continue;
item.getSubItems(tab, subItems);
}
Set<ItemStack> set = new ObjectOpenCustomHashSet<>(ItemStackHashStrategy.comparingItemDamageCount());
set.addAll(subItems);
return set;
return new ObjectOpenCustomHashSet<>(subItems, ItemStackHashStrategy.comparingItemDamageCount());
}

/**
Expand Down
Loading

0 comments on commit fbc0f91

Please sign in to comment.