55import java .util .Collections ;
66import java .util .List ;
77import java .util .Optional ;
8- import java .util .function .Consumer ;
98import java .util .function .Function ;
109import mekanism .api .Action ;
1110import 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}
0 commit comments