Skip to content

Commit

Permalink
feat: wireless autocrafting monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Nov 17, 2024
1 parent 6818308 commit 4bb264d
Show file tree
Hide file tree
Showing 35 changed files with 692 additions and 5 deletions.
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 @@ -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 @@ -29,6 +29,7 @@
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 @@ -826,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 @@ -6,4 +6,6 @@ interface AutocraftingMonitor extends TaskStatusProvider {
void addWatcher(AutocraftingMonitorWatcher watcher);

void removeWatcher(AutocraftingMonitorWatcher watcher);

boolean isAutocraftingMonitorActive();
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected boolean doesBlockStateChangeWarrantNetworkNodeUpdate(final BlockState

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

@Override
Expand Down Expand Up @@ -117,4 +117,9 @@ public void addWatcher(final AutocraftingMonitorWatcher watcher) {
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
@@ -0,0 +1,81 @@
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.autocrafting.AutocraftingNetworkComponent;
import com.refinedmods.refinedstorage.api.network.energy.EnergyNetworkComponent;
import com.refinedmods.refinedstorage.common.Platform;
import com.refinedmods.refinedstorage.common.api.support.network.item.NetworkItemContext;

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

class WirelessAutocraftingMonitor implements AutocraftingMonitor {
private final NetworkItemContext context;

WirelessAutocraftingMonitor(final NetworkItemContext context) {
this.context = context;
}

private Optional<AutocraftingNetworkComponent> getAutocrafting() {
return context.resolveNetwork().map(network -> network.getComponent(AutocraftingNetworkComponent.class));
}

@Override
public void addWatcher(final AutocraftingMonitorWatcher watcher) {
context.drainEnergy(Platform.INSTANCE.getConfig().getWirelessAutocraftingMonitor().getOpenEnergyUsage());
}

@Override
public void removeWatcher(final AutocraftingMonitorWatcher watcher) {
// no op
}

@Override
public boolean isAutocraftingMonitorActive() {
final boolean networkActive = context.resolveNetwork().map(
network -> network.getComponent(EnergyNetworkComponent.class).getStored() > 0
).orElse(false);
return networkActive && context.isActive();
}

@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);
context.drainEnergy(Platform.INSTANCE.getConfig().getWirelessAutocraftingMonitor().getCancelEnergyUsage());
});
}

@Override
public void cancelAll() {
getAutocrafting().ifPresent(autocrafting -> {
autocrafting.cancelAll();
context.drainEnergy(
Platform.INSTANCE.getConfig().getWirelessAutocraftingMonitor().getCancelAllEnergyUsage()
);
});
}

@Override
public void testUpdate() {
getAutocrafting().ifPresent(AutocraftingNetworkComponent::testUpdate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.refinedmods.refinedstorage.common.autocrafting.monitor;

import com.refinedmods.refinedstorage.common.content.Menus;

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

public class WirelessAutocraftingMonitorContainerMenu extends AbstractAutocraftingMonitorContainerMenu {
public WirelessAutocraftingMonitorContainerMenu(final int syncId,
final Inventory playerInventory,
final AutocraftingMonitorData data) {
super(Menus.INSTANCE.getWirelessAutocraftingMonitor(), syncId, playerInventory, data);
}

WirelessAutocraftingMonitorContainerMenu(final int syncId,
final Inventory playerInventory,
final AutocraftingMonitor autocraftingMonitor) {
super(Menus.INSTANCE.getWirelessAutocraftingMonitor(), syncId, playerInventory.player, autocraftingMonitor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.refinedmods.refinedstorage.common.autocrafting.monitor;

import com.refinedmods.refinedstorage.common.support.containermenu.ExtendedMenuProvider;

import javax.annotation.Nullable;

import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.StreamEncoder;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;

class WirelessAutocraftingMonitorExtendedMenuProvider implements ExtendedMenuProvider<AutocraftingMonitorData> {
private final Component name;
private final AutocraftingMonitor autocraftingMonitor;

WirelessAutocraftingMonitorExtendedMenuProvider(final Component name,
final AutocraftingMonitor autocraftingMonitor) {
this.name = name;
this.autocraftingMonitor = autocraftingMonitor;
}

@Override
public AutocraftingMonitorData getMenuData() {
return new AutocraftingMonitorData(
autocraftingMonitor.getStatuses(),
autocraftingMonitor.isAutocraftingMonitorActive()
);
}

@Override
public StreamEncoder<RegistryFriendlyByteBuf, AutocraftingMonitorData> getMenuCodec() {
return AutocraftingMonitorData.STREAM_CODEC;
}

@Override
public Component getDisplayName() {
return name;
}

@Nullable
@Override
public AbstractContainerMenu createMenu(final int syncId, final Inventory inventory, final Player player) {
return new WirelessAutocraftingMonitorContainerMenu(syncId, inventory, autocraftingMonitor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.refinedmods.refinedstorage.common.autocrafting.monitor;

import com.refinedmods.refinedstorage.api.network.energy.EnergyStorage;
import com.refinedmods.refinedstorage.api.network.impl.energy.EnergyStorageImpl;
import com.refinedmods.refinedstorage.common.Platform;
import com.refinedmods.refinedstorage.common.api.RefinedStorageApi;
import com.refinedmods.refinedstorage.common.api.security.SecurityHelper;
import com.refinedmods.refinedstorage.common.api.support.energy.AbstractNetworkEnergyItem;
import com.refinedmods.refinedstorage.common.api.support.network.item.NetworkItemContext;
import com.refinedmods.refinedstorage.common.api.support.slotreference.SlotReference;
import com.refinedmods.refinedstorage.common.content.ContentNames;
import com.refinedmods.refinedstorage.common.security.BuiltinPermission;

import javax.annotation.Nullable;

import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;

import static java.util.Objects.requireNonNullElse;

public class WirelessAutocraftingMonitorItem extends AbstractNetworkEnergyItem {
private final boolean creative;

public WirelessAutocraftingMonitorItem(final boolean creative) {
super(
new Item.Properties().stacksTo(1),
RefinedStorageApi.INSTANCE.getEnergyItemHelper(),
RefinedStorageApi.INSTANCE.getNetworkItemHelper()
);
this.creative = creative;
}

public EnergyStorage createEnergyStorage(final ItemStack stack) {
final EnergyStorage energyStorage = new EnergyStorageImpl(
Platform.INSTANCE.getConfig().getWirelessAutocraftingMonitor().getEnergyCapacity()
);
return RefinedStorageApi.INSTANCE.asItemEnergyStorage(energyStorage, stack);
}

@Override
protected void use(@Nullable final Component name,
final ServerPlayer player,
final SlotReference slotReference,
final NetworkItemContext context) {
final boolean isAllowed = context.resolveNetwork()
.map(network -> SecurityHelper.isAllowed(player, BuiltinPermission.OPEN, network))
.orElse(true);
if (!isAllowed) {
RefinedStorageApi.INSTANCE.sendNoPermissionToOpenMessage(
player,
ContentNames.WIRELESS_AUTOCRAFTING_MONITOR
);
return;
}
final WirelessAutocraftingMonitor autocraftingMonitor = new WirelessAutocraftingMonitor(context);
final Component correctedName = requireNonNullElse(
name,
creative ? ContentNames.CREATIVE_WIRELESS_AUTOCRAFTING_MONITOR : ContentNames.WIRELESS_AUTOCRAFTING_MONITOR
);
final var provider = new WirelessAutocraftingMonitorExtendedMenuProvider(correctedName, autocraftingMonitor);
Platform.INSTANCE.getMenuOpener().openMenu(player, provider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public final class ContentIds {
public static final ResourceLocation AUTOCRAFTER = createIdentifier("autocrafter");
public static final ResourceLocation AUTOCRAFTER_MANAGER = createIdentifier("autocrafter_manager");
public static final ResourceLocation AUTOCRAFTING_MONITOR = createIdentifier("autocrafting_monitor");
public static final ResourceLocation WIRELESS_AUTOCRAFTING_MONITOR = createIdentifier(
"wireless_autocrafting_monitor"
);
public static final ResourceLocation CREATIVE_WIRELESS_AUTOCRAFTING_MONITOR = createIdentifier(
"creative_wireless_autocrafting_monitor"
);

private ContentIds() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public final class ContentNames {
public static final MutableComponent AUTOCRAFTER = name("autocrafter");
public static final MutableComponent AUTOCRAFTER_MANAGER = name("autocrafter_manager");
public static final MutableComponent AUTOCRAFTING_MONITOR = name("autocrafting_monitor");
public static final MutableComponent WIRELESS_AUTOCRAFTING_MONITOR = createTranslation(
"item",
"wireless_autocrafting_monitor"
);
public static final MutableComponent CREATIVE_WIRELESS_AUTOCRAFTING_MONITOR = createTranslation(
"item",
"creative_wireless_autocrafting_monitor"
);

public static final String CLEAR_CRAFTING_MATRIX_TO_NETWORK_TRANSLATION_KEY =
createTranslationKey("key", "clear_crafting_grid_matrix_to_network");
Expand All @@ -50,6 +58,10 @@ public final class ContentNames {
public static final String FOCUS_SEARCH_BAR_TRANSLATION_KEY = createTranslationKey("key", "focus_search_bar");
public static final String OPEN_WIRELESS_GRID_TRANSLATION_KEY = createTranslationKey("key", "open_wireless_grid");
public static final String OPEN_PORTABLE_GRID_TRANSLATION_KEY = createTranslationKey("key", "open_portable_grid");
public static final String OPEN_WIRELESS_AUTOCRAFTING_MONITOR_TRANSLATION_KEY = createTranslationKey(
"key",
"open_wireless_autocrafting_monitor"
);

private ContentNames() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,8 @@ private static void appendItems(final Consumer<ItemStack> consumer) {
itemConsumer.accept(Items.INSTANCE.getSecurityCard());
itemConsumer.accept(Items.INSTANCE.getFallbackSecurityCard());
itemConsumer.accept(Items.INSTANCE.getPattern());
itemConsumer.accept(Items.INSTANCE.getWirelessAutocraftingMonitor());
consumer.accept(Items.INSTANCE.getWirelessAutocraftingMonitor().createAtEnergyCapacity());
itemConsumer.accept(Items.INSTANCE.getCreativeWirelessAutocraftingMonitor());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public final class DefaultEnergyUsage {
public static final long PORTABLE_GRID_INSERT = 5;
public static final long PORTABLE_GRID_EXTRACT = 5;

public static final long WIRELESS_AUTOCRAFTING_MONITOR_CAPACITY = 1000;
public static final long WIRELESS_AUTOCRAFTING_MONITOR_OPEN = 5;
public static final long WIRELESS_AUTOCRAFTING_MONITOR_CANCEL = 5;
public static final long WIRELESS_AUTOCRAFTING_MONITOR_CANCEL_ALL = 5;

private DefaultEnergyUsage() {
}
}
Loading

0 comments on commit 4bb264d

Please sign in to comment.