Skip to content

Commit

Permalink
feat: autocrafting monitor respects activeness
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Nov 16, 2024
1 parent d93aa71 commit eb20e27
Show file tree
Hide file tree
Showing 20 changed files with 368 additions and 197 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
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.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;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.MenuType;

public abstract class AbstractAutocraftingMonitorContainerMenu extends AbstractBaseContainerMenu
implements TaskStatusListener, AutocraftingMonitorWatcher {
private final Map<TaskId, TaskStatus> statusByTaskId;
private final List<TaskStatus.TaskInfo> tasks;
private final List<TaskStatus.TaskInfo> tasksView;
@Nullable
private final TaskStatusProvider taskStatusProvider;
private final Player player;

@Nullable
private AutocraftingMonitorListener listener;
@Nullable
private TaskId currentTaskId;
private boolean active;

protected AbstractAutocraftingMonitorContainerMenu(final MenuType<?> menuType,
final int syncId,
final Inventory playerInventory,
final AutocraftingMonitorData data) {
super(menuType, syncId);
this.statusByTaskId = data.statuses().stream().collect(Collectors.toMap(
s -> s.info().id(),
s -> s
));
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.active = data.active();
this.player = playerInventory.player;
}

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

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

@Override
public void broadcastChanges() {
super.broadcastChanges();
if (taskStatusProvider instanceof TaskStatusProviderImpl taskStatusProviderImpl) {
taskStatusProviderImpl.testTick();
}
}

void setListener(@Nullable final AutocraftingMonitorListener listener) {
this.listener = listener;
}

List<TaskStatus.Item> getCurrentItems() {
final TaskStatus status = statusByTaskId.get(currentTaskId);
if (status == null) {
return Collections.emptyList();
}
return status.items();
}

List<TaskStatus.TaskInfo> getTasks() {
return tasksView;
}

float getPercentageCompleted(final TaskId taskId) {
final TaskStatus status = statusByTaskId.get(taskId);
return status == null ? 0 : status.percentageCompleted();
}

void setCurrentTaskId(@Nullable final TaskId taskId) {
this.currentTaskId = taskId;
loadCurrentTask();
}

void loadCurrentTask() {
if (listener != null) {
listener.currentTaskChanged(currentTaskId == null ? null : statusByTaskId.get(currentTaskId));
}
}

@Override
public void taskStatusChanged(final TaskStatus status) {
if (taskStatusProvider != null && player instanceof ServerPlayer serverPlayer) {
S2CPackets.sendAutocraftingMonitorTaskStatusChanged(serverPlayer, status);
return;
}
statusByTaskId.put(status.info().id(), status);
}

@Override
public void taskRemoved(final TaskId id) {
if (taskStatusProvider != null && player instanceof ServerPlayer serverPlayer) {
S2CPackets.sendAutocraftingMonitorTaskRemoved(serverPlayer, id);
return;
}
statusByTaskId.remove(id);
tasks.removeIf(task -> task.id().equals(id));
if (listener != null) {
listener.taskRemoved(id);
}
if (id.equals(currentTaskId)) {
this.currentTaskId = tasks.isEmpty() ? null : tasks.getFirst().id();
loadCurrentTask();
}
}

@Override
public void taskAdded(final TaskStatus status) {
if (taskStatusProvider != null && player instanceof ServerPlayer serverPlayer) {
S2CPackets.sendAutocraftingMonitorTaskAdded(serverPlayer, status);
return;
}
statusByTaskId.put(status.info().id(), status);
tasks.add(status.info());
if (listener != null) {
listener.taskAdded(status);
}
if (currentTaskId == null) {
this.currentTaskId = status.info().id();
loadCurrentTask();
}
}

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

void cancelCurrentTask() {
if (currentTaskId != null) {
C2SPackets.sendAutocraftingMonitorCancel(currentTaskId);
}
}

public void cancelAllTasks() {
if (taskStatusProvider != null) {
taskStatusProvider.cancelAll();
} else {
C2SPackets.sendAutocraftingMonitorCancelAll();
}
}

@Override
public void activeChanged(final boolean newActive) {
if (player instanceof ServerPlayer serverPlayer) {
S2CPackets.sendAutocraftingMonitorActive(serverPlayer, newActive);
} else {
this.active = newActive;
loadCurrentTask();
}
}

boolean isActive() {
return active;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import com.refinedmods.refinedstorage.common.support.containermenu.NetworkNodeExtendedMenuProvider;
import com.refinedmods.refinedstorage.common.support.network.AbstractBaseNetworkNodeContainerBlockEntity;

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

import net.minecraft.core.BlockPos;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
Expand All @@ -19,12 +22,28 @@

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

public AutocraftingMonitorBlockEntity(final BlockPos pos, final BlockState state) {
super(BlockEntities.INSTANCE.getAutocraftingMonitor(), pos, state, new SimpleNetworkNode(
Platform.INSTANCE.getConfig().getAutocraftingMonitor().getEnergyUsage()
));
}

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);
watchers.forEach(watcher -> watcher.activeChanged(newActive));
}

@Override
public Component getName() {
return overrideName(ContentNames.AUTOCRAFTING_MONITOR);
Expand All @@ -38,7 +57,7 @@ protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState

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

@Override
Expand All @@ -48,6 +67,6 @@ 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());
return new AutocraftingMonitorContainerMenu(syncId, player, new TaskStatusProviderImpl(), this);
}
}
Loading

0 comments on commit eb20e27

Please sign in to comment.