-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6818308
commit 4bb264d
Showing
35 changed files
with
692 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...m/refinedmods/refinedstorage/common/autocrafting/monitor/WirelessAutocraftingMonitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
.../refinedstorage/common/autocrafting/monitor/WirelessAutocraftingMonitorContainerMenu.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...dstorage/common/autocrafting/monitor/WirelessAutocraftingMonitorExtendedMenuProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...finedmods/refinedstorage/common/autocrafting/monitor/WirelessAutocraftingMonitorItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.