Skip to content

Commit fb90bc8

Browse files
committed
Improved logistics manager to handle empty requests from redstone requesters
1 parent 9464f73 commit fb90bc8

6 files changed

Lines changed: 45 additions & 36 deletions

File tree

README.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
# ![deployer.png](src/main/resources/deployer.png)
22
Deployer is a [Create]() library addon built to bring stability over delicate non-modular code in create.
33

4-
A lot of features, like packages,
5-
factory gauges and others are often coded in create without thinking about possible addons that want to expand that feature,
6-
and here is where we come on.
7-
8-
Deployer aims to bring stability over this topic, by modifying create code in a very precise way:
9-
10-
- We do not overwrite any of the original function, which is the main reason of mod incompatibility
11-
- Every code injection is as precise as possible, and studied to be compatible with eventual code changes
12-
- We leave create's code implementation the same on most things, so that other mods will not find any strange behaviour on their blocks
4+
Our philosophy is "less compatibility, more stability" which makes every feature in the mod coded while thinking about any kind of modification from other mods. We prefer other mods to just not support our features, instead of just crashing.
5+
**This doesn't mean we don't care about compatibility! Please let us know if theres any mod you want to see compatible with Deployer!**
136

7+
### Deployer is still in beta and might still cause crashes with other mods. We will work on that 24h/7
148

159
## Features
1610

@@ -41,4 +35,4 @@ This is configurable through the server config
4135

4236
<img src="fluid_fix.png" style="width:300px"> For example, cauldrons couldn't be handled before
4337

44-
## Please remember that Deployer is still under development and under his first stages
38+
### Please remember that Deployer is still under development and under his first stages

src/main/java/net/liukrast/deployer/lib/DeployerWailaPlugin.java renamed to src/main/java/net/liukrast/deployer/lib/compat/WailaCompat.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package net.liukrast.deployer.lib;
1+
package net.liukrast.deployer.lib.compat;
22

33
import com.simibubi.create.AllBlocks;
44
import com.simibubi.create.content.logistics.factoryBoard.FactoryPanelBlock;
@@ -12,7 +12,7 @@
1212

1313
@SuppressWarnings("unused")
1414
@WailaPlugin
15-
public class DeployerWailaPlugin implements IWailaPlugin {
15+
public class WailaCompat implements IWailaPlugin {
1616

1717
@Override
1818
public void registerClient(IWailaClientRegistration registration) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package net.liukrast.deployer.lib.helper.extensions;
2+
3+
public interface PRHExtension {
4+
boolean isOrderComplete(int orderId);
5+
}

src/main/java/net/liukrast/deployer/lib/logistics/packagerLink/LogisticsGenericManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ public static <K,V,H> boolean broadcastPackageRequest(StockInventoryType<K,V,H>
9898
* */
9999
@SuppressWarnings("unchecked")
100100
public static boolean broadcastAllPackageRequest(PackageOrderWithCrafts defaultOrder, UUID freqId, LogisticallyLinkedBehaviour.RequestType type, Map<StockInventoryType<?,?,?>, GenericOrderContained<?>> rq, String address) {
101-
if(defaultOrder.isEmpty() && rq.values().stream().allMatch(GenericOrderContained::isEmpty))
101+
boolean stockOrdersEmpty = rq.values().stream().allMatch(GenericOrderContained::isEmpty);
102+
if(defaultOrder.isEmpty() && stockOrdersEmpty)
102103
return false;
103104
Multimap<PackagerBlockEntity, PackagingRequest> requests = LogisticsManager.findPackagersForRequest(freqId, defaultOrder, null, address);
104105

@@ -111,11 +112,12 @@ public static boolean broadcastAllPackageRequest(PackageOrderWithCrafts defaultO
111112
int id = vals.isEmpty() ? LogisticsManagerAccessor.getR().nextInt() :
112113
Optional.ofNullable(vals.iterator().next()).map(PackagingRequest::orderId)
113114
.orElseGet(() -> LogisticsManagerAccessor.getR().nextInt());
114-
if(!rq.isEmpty()) {
115+
if(!rq.isEmpty() && !stockOrdersEmpty) {
115116
int index = vals.isEmpty() ? 0 : 1;
116117
Iterator<Map.Entry<StockInventoryType<?, ?, ?>, GenericOrderContained<?>>> it = rq.entrySet().iterator();
117118
while(it.hasNext()) {
118119
Map.Entry<StockInventoryType<?, ?, ?>, GenericOrderContained<?>> entry = it.next();
120+
if(rq.isEmpty()) continue;
119121
boolean isLast = !it.hasNext();
120122
if(broadcastPackageRequest((StockInventoryType<Object, Object, Object>) entry.getKey(), freqId, type, (GenericOrderContained<Object>) entry.getValue(), null, address, () -> id, index, isLast))
121123
index++;

src/main/java/net/liukrast/deployer/lib/mixin/PackageRepackageHelperMixin.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
public abstract class PackageRepackageHelperMixin implements PRHExtension {
3838

3939
@Unique
40-
private final Map<StockInventoryType<?,?,?>, Map<Integer, List<ItemStack>>> deployer$collectedPackages = new HashMap<>();
40+
private final Map<StockInventoryType<?, ?, ?>, Map<Integer, List<ItemStack>>> deployer$collectedPackages = new HashMap<>();
4141

4242
@Inject(method = "clear", at = @At("TAIL"))
4343
private void clear(CallbackInfo ci) {
@@ -46,28 +46,28 @@ private void clear(CallbackInfo ci) {
4646

4747
@Inject(method = "isFragmented", at = @At("RETURN"), cancellable = true)
4848
private void isFragmented(ItemStack box, CallbackInfoReturnable<Boolean> cir) {
49-
if(!(box.getItem() instanceof GenericPackageItem generic)) return;
49+
if (!(box.getItem() instanceof GenericPackageItem generic)) return;
5050
cir.setReturnValue(box.has(generic.getType().packageHandler().packageOrderData()));
5151
}
5252

5353
@ModifyVariable(method = "addPackageFragment", at = @At("STORE"), name = "collectedOrder")
5454
private List<ItemStack> addPackageFragment(List<ItemStack> value, @Local(argsOnly = true, name = "arg1") ItemStack box, @Local(name = "collectedOrderId") int collectedOrderId) {
55-
if(!(box.getItem() instanceof GenericPackageItem item)) return value;
55+
if (!(box.getItem() instanceof GenericPackageItem item)) return value;
5656
return deployer$collectedPackages
5757
.computeIfAbsent(item.getType(), $ -> new HashMap<>())
5858
.computeIfAbsent(collectedOrderId, $ -> Lists.newArrayList());
5959
}
6060

6161
@WrapOperation(method = "addPackageFragment", at = @At(value = "INVOKE", target = "Lcom/simibubi/create/content/logistics/packager/repackager/PackageRepackageHelper;isOrderComplete(I)Z"))
6262
private boolean addPackageFragment(PackageRepackageHelper instance, int orderId, Operation<Boolean> original, @Local(argsOnly = true, name = "arg1") ItemStack box) {
63-
if(!(box.getItem() instanceof GenericPackageItem item)) return original.call(instance, orderId);
63+
if (!(box.getItem() instanceof GenericPackageItem item)) return original.call(instance, orderId);
6464
return deployer$isOrderComplete(item.getType(), orderId);
6565
}
6666

6767
@Inject(method = "repack", at = @At("RETURN"))
6868
private void repack(int orderId, RandomSource r, CallbackInfoReturnable<List<BigItemStack>> cir) {
6969
var list = cir.getReturnValue();
70-
for(StockInventoryType<?,?,?> type : deployer$collectedPackages.keySet()) {
70+
for (StockInventoryType<?, ?, ?> type : deployer$collectedPackages.keySet()) {
7171
list.addAll(deployer$repack(type, orderId, r));
7272
}
7373
}
@@ -80,15 +80,15 @@ private void repack(int orderId, RandomSource r, CallbackInfoReturnable<List<Big
8080
GenericOrderContained<V> orderContext = null;
8181
AbstractInventorySummary<K, V> summary = type.networkHandler().createSummary();
8282
var li = deployer$collectedPackages.computeIfAbsent(type, $ -> new HashMap<>()).get(orderId);
83-
if(li != null) {
83+
if (li != null) {
8484
for (ItemStack box : li) {
8585
address = PackageItem.getAddress(box);
8686
var c = box.get(DeployerDataComponents.ORDER_STOCK_TYPE_DATA);
87-
if(c != null) typeData = c;
87+
if (c != null) typeData = c;
8888
var comp = type.packageHandler().packageOrderData();
8989
if (box.has(comp)) {
9090
var compGot = box.get(comp);
91-
if(compGot != null) {
91+
if (compGot != null) {
9292
GenericOrderContained<V> context = compGot.orderContext();
9393
if (context != null && !context.isEmpty())
9494
orderContext = context;
@@ -136,7 +136,7 @@ private void repack(int orderId, RandomSource r, CallbackInfoReturnable<List<Big
136136
continue;
137137
if (targetedEntry != null) {
138138
targetAmount = type.valueHandler().getCount(targetedEntry);
139-
if(!type.valueHandler().equalsIgnoreCount(entry, targetedEntry))
139+
if (!type.valueHandler().equalsIgnoreCount(entry, targetedEntry))
140140
continue;
141141
}
142142

@@ -148,7 +148,7 @@ private void repack(int orderId, RandomSource r, CallbackInfoReturnable<List<Big
148148

149149
V output = type.valueHandler().copyWithCount(entry, removedAmount);
150150
targetAmount -= removedAmount;
151-
if(targetedEntry != null)
151+
if (targetedEntry != null)
152152
type.valueHandler().setCount(targetedEntry, targetAmount);
153153
type.valueHandler().setCount(entry, type.valueHandler().getCount(entry) - removedAmount);
154154
outputSlots.add(output);
@@ -180,7 +180,7 @@ private void repack(int orderId, RandomSource r, CallbackInfoReturnable<List<Big
180180

181181
for (BigItemStack box : exportingPackages) {
182182
PackageItem.addAddress(box.stack, address);
183-
if(typeData != null) box.stack.set(DeployerDataComponents.ORDER_STOCK_TYPE_DATA, typeData);
183+
if (typeData != null) box.stack.set(DeployerDataComponents.ORDER_STOCK_TYPE_DATA, typeData);
184184
}
185185

186186
for (int i = 0; i < exportingPackages.size(); i++) {
@@ -196,7 +196,7 @@ private void repack(int orderId, RandomSource r, CallbackInfoReturnable<List<Big
196196
}
197197

198198
@Override
199-
public <K,V,H> boolean deployer$isOrderComplete(StockInventoryType<K, V, H> type, int orderId) {
199+
public <K, V, H> boolean deployer$isOrderComplete(StockInventoryType<K, V, H> type, int orderId) {
200200
boolean finalLinkReached = false;
201201
Links:
202202
for (int linkCounter = 0; linkCounter < 1000; linkCounter++) {
@@ -226,17 +226,17 @@ private void repack(int orderId, RandomSource r, CallbackInfoReturnable<List<Big
226226
private List<BigItemStack> repackBasedOnRecipes(List<BigItemStack> original) {
227227
List<ItemStack> copied = original.stream().map(big -> big.stack.copy()).toList();
228228
List<ItemStack> result = NonNullList.withSize(copied.size(), ItemStack.EMPTY);
229-
for(var stack : copied) {
230-
for(int i = 0; i < result.size(); i++) {
229+
for (var stack : copied) {
230+
for (int i = 0; i < result.size(); i++) {
231231
var slot = result.get(i);
232-
if(slot.isEmpty()) {
232+
if (slot.isEmpty()) {
233233
result.set(i, stack);
234234
break;
235-
} else if(ItemStack.isSameItemSameComponents(stack, slot)) {
235+
} else if (ItemStack.isSameItemSameComponents(stack, slot)) {
236236
int canPut = slot.getMaxStackSize() - slot.getCount();
237-
slot.setCount(slot.getCount() + Mth.clamp(stack.getCount(),0,canPut));
238-
stack.setCount(Math.max(stack.getCount()-canPut, 0));
239-
if(stack.getCount() == 0) break;
237+
slot.setCount(slot.getCount() + Mth.clamp(stack.getCount(), 0, canPut));
238+
stack.setCount(Math.max(stack.getCount() - canPut, 0));
239+
if (stack.getCount() == 0) break;
240240
}
241241
}
242242
}
@@ -247,4 +247,12 @@ private List<BigItemStack> repackBasedOnRecipes(List<BigItemStack> original) {
247247
private ItemStack repackBasedOnRecipes(ItemStack instance, int i, Operation<ItemStack> original) {
248248
return instance.copy();
249249
}
250+
251+
@Inject(method = "isOrderComplete", at = @At("HEAD"), cancellable = true)
252+
private void isOrderComplete(int orderId, CallbackInfoReturnable<Boolean> cir) {
253+
if(this instanceof net.liukrast.deployer.lib.helper.extensions.PRHExtension prh) {
254+
cir.setReturnValue(prh.isOrderComplete(orderId));
255+
cir.cancel();
256+
}
257+
}
250258
}

src/main/java/net/liukrast/deployer/lib/mixin/RedstoneRequesterBlockEntityMixin.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public abstract class RedstoneRequesterBlockEntityMixin extends StockCheckingBlo
4343
@Unique
4444
private final Map<StockInventoryType<?,?,?>, GenericOrderContained<?>> deployer$encodedRequests = new HashMap<>();
4545

46+
@Deprecated
4647
@Unique
4748
private boolean deployer$triggerRequest$local$anySucceeded;
4849

@@ -58,7 +59,7 @@ public RedstoneRequesterBlockEntityMixin(BlockEntityType<?> type, BlockPos pos,
5859
@SuppressWarnings("unchecked")
5960
@Override
6061
public <K, V, H> GenericOrderContained<V> deployer$getEncodedRequest(StockInventoryType<K, V, H> type) {
61-
return (GenericOrderContained<V>) deployer$encodedRequests.computeIfAbsent(type, t -> GenericOrderContained.empty());
62+
return (GenericOrderContained<V>) deployer$encodedRequests.getOrDefault(type, GenericOrderContained.empty());
6263
}
6364

6465
@Inject(method = "triggerRequest", at = @At("HEAD"), cancellable = true)
@@ -70,10 +71,9 @@ private void triggerRequest(CallbackInfo ci) {
7071
this.deployer$triggerRequest$local$anySucceeded = anySucceeded;
7172
}
7273

73-
@SuppressWarnings("unchecked")
7474
@Unique
7575
private <K,V,H> boolean deployer$triggerRequest(StockInventoryType<K, V, H> type, CallbackInfo ci) {
76-
GenericOrderContained<V> encodedRequest = (GenericOrderContained<V>) deployer$encodedRequests.computeIfAbsent(type, t -> GenericOrderContained.empty());
76+
GenericOrderContained<V> encodedRequest = deployer$getEncodedRequest(type);
7777

7878
if(encodedRequest.isEmpty())
7979
return false;

0 commit comments

Comments
 (0)