Skip to content

Commit

Permalink
refactor: autocrafting monitor now delegates to network
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Nov 17, 2024
1 parent 3a5b99b commit 6818308
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public interface TaskStatusProvider {
void cancel(TaskId taskId);

void cancelAll();

void testUpdate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.refinedmods.refinedstorage.common.autocrafting.monitor.AutocraftingMonitorBlockEntity;
import com.refinedmods.refinedstorage.common.autocrafting.monitor.AutocraftingMonitorContainerMenu;
import com.refinedmods.refinedstorage.common.autocrafting.monitor.AutocraftingMonitorData;
import com.refinedmods.refinedstorage.common.autocrafting.monitor.TaskStatusProviderImpl;
import com.refinedmods.refinedstorage.common.autocrafting.patterngrid.PatternGridBlockEntity;
import com.refinedmods.refinedstorage.common.autocrafting.patterngrid.PatternGridContainerMenu;
import com.refinedmods.refinedstorage.common.autocrafting.patterngrid.PatternGridData;
Expand Down Expand Up @@ -276,7 +277,7 @@ private void registerNetworkComponents() {
);
RefinedStorageApi.INSTANCE.getNetworkComponentMapFactory().addFactory(
AutocraftingNetworkComponent.class,
network -> new AutocraftingNetworkComponentImpl()
network -> new AutocraftingNetworkComponentImpl(new TaskStatusProviderImpl())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.refinedmods.refinedstorage.api.autocrafting.TaskId;
import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatus;
import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatusListener;
import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatusProvider;
import com.refinedmods.refinedstorage.common.support.AbstractBaseContainerMenu;
import com.refinedmods.refinedstorage.common.support.packet.c2s.C2SPackets;
import com.refinedmods.refinedstorage.common.support.packet.s2c.S2CPackets;
Expand All @@ -25,7 +24,7 @@ public abstract class AbstractAutocraftingMonitorContainerMenu extends AbstractB
private final List<TaskStatus.TaskInfo> tasks;
private final List<TaskStatus.TaskInfo> tasksView;
@Nullable
private final TaskStatusProvider taskStatusProvider;
private final AutocraftingMonitor autocraftingMonitor;
private final Player player;

@Nullable
Expand All @@ -46,38 +45,40 @@ protected AbstractAutocraftingMonitorContainerMenu(final MenuType<?> menuType,
this.tasks = data.statuses().stream().map(TaskStatus::info).collect(Collectors.toList());
this.tasksView = Collections.unmodifiableList(tasks);
this.currentTaskId = data.statuses().isEmpty() ? null : data.statuses().getFirst().info().id();
this.taskStatusProvider = null;
this.autocraftingMonitor = null;
this.active = data.active();
this.player = playerInventory.player;
}

protected AbstractAutocraftingMonitorContainerMenu(final MenuType<?> menuType,
final int syncId,
final Player player,
final TaskStatusProvider taskStatusProvider) {
AbstractAutocraftingMonitorContainerMenu(final MenuType<?> menuType,
final int syncId,
final Player player,
final AutocraftingMonitor autocraftingMonitor) {
super(menuType, syncId);
this.statusByTaskId = Collections.emptyMap();
this.tasks = Collections.emptyList();
this.tasksView = Collections.emptyList();
this.currentTaskId = null;
this.taskStatusProvider = taskStatusProvider;
this.autocraftingMonitor = autocraftingMonitor;
this.player = player;
taskStatusProvider.addListener(this);
this.autocraftingMonitor.addListener(this);
this.autocraftingMonitor.addWatcher(this);
}

@Override
public void removed(final Player removedPlayer) {
super.removed(removedPlayer);
if (taskStatusProvider != null) {
taskStatusProvider.removeListener(this);
if (autocraftingMonitor != null) {
autocraftingMonitor.removeListener(this);
autocraftingMonitor.removeWatcher(this);
}
}

@Override
public void broadcastChanges() {
super.broadcastChanges();
if (taskStatusProvider instanceof TaskStatusProviderImpl taskStatusProviderImpl) {
taskStatusProviderImpl.testTick();
if (autocraftingMonitor != null) {
autocraftingMonitor.testUpdate();
}
}

Expand Down Expand Up @@ -115,7 +116,7 @@ void loadCurrentTask() {

@Override
public void taskStatusChanged(final TaskStatus status) {
if (taskStatusProvider != null && player instanceof ServerPlayer serverPlayer) {
if (autocraftingMonitor != null && player instanceof ServerPlayer serverPlayer) {
S2CPackets.sendAutocraftingMonitorTaskStatusChanged(serverPlayer, status);
return;
}
Expand All @@ -124,7 +125,7 @@ public void taskStatusChanged(final TaskStatus status) {

@Override
public void taskRemoved(final TaskId id) {
if (taskStatusProvider != null && player instanceof ServerPlayer serverPlayer) {
if (autocraftingMonitor != null && player instanceof ServerPlayer serverPlayer) {
S2CPackets.sendAutocraftingMonitorTaskRemoved(serverPlayer, id);
return;
}
Expand All @@ -141,7 +142,7 @@ public void taskRemoved(final TaskId id) {

@Override
public void taskAdded(final TaskStatus status) {
if (taskStatusProvider != null && player instanceof ServerPlayer serverPlayer) {
if (autocraftingMonitor != null && player instanceof ServerPlayer serverPlayer) {
S2CPackets.sendAutocraftingMonitorTaskAdded(serverPlayer, status);
return;
}
Expand All @@ -157,8 +158,8 @@ public void taskAdded(final TaskStatus status) {
}

public void cancelTask(final TaskId taskId) {
if (taskStatusProvider != null) {
taskStatusProvider.cancel(taskId);
if (autocraftingMonitor != null) {
autocraftingMonitor.cancel(taskId);
}
}

Expand All @@ -169,8 +170,8 @@ void cancelCurrentTask() {
}

public void cancelAllTasks() {
if (taskStatusProvider != null) {
taskStatusProvider.cancelAll();
if (autocraftingMonitor != null) {
autocraftingMonitor.cancelAll();
} else {
C2SPackets.sendAutocraftingMonitorCancelAll();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.refinedmods.refinedstorage.common.autocrafting.monitor;

import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatusProvider;

interface AutocraftingMonitor extends TaskStatusProvider {
void addWatcher(AutocraftingMonitorWatcher watcher);

void removeWatcher(AutocraftingMonitorWatcher watcher);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.refinedmods.refinedstorage.common.autocrafting.monitor;

import com.refinedmods.refinedstorage.api.autocrafting.TaskId;
import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatus;
import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatusListener;
import com.refinedmods.refinedstorage.api.network.Network;
import com.refinedmods.refinedstorage.api.network.autocrafting.AutocraftingNetworkComponent;
import com.refinedmods.refinedstorage.api.network.impl.node.SimpleNetworkNode;
import com.refinedmods.refinedstorage.common.Platform;
import com.refinedmods.refinedstorage.common.content.BlockEntities;
Expand All @@ -8,7 +13,10 @@
import com.refinedmods.refinedstorage.common.support.containermenu.NetworkNodeExtendedMenuProvider;
import com.refinedmods.refinedstorage.common.support.network.AbstractBaseNetworkNodeContainerBlockEntity;

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

import net.minecraft.core.BlockPos;
Expand All @@ -21,7 +29,7 @@
import net.minecraft.world.level.block.state.BlockState;

public class AutocraftingMonitorBlockEntity extends AbstractBaseNetworkNodeContainerBlockEntity<SimpleNetworkNode>
implements NetworkNodeExtendedMenuProvider<AutocraftingMonitorData> {
implements NetworkNodeExtendedMenuProvider<AutocraftingMonitorData>, AutocraftingMonitor {
private final Set<AutocraftingMonitorWatcher> watchers = new HashSet<>();

public AutocraftingMonitorBlockEntity(final BlockPos pos, final BlockState state) {
Expand All @@ -30,14 +38,6 @@ public AutocraftingMonitorBlockEntity(final BlockPos pos, final BlockState state
));
}

void addWatcher(final AutocraftingMonitorWatcher watcher) {
watchers.add(watcher);
}

void removeWatcher(final AutocraftingMonitorWatcher watcher) {
watchers.remove(watcher);
}

@Override
protected void activenessChanged(final boolean newActive) {
super.activenessChanged(newActive);
Expand All @@ -57,7 +57,7 @@ protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState

@Override
public AutocraftingMonitorData getMenuData() {
return new AutocraftingMonitorData(new TaskStatusProviderImpl().getStatuses(), mainNetworkNode.isActive());
return new AutocraftingMonitorData(getStatuses(), mainNetworkNode.isActive());
}

@Override
Expand All @@ -67,6 +67,54 @@ public StreamEncoder<RegistryFriendlyByteBuf, AutocraftingMonitorData> getMenuCo

@Override
public AbstractContainerMenu createMenu(final int syncId, final Inventory inventory, final Player player) {
return new AutocraftingMonitorContainerMenu(syncId, player, new TaskStatusProviderImpl(), this);
return new AutocraftingMonitorContainerMenu(syncId, player, this);
}

private Optional<AutocraftingNetworkComponent> getAutocrafting() {
final Network network = mainNetworkNode.getNetwork();
if (network == null) {
return Optional.empty();
}
return Optional.of(network.getComponent(AutocraftingNetworkComponent.class));
}

@Override
public List<TaskStatus> getStatuses() {
return getAutocrafting().map(AutocraftingNetworkComponent::getStatuses).orElse(Collections.emptyList());
}

@Override
public void addListener(final TaskStatusListener listener) {
getAutocrafting().ifPresent(autocrafting -> autocrafting.addListener(listener));
}

@Override
public void removeListener(final TaskStatusListener listener) {
getAutocrafting().ifPresent(autocrafting -> autocrafting.removeListener(listener));
}

@Override
public void cancel(final TaskId taskId) {
getAutocrafting().ifPresent(autocrafting -> autocrafting.cancel(taskId));
}

@Override
public void cancelAll() {
getAutocrafting().ifPresent(AutocraftingNetworkComponent::cancelAll);
}

@Override
public void testUpdate() {
getAutocrafting().ifPresent(AutocraftingNetworkComponent::testUpdate);
}

@Override
public void addWatcher(final AutocraftingMonitorWatcher watcher) {
watchers.add(watcher);
}

@Override
public void removeWatcher(final AutocraftingMonitorWatcher watcher) {
watchers.remove(watcher);
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,30 @@
package com.refinedmods.refinedstorage.common.autocrafting.monitor;

import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatusProvider;
import com.refinedmods.refinedstorage.common.content.Menus;
import com.refinedmods.refinedstorage.common.support.RedstoneMode;
import com.refinedmods.refinedstorage.common.support.containermenu.ClientProperty;
import com.refinedmods.refinedstorage.common.support.containermenu.PropertyTypes;
import com.refinedmods.refinedstorage.common.support.containermenu.ServerProperty;

import javax.annotation.Nullable;

import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;

public class AutocraftingMonitorContainerMenu extends AbstractAutocraftingMonitorContainerMenu {
@Nullable
private final AutocraftingMonitorBlockEntity autocraftingMonitor;

public AutocraftingMonitorContainerMenu(final int syncId,
final Inventory playerInventory,
final AutocraftingMonitorData data) {
super(Menus.INSTANCE.getAutocraftingMonitor(), syncId, playerInventory, data);
registerProperty(new ClientProperty<>(PropertyTypes.REDSTONE_MODE, RedstoneMode.IGNORE));
this.autocraftingMonitor = null;
}

AutocraftingMonitorContainerMenu(final int syncId,
final Player player,
final TaskStatusProvider taskStatusProvider,
final AutocraftingMonitorBlockEntity autocraftingMonitor) {
super(Menus.INSTANCE.getAutocraftingMonitor(), syncId, player, taskStatusProvider);
super(Menus.INSTANCE.getAutocraftingMonitor(), syncId, player, autocraftingMonitor);
registerProperty(new ServerProperty<>(
PropertyTypes.REDSTONE_MODE,
autocraftingMonitor::getRedstoneMode,
autocraftingMonitor::setRedstoneMode
));
this.autocraftingMonitor = autocraftingMonitor;
autocraftingMonitor.addWatcher(this);
}

@Override
public void removed(final Player removedPlayer) {
super.removed(removedPlayer);
if (autocraftingMonitor != null) {
autocraftingMonitor.removeWatcher(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ private void renderItemText(final GuiGraphics graphics,
final int y) {
int yy = y;
if (item.stored() > 0) {
renderItemText(graphics, "stored", rendering, x, yy, item.missing());
renderItemText(graphics, "stored", rendering, x, yy, item.stored());
yy += 7;
}
if (item.missing() > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.refinedmods.refinedstorage.common.autocrafting.monitor;

@FunctionalInterface
interface AutocraftingMonitorWatcher {
public interface AutocraftingMonitorWatcher {
void activeChanged(boolean active);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;

class TaskStatusProviderImpl implements TaskStatusProvider {
public class TaskStatusProviderImpl implements TaskStatusProvider {
private static final Item[] ITEM_SET = new Item[] {
Items.DIRT,
Items.DIAMOND,
Expand Down Expand Up @@ -66,12 +66,13 @@ public void cancelAll() {
copy.forEach(s -> listeners.forEach(l -> l.taskRemoved(s.info().id())));
}

void testTick() {
@Override
public void testUpdate() {
if (ticks++ % 10 != 0) {
return;
}
final int chance = r.nextInt(100);
if (chance < 50) {
if (chance < 50 && statuses.size() < 100) {
final TaskStatus status = generateTaskStatus();
statuses.add(status);
listeners.forEach(l -> l.taskAdded(status));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,14 @@ public void setViewType(final GridViewType viewType) {
}

public void setSearchBox(final GridSearchBox searchBox) {
registerViewUpdatingListener(searchBox);
configureSearchBox(searchBox);
}

private void registerViewUpdatingListener(final GridSearchBox theSearchBox) {
theSearchBox.addListener(text -> {
searchBox.addListener(text -> {
final boolean valid = onSearchTextChanged(text);
theSearchBox.setValid(valid);
searchBox.setValid(valid);
});
if (Platform.INSTANCE.getConfig().getGrid().isRememberSearchQuery()) {
searchBox.setValue(lastSearchQuery);
searchBox.addListener(AbstractGridContainerMenu::updateLastSearchQuery);
}
}

private boolean onSearchTextChanged(final String text) {
Expand All @@ -228,13 +227,6 @@ private boolean onSearchTextChanged(final String text) {
}
}

private void configureSearchBox(final GridSearchBox theSearchBox) {
if (Platform.INSTANCE.getConfig().getGrid().isRememberSearchQuery()) {
theSearchBox.setValue(lastSearchQuery);
theSearchBox.addListener(AbstractGridContainerMenu::updateLastSearchQuery);
}
}

private static void updateLastSearchQuery(final String text) {
lastSearchQuery = text;
}
Expand Down
Loading

0 comments on commit 6818308

Please sign in to comment.