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

Wireless Autocrafting Monitor #720

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Added

- Autocrafting Monitor
- Wireless Autocrafting Monitor
- Creative Wireless Autocrafting Monitor

### Changed

Expand Down
1 change: 1 addition & 0 deletions config/checkstyle/checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<suppress checks="JavadocPackage" files="test[\\/].*.java"/>
<!-- Mod initializer can be longer due to initialization logic, config can be long too -->
<suppress checks="FileLength" files="(ModInitializer.*\.java|ConfigImpl\.java)"/>
<suppress checks="MethodCount" files="Items.java"/>
<!-- Shadow target contains underscore -->
<suppress checks="MemberName" files="ModelBakerImplMixin.java"/>
<suppress checks="HideUtilityClassConstructor" files="GridClearPacket.java"/>
Expand Down
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 @@ -108,6 +108,7 @@ public AutocraftingPreviewScreen create(final AutocraftingPreviewContainerMenu m
});
registration.register(Menus.INSTANCE.getAutocrafterManager(), AutocrafterManagerScreen::new);
registration.register(Menus.INSTANCE.getAutocraftingMonitor(), AutocraftingMonitorScreen::new);
registration.register(Menus.INSTANCE.getWirelessAutocraftingMonitor(), AutocraftingMonitorScreen::new);
}

protected static void registerAlternativeGridHints() {
Expand Down Expand Up @@ -142,6 +143,14 @@ protected static void handleInputEvents() {
Items.INSTANCE.getCreativePortableGrid()
);
}
final KeyMapping openWirelessAutocraftingMonitor = KeyMappings.INSTANCE.getOpenWirelessAutocraftingMonitor();
while (openWirelessAutocraftingMonitor != null && openWirelessAutocraftingMonitor.consumeClick()) {
RefinedStorageApi.INSTANCE.useSlotReferencedItem(
player,
Items.INSTANCE.getWirelessAutocraftingMonitor(),
Items.INSTANCE.getCreativeWirelessAutocraftingMonitor()
);
}
}

protected static void registerDiskModels() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
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.monitor.WirelessAutocraftingMonitorContainerMenu;
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 +278,7 @@ private void registerNetworkComponents() {
);
RefinedStorageApi.INSTANCE.getNetworkComponentMapFactory().addFactory(
AutocraftingNetworkComponent.class,
network -> new AutocraftingNetworkComponentImpl()
network -> new AutocraftingNetworkComponentImpl(new TaskStatusProviderImpl())
);
}

Expand Down Expand Up @@ -825,6 +827,13 @@ protected final void registerMenus(final RegistryCallback<MenuType<?>> callback,
AutocraftingMonitorData.STREAM_CODEC
)
));
Menus.INSTANCE.setWirelessAutocraftingMonitor(callback.register(
ContentIds.WIRELESS_AUTOCRAFTING_MONITOR,
() -> extendedMenuTypeFactory.create(
WirelessAutocraftingMonitorContainerMenu::new,
AutocraftingMonitorData.STREAM_CODEC
)
));
}

protected final void registerLootFunctions(final RegistryCallback<LootItemFunctionType<?>> callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public interface Config {

SimpleEnergyUsageEntry getAutocraftingMonitor();

WirelessAutocraftingMonitorEntry getWirelessAutocraftingMonitor();

interface SimpleEnergyUsageEntry {
long getEnergyUsage();
}
Expand Down Expand Up @@ -228,4 +230,14 @@ interface AutocrafterManagerEntry extends SimpleEnergyUsageEntry {

AutocrafterManagerViewType getViewType();
}

interface WirelessAutocraftingMonitorEntry {
long getEnergyCapacity();

long getOpenEnergyUsage();

long getCancelEnergyUsage();

long getCancelAllEnergyUsage();
}
}
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,11 @@
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);

boolean isAutocraftingMonitorActive();
}
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(), isAutocraftingMonitorActive());
}

@Override
Expand All @@ -67,6 +67,59 @@ 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);
}

@Override
public boolean isAutocraftingMonitorActive() {
return mainNetworkNode.isActive();
}
}
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);
}
}
}
Loading
Loading