Skip to content

Commit

Permalink
feat: implement controller based crafting CPU
Browse files Browse the repository at this point in the history
  • Loading branch information
leytilera committed Jan 28, 2023
1 parent e373486 commit a4003cf
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/api/java/appeng/api/networking/IControllerCache.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package appeng.api.networking;

import appeng.api.config.Actionable;
import appeng.api.storage.data.IAEItemStack;
import java.util.Set;

import appeng.api.networking.crafting.ICraftingCPU;

public interface IControllerCache extends IGridCache {

Expand All @@ -15,6 +16,6 @@ public interface IControllerCache extends IGridCache {

IGridHost getController();

boolean requestCrafting(IAEItemStack stack, Actionable actionable);
Set<ICraftingCPU> getCPUs();

}
8 changes: 8 additions & 0 deletions src/main/java/appeng/block/legacy/BlockLegacyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import appeng.core.sync.GuiBridge;
import appeng.tile.legacy.TileLegacyController;
import appeng.util.Platform;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
Expand Down Expand Up @@ -44,6 +45,13 @@ public boolean onActivated(
return true;
}

@Override
public void breakBlock(World w, int x, int y, int z, Block a, int b) {
TileLegacyController tile = this.getTileEntity(w, x, y, z);
tile.breakBlock();
super.breakBlock(w, x, y, z, a, b);
}

@Override
public IIcon getIcon(IBlockAccess w, int x, int y, int z, int s) {
TileEntity te = w.getTileEntity(x, y, z);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private boolean isSimulation() {
dsp = GuiText.Simulation.getLocal();
} else {
dsp = this.ccc.getCpuAvailableBytes() > 0
? (GuiText.Bytes.getLocal() + ": " + this.ccc.getCpuAvailableBytes()
? (GuiText.Bytes.getLocal() + ": " + (this.ccc.getCpuAvailableBytes() == Long.MAX_VALUE ? "Infinite" : this.ccc.getCpuAvailableBytes())
+ " : " + GuiText.CoProcessors.getLocal() + ": "
+ this.ccc.getCpuCoProcessors())
: GuiText.Bytes.getLocal() + ": N/A : " + GuiText.CoProcessors.getLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

import java.util.concurrent.Future;

import appeng.api.config.Actionable;
import appeng.api.networking.IControllerCache;
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridHost;
import appeng.api.networking.IGridNode;
Expand Down Expand Up @@ -89,10 +87,9 @@ public void serverPacketData(
final ContainerOpenContext context = cca.getOpenContext();

IRequestGrid rg = g.getCache(IRequestGrid.class);
IControllerCache cgc = g.getCache(IControllerCache.class);
IAEItemStack leftover = rg.requestItems(cca.getItemToCraft());

if (leftover == null || cgc.requestCrafting(leftover, Actionable.MODULATE)) {
if (leftover == null) {
cca.closeGui();
return;
}
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/appeng/me/cache/ControllerGridCache.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package appeng.me.cache;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import appeng.api.config.Actionable;
Expand All @@ -9,9 +10,11 @@
import appeng.api.networking.IGridHost;
import appeng.api.networking.IGridNode;
import appeng.api.networking.IGridStorage;
import appeng.api.networking.crafting.ICraftingCPU;
import appeng.api.storage.data.IAEItemStack;
import appeng.core.AEConfig;
import appeng.core.features.AEFeature;
import appeng.me.cluster.implementations.InternalCraftingCPU;
import appeng.tile.legacy.TileLegacyController;
import appeng.tile.networking.TileController;

Expand Down Expand Up @@ -90,10 +93,16 @@ public IGridHost getController() {
}

@Override
public boolean requestCrafting(IAEItemStack stack, Actionable actionable) {
//ICraftingGrid craftingGrid = grid.getCache(ICraftingGrid.class);
//return craftingGrid.getCpus().isEmpty() && this.hasController();
return false; // TODO: implement legacy crafting
public Set<ICraftingCPU> getCPUs() {
Set<ICraftingCPU> cpus = new HashSet<>();
IGridHost h = getController();
if (h instanceof TileLegacyController) {
TileLegacyController controller = (TileLegacyController) h;
for (InternalCraftingCPU cpu : controller.cpus) {
cpus.add(cpu);
}
}
return cpus;
}

}
6 changes: 5 additions & 1 deletion src/main/java/appeng/me/cache/CraftingGridCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import appeng.api.config.AccessRestriction;
import appeng.api.config.Actionable;
import appeng.api.networking.IControllerCache;
import appeng.api.networking.IGrid;
import appeng.api.networking.IGridHost;
import appeng.api.networking.IGridNode;
Expand All @@ -52,6 +53,7 @@
import appeng.me.helpers.GenericInterestManager;
import appeng.tile.crafting.TileCraftingStorageTile;
import appeng.tile.crafting.TileCraftingTile;
import appeng.tile.legacy.TileLegacyController;
import appeng.util.ItemSorters;
import com.google.common.collect.*;
import net.minecraft.world.World;
Expand Down Expand Up @@ -181,7 +183,7 @@ public void addNode(final IGridNode gridNode, final IGridHost machine) {
}
}

if (machine instanceof TileCraftingTile) {
if (machine instanceof TileCraftingTile || machine instanceof TileLegacyController) {
this.updateList = true;
}

Expand Down Expand Up @@ -259,6 +261,8 @@ private void updatePatterns() {

private void updateCPUClusters() {
this.craftingCPUs.clear();
IControllerCache cc = this.grid.getCache(IControllerCache.class);
craftingCPUs.addAll(cc.getCPUs());

for (final IGridNode cst : this.grid.getMachines(TileCraftingStorageTile.class)) {
final TileCraftingStorageTile tile
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package appeng.me.cluster.implementations;

import appeng.api.networking.IGrid;
import appeng.api.networking.crafting.ICraftingGrid;
import appeng.api.networking.energy.IEnergyGrid;
import appeng.api.networking.security.BaseActionSource;
import appeng.api.networking.security.MachineSource;
import appeng.me.GridAccessException;
import appeng.me.cache.CraftingGridCache;
import appeng.tile.legacy.TileLegacyController;
import net.minecraft.world.World;

public class InternalCraftingCPU extends AbstractCraftingCPU {

private TileLegacyController controller;
private MachineSource actionSource;
private boolean isDestroyed = false;
private final int[] usedOps = new int[3];
private int cpuNum;

public InternalCraftingCPU(TileLegacyController controller, int cpuNum) {
this.controller = controller;
this.actionSource = new MachineSource(controller);
this.cpuNum = cpuNum;
}

@Override
public BaseActionSource getActionSource() {
return this.actionSource;
}

@Override
public long getAvailableStorage() {
return Long.MAX_VALUE;
}

@Override
public int getCoProcessors() {
return 0;
}

@Override
public String getName() {
return "Controller #" + this.cpuNum;
}

public void updateCPUNum(int num) {
this.cpuNum = num;
}

@Override
public void updateCraftingLogic(IGrid grid, IEnergyGrid eg, ICraftingGrid cc) {
if (!this.isActive()) {
return;
}

if (this.myLastLink != null) {
if (this.myLastLink.isCanceled()) {
this.myLastLink = null;
this.cancel();
}
}

if (this.isComplete) {
if (this.inventory.getItemList().isEmpty()) {
return;
}

this.storeItems();
return;
}

this.waiting = false;
if (this.waiting || this.tasks.isEmpty()) // nothing to do here...
{
return;
}

this.remainingOperations = 1
- (this.usedOps[0] + this.usedOps[1] + this.usedOps[2]);
final int started = this.remainingOperations;

if (this.remainingOperations > 0) {
do {
this.somethingChanged = false;
this.executeCrafting(eg, (CraftingGridCache)cc);
} while (this.somethingChanged && this.remainingOperations > 0);
}
this.usedOps[2] = this.usedOps[1];
this.usedOps[1] = this.usedOps[0];
this.usedOps[0] = started - this.remainingOperations;

if (this.remainingOperations > 0 && !this.somethingChanged) {
this.waiting = true;
}
}

@Override
public boolean isActive() {
return this.controller.getProxy().isActive();
}

@Override
public boolean isDestroyed() {
return this.isDestroyed;
}

public void destroy() {
this.isDestroyed = true;
}

@Override
protected IGrid getGrid() {
try {
return controller.getProxy().getGrid();
} catch (GridAccessException e) {
return null;
}
}

@Override
protected void markDirty() {
controller.markDirty();

}

@Override
protected void updateCPU() {

}

@Override
protected World getWorld() {
return controller.getWorldObj();
}

}
Loading

0 comments on commit a4003cf

Please sign in to comment.