Skip to content

Commit

Permalink
Merge pull request #4 from koiNoCirculation/add-allow-missing-mode
Browse files Browse the repository at this point in the history
添加缺失合成功能
  • Loading branch information
koiNoCirculation authored Sep 4, 2024
2 parents 32fa2fd + 883ae33 commit c3c5e8b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
50 changes: 33 additions & 17 deletions src/main/java/dev/youtiao/aemobile/blocks/TileAEMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import appeng.api.networking.crafting.ICraftingLink;
import appeng.api.networking.security.IActionHost;
import appeng.api.networking.security.MachineSource;
import appeng.api.networking.security.PlayerSource;
import appeng.api.networking.storage.IStorageGrid;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.IMEMonitor;
Expand All @@ -21,12 +22,15 @@
import appeng.util.item.AEItemStack;
import appeng.util.item.ItemList;
import com.google.common.collect.ImmutableList;
import com.mojang.authlib.GameProfile;
import dev.youtiao.aemobile.web.util.FakePlayerGetCraftFailure;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.util.ForgeDirection;
import org.apache.commons.lang3.tuple.Pair;

Expand Down Expand Up @@ -55,7 +59,7 @@

public class TileAEMonitor extends TileEntity {


private static final GameProfile FAKEUUID = new GameProfile( UUID.randomUUID(), "NIGGER");
public static Map<String, Set<PosTuple>> tilesInTheWorld = new HashMap<>();

private PosTuple posTuple;
Expand Down Expand Up @@ -148,13 +152,17 @@ public static class CraftRequest {

private int cpuId;

private boolean allowMissing;

private String nbt;

public CraftRequest(String item_name, int meta, long count, int cpuId) {
public CraftRequest(String item_name, int meta, long count, int cpuId, String nbt, boolean allowMissing) {
this.item_name = item_name;
this.meta = meta;
this.count = count;
this.cpuId = cpuId;
this.allowMissing = allowMissing;
this.nbt = nbt;
}

public String getItem_name() {
Expand All @@ -172,6 +180,10 @@ public int getMeta() {
public String getNbt() {
return nbt;
}

public boolean isAllowMissing() {
return allowMissing;
}
}

public static class PosTuple {
Expand Down Expand Up @@ -596,7 +608,7 @@ public Response<String> submitCraftJob(CraftRequest request) {
ICraftingGrid cg = null;
cg = gridProxyable.getProxy().getGrid().getCache(ICraftingGrid.class);
ItemStack itemStack = new ItemStack((Item) Item.itemRegistry.getObject(request.item_name), 1, request.meta);
itemStack.setTagCompound(readNBTFromBase64(request.nbt));
itemStack.setTagCompound(readNBTFromBase64(request.getNbt()));
AEItemStack aeItemStack = AEItemStack.create(itemStack);
aeItemStack.setStackSize(request.count);
ICraftingGrid finalCg = cg;
Expand All @@ -608,7 +620,7 @@ public Response<String> submitCraftJob(CraftRequest request) {
gridProxyable.getProxy().getGrid(),
new MachineSource((IActionHost) gridProxyable.getProxy().getMachine()),
aeItemStack,
CraftingMode.STANDARD,
request.allowMissing ? CraftingMode.IGNORE_MISSING : CraftingMode.STANDARD,
null);
} else {
jobFuture = finalCg.beginCraftingJob(
Expand All @@ -628,19 +640,23 @@ public Response<String> submitCraftJob(CraftRequest request) {
i++;
}
ICraftingJob iCraftingJob = jobFuture.get();

CraftingCPUCluster finalSelected = selected;
ICraftingLink g = finalCg.submitJob(
iCraftingJob,
null,
finalSelected,
true,
new MachineSource((IActionHost) gridProxyable.getProxy().getMachine()));
if (g != null) {
return Response.ofSuccess("Successfully submitted craft job");
} else {
return Response.ofError("Failed submitting craft job");
}
final CraftingCPUCluster finalSelected = selected;
FakePlayerGetCraftFailure errorRetriever = new FakePlayerGetCraftFailure((WorldServer) worldObj, FAKEUUID);
FutureTask<Response> r = new FutureTask<>(() -> {
ICraftingLink g = finalCg.submitJob(
iCraftingJob,
null,
finalSelected,
true,
new PlayerSource(errorRetriever, (IActionHost) gridProxyable.getProxy().getMachine()));
if (g != null) {
return Response.ofSuccess("Successfully submitted craft job");
} else {
return Response.ofError(errorRetriever.getErrorMessage());
}
});
tasks.add(r);
return r.get();
} catch (GridAccessException e) {
return Response.ofError(e.getMessage());
} catch (ExecutionException | InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

import javax.annotation.Nullable;
import java.time.Duration;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -166,15 +167,17 @@ public TileAEMonitor.Response startCraftingJob(@RequestParam String ownerUUID,
@RequestParam String item,
@RequestParam int meta,
@RequestParam long count,
@RequestParam int cpuId) {
@RequestParam int cpuId,
@RequestParam @Nullable String nbt,
@RequestParam boolean allowMissing) {
Set<TileAEMonitor.PosTuple> tuples = TileAEMonitor.tilesInTheWorld.get(ownerUUID.toLowerCase());
if (tuples == null) {
return TileAEMonitor.Response.ofError(String.format("UUID %s has not placed any ae monitor blocks in the world", ownerUUID));
}
if (tuples.contains(new TileAEMonitor.PosTuple(dimid, x, y, z))) {
TileEntity tileEntity = DimensionManager.getWorld(dimid).getTileEntity(x, y, z);
if (tileEntity != null) {
return ((TileAEMonitor)tileEntity).submitCraftJob(new TileAEMonitor.CraftRequest(item, meta, count, cpuId));
return ((TileAEMonitor)tileEntity).submitCraftJob(new TileAEMonitor.CraftRequest(item, meta, count, cpuId, nbt, allowMissing));
} else {
return TileAEMonitor.Response.ofError("Internal error");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.youtiao.aemobile.web.util;

import com.mojang.authlib.GameProfile;
import net.minecraft.util.IChatComponent;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.util.FakePlayer;

public class FakePlayerGetCraftFailure extends FakePlayer {
private String errorMessage = null;

public FakePlayerGetCraftFailure(WorldServer world, GameProfile name) {
super(world, name);
}
public String getErrorMessage() {
return errorMessage;
}
@Override
public void addChatMessage(IChatComponent p_145747_1_) {
errorMessage = p_145747_1_.getFormattedText();
}
}

0 comments on commit c3c5e8b

Please sign in to comment.