-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #621 from ghzdude/gh/new-drawer-void-fix
Second Attempt for fixing Storage Drawers Voiding near full capacity
- Loading branch information
Showing
8 changed files
with
311 additions
and
1 deletion.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/mod/acgaming/universaltweaks/mods/storagedrawers/api/IAuxData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package mod.acgaming.universaltweaks.mods.storagedrawers.api; | ||
|
||
import it.unimi.dsi.fastutil.ints.Int2IntArrayMap; | ||
import it.unimi.dsi.fastutil.ints.Int2IntMap; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public interface IAuxData | ||
{ | ||
String KEY = "insertion_data"; | ||
|
||
void UT$setData(String key, Object value); | ||
|
||
@Nullable | ||
Object UT$getData(String key); | ||
|
||
@NotNull | ||
default Int2IntMap getOrCreateData() { | ||
Int2IntMap v = (Int2IntMap) UT$getData(KEY); | ||
if (v == null) { | ||
v = new Int2IntArrayMap(); | ||
UT$setData(KEY, v); | ||
} | ||
return v; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/mod/acgaming/universaltweaks/mods/storagedrawers/api/SlotGroupAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package mod.acgaming.universaltweaks.mods.storagedrawers.api; | ||
|
||
public interface SlotGroupAccessor | ||
{ | ||
int UT$getSlot(); | ||
|
||
int UT$getIndex(); | ||
|
||
Class<?> UT$getType(); | ||
} |
127 changes: 127 additions & 0 deletions
127
.../java/mod/acgaming/universaltweaks/mods/storagedrawers/mixin/UTDrawerControllerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package mod.acgaming.universaltweaks.mods.storagedrawers.mixin; | ||
|
||
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; | ||
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup; | ||
import com.jaquadro.minecraft.storagedrawers.block.tile.TileEntityController; | ||
import com.jaquadro.minecraft.storagedrawers.capabilities.DrawerItemRepository; | ||
import com.llamalad7.mixinextras.sugar.Local; | ||
import com.llamalad7.mixinextras.sugar.ref.LocalIntRef; | ||
import mod.acgaming.universaltweaks.mods.storagedrawers.api.IAuxData; | ||
import mod.acgaming.universaltweaks.mods.storagedrawers.api.SlotGroupAccessor; | ||
|
||
import net.minecraft.item.ItemStack; | ||
|
||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Iterator; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
|
||
@Mixin(value = TileEntityController.class, remap = false) | ||
public class UTDrawerControllerMixin { | ||
|
||
@Mixin(targets = "com.jaquadro.minecraft.storagedrawers.block.tile.TileEntityController$ItemRepository", | ||
remap = false) | ||
public static abstract class RepositoryMixin extends DrawerItemRepository { | ||
|
||
@Shadow | ||
@Final | ||
TileEntityController this$0; | ||
|
||
@Shadow | ||
protected abstract boolean hasAccess(IDrawerGroup group, IDrawer drawer); | ||
|
||
public RepositoryMixin(IDrawerGroup group) { | ||
super(group); | ||
} | ||
|
||
// should probably use unreflect for this | ||
@Unique | ||
Method UT$getGroupForSlotRecord = null; | ||
|
||
@Unique | ||
private IDrawerGroup UT$getDrawerGroup(TileEntityController controller, SlotGroupAccessor o) { | ||
try { | ||
if (UT$getGroupForSlotRecord == null) { | ||
UT$getGroupForSlotRecord = TileEntityController.class | ||
.getDeclaredMethod("getGroupForSlotRecord", o.UT$getType()); | ||
UT$getGroupForSlotRecord.setAccessible(true); | ||
} | ||
|
||
return (IDrawerGroup) UT$getGroupForSlotRecord.invoke(controller, o); | ||
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Inject(method = "insertItem", | ||
at = @At(value = "INVOKE_ASSIGN", | ||
target = "Ljava/util/Collection;iterator()Ljava/util/Iterator;"), | ||
cancellable = true) | ||
public void readData(ItemStack stack, boolean simulate, Predicate<ItemStack> predicate, | ||
CallbackInfoReturnable<ItemStack> cir, | ||
@Local Iterator<SlotGroupAccessor> iterator, | ||
@Local LocalIntRef amount, | ||
@Local Set<Integer> checkedSlots) { | ||
while (iterator.hasNext()) { | ||
SlotGroupAccessor record = iterator.next(); | ||
IDrawerGroup candidateGroup = UT$getDrawerGroup(this$0, record); | ||
if (candidateGroup != null) { | ||
IDrawer drawerx = candidateGroup.getDrawer(record.UT$getSlot()); | ||
if (!drawerx.isEmpty() && this.testPredicateInsert(drawerx, stack, predicate) && | ||
this.hasAccess(candidateGroup, drawerx)) { | ||
if (simulate) { | ||
int inserted = ((IAuxData) drawerx).getOrCreateData().get(record.UT$getSlot()); | ||
if (inserted + drawerx.getStoredItemCount() == drawerx.getMaxCapacity()) { | ||
continue; | ||
} | ||
} | ||
amount.set(simulate ? | ||
Math.max(amount.get() - drawerx.getAcceptingRemainingCapacity(), 0) : | ||
drawerx.adjustStoredItemCount(amount.get())); | ||
if (amount.get() == 0) { | ||
cir.setReturnValue(ItemStack.EMPTY); | ||
} | ||
|
||
if (simulate) { | ||
checkedSlots.add(record.UT$getIndex()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Mixin(targets = "com.jaquadro.minecraft.storagedrawers.block.tile.TileEntityController$SlotRecord", remap = false) | ||
private static abstract class SlotRecordMixin implements SlotGroupAccessor { | ||
|
||
@Shadow | ||
public int slot; | ||
|
||
@Shadow | ||
public int index; | ||
|
||
@Override | ||
public int UT$getSlot() { | ||
return slot; | ||
} | ||
|
||
@Override | ||
public int UT$getIndex() { | ||
return index; | ||
} | ||
|
||
@Override | ||
public Class<?> UT$getType() { | ||
return getClass(); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/mod/acgaming/universaltweaks/mods/storagedrawers/mixin/UTDrawerGroupMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package mod.acgaming.universaltweaks.mods.storagedrawers.mixin; | ||
|
||
|
||
import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.StandardDrawerGroup; | ||
import mod.acgaming.universaltweaks.mods.storagedrawers.api.IAuxData; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
|
||
@Mixin(value = StandardDrawerGroup.class, remap = false) | ||
public abstract class UTDrawerGroupMixin { | ||
|
||
@Mixin(value = StandardDrawerGroup.DrawerData.class, remap = false) | ||
public abstract static class DrawerDataMixin implements IAuxData | ||
{ | ||
|
||
@Shadow | ||
public abstract void setExtendedData(String key, Object data); | ||
|
||
@Shadow | ||
public abstract Object getExtendedData(String key); | ||
|
||
@Override | ||
public void UT$setData(String key, Object value) { | ||
setExtendedData(key, value); | ||
} | ||
|
||
@Override | ||
public @Nullable Object UT$getData(String key) { | ||
return getExtendedData(key); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...ain/java/mod/acgaming/universaltweaks/mods/storagedrawers/mixin/UTDrawerHandlerMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package mod.acgaming.universaltweaks.mods.storagedrawers.mixin; | ||
|
||
import mod.acgaming.universaltweaks.mods.storagedrawers.api.IAuxData; | ||
|
||
import net.minecraft.item.ItemStack; | ||
|
||
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; | ||
import com.jaquadro.minecraft.storagedrawers.capabilities.DrawerItemHandler; | ||
import com.llamalad7.mixinextras.sugar.Local; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
@Mixin(value = DrawerItemHandler.class, remap = false) | ||
public abstract class UTDrawerHandlerMixin { | ||
|
||
@Inject(method = "insertItemInternal", | ||
at = @At(value = "INVOKE_ASSIGN", | ||
target = "Lcom/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup;getDrawer(I)Lcom/jaquadro/minecraft/storagedrawers/api/storage/IDrawer;")) | ||
public void setData(int slot, ItemStack stack, boolean simulate, | ||
CallbackInfoReturnable<ItemStack> cir, | ||
@Local IDrawer drawer) { | ||
int inserted = drawer.isEmpty() ? | ||
drawer.getAcceptingMaxCapacity(stack) : | ||
drawer.getAcceptingRemainingCapacity(); | ||
|
||
if (drawer instanceof IAuxData) { | ||
((IAuxData) drawer).getOrCreateData().put(slot, inserted); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
.../java/mod/acgaming/universaltweaks/mods/storagedrawers/mixin/UTDrawerRepositoryMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package mod.acgaming.universaltweaks.mods.storagedrawers.mixin; | ||
|
||
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer; | ||
import com.jaquadro.minecraft.storagedrawers.capabilities.DrawerItemRepository; | ||
|
||
import com.llamalad7.mixinextras.sugar.Local; | ||
|
||
import mod.acgaming.universaltweaks.mods.storagedrawers.api.IAuxData; | ||
|
||
import net.minecraft.item.ItemStack; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import java.util.function.Predicate; | ||
|
||
@Mixin(value = DrawerItemRepository.class, remap = false) | ||
public class UTDrawerRepositoryMixin { | ||
|
||
@Inject(method = "insertItem", | ||
at = @At(value = "INVOKE_ASSIGN", | ||
target = "Lcom/jaquadro/minecraft/storagedrawers/api/storage/IDrawerGroup;getDrawer(I)Lcom/jaquadro/minecraft/storagedrawers/api/storage/IDrawer;"), | ||
cancellable = true) | ||
public void readData(ItemStack stack, boolean simulate, Predicate<ItemStack> predicate, | ||
CallbackInfoReturnable<ItemStack> cir, | ||
@Local IDrawer drawer, @Local(ordinal = 3) int slot) { | ||
if (drawer instanceof IAuxData) { | ||
int inserted = ((IAuxData) drawer).getOrCreateData().get(slot); | ||
if (simulate && inserted + drawer.getStoredItemCount() == drawer.getMaxCapacity()) { | ||
cir.setReturnValue(stack); | ||
} | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...n/java/mod/acgaming/universaltweaks/mods/storagedrawers/mixin/UTFractionalGroupMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package mod.acgaming.universaltweaks.mods.storagedrawers.mixin; | ||
|
||
|
||
import com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.FractionalDrawerGroup; | ||
import mod.acgaming.universaltweaks.mods.storagedrawers.api.IAuxData; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
|
||
@Mixin(value = FractionalDrawerGroup.class, remap = false) | ||
public abstract class UTFractionalGroupMixin { | ||
|
||
@Mixin(targets = "com.jaquadro.minecraft.storagedrawers.block.tile.tiledata.FractionalDrawerGroup$FractionalDrawer", | ||
remap = false) | ||
public static abstract class FractionalDataMixin implements IAuxData { | ||
|
||
@Shadow | ||
public abstract void setExtendedData(String key, Object data); | ||
|
||
@Shadow | ||
public abstract Object getExtendedData(String key); | ||
|
||
@Override | ||
public void UT$setData(String key, Object value) { | ||
setExtendedData(key, value); | ||
} | ||
|
||
@Override | ||
public @Nullable Object UT$getData(String key) { | ||
return getExtendedData(key); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters