feat: implement RequestGridCache
This commit is contained in:
parent
bb0c32d2c0
commit
50879c2147
|
@ -1,5 +1,8 @@
|
||||||
package appeng.api.networking;
|
package appeng.api.networking;
|
||||||
|
|
||||||
|
import appeng.api.config.Actionable;
|
||||||
|
import appeng.api.storage.data.IAEItemStack;
|
||||||
|
|
||||||
public interface IControllerCache extends IGridCache {
|
public interface IControllerCache extends IGridCache {
|
||||||
|
|
||||||
boolean requiresChannels();
|
boolean requiresChannels();
|
||||||
|
@ -11,5 +14,7 @@ public interface IControllerCache extends IGridCache {
|
||||||
boolean hasController();
|
boolean hasController();
|
||||||
|
|
||||||
IGridHost getController();
|
IGridHost getController();
|
||||||
|
|
||||||
|
boolean requestCrafting(IAEItemStack stack, Actionable actionable);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
package appeng.api.networking.events;
|
||||||
|
|
||||||
|
public class MENetworkRequestableChange extends MENetworkEvent {
|
||||||
|
|
||||||
|
}
|
|
@ -20,6 +20,8 @@ package appeng.core.sync.packets;
|
||||||
|
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
import appeng.api.config.Actionable;
|
||||||
|
import appeng.api.networking.IControllerCache;
|
||||||
import appeng.api.networking.IGrid;
|
import appeng.api.networking.IGrid;
|
||||||
import appeng.api.networking.IGridHost;
|
import appeng.api.networking.IGridHost;
|
||||||
import appeng.api.networking.IGridNode;
|
import appeng.api.networking.IGridNode;
|
||||||
|
@ -85,13 +87,12 @@ public class PacketCraftRequest extends AppEngPacket {
|
||||||
|
|
||||||
cca.getItemToCraft().setStackSize(this.amount);
|
cca.getItemToCraft().setStackSize(this.amount);
|
||||||
final ContainerOpenContext context = cca.getOpenContext();
|
final ContainerOpenContext context = cca.getOpenContext();
|
||||||
if (context == null) return;
|
|
||||||
|
|
||||||
IRequestGrid rg = g.getCache(IRequestGrid.class);
|
IRequestGrid rg = g.getCache(IRequestGrid.class);
|
||||||
|
IControllerCache cgc = g.getCache(IControllerCache.class);
|
||||||
IAEItemStack leftover = rg.requestItems(cca.getItemToCraft());
|
IAEItemStack leftover = rg.requestItems(cca.getItemToCraft());
|
||||||
|
|
||||||
if (leftover == null) {
|
if (leftover == null || cgc.requestCrafting(leftover, Actionable.MODULATE)) {
|
||||||
final TileEntity te = context.getTile();
|
|
||||||
cca.closeGui();
|
cca.closeGui();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,13 @@ package appeng.me.cache;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import appeng.api.config.Actionable;
|
||||||
import appeng.api.networking.IControllerCache;
|
import appeng.api.networking.IControllerCache;
|
||||||
import appeng.api.networking.IGrid;
|
import appeng.api.networking.IGrid;
|
||||||
import appeng.api.networking.IGridHost;
|
import appeng.api.networking.IGridHost;
|
||||||
import appeng.api.networking.IGridNode;
|
import appeng.api.networking.IGridNode;
|
||||||
import appeng.api.networking.IGridStorage;
|
import appeng.api.networking.IGridStorage;
|
||||||
|
import appeng.api.storage.data.IAEItemStack;
|
||||||
import appeng.core.AEConfig;
|
import appeng.core.AEConfig;
|
||||||
import appeng.core.features.AEFeature;
|
import appeng.core.features.AEFeature;
|
||||||
import appeng.tile.legacy.TileLegacyController;
|
import appeng.tile.legacy.TileLegacyController;
|
||||||
|
@ -86,5 +88,12 @@ public class ControllerGridCache implements IControllerCache {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,14 +9,13 @@ import java.util.Set;
|
||||||
|
|
||||||
import appeng.api.config.AccessRestriction;
|
import appeng.api.config.AccessRestriction;
|
||||||
import appeng.api.config.Actionable;
|
import appeng.api.config.Actionable;
|
||||||
import appeng.api.networking.IControllerCache;
|
|
||||||
import appeng.api.networking.IGrid;
|
import appeng.api.networking.IGrid;
|
||||||
import appeng.api.networking.IGridHost;
|
import appeng.api.networking.IGridHost;
|
||||||
import appeng.api.networking.IGridNode;
|
import appeng.api.networking.IGridNode;
|
||||||
import appeng.api.networking.IGridStorage;
|
import appeng.api.networking.IGridStorage;
|
||||||
import appeng.api.networking.crafting.ICraftingGrid;
|
|
||||||
import appeng.api.networking.events.MENetworkEventSubscribe;
|
import appeng.api.networking.events.MENetworkEventSubscribe;
|
||||||
import appeng.api.networking.events.MENetworkPostCacheConstruction;
|
import appeng.api.networking.events.MENetworkPostCacheConstruction;
|
||||||
|
import appeng.api.networking.events.MENetworkRequestableChange;
|
||||||
import appeng.api.networking.request.IRequestGrid;
|
import appeng.api.networking.request.IRequestGrid;
|
||||||
import appeng.api.networking.request.IRequestProvider;
|
import appeng.api.networking.request.IRequestProvider;
|
||||||
import appeng.api.networking.security.BaseActionSource;
|
import appeng.api.networking.security.BaseActionSource;
|
||||||
|
@ -26,14 +25,12 @@ import appeng.api.storage.IMEInventoryHandler;
|
||||||
import appeng.api.storage.StorageChannel;
|
import appeng.api.storage.StorageChannel;
|
||||||
import appeng.api.storage.data.IAEItemStack;
|
import appeng.api.storage.data.IAEItemStack;
|
||||||
import appeng.api.storage.data.IItemList;
|
import appeng.api.storage.data.IItemList;
|
||||||
import appeng.util.item.AEItemStack;
|
|
||||||
|
|
||||||
public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEItemStack>, ICellProvider {
|
public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEItemStack>, ICellProvider {
|
||||||
|
|
||||||
private IGrid grid;
|
private IGrid grid;
|
||||||
private IStorageGrid storageGrid;
|
private IStorageGrid storageGrid;
|
||||||
private IControllerCache controllerGrid;
|
private Map<IAEItemStack, Requestable> requestable = new HashMap<>();
|
||||||
private Map<AEItemStack, Set<IRequestProvider>> requestable = new HashMap<>();
|
|
||||||
private Set<IRequestProvider> requestProviders = new HashSet<>();
|
private Set<IRequestProvider> requestProviders = new HashSet<>();
|
||||||
|
|
||||||
public RequestGridCache(IGrid grid) {
|
public RequestGridCache(IGrid grid) {
|
||||||
|
@ -49,6 +46,7 @@ public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEIt
|
||||||
public void removeNode(IGridNode gridNode, IGridHost machine) {
|
public void removeNode(IGridNode gridNode, IGridHost machine) {
|
||||||
if (machine instanceof IRequestProvider) {
|
if (machine instanceof IRequestProvider) {
|
||||||
requestProviders.remove(machine);
|
requestProviders.remove(machine);
|
||||||
|
recalcRequestable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,6 +54,7 @@ public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEIt
|
||||||
public void addNode(IGridNode gridNode, IGridHost machine) {
|
public void addNode(IGridNode gridNode, IGridHost machine) {
|
||||||
if (machine instanceof IRequestProvider) {
|
if (machine instanceof IRequestProvider) {
|
||||||
requestProviders.add((IRequestProvider)machine);
|
requestProviders.add((IRequestProvider)machine);
|
||||||
|
recalcRequestable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,10 +73,33 @@ public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEIt
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MENetworkEventSubscribe
|
||||||
|
public void requestableChange(MENetworkRequestableChange event) {
|
||||||
|
recalcRequestable();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void recalcRequestable() {
|
||||||
|
requestable.clear();
|
||||||
|
for (IRequestProvider provider : requestProviders) {
|
||||||
|
Set<IAEItemStack> stacks = provider.getRequestableItems();
|
||||||
|
for (IAEItemStack stack : stacks) {
|
||||||
|
if (requestable.containsKey(stack)) {
|
||||||
|
Requestable r = requestable.get(stack);
|
||||||
|
r.addProvider(provider);
|
||||||
|
r.increaseAmount(stack.getStackSize());
|
||||||
|
} else {
|
||||||
|
Requestable r = new Requestable(stack.getStackSize());
|
||||||
|
r.addProvider(provider);
|
||||||
|
requestable.put(stack, r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<IAEItemStack> getRequestableItems() {
|
public Set<IAEItemStack> getRequestableItems() {
|
||||||
Set<IAEItemStack> list = new HashSet<>();
|
Set<IAEItemStack> list = new HashSet<>();
|
||||||
for(AEItemStack stack : requestable.keySet()) {
|
for(IAEItemStack stack : requestable.keySet()) {
|
||||||
list.add(stack.copy());
|
list.add(stack.copy());
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
|
@ -85,8 +107,16 @@ public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEIt
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IAEItemStack requestItems(IAEItemStack stack) {
|
public IAEItemStack requestItems(IAEItemStack stack) {
|
||||||
// TODO: Implement request mechanism
|
if (!requestable.containsKey(stack)) return stack;
|
||||||
return stack;
|
|
||||||
|
Requestable r = requestable.get(stack);
|
||||||
|
IAEItemStack toRequest = stack;
|
||||||
|
for(IRequestProvider provider : r.providers) {
|
||||||
|
if (toRequest == null) break;
|
||||||
|
toRequest = provider.requestStack(toRequest, Actionable.MODULATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
return toRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -101,11 +131,12 @@ public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEIt
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IItemList<IAEItemStack> getAvailableItems(IItemList<IAEItemStack> out) {
|
public IItemList<IAEItemStack> getAvailableItems(IItemList<IAEItemStack> out) {
|
||||||
Set<AEItemStack> items = requestable.keySet();
|
Set<IAEItemStack> items = requestable.keySet();
|
||||||
for (IAEItemStack s : items) {
|
for (IAEItemStack s : items) {
|
||||||
IAEItemStack stack = s.copy();
|
IAEItemStack stack = s.copy();
|
||||||
stack.reset();
|
stack.reset();
|
||||||
stack.setCountRequestable(1); // TODO: use a value, that makes sense
|
Requestable r = requestable.get(s);
|
||||||
|
stack.setCountRequestable(r.amount);
|
||||||
out.add(stack);
|
out.add(stack);
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
|
@ -149,7 +180,6 @@ public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEIt
|
||||||
@MENetworkEventSubscribe
|
@MENetworkEventSubscribe
|
||||||
public void afterCacheConstruction(final MENetworkPostCacheConstruction cacheConstruction) {
|
public void afterCacheConstruction(final MENetworkPostCacheConstruction cacheConstruction) {
|
||||||
this.storageGrid = this.grid.getCache(IStorageGrid.class);
|
this.storageGrid = this.grid.getCache(IStorageGrid.class);
|
||||||
this.controllerGrid = this.grid.getCache(IControllerCache.class);
|
|
||||||
this.storageGrid.registerCellProvider(this);
|
this.storageGrid.registerCellProvider(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,9 +194,24 @@ public class RequestGridCache implements IRequestGrid, IMEInventoryHandler<IAEIt
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean useLegacyCrafting() {
|
static class Requestable {
|
||||||
ICraftingGrid craftingGrid = grid.getCache(ICraftingGrid.class);
|
|
||||||
return craftingGrid.getCpus().isEmpty() && controllerGrid.hasController();
|
public Set<IRequestProvider> providers;
|
||||||
|
public long amount;
|
||||||
|
|
||||||
|
public Requestable(long amount) {
|
||||||
|
this.providers = new HashSet<>();
|
||||||
|
this.amount = amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addProvider(IRequestProvider provider) {
|
||||||
|
this.providers.add(provider);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void increaseAmount(long amount) {
|
||||||
|
this.amount += amount;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue