Skip to content

Commit b61e724

Browse files
committed
Continued removal of capturing lambdas
1 parent 8da0a1f commit b61e724

File tree

6 files changed

+46
-34
lines changed

6 files changed

+46
-34
lines changed

src/main/java/mekanism/common/item/ItemConfigurator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public InteractionResult useOn(UseOnContext context) {
142142
//TODO: Switch this to items being handled by TileEntityMekanism, energy handled here (via lambdas?)
143143
for (IInventorySlot inventorySlot : inv.getInventorySlots(null)) {
144144
if (!inventorySlot.isEmpty()) {
145-
InventoryUtils.dropStack(inventorySlot.getStack().copy(), slotStack -> Block.popResourceFromFace(world, pos, side, slotStack));
145+
InventoryUtils.dropStack(world, pos, side, inventorySlot.getStack().copy(), Block::popResourceFromFace);
146146
inventorySlot.setEmpty();
147147
}
148148
}

src/main/java/mekanism/common/tile/laser/TileEntityLaserTractorBeam.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import net.minecraft.world.item.ItemStack;
2828
import net.minecraft.world.level.block.Block;
2929
import net.minecraft.world.level.block.state.BlockState;
30-
import net.neoforged.neoforge.common.util.Lazy;
3130
import org.jetbrains.annotations.NotNull;
3231

3332
public class TileEntityLaserTractorBeam extends TileEntityLaserReceptor {
@@ -65,9 +64,8 @@ protected void handleBreakBlock(BlockState state, BlockPos hitPos, Player player
6564
breakBlock(state, hitPos);
6665
CommonWorldTickHandler.fallbackItemCollector = null;
6766
if (!drops.isEmpty()) {
68-
Lazy<Direction> direction = Lazy.of(this::getDirection);
69-
Lazy<BlockPos> dropPos = Lazy.of(() -> worldPosition.relative(direction.get(), 2));
70-
Lazy<Direction> opposite = Lazy.of(() -> direction.get().getOpposite());
67+
BlockPos dropPos = null;
68+
Direction opposite = null;
7169
List<IInventorySlot> inventorySlots = getInventorySlots(null);
7270
for (ItemStack drop : drops) {
7371
//Try inserting it first where it can stack and then into empty slots
@@ -76,7 +74,12 @@ protected void handleBreakBlock(BlockState state, BlockPos hitPos, Player player
7674
//If we have some drop left over that we couldn't fit, then spawn it into the world
7775
// Note: We use an adjusted position and an opposite direction to provide the item with momentum towards the tractor beam
7876
// so that even though we couldn't fit the items into our inventory we can still have them appear to be "pulled" to the tractor beam
79-
Block.popResourceFromFace(level, dropPos.get(), opposite.get(), drop);
77+
if (dropPos == null) {
78+
Direction direction = getDirection();
79+
dropPos = worldPosition.relative(direction, 2);
80+
opposite = direction.getOpposite();
81+
}
82+
Block.popResourceFromFace(level, dropPos, opposite, drop);
8083
}
8184
}
8285
}

src/main/java/mekanism/common/util/InventoryUtils.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.Collections;
66
import java.util.List;
77
import java.util.Optional;
8-
import java.util.function.Consumer;
98
import java.util.function.Function;
109
import mekanism.api.Action;
1110
import mekanism.api.AutomationType;
@@ -43,7 +42,8 @@ private InventoryUtils() {
4342
*/
4443
public static void dropItemContents(ItemEntity entity, DamageSource source) {
4544
ItemStack stack = entity.getItem();
46-
if (!entity.level().isClientSide && !stack.isEmpty()) {
45+
Level level = entity.level();
46+
if (!level.isClientSide && !stack.isEmpty()) {
4747
if (source.getEntity() instanceof Player player) {
4848
//If the destroyer is a player use security utils to properly check for access
4949
if (!IItemSecurityUtils.INSTANCE.canAccess(player, stack)) {
@@ -54,40 +54,42 @@ public static void dropItemContents(ItemEntity entity, DamageSource source) {
5454
return;
5555
}
5656
int scalar = stack.getCount();
57-
Consumer<ItemStack> dropper = slotStack -> entity.level().addFreshEntity(new ItemEntity(entity.level(), entity.getX(), entity.getY(), entity.getZ(), slotStack));
57+
BlockPos blockPos = entity.blockPosition();
58+
ItemDropper dropper = (lvl, pos, ignored, slotStack) -> lvl.addFreshEntity(new ItemEntity(lvl, pos.getX(), pos.getY(), pos.getZ(), slotStack));
5859
//Note: This instanceof check must be checked before the container type to allow overriding what contents can be dropped
5960
if (stack.getItem() instanceof IDroppableContents inventory) {
6061
if (inventory.canContentsDrop(stack)) {
6162
scalar = inventory.getScalar(stack);
62-
dropItemContents(inventory.getDroppedSlots(stack), scalar, dropper);
63+
dropItemContents(level, blockPos, inventory.getDroppedSlots(stack), scalar, dropper);
6364
} else {
6465
//Explicitly denying dropping items
6566
return;
6667
}
6768
} else if (ContainerType.ITEM.supports(stack)) {
68-
dropItemContents(ContainerType.ITEM.getAttachmentContainersIfPresent(stack), scalar, dropper);
69+
dropItemContents(level, blockPos, ContainerType.ITEM.getAttachmentContainersIfPresent(stack), scalar, dropper);
6970
}
7071
Optional<UpgradeAware> existingUpgrades = stack.getExistingData(MekanismAttachmentTypes.UPGRADES);
7172
if (existingUpgrades.isPresent()) {
7273
UpgradeAware upgradeAware = existingUpgrades.get();
73-
dropItemContents(upgradeAware.getInventorySlots(null), scalar, dropper);
74-
dropItemContents(upgradeAware.getUpgrades().entrySet(), scalar, dropper, entry -> UpgradeUtils.getStack(entry.getKey(), entry.getValue()));
74+
dropItemContents(level, blockPos, upgradeAware.getInventorySlots(null), scalar, dropper);
75+
dropItemContents(level, blockPos, upgradeAware.getUpgrades().entrySet(), scalar, dropper, entry -> UpgradeUtils.getStack(entry.getKey(), entry.getValue()));
7576
}
7677
IModuleContainer moduleContainer = IModuleHelper.INSTANCE.getModuleContainerNullable(stack);
7778
if (moduleContainer != null) {
78-
dropItemContents(moduleContainer.modules(), scalar, dropper, module -> module.getData().getItemProvider().getItemStack(module.getInstalledCount()));
79+
dropItemContents(level, blockPos, moduleContainer.modules(), scalar, dropper, module -> module.getData().getItemProvider().getItemStack(module.getInstalledCount()));
7980
}
8081
}
8182
}
8283

83-
private static void dropItemContents(List<IInventorySlot> slots, int scalar, Consumer<ItemStack> dropper) {
84-
dropItemContents(slots, scalar, dropper, slot -> slot.getStack().copy());
84+
private static void dropItemContents(Level level, BlockPos pos, List<IInventorySlot> slots, int scalar, ItemDropper dropper) {
85+
dropItemContents(level, pos, slots, scalar, dropper, slot -> slot.getStack().copy());
8586
}
8687

8788
/**
8889
* @param stackExtractor It is expected the stack returned by the stack extractor can be safely mutated
8990
*/
90-
private static <T> void dropItemContents(Collection<T> toDrop, int scalar, Consumer<ItemStack> dropper, Function<T, ItemStack> stackExtractor) {
91+
private static <T> void dropItemContents(Level level, BlockPos pos, Collection<T> toDrop, int scalar, ItemDropper dropper,
92+
Function<T, ItemStack> stackExtractor) {
9193
for (T drop : toDrop) {
9294
ItemStack stackToDrop = stackExtractor.apply(drop);
9395
if (!stackToDrop.isEmpty()) {
@@ -106,7 +108,7 @@ private static <T> void dropItemContents(Collection<T> toDrop, int scalar, Consu
106108
}
107109
}
108110
//Copy the stack as the passed slot is likely to be the actual backing slot
109-
dropStack(stackToDrop, dropper);
111+
dropStack(level, pos, null, stackToDrop, dropper);
110112
}
111113
}
112114
}
@@ -117,23 +119,23 @@ private static <T> void dropItemContents(Collection<T> toDrop, int scalar, Consu
117119
* @param stack Item Stack to drop, may be passed directly to the dropper.
118120
* @param dropper Called to drop the item.
119121
*/
120-
public static void dropStack(ItemStack stack, Consumer<ItemStack> dropper) {
122+
public static void dropStack(Level level, BlockPos pos, Direction side, ItemStack stack, ItemDropper dropper) {
121123
int count = stack.getCount();
122124
int max = stack.getMaxStackSize();
123125
if (count > max) {
124126
//If we have more than a stack of the item (such as we are a bin) or some other thing that allows for compressing
125127
// stack counts, drop as many stacks as we need at their max size
126128
while (count > max) {
127-
dropper.accept(stack.copyWithCount(max));
129+
dropper.drop(level, pos, side, stack.copyWithCount(max));
128130
count -= max;
129131
}
130132
if (count > 0) {
131133
//If we have anything left to drop afterward, do so
132-
dropper.accept(stack.copyWithCount(count));
134+
dropper.drop(level, pos, side, stack.copyWithCount(count));
133135
}
134136
} else {
135137
//If we have a valid stack, we can just directly drop that instead without requiring any copies
136-
dropper.accept(stack);
138+
dropper.drop(level, pos, side, stack);
137139
}
138140
}
139141

@@ -226,4 +228,9 @@ public static ItemStack insertItem(List<? extends IInventorySlot> slots, @NotNul
226228
}
227229
return stack;
228230
}
231+
232+
public interface ItemDropper {
233+
234+
void drop(Level level, BlockPos pos, Direction side, ItemStack stack);
235+
}
229236
}

src/main/java/mekanism/common/util/MekanismUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,12 @@ private static void onChangedPotionEffect(LivingEntity entity, MobEffectInstance
552552
}
553553

554554
public static boolean isSameTypeFactory(Block block, Block factoryBlockType) {
555-
return Attribute.matches(block, AttributeFactoryType.class, attribute -> {
555+
AttributeFactoryType attribute = Attribute.get(block, AttributeFactoryType.class);
556+
if (attribute != null) {
556557
AttributeFactoryType otherType = Attribute.get(factoryBlockType, AttributeFactoryType.class);
557558
return otherType != null && attribute.getFactoryType() == otherType.getFactoryType();
558-
});
559+
}
560+
return false;
559561
}
560562

561563
/**

src/main/java/mekanism/common/util/StorageUtils.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import java.util.List;
44
import java.util.Optional;
5-
import java.util.function.Function;
5+
import java.util.function.BiFunction;
66
import mekanism.api.Action;
77
import mekanism.api.chemical.Chemical;
88
import mekanism.api.chemical.ChemicalStack;
@@ -72,17 +72,17 @@ public static void addStoredGas(@NotNull ItemStack stack, @NotNull List<Componen
7272

7373
public static void addStoredGas(@NotNull ItemStack stack, @NotNull List<Component> tooltip, boolean showMissingCap, boolean showAttributes,
7474
ILangEntry emptyLangEntry) {
75-
addStoredChemical(stack, tooltip, showMissingCap, showAttributes, emptyLangEntry, stored -> {
75+
addStoredChemical(stack, tooltip, showMissingCap, showAttributes, emptyLangEntry, (stored, emptyLang) -> {
7676
if (stored.isEmpty()) {
77-
return emptyLangEntry.translateColored(EnumColor.GRAY);
77+
return emptyLang.translateColored(EnumColor.GRAY);
7878
}
7979
return MekanismLang.STORED.translateColored(EnumColor.ORANGE, EnumColor.ORANGE, stored, EnumColor.GRAY,
8080
MekanismLang.GENERIC_MB.translate(TextUtils.format(stored.getAmount())));
8181
}, Capabilities.GAS.item(), ContainerType.GAS);
8282
}
8383

8484
public static <CHEMICAL extends Chemical<CHEMICAL>, STACK extends ChemicalStack<CHEMICAL>> void addStoredChemical(@NotNull ItemStack stack, @NotNull List<Component> tooltip,
85-
boolean showMissingCap, boolean showAttributes, ILangEntry emptyLangEntry, Function<STACK, Component> storedFunction, ItemCapability<? extends IChemicalHandler<CHEMICAL, STACK>, Void> capability,
85+
boolean showMissingCap, boolean showAttributes, ILangEntry emptyLangEntry, BiFunction<STACK, ILangEntry, Component> storedFunction, ItemCapability<? extends IChemicalHandler<CHEMICAL, STACK>, Void> capability,
8686
ContainerType<?, ? extends IChemicalHandler<CHEMICAL, STACK>, ?> containerType) {
8787
IChemicalHandler<CHEMICAL, STACK> handler = stack.getCapability(capability);
8888
if (handler == null) {
@@ -92,7 +92,7 @@ public static <CHEMICAL extends Chemical<CHEMICAL>, STACK extends ChemicalStack<
9292
if (handler != null) {
9393
for (int tank = 0, tanks = handler.getTanks(); tank < tanks; tank++) {
9494
STACK chemicalInTank = handler.getChemicalInTank(tank);
95-
tooltip.add(storedFunction.apply(chemicalInTank));
95+
tooltip.add(storedFunction.apply(chemicalInTank, emptyLangEntry));
9696
if (showAttributes) {
9797
ChemicalUtil.addAttributeTooltips(tooltip, chemicalInTank.getType());
9898
}
@@ -107,25 +107,25 @@ public static void addStoredFluid(@NotNull ItemStack stack, @NotNull List<Compon
107107
}
108108

109109
public static void addStoredFluid(@NotNull ItemStack stack, @NotNull List<Component> tooltip, boolean showMissingCap, ILangEntry emptyLangEntry) {
110-
addStoredFluid(stack, tooltip, showMissingCap, emptyLangEntry, stored -> {
110+
addStoredFluid(stack, tooltip, showMissingCap, emptyLangEntry, (stored, emptyLang) -> {
111111
if (stored.isEmpty()) {
112-
return emptyLangEntry.translateColored(EnumColor.GRAY);
112+
return emptyLang.translateColored(EnumColor.GRAY);
113113
}
114114
return MekanismLang.STORED.translateColored(EnumColor.ORANGE, EnumColor.ORANGE, stored, EnumColor.GRAY,
115115
MekanismLang.GENERIC_MB.translate(TextUtils.format(stored.getAmount())));
116116
});
117117
}
118118

119119
public static void addStoredFluid(@NotNull ItemStack stack, @NotNull List<Component> tooltip, boolean showMissingCap, ILangEntry emptyLangEntry,
120-
Function<FluidStack, Component> storedFunction) {
120+
BiFunction<FluidStack, ILangEntry, Component> storedFunction) {
121121
IFluidHandlerItem handler = Capabilities.FLUID.getCapability(stack);
122122
if (handler == null) {
123123
//Fall back to trying to look up the stored fluid by the container type if the stack doesn't expose it
124124
handler = (IFluidHandlerItem) ContainerType.FLUID.getAttachmentIfPresent(stack);
125125
}
126126
if (handler != null) {
127127
for (int tank = 0, tanks = handler.getTanks(); tank < tanks; tank++) {
128-
tooltip.add(storedFunction.apply(handler.getFluidInTank(tank)));
128+
tooltip.add(storedFunction.apply(handler.getFluidInTank(tank), emptyLangEntry));
129129
}
130130
} else if (showMissingCap) {
131131
tooltip.add(emptyLangEntry.translate());

src/main/java/mekanism/common/util/TransporterUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static void drop(LogisticalTransporterBase transporter, TransporterStack
7575
blockPos = transporter.getBlockPos();
7676
}
7777
TransporterManager.remove(transporter.getLevel(), stack);
78-
InventoryUtils.dropStack(stack.itemStack, item -> Block.popResource(transporter.getLevel(), blockPos, item));
78+
InventoryUtils.dropStack(transporter.getLevel(), blockPos, null, stack.itemStack, (level, pos, ignored, item) -> Block.popResource(level, pos, item));
7979
}
8080

8181
public static float[] getStackPosition(LogisticalTransporterBase transporter, TransporterStack stack, float partial) {

0 commit comments

Comments
 (0)