Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow crafting jobs to be requested from conversion monitors #7109

Merged
merged 7 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ public interface ICraftingWatcherNode extends IGridNodeService {
/**
* Called when a crafting status changes.
*
* @param craftingGrid current crafting grid
* @param what changed key
* @param what changed key
*/
void onRequestChange(ICraftingService craftingGrid, AEKey what);
void onRequestChange(AEKey what);

void onCraftableChange(AEKey what);
62832 marked this conversation as resolved.
Show resolved Hide resolved
}
23 changes: 20 additions & 3 deletions src/main/java/appeng/me/service/CraftingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public class CraftingService implements ICraftingService, IGridServiceProvider {
this.interests);
private final IEnergyService energyGrid;
private final Set<AEKey> currentlyCrafting = new HashSet<>();
private final Set<AEKey> currentlyCraftable = new HashSet<>();
private boolean updateList = false;

public CraftingService(IGrid grid, IStorageService storageGrid, IEnergyService energyGrid) {
Expand All @@ -141,23 +142,39 @@ public void onServerEndTick() {
this.craftingLinks.values().removeIf(nexus -> nexus.isDead(this.grid, this));

var previouslyCrafting = new HashSet<>(currentlyCrafting);
var previouslyCraftable = new HashSet<>(currentlyCraftable);
this.currentlyCrafting.clear();
this.currentlyCraftable.clear();

for (CraftingCPUCluster cpu : this.craftingCPUClusters) {
cpu.craftingLogic.tickCraftingLogic(energyGrid, this);

cpu.craftingLogic.getAllWaitingFor(this.currentlyCrafting);
}
currentlyCraftable.addAll(getCraftables(k -> true));

// Notify watchers about items no longer being crafted
var changed = new HashSet<AEKey>();
changed.addAll(Sets.difference(previouslyCrafting, currentlyCrafting));
changed.addAll(Sets.difference(currentlyCrafting, previouslyCrafting));
for (var what : changed) {
for (var watcher : interestManager.get(what)) {
watcher.getHost().onRequestChange(this, what);
watcher.getHost().onRequestChange(what);
}
for (var watcher : interestManager.getAllStacksWatchers()) {
watcher.getHost().onRequestChange(what);
}
}

// Notify watchers about items no longer craftable
var changedCraftable = new HashSet<AEKey>();
changedCraftable.addAll(Sets.difference(previouslyCraftable, currentlyCraftable));
changedCraftable.addAll(Sets.difference(currentlyCraftable, previouslyCraftable));
for (var what : changedCraftable) {
for (var watcher : interestManager.get(what)) {
watcher.getHost().onCraftableChange(what);
}
for (var watcher : interestManager.getAllStacksWatchers()) {
watcher.getHost().onRequestChange(this, what);
watcher.getHost().onCraftableChange(what);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import appeng.api.networking.IGrid;
import appeng.api.networking.IStackWatcher;
import appeng.api.networking.crafting.ICraftingProvider;
import appeng.api.networking.crafting.ICraftingService;
import appeng.api.networking.crafting.ICraftingWatcherNode;
import appeng.api.networking.storage.IStorageWatcherNode;
import appeng.api.parts.IPartItem;
Expand Down Expand Up @@ -117,9 +116,13 @@ public void updateWatcher(IStackWatcher newWatcher) {
}

@Override
public void onRequestChange(ICraftingService craftingGrid, AEKey what) {
public void onRequestChange(AEKey what) {
updateState();
}

@Override
public void onCraftableChange(AEKey what) {
}
};

public StorageLevelEmitterPart(IPartItem<?> partItem) {
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/appeng/parts/reporting/AbstractMonitorPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@

import appeng.api.behaviors.ContainerItemStrategies;
import appeng.api.implementations.parts.IStorageMonitorPart;
import appeng.api.networking.IGrid;
import appeng.api.networking.IStackWatcher;
import appeng.api.networking.crafting.ICraftingService;
import appeng.api.networking.storage.IStorageService;
import appeng.api.networking.storage.IStorageWatcherNode;
import appeng.api.orientation.BlockOrientation;
import appeng.api.parts.IPartItem;
Expand Down Expand Up @@ -206,7 +205,7 @@ public boolean onPartShiftActivate(Player player, InteractionHand hand, Vec3 pos
}

// update the system...
private void configureWatchers() {
protected void configureWatchers() {
if (this.myWatcher != null) {
this.myWatcher.reset();
}
Expand All @@ -216,15 +215,15 @@ private void configureWatchers() {
this.myWatcher.add(this.configuredItem);
}

getMainNode().ifPresent(grid -> updateReportingValue(grid.getStorageService(), grid.getCraftingService()));
getMainNode().ifPresent(this::updateReportingValue);
}
}

private void updateReportingValue(IStorageService storageService, ICraftingService craftingService) {
protected void updateReportingValue(IGrid grid) {
this.lastHumanReadableText = null;
if (this.configuredItem != null) {
this.amount = storageService.getCachedInventory().get(this.configuredItem);
this.canCraft = craftingService.isCraftable(this.configuredItem);
this.amount = grid.getStorageService().getCachedInventory().get(this.configuredItem);
this.canCraft = grid.getCraftingService().isCraftable(this.configuredItem);
} else {
this.amount = 0;
this.canCraft = false;
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/appeng/parts/reporting/ConversionMonitorPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.phys.Vec3;

import appeng.api.networking.IStackWatcher;
import appeng.api.networking.crafting.ICraftingWatcherNode;
import appeng.api.parts.IPartItem;
import appeng.api.parts.IPartModel;
import appeng.api.stacks.AEItemKey;
import appeng.api.stacks.AEKey;
import appeng.api.storage.ITerminalHost;
import appeng.api.storage.MEStorage;
import appeng.api.storage.StorageHelper;
Expand Down Expand Up @@ -68,8 +71,27 @@ public class ConversionMonitorPart extends AbstractMonitorPart implements ITermi
public static final IPartModel MODELS_LOCKED_HAS_CHANNEL = new PartModel(MODEL_BASE, MODEL_LOCKED_ON,
MODEL_STATUS_HAS_CHANNEL);

private IStackWatcher craftingWatcher;

public ConversionMonitorPart(IPartItem<?> partItem) {
super(partItem, true);

getMainNode().addService(ICraftingWatcherNode.class, new ICraftingWatcherNode() {
@Override
public void updateWatcher(IStackWatcher newWatcher) {
craftingWatcher = newWatcher;
configureWatchers();
}

@Override
public void onRequestChange(AEKey what) {
}

@Override
public void onCraftableChange(AEKey what) {
getMainNode().ifPresent(ConversionMonitorPart.this::updateReportingValue);
}
});
}

@Override
Expand Down Expand Up @@ -217,6 +239,23 @@ private void extractItem(Player player, int count) {
});
}

@Override
protected void configureWatchers() {
super.configureWatchers();

if (craftingWatcher != null) {
craftingWatcher.reset();
}

if (getDisplayed() != null) {
if (craftingWatcher != null) {
craftingWatcher.add(getDisplayed());
}

getMainNode().ifPresent(this::updateReportingValue);
62832 marked this conversation as resolved.
Show resolved Hide resolved
}
}

@Override
public IPartModel getStaticModels() {
return this.selectModel(MODELS_OFF, MODELS_ON, MODELS_HAS_CHANNEL, MODELS_LOCKED_OFF, MODELS_LOCKED_ON,
Expand Down