1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
package com.notnite.gloppers.mixin;
import net.minecraft.block.entity.Hopper;
import net.minecraft.block.entity.HopperBlockEntity;
import net.minecraft.entity.ItemEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.Direction;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(HopperBlockEntity.class)
public abstract class HopperBlockEntityMixin {
@Unique
private static int dirtySlotState = 0;
@Shadow
private DefaultedList<ItemStack> inventory;
@Unique
private static boolean canTransfer(Inventory to, ItemStack stack) {
try {
if (to instanceof HopperBlockEntity) {
var hopperName = ((HopperBlockEntity) to).getName().copyContentOnly().getString();
var itemRegistryEntry = stack.getRegistryEntry().getKey();
if (itemRegistryEntry.isEmpty()) return false;
var itemName = itemRegistryEntry.get().getValue().getPath();
if (hopperName.startsWith("!")) {
var globs = hopperName.substring(1).split(",");
for (var glob : globs) {
var strippedGlob = glob.replaceAll("[^a-zA-Z0-9_*?]", "");
var regex = strippedGlob.replace(".", "\\.").replace("*", ".*").replace("?", ".");
if (itemName.matches(regex)) return true;
}
// No globs matched, so don't transfer
return false;
}
}
} catch (Exception e) {
// ignored
}
// Doesn't have a glob (or exception), so transfer
return true;
}
// We can't decrement the item stack before it gets passed to transfer, because we may not be allowed to transfer,
// eating an item. We know removeStack will get called immediately before transfer, so while it's messy, we can use
// a static variable to keep track of what slot we're removing from.
@Redirect(
method = "insert",
at = @At(value = "INVOKE", target = "Lnet/minecraft/inventory/Inventory;removeStack(II)Lnet/minecraft/item/ItemStack;")
)
private static ItemStack insert$gloppersRemoveStack(Inventory instance, int slot, int amount) {
dirtySlotState = slot;
return instance.getStack(slot);
}
// This works by mixing into the call of HopperBlockEntity::transfer and returning a stubbed item stack if it's not
// allowed to transfer. I wanted to instead insert a continue statement into the for loop, but I couldn't figure out
// a good way to do that.
@Redirect(
method = "insert",
at = @At(value = "INVOKE", target = "Lnet/minecraft/block/entity/HopperBlockEntity;transfer(Lnet/minecraft/inventory/Inventory;Lnet/minecraft/inventory/Inventory;Lnet/minecraft/item/ItemStack;Lnet/minecraft/util/math/Direction;)Lnet/minecraft/item/ItemStack;")
)
private static ItemStack insert$gloppersTransfer(
Inventory from, Inventory to, ItemStack stack, Direction side
) {
if (!canTransfer(to, stack)) {
// The return value of this is only used to check if it's empty, and if so, returns that it succeeded.
// We can just return the item stack we were given, as we didn't remove from it.
return stack;
}
// Make sure to remove the item here, because we didn't in the actual call.
return HopperBlockEntity.transfer(from, to, from.removeStack(dirtySlotState, 1), side);
}
// This handles the case where a hopper extracts from a hopper above it (such as two anvils facing forward, stacked
// on top of one another).
@Inject(
method = "extract(Lnet/minecraft/block/entity/Hopper;Lnet/minecraft/inventory/Inventory;ILnet/minecraft/util/math/Direction;)Z",
at = @At("HEAD"),
cancellable = true
)
private static void extract(Hopper hopper, Inventory inventory, int slot, Direction
side, CallbackInfoReturnable<Boolean> cir) {
var item = inventory.getStack(slot);
if (!canTransfer(hopper, item)) cir.setReturnValue(false);
}
// This handles the case where an item entity is dropped onto the hopper from above.
@Inject(
method = "extract(Lnet/minecraft/inventory/Inventory;Lnet/minecraft/entity/ItemEntity;)Z",
at = @At("HEAD"),
cancellable = true
)
private static void extract(Inventory inventory, ItemEntity itemEntity, CallbackInfoReturnable<Boolean> cir) {
if (!canTransfer(inventory, itemEntity.getStack())) cir.setReturnValue(false);
}
}
|