fix: MAC now saves crafting jobs

- MAC is now AE2 multiblock
- MAC now only needs 1 channel
- MAC has a master assembler
This commit is contained in:
LordMZTE 2023-01-05 19:20:09 +01:00
parent e05bf29fa8
commit cf4610a333
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
6 changed files with 216 additions and 89 deletions

View file

@ -3,3 +3,4 @@
- legacy blocks aren't colorable
- BlockLegacyDisplays don't rotate their faces correctly on the top and bottom
- pattern encoder can create pattern with no inputs
- shift-clicking in the pattern encoder is less than ideal

View file

@ -26,8 +26,8 @@ import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEItemStack>, ICellProvider {
public class RequestGridCache
implements IRequestGrid, IMEInventoryHandler<IAEItemStack>, ICellProvider {
private IGrid grid;
private IStorageGrid storageGrid;
private Map<IAEItemStack, Requestable> requestable = new HashMap<>();
@ -38,9 +38,7 @@ public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEIt
}
@Override
public void onUpdateTick() {
}
public void onUpdateTick() {}
@Override
public void removeNode(IGridNode gridNode, IGridHost machine) {
@ -53,25 +51,19 @@ public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEIt
@Override
public void addNode(IGridNode gridNode, IGridHost machine) {
if (machine instanceof IRequestProvider) {
requestProviders.add((IRequestProvider)machine);
requestProviders.add((IRequestProvider) machine);
recalcRequestable();
}
}
@Override
public void onSplit(IGridStorage destinationStorage) {
}
public void onSplit(IGridStorage destinationStorage) {}
@Override
public void onJoin(IGridStorage sourceStorage) {
}
public void onJoin(IGridStorage sourceStorage) {}
@Override
public void populateGridStorage(IGridStorage destinationStorage) {
}
public void populateGridStorage(IGridStorage destinationStorage) {}
@MENetworkEventSubscribe
public void requestableChange(MENetworkRequestableChange event) {
@ -94,13 +86,15 @@ public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEIt
}
}
}
storageGrid.postAlterationOfStoredItems(StorageChannel.ITEMS, requestable.keySet(), new BaseActionSource());
storageGrid.postAlterationOfStoredItems(
StorageChannel.ITEMS, requestable.keySet(), new BaseActionSource()
);
}
@Override
public Set<IAEItemStack> getRequestableItems() {
Set<IAEItemStack> list = new HashSet<>();
for(IAEItemStack stack : requestable.keySet()) {
for (IAEItemStack stack : requestable.keySet()) {
list.add(stack.copy());
}
return list;
@ -108,12 +102,14 @@ public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEIt
@Override
public IAEItemStack requestItems(IAEItemStack stack) {
if (!requestable.containsKey(stack)) return stack;
if (!requestable.containsKey(stack))
return stack;
Requestable r = requestable.get(stack);
IAEItemStack toRequest = stack;
for(IRequestProvider provider : r.providers) {
if (toRequest == null) break;
for (IRequestProvider provider : r.providers) {
if (toRequest == null)
break;
toRequest = provider.requestStack(toRequest, Actionable.MODULATE);
}
@ -121,12 +117,14 @@ public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEIt
}
@Override
public IAEItemStack injectItems(IAEItemStack input, Actionable type, BaseActionSource src) {
public IAEItemStack
injectItems(IAEItemStack input, Actionable type, BaseActionSource src) {
return input;
}
@Override
public IAEItemStack extractItems(IAEItemStack request, Actionable mode, BaseActionSource src) {
public IAEItemStack
extractItems(IAEItemStack request, Actionable mode, BaseActionSource src) {
return null;
}
@ -179,7 +177,8 @@ public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEIt
}
@MENetworkEventSubscribe
public void afterCacheConstruction(final MENetworkPostCacheConstruction cacheConstruction) {
public void
afterCacheConstruction(final MENetworkPostCacheConstruction cacheConstruction) {
this.storageGrid = this.grid.getCache(IStorageGrid.class);
this.storageGrid.registerCellProvider(this);
}
@ -196,7 +195,6 @@ public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEIt
}
static class Requestable {
public Set<IRequestProvider> providers;
public long amount;
@ -212,7 +210,5 @@ public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEIt
public void increaseAmount(long amount) {
this.amount += amount;
}
}
}

View file

@ -2,7 +2,7 @@ package appeng.me.cluster;
import appeng.api.util.WorldCoord;
public interface IAssemblerMB {
public interface IAssemblerMB extends IAEMultiBlock {
WorldCoord getLocation();
IAssemblerCluster getCluster();

View file

@ -4,22 +4,13 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import appeng.api.config.Actionable;
import appeng.api.networking.IGridHost;
import appeng.api.networking.crafting.ICraftingPatternDetails;
import appeng.api.networking.security.MachineSource;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.util.WorldCoord;
import appeng.me.GridAccessException;
import appeng.me.cluster.IAssemblerCluster;
import appeng.tile.legacy.TileAssembler;
import appeng.tile.legacy.TileAssemblerMB;
import appeng.util.item.AEItemStack;
import com.google.common.collect.Iterators;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
public class AssemblerCluster implements IAssemblerCluster {
public WorldCoord min;
@ -31,62 +22,34 @@ public class AssemblerCluster implements IAssemblerCluster {
public List<TileAssemblerMB> mb = new ArrayList<>();
public List<TileAssembler> assemblers = new ArrayList<>();
public long inst;
public Job[] jobs;
public AssemblerCluster(WorldCoord _min, WorldCoord _max) {
this.min = _min;
this.max = _max;
}
public void onOperation() {
for (int i = 0; i < this.jobs.length; i++) {
if (this.jobs[i] == null)
continue;
ItemStack out = this.jobs[i].det.getOutput(
this.jobs[i].inv, this.assemblers.get(0).getWorldObj()
);
if (out != null) {
try {
IMEMonitor<IAEItemStack> inv
= this.assemblers.get(0).getProxy().getStorage().getItemInventory(
);
inv.injectItems(
AEItemStack.create(out),
Actionable.MODULATE,
new MachineSource(this.assemblers.get(0))
);
} catch (GridAccessException kek) {}
}
this.jobs[i] = null;
public void initMaster() {
if (this.getMaster().jobs == null
|| this.getMaster().jobs.length != this.accelerators) {
this.getMaster().jobs = new TileAssembler.Job[this.accelerators];
}
for (int i = 1; i < this.assemblers.size(); i++) {
// slaveify old masters
this.assemblers.get(i).jobs = null;
}
}
public void onOperation() {
this.getMaster().onOperation();
}
public TileAssembler getAssembler(int i) {
return this.assemblers.get(i);
}
public boolean addCraft(Job job) {
for (int i = 0; i < this.jobs.length; i++) {
if (this.jobs[i] == null) {
this.jobs[i] = job;
return true;
}
}
return false;
}
public boolean canCraft() {
for (Job j : this.jobs)
if (j == null)
return true;
return false;
return this.getMaster().canCraft();
}
public void destroy() {
@ -136,13 +99,7 @@ public class AssemblerCluster implements IAssemblerCluster {
);
}
public static class Job {
public ICraftingPatternDetails det;
public InventoryCrafting inv;
public Job(ICraftingPatternDetails det, InventoryCrafting inv) {
this.det = det;
this.inv = inv;
}
public TileAssembler getMaster() {
return this.getAssembler(0);
}
}

View file

@ -2,20 +2,29 @@ package appeng.tile.legacy;
import java.util.ArrayList;
import appeng.api.config.Actionable;
import appeng.api.implementations.ICraftingPatternItem;
import appeng.api.networking.GridFlags;
import appeng.api.networking.IAssemblerCache;
import appeng.api.networking.crafting.ICraftingPatternDetails;
import appeng.api.networking.crafting.ICraftingProvider;
import appeng.api.networking.crafting.ICraftingProviderHelper;
import appeng.api.networking.events.MENetworkCraftingPatternChange;
import appeng.api.networking.security.MachineSource;
import appeng.api.storage.IMEMonitor;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.util.WorldCoord;
import appeng.block.legacy.BlockAssemblerHeatVent;
import appeng.block.legacy.BlockAssemblerWall;
import appeng.container.ContainerNull;
import appeng.items.misc.ItemEncodedPattern;
import appeng.me.GridAccessException;
import appeng.me.cache.AssemblerGridCache;
import appeng.me.cluster.IAssemblerCluster;
import appeng.me.cluster.IAssemblerMB;
import appeng.me.cluster.implementations.AssemblerCluster;
import appeng.me.helpers.AENetworkProxy;
import appeng.me.helpers.AENetworkProxyMultiblock;
import appeng.tile.TileEvent;
import appeng.tile.events.TileEventType;
import appeng.tile.grid.AENetworkTile;
@ -26,12 +35,14 @@ import appeng.util.InventoryAdaptor;
import appeng.util.Platform;
import appeng.util.inv.AdaptorIInventory;
import appeng.util.inv.IInventoryDestination;
import appeng.util.item.AEItemStack;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
@ -41,11 +52,20 @@ public class TileAssembler extends AENetworkTile
implements IAssemblerMB, IAEAppEngInventory, IInventory, ICraftingProvider {
AssemblerCluster ac = null;
AppEngInternalInventory inv = new AppEngInternalInventory(this, 54);
public Job[] jobs;
public TileAssembler() {
super();
// TODO: WTF
//super.updatesOnPower = false;
this.getProxy().setFlags(GridFlags.REQUIRE_CHANNEL, GridFlags.MULTIBLOCK);
}
@Override
protected AENetworkProxy createProxy() {
return new AENetworkProxyMultiblock(
this, "proxy", this.getItemFromTile(this), true
);
}
@Override
@ -128,6 +148,7 @@ public class TileAssembler extends AENetworkTile
if (ac != null && lastMB != null) {
lastMB.sendUpdate(true, null);
ac.initMaster();
try {
IAssemblerCache cache
= lastMB.getProxy().getGrid().getCache(IAssemblerCache.class);
@ -194,8 +215,6 @@ public class TileAssembler extends AENetworkTile
}
}
ac.jobs = new AssemblerCluster.Job[ac.accelerators];
if (ac.assemblers.size() > 0) {
return ac;
} else {
@ -203,6 +222,14 @@ public class TileAssembler extends AENetworkTile
}
}
public boolean canCraft() {
for (Job j : this.jobs)
if (j == null)
return true;
return false;
}
public static boolean verifyUnownedRegionInner(
IBlockAccess w,
int minx,
@ -489,12 +516,48 @@ public class TileAssembler extends AENetworkTile
if (((ICraftingPatternItem) is.getItem())
.getPatternForItem(is, this.worldObj)
.equals(patternDetails)) {
return this.ac.addCraft(new AssemblerCluster.Job(patternDetails, table));
return this.ac.getMaster().addCraft(new Job(patternDetails, table));
}
}
return false;
}
public void onOperation() {
for (int i = 0; i < this.jobs.length; i++) {
if (this.jobs[i] == null)
continue;
ItemStack out = this.jobs[i].det.getOutput(this.jobs[i].inv, this.worldObj);
if (out != null) {
try {
IMEMonitor<IAEItemStack> inv
= this.getProxy().getStorage().getItemInventory();
inv.injectItems(
AEItemStack.create(out),
Actionable.MODULATE,
new MachineSource(this)
);
} catch (GridAccessException kek) {}
}
this.jobs[i] = null;
}
}
public boolean addCraft(Job job) {
for (int i = 0; i < this.jobs.length; i++) {
if (this.jobs[i] == null) {
this.jobs[i] = job;
return true;
}
}
return false;
}
@Override
public boolean isBusy() {
if (this.ac == null)
@ -520,13 +583,102 @@ public class TileAssembler extends AENetworkTile
}
}
public boolean isMaster() {
return this.jobs != null;
}
@TileEvent(TileEventType.WORLD_NBT_WRITE)
public void writeToNbtTileAssembler(NBTTagCompound nbt) {
this.inv.writeToNBT(nbt, "inv");
if (this.isMaster()) {
NBTTagList jobs = new NBTTagList();
for (Job j : this.jobs) {
if (j == null)
continue;
jobs.appendTag(j.writeToNBT(new NBTTagCompound()));
}
nbt.setTag("jobs", jobs);
nbt.setInteger("jobc", this.jobs.length);
}
}
@TileEvent(TileEventType.WORLD_NBT_READ)
public void readFromNbtTileAssembler(NBTTagCompound nbt) {
System.out.println("ALEC: " + nbt);
this.inv.readFromNBT(nbt, "inv");
if (nbt.hasKey("jobs")) {
NBTTagList jobs = nbt.getTagList("jobs", 10);
this.jobs = new Job[nbt.getInteger("jobc")];
for (int i = 0; i < jobs.tagCount(); i++) {
this.jobs[i] = Job.readFromNBT(jobs.getCompoundTagAt(i), this.worldObj);
}
}
}
public static class Job {
public ICraftingPatternDetails det;
public InventoryCrafting inv;
public Job(ICraftingPatternDetails det, InventoryCrafting inv) {
this.det = det;
this.inv = inv;
}
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
nbt.setTag("pattern", this.det.getPattern().writeToNBT(new NBTTagCompound()));
NBTTagList input = new NBTTagList();
for (int i = 0; i < this.inv.getSizeInventory(); i++) {
if (this.inv.getStackInSlot(i) == null) {
input.appendTag(new NBTTagCompound());
} else {
input.appendTag(
this.inv.getStackInSlot(i).writeToNBT(new NBTTagCompound())
);
}
}
nbt.setTag("input", input);
return nbt;
}
public static Job readFromNBT(NBTTagCompound nbt, World w) {
ItemStack patternStack
= ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("pattern"));
ICraftingPatternDetails det = ((ItemEncodedPattern) patternStack.getItem())
.getPatternForItem(patternStack, w);
NBTTagList input = nbt.getTagList("input", 10);
InventoryCrafting inv = new InventoryCrafting(new ContainerNull(), 3, 3);
for (int i = 0; i < input.tagCount(); i++) {
if (input.getCompoundTagAt(i).hasNoTags()) {
inv.setInventorySlotContents(i, null);
} else {
inv.setInventorySlotContents(
i, ItemStack.loadItemStackFromNBT(input.getCompoundTagAt(i))
);
}
}
return new Job(det, inv);
}
}
@Override
public void disconnect(boolean b) {
// TODO Auto-generated method stub
}
@Override
public boolean isValid() {
return this.isComplete();
}
}

View file

@ -3,10 +3,13 @@ package appeng.tile.legacy;
import java.util.List;
import appeng.api.implementations.ICraftingPatternItem;
import appeng.api.networking.GridFlags;
import appeng.api.networking.crafting.ICraftingPatternDetails;
import appeng.me.cluster.IAssemblerCluster;
import appeng.me.cluster.IAssemblerMB;
import appeng.me.cluster.implementations.AssemblerCluster;
import appeng.me.helpers.AENetworkProxy;
import appeng.me.helpers.AENetworkProxyMultiblock;
import appeng.tile.TileEvent;
import appeng.tile.events.TileEventType;
import appeng.tile.grid.AENetworkTile;
@ -29,6 +32,7 @@ public class TileAssemblerMB extends AENetworkTile implements IAssemblerMB, IInv
// TODO: WTF
//super.updatesOnPower = false;
this.getProxy().setIdlePowerUsage(0.0);
this.getProxy().setFlags(GridFlags.REQUIRE_CHANNEL, GridFlags.MULTIBLOCK);
}
@Override
@ -36,6 +40,13 @@ public class TileAssemblerMB extends AENetworkTile implements IAssemblerMB, IInv
return false;
}
@Override
protected AENetworkProxy createProxy() {
return new AENetworkProxyMultiblock(
this, "proxy", this.getItemFromTile(this), true
);
}
// TODO: WTF
//protected void terminate() {
// super.terminate();
@ -402,4 +413,14 @@ public class TileAssemblerMB extends AENetworkTile implements IAssemblerMB, IInv
@Override
public void getDrops(World w, int x, int y, int z, List<ItemStack> drops) {}
@Override
public void disconnect(boolean b) {
// TODO Auto-generated method stub
}
@Override
public boolean isValid() {
return this.isComplete();
}
}