From bca26d67f226404ddf5a3d9e34425047d8f4aa3e Mon Sep 17 00:00:00 2001 From: raoulvdberge Date: Sun, 14 Jan 2024 21:45:24 +0100 Subject: [PATCH] feat: portable grid item --- .../energy/AbstractProxyEnergyStorage.java | 32 +++ .../impl/energy/ProxyEnergyStorageTest.java | 33 +++ .../common/AbstractClientModInitializer.java | 3 +- .../common/AbstractModInitializer.java | 26 ++- .../common/content/CreativeModeTabItems.java | 1 + .../platform/common/content/Items.java | 13 +- .../platform/common/content/Menus.java | 23 +- .../common/storage/DiskInventory.java | 6 +- .../AbstractDiskDriveBlockEntity.java | 2 +- .../AbstractPortableGridBlockEntity.java | 147 +++---------- ...=> AbstractPortableGridContainerMenu.java} | 48 +++-- .../storage/portablegrid/PortableGrid.java | 134 ++++++++++++ .../PortableGridBlockContainerMenu.java | 39 ++++ .../portablegrid/PortableGridBlockItem.java | 201 ++++++++++++++++++ .../PortableGridBlockItemRenderInfo.java | 6 + .../portablegrid/PortableGridItem.java | 11 - .../PortableGridItemContainerMenu.java | 39 ++++ .../PortableGridItemExtendedMenuProvider.java | 54 +++++ .../PortableGridLootItemFunction.java | 7 +- .../portablegrid/PortableGridOperations.java | 4 +- .../portablegrid/PortableGridScreen.java | 6 +- .../support/energy/ItemEnergyStorage.java | 10 +- .../models/block/disk/led_full.json | 41 ++++ .../models/block/disk/led_near_capacity.json | 41 ++++ .../models/block/disk/led_normal.json | 40 ++++ .../platform/fabric/ModInitializerImpl.java | 23 ++ .../fabric/storage/portablegrid/DiskLeds.java | 21 ++ .../portablegrid/PortableGridBakedModel.java | 41 +++- .../PortableGridUnbakedModel.java | 16 +- .../platform/forge/ModInitializer.java | 22 ++ .../storage/portablegrid/DiskLedBakers.java | 20 ++ .../portablegrid/PortableGridBakedModel.java | 35 ++- .../PortableGridUnbakedGeometry.java | 16 +- .../api/storage/StateTrackedStorage.java | 2 +- 34 files changed, 951 insertions(+), 212 deletions(-) create mode 100644 refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/energy/AbstractProxyEnergyStorage.java create mode 100644 refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/energy/ProxyEnergyStorageTest.java rename refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/{PortableGridContainerMenu.java => AbstractPortableGridContainerMenu.java} (55%) create mode 100644 refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGrid.java create mode 100644 refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridBlockContainerMenu.java create mode 100644 refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridBlockItem.java create mode 100644 refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridBlockItemRenderInfo.java delete mode 100644 refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridItem.java create mode 100644 refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridItemContainerMenu.java create mode 100644 refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridItemExtendedMenuProvider.java create mode 100644 refinedstorage2-platform-common/src/main/resources/assets/refinedstorage2/models/block/disk/led_full.json create mode 100644 refinedstorage2-platform-common/src/main/resources/assets/refinedstorage2/models/block/disk/led_near_capacity.json create mode 100644 refinedstorage2-platform-common/src/main/resources/assets/refinedstorage2/models/block/disk/led_normal.json create mode 100644 refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/storage/portablegrid/DiskLeds.java create mode 100644 refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/storage/portablegrid/DiskLedBakers.java diff --git a/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/energy/AbstractProxyEnergyStorage.java b/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/energy/AbstractProxyEnergyStorage.java new file mode 100644 index 000000000..9c55dfc9c --- /dev/null +++ b/refinedstorage2-network/src/main/java/com/refinedmods/refinedstorage2/api/network/impl/energy/AbstractProxyEnergyStorage.java @@ -0,0 +1,32 @@ +package com.refinedmods.refinedstorage2.api.network.impl.energy; + +import com.refinedmods.refinedstorage2.api.core.Action; +import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; + +public abstract class AbstractProxyEnergyStorage implements EnergyStorage { + private final EnergyStorage energyStorage; + + public AbstractProxyEnergyStorage(final EnergyStorage energyStorage) { + this.energyStorage = energyStorage; + } + + @Override + public long getStored() { + return energyStorage.getStored(); + } + + @Override + public long getCapacity() { + return energyStorage.getCapacity(); + } + + @Override + public long receive(final long amount, final Action action) { + return energyStorage.receive(amount, action); + } + + @Override + public long extract(final long amount, final Action action) { + return energyStorage.extract(amount, action); + } +} diff --git a/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/energy/ProxyEnergyStorageTest.java b/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/energy/ProxyEnergyStorageTest.java new file mode 100644 index 000000000..2e391cbb1 --- /dev/null +++ b/refinedstorage2-network/src/test/java/com/refinedmods/refinedstorage2/api/network/impl/energy/ProxyEnergyStorageTest.java @@ -0,0 +1,33 @@ +package com.refinedmods.refinedstorage2.api.network.impl.energy; + +import com.refinedmods.refinedstorage2.api.core.Action; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class ProxyEnergyStorageTest { + AbstractProxyEnergyStorage sut; + + @BeforeEach + void setUp() { + sut = new AbstractProxyEnergyStorage(new EnergyStorageImpl(100)) { + }; + } + + @Test + void testProxy() { + // Act + final long capacity = sut.getCapacity(); + final long received = sut.receive(100, Action.EXECUTE); + final long extracted = sut.extract(15, Action.EXECUTE); + final long stored = sut.getStored(); + + // Assert + assertThat(capacity).isEqualTo(100); + assertThat(received).isEqualTo(100); + assertThat(extracted).isEqualTo(15); + assertThat(stored).isEqualTo(100 - 15); + } +} diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/AbstractClientModInitializer.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/AbstractClientModInitializer.java index 3a40062d5..89068d786 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/AbstractClientModInitializer.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/AbstractClientModInitializer.java @@ -65,7 +65,8 @@ protected static void registerScreens(final ScreenRegistration registration) { registration.register(Menus.INSTANCE.getWirelessTransmitter(), WirelessTransmitterScreen::new); registration.register(Menus.INSTANCE.getStorageMonitor(), StorageMonitorScreen::new); registration.register(Menus.INSTANCE.getNetworkTransmitter(), NetworkTransmitterScreen::new); - registration.register(Menus.INSTANCE.getPortableGrid(), PortableGridScreen::new); + registration.register(Menus.INSTANCE.getPortableGridBlock(), PortableGridScreen::new); + registration.register(Menus.INSTANCE.getPortableGridItem(), PortableGridScreen::new); } protected static void registerAlternativeGridHints() { diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/AbstractModInitializer.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/AbstractModInitializer.java index 915c51450..61a9cbd57 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/AbstractModInitializer.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/AbstractModInitializer.java @@ -66,8 +66,9 @@ import com.refinedmods.refinedstorage2.platform.common.storage.externalstorage.ExternalStorageContainerMenu; import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.AbstractPortableGridBlockEntity; import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridBlock; -import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridContainerMenu; -import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridItem; +import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridBlockContainerMenu; +import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridBlockItem; +import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridItemContainerMenu; import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridLootItemFunction; import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridType; import com.refinedmods.refinedstorage2.platform.common.storage.storageblock.FluidStorageBlock; @@ -304,7 +305,9 @@ protected final void registerItems( final RegistryCallback callback, final Supplier regulatorUpgradeItemSupplier, final Supplier wirelessGridItemSupplier, - final Supplier creativeWirelessGridItemSupplier + final Supplier creativeWirelessGridItemSupplier, + final Supplier portableGridBlockItemSupplier, + final Supplier creativePortableGridBlockItemSupplier ) { registerSimpleItems(callback); Blocks.INSTANCE.getGrid().registerItems(callback); @@ -329,13 +332,10 @@ protected final void registerItems( creativeWirelessGridItemSupplier )); callback.register(STORAGE_MONITOR, () -> new SimpleBlockItem(Blocks.INSTANCE.getStorageMonitor())); - Items.INSTANCE.setPortableGrid(callback.register( - PORTABLE_GRID, - () -> new PortableGridItem(Blocks.INSTANCE.getPortableGrid()) - )); + Items.INSTANCE.setPortableGrid(callback.register(PORTABLE_GRID, portableGridBlockItemSupplier)); Items.INSTANCE.setCreativePortableGrid(callback.register( CREATIVE_PORTABLE_GRID, - () -> new PortableGridItem(Blocks.INSTANCE.getCreativePortableGrid()) + creativePortableGridBlockItemSupplier )); } @@ -706,9 +706,13 @@ protected final void registerMenus(final RegistryCallback> callback, NETWORK_TRANSMITTER, () -> menuTypeFactory.create(NetworkTransmitterContainerMenu::new) )); - Menus.INSTANCE.setPortableGrid(callback.register( - PORTABLE_GRID, - () -> menuTypeFactory.create(PortableGridContainerMenu::new) + Menus.INSTANCE.setPortableGridBlock(callback.register( + createIdentifier("portable_grid_block"), + () -> menuTypeFactory.create(PortableGridBlockContainerMenu::new) + )); + Menus.INSTANCE.setPortableGridItem(callback.register( + createIdentifier("portable_grid_item"), + () -> menuTypeFactory.create(PortableGridItemContainerMenu::new) )); } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/content/CreativeModeTabItems.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/content/CreativeModeTabItems.java index b7ea19476..239dd8ea5 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/content/CreativeModeTabItems.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/content/CreativeModeTabItems.java @@ -38,6 +38,7 @@ private static void appendBlocks(final Consumer consumer) { appendBlockColors(consumer, Blocks.INSTANCE.getGrid()); appendBlockColors(consumer, Blocks.INSTANCE.getCraftingGrid()); itemConsumer.accept(Items.INSTANCE.getPortableGrid()); + consumer.accept(Items.INSTANCE.getPortableGrid().createAtEnergyCapacity()); itemConsumer.accept(Items.INSTANCE.getCreativePortableGrid()); Items.INSTANCE.getDetectors().stream().map(Supplier::get).forEach(itemConsumer); itemConsumer.accept(Blocks.INSTANCE.getInterface()); diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/content/Items.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/content/Items.java index 392c091c4..7e3259919 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/content/Items.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/content/Items.java @@ -6,6 +6,7 @@ import com.refinedmods.refinedstorage2.platform.common.misc.ProcessorItem; import com.refinedmods.refinedstorage2.platform.common.storage.FluidStorageType; import com.refinedmods.refinedstorage2.platform.common.storage.ItemStorageType; +import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridBlockItem; import java.util.ArrayList; import java.util.Collections; @@ -87,9 +88,9 @@ public final class Items { @Nullable private Supplier networkCard; @Nullable - private Supplier portableGrid; + private Supplier portableGrid; @Nullable - private Supplier creativePortableGrid; + private Supplier creativePortableGrid; private Items() { } @@ -398,19 +399,19 @@ public void setNetworkCard(final Supplier supplier) { this.networkCard = supplier; } - public Item getPortableGrid() { + public PortableGridBlockItem getPortableGrid() { return requireNonNull(portableGrid).get(); } - public void setPortableGrid(final Supplier supplier) { + public void setPortableGrid(final Supplier supplier) { this.portableGrid = supplier; } - public Item getCreativePortableGrid() { + public PortableGridBlockItem getCreativePortableGrid() { return requireNonNull(creativePortableGrid).get(); } - public void setCreativePortableGrid(final Supplier supplier) { + public void setCreativePortableGrid(final Supplier supplier) { this.creativePortableGrid = supplier; } } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/content/Menus.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/content/Menus.java index c03d85145..98e8a9e96 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/content/Menus.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/content/Menus.java @@ -13,7 +13,8 @@ import com.refinedmods.refinedstorage2.platform.common.networking.NetworkTransmitterContainerMenu; import com.refinedmods.refinedstorage2.platform.common.storage.diskdrive.DiskDriveContainerMenu; import com.refinedmods.refinedstorage2.platform.common.storage.externalstorage.ExternalStorageContainerMenu; -import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridContainerMenu; +import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridBlockContainerMenu; +import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridItemContainerMenu; import com.refinedmods.refinedstorage2.platform.common.storage.storageblock.FluidStorageBlockContainerMenu; import com.refinedmods.refinedstorage2.platform.common.storage.storageblock.ItemStorageBlockContainerMenu; import com.refinedmods.refinedstorage2.platform.common.storagemonitor.StorageMonitorContainerMenu; @@ -67,7 +68,9 @@ public final class Menus { @Nullable private Supplier> networkTransmitter; @Nullable - private Supplier> portableGrid; + private Supplier> portableGridBlock; + @Nullable + private Supplier> portableGridItem; private Menus() { } @@ -216,11 +219,19 @@ public void setNetworkTransmitter(final Supplier getPortableGrid() { - return requireNonNull(portableGrid).get(); + public MenuType getPortableGridBlock() { + return requireNonNull(portableGridBlock).get(); + } + + public void setPortableGridBlock(final Supplier> portableGridBlock) { + this.portableGridBlock = portableGridBlock; + } + + public MenuType getPortableGridItem() { + return requireNonNull(portableGridItem).get(); } - public void setPortableGrid(final Supplier> portableGrid) { - this.portableGrid = portableGrid; + public void setPortableGridItem(final Supplier> portableGridItem) { + this.portableGridItem = portableGridItem; } } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/DiskInventory.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/DiskInventory.java index 79d8f182b..77c2014ec 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/DiskInventory.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/DiskInventory.java @@ -43,14 +43,14 @@ public boolean canPlaceItem(final int slot, final ItemStack stack) { public ItemStack removeItem(final int slot, final int amount) { // Forge InvWrapper calls this instead of setItem. final ItemStack result = super.removeItem(slot, amount); - listener.onDiskChanged(slot); + listener.onDiskChanged(this, slot); return result; } @Override public void setItem(final int slot, final ItemStack stack) { super.setItem(slot, stack); - listener.onDiskChanged(slot); + listener.onDiskChanged(this, slot); } @Override @@ -108,6 +108,6 @@ private StorageState getState(final CompoundTag tag) { @FunctionalInterface public interface DiskListener { - void onDiskChanged(int slot); + void onDiskChanged(DiskInventory inventory, int slot); } } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/AbstractDiskDriveBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/AbstractDiskDriveBlockEntity.java index 7623c431b..6ed95037b 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/AbstractDiskDriveBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/diskdrive/AbstractDiskDriveBlockEntity.java @@ -61,7 +61,7 @@ protected AbstractDiskDriveBlockEntity(final BlockPos pos, final BlockState stat PlatformApi.INSTANCE.getStorageChannelTypeRegistry().getAll(), AMOUNT_OF_DISKS )); - this.diskInventory = new DiskInventory(this::onDiskChanged, getNode().getSize()); + this.diskInventory = new DiskInventory((inventory, slot) -> onDiskChanged(slot), getNode().getSize()); this.filter = FilterWithFuzzyMode.createAndListenForUniqueTemplates( ResourceContainerImpl.createForFilter(), this::setChanged, diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/AbstractPortableGridBlockEntity.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/AbstractPortableGridBlockEntity.java index 7bbc7851a..64ab1e085 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/AbstractPortableGridBlockEntity.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/AbstractPortableGridBlockEntity.java @@ -1,27 +1,11 @@ package com.refinedmods.refinedstorage2.platform.common.storage.portablegrid; import com.refinedmods.refinedstorage2.api.core.Action; -import com.refinedmods.refinedstorage2.api.grid.operations.GridOperations; -import com.refinedmods.refinedstorage2.api.grid.operations.NoopGridOperations; -import com.refinedmods.refinedstorage2.api.grid.watcher.GridStorageChannelProvider; -import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcher; -import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcherManager; -import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcherManagerImpl; import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; -import com.refinedmods.refinedstorage2.api.storage.Actor; -import com.refinedmods.refinedstorage2.api.storage.NoopStorage; -import com.refinedmods.refinedstorage2.api.storage.StateTrackedStorage; -import com.refinedmods.refinedstorage2.api.storage.Storage; -import com.refinedmods.refinedstorage2.api.storage.StorageState; -import com.refinedmods.refinedstorage2.api.storage.TrackedResourceAmount; -import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel; -import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType; import com.refinedmods.refinedstorage2.platform.api.PlatformApi; import com.refinedmods.refinedstorage2.platform.api.configurationcard.ConfigurationCardTarget; import com.refinedmods.refinedstorage2.platform.api.grid.Grid; -import com.refinedmods.refinedstorage2.platform.api.storage.channel.PlatformStorageChannelType; import com.refinedmods.refinedstorage2.platform.api.support.energy.TransferableBlockEntityEnergy; -import com.refinedmods.refinedstorage2.platform.api.support.resource.ItemResource; import com.refinedmods.refinedstorage2.platform.common.Platform; import com.refinedmods.refinedstorage2.platform.common.content.BlockEntities; import com.refinedmods.refinedstorage2.platform.common.content.ContentNames; @@ -29,7 +13,6 @@ import com.refinedmods.refinedstorage2.platform.common.storage.Disk; import com.refinedmods.refinedstorage2.platform.common.storage.DiskInventory; import com.refinedmods.refinedstorage2.platform.common.storage.DiskStateChangeListener; -import com.refinedmods.refinedstorage2.platform.common.storage.channel.StorageChannelTypes; import com.refinedmods.refinedstorage2.platform.common.support.RedstoneMode; import com.refinedmods.refinedstorage2.platform.common.support.RedstoneModeSettings; import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ExtendedMenuProvider; @@ -37,9 +20,6 @@ import com.refinedmods.refinedstorage2.platform.common.support.energy.CreativeEnergyStorage; import com.refinedmods.refinedstorage2.platform.common.util.ContainerUtil; -import java.util.Collections; -import java.util.List; -import java.util.Set; import javax.annotation.Nullable; import com.google.common.util.concurrent.RateLimiter; @@ -52,7 +32,6 @@ import net.minecraft.network.protocol.game.ClientGamePacketListener; import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; import net.minecraft.server.level.ServerPlayer; -import net.minecraft.world.SimpleContainer; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; @@ -62,8 +41,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public abstract class AbstractPortableGridBlockEntity extends BlockEntity implements Grid, ExtendedMenuProvider, - ConfigurationCardTarget, GridStorageChannelProvider, TransferableBlockEntityEnergy { +public abstract class AbstractPortableGridBlockEntity extends BlockEntity implements ExtendedMenuProvider, + ConfigurationCardTarget, TransferableBlockEntityEnergy { private static final Logger LOGGER = LoggerFactory.getLogger(AbstractPortableGridBlockEntity.class); private static final String TAG_DISK_INVENTORY = "inv"; @@ -78,16 +57,22 @@ public abstract class AbstractPortableGridBlockEntity extends BlockEntity implem private final DiskStateChangeListener diskStateListener = new DiskStateChangeListener(this); private final EnergyStorage energyStorage; private final RateLimiter activenessChangeRateLimiter = RateLimiter.create(1); - private final GridWatcherManager watchers = new GridWatcherManagerImpl(); + private final PortableGrid grid; private RedstoneMode redstoneMode = RedstoneMode.IGNORE; - @Nullable - private PortableGridStorage storage; protected AbstractPortableGridBlockEntity(final PortableGridType type, final BlockPos pos, final BlockState state) { super(getBlockEntityType(type), pos, state); - this.diskInventory = new DiskInventory(this::onDiskChanged, 1); + this.diskInventory = new DiskInventory((inventory, slot) -> onDiskChanged(), 1); this.energyStorage = createEnergyStorage(type, this); + this.grid = new PortableGrid(energyStorage, diskInventory, diskStateListener) { + @Override + public boolean isGridActive() { + return super.isGridActive() + && level != null + && redstoneMode.isActive(level.hasNeighborSignal(worldPosition)); + } + }; } private static EnergyStorage createEnergyStorage(final PortableGridType type, final BlockEntity blockEntity) { @@ -100,13 +85,17 @@ private static EnergyStorage createEnergyStorage(final PortableGridType type, fi ); } + Grid getGrid() { + return grid; + } + void update(final BlockState state) { diskStateListener.updateIfNecessary(); - final boolean newActive = isGridActive(); + final boolean newActive = grid.isGridActive(); final boolean activenessNeedsUpdate = state.getValue(PortableGridBlock.ACTIVE) != newActive; if (activenessNeedsUpdate && activenessChangeRateLimiter.tryAcquire()) { updateActivenessBlockState(state, newActive); - watchers.activeChanged(newActive); + grid.activeChanged(newActive); } } @@ -122,12 +111,12 @@ private void updateActivenessBlockState(final BlockState state, final boolean ac } } - private void onDiskChanged(final int slot) { + private void onDiskChanged() { final boolean isJustPlacedIntoLevelOrLoading = level == null || level.isClientSide(); if (isJustPlacedIntoLevelOrLoading) { return; } - updateStorage(); + grid.updateStorage(); diskStateListener.immediateUpdate(); setChanged(); } @@ -142,30 +131,7 @@ public void setLevel(final Level level) { private void initialize(final Level level) { diskInventory.setStorageRepository(PlatformApi.INSTANCE.getStorageRepository(level)); - updateStorage(); - } - - private void updateStorage() { - watchers.detachAll(this); - this.storage = diskInventory.resolve(0) - .map(diskStorage -> StateTrackedStorage.of(diskStorage, diskStateListener)) - .map(PortableGridStorage::new) - .orElse(null); - watchers.attachAll(this); - } - - @Override - public Set> getStorageChannelTypes() { - return storage == null ? Collections.emptySet() : Set.of(storage.getStorageChannelType()); - } - - @Override - @SuppressWarnings("unchecked") - public StorageChannel getStorageChannel(final StorageChannelType type) { - if (storage == null || type != storage.getStorageChannelType()) { - throw new IllegalArgumentException(); - } - return (StorageChannel) storage.getStorageChannel(); + grid.updateStorage(); } @Override @@ -221,7 +187,7 @@ public Packet getUpdatePacket() { @Override public CompoundTag getUpdateTag() { final CompoundTag tag = new CompoundTag(); - tag.put(TAG_DISKS, diskInventory.toSyncTag(idx -> getStorageState())); + tag.put(TAG_DISKS, diskInventory.toSyncTag(idx -> grid.getStorageState())); return tag; } @@ -234,69 +200,6 @@ public void setRedstoneMode(final RedstoneMode redstoneMode) { setChanged(); } - private StorageState getStorageState() { - if (storage == null) { - return StorageState.NONE; - } - if (!isGridActive()) { - return StorageState.INACTIVE; - } - return storage.getState(); - } - - @Override - public void addWatcher(final GridWatcher watcher, final Class actorType) { - energyStorage.extract(Platform.INSTANCE.getConfig().getPortableGrid().getOpenEnergyUsage(), Action.EXECUTE); - watchers.addWatcher(watcher, actorType, this); - } - - @Override - public void removeWatcher(final GridWatcher watcher) { - watchers.removeWatcher(watcher, this); - } - - @Override - @SuppressWarnings("unchecked") - public Storage getItemStorage() { - if (storage == null || storage.getStorageChannelType() != StorageChannelTypes.ITEM) { - return new NoopStorage<>(); - } - return (Storage) storage.getStorageChannel(); - } - - @Override - public boolean isGridActive() { - return energyStorage.getStored() > 0 - && level != null - && redstoneMode.isActive(level.hasNeighborSignal(worldPosition)); - } - - @Override - @SuppressWarnings("unchecked") - public List> getResources(final StorageChannelType type, - final Class actorType) { - if (storage == null || storage.getStorageChannelType() != type) { - return Collections.emptyList(); - } - final StorageChannel casted = (StorageChannel) storage.getStorageChannel(); - return casted.getAll().stream().map(resource -> new TrackedResourceAmount<>( - resource, - casted.findTrackedResourceByActorType(resource.getResource(), actorType).orElse(null) - )).toList(); - } - - @Override - @SuppressWarnings("unchecked") - public GridOperations createOperations(final PlatformStorageChannelType storageChannelType, - final Actor actor) { - if (storage == null || storage.getStorageChannelType() != storageChannelType) { - return new NoopGridOperations<>(); - } - final StorageChannel casted = (StorageChannel) storage.getStorageChannel(); - final GridOperations operations = storageChannelType.createGridOperations(casted, actor); - return new PortableGridOperations<>(operations, energyStorage); - } - @Override public Component getDisplayName() { return ContentNames.PORTABLE_GRID; @@ -305,17 +208,17 @@ public Component getDisplayName() { @Override @Nullable public AbstractGridContainerMenu createMenu(final int syncId, final Inventory inventory, final Player player) { - return new PortableGridContainerMenu(syncId, inventory, this); + return new PortableGridBlockContainerMenu(syncId, inventory, this); } @Override public void writeScreenOpeningData(final ServerPlayer player, final FriendlyByteBuf buf) { - PlatformApi.INSTANCE.writeGridScreenOpeningData(this, buf); + PlatformApi.INSTANCE.writeGridScreenOpeningData(grid, buf); buf.writeLong(energyStorage.getStored()); buf.writeLong(energyStorage.getCapacity()); } - public SimpleContainer getDiskInventory() { + DiskInventory getDiskInventory() { return diskInventory; } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridContainerMenu.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/AbstractPortableGridContainerMenu.java similarity index 55% rename from refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridContainerMenu.java rename to refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/AbstractPortableGridContainerMenu.java index 66ad3a56d..ab9a16b06 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridContainerMenu.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/AbstractPortableGridContainerMenu.java @@ -1,12 +1,10 @@ package com.refinedmods.refinedstorage2.platform.common.storage.portablegrid; +import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; +import com.refinedmods.refinedstorage2.platform.api.grid.Grid; import com.refinedmods.refinedstorage2.platform.api.storage.StorageContainerItem; -import com.refinedmods.refinedstorage2.platform.common.content.Menus; import com.refinedmods.refinedstorage2.platform.common.grid.AbstractGridContainerMenu; -import com.refinedmods.refinedstorage2.platform.common.support.RedstoneMode; -import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ClientProperty; -import com.refinedmods.refinedstorage2.platform.common.support.containermenu.PropertyTypes; -import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ServerProperty; +import com.refinedmods.refinedstorage2.platform.common.storage.DiskInventory; import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ValidatedSlot; import com.refinedmods.refinedstorage2.platform.common.support.energy.EnergyContainerMenu; import com.refinedmods.refinedstorage2.platform.common.support.energy.EnergyInfo; @@ -16,39 +14,43 @@ import net.minecraft.network.FriendlyByteBuf; import net.minecraft.world.SimpleContainer; import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.inventory.MenuType; import net.minecraft.world.inventory.Slot; -public class PortableGridContainerMenu extends AbstractGridContainerMenu implements EnergyContainerMenu { +public abstract class AbstractPortableGridContainerMenu extends AbstractGridContainerMenu + implements EnergyContainerMenu { private final SimpleContainer diskInventory; private final EnergyInfo energyInfo; @Nullable private Slot diskSlot; - public PortableGridContainerMenu(final int syncId, final Inventory playerInventory, final FriendlyByteBuf buf) { - super(Menus.INSTANCE.getPortableGrid(), syncId, playerInventory, buf); + AbstractPortableGridContainerMenu( + final MenuType menuType, + final int syncId, + final Inventory playerInventory, + final FriendlyByteBuf buf + ) { + super(menuType, syncId, playerInventory, buf); this.diskInventory = new SimpleContainer(1); this.energyInfo = EnergyInfo.forClient(playerInventory.player, buf.readLong(), buf.readLong()); - addSlots(0); - registerProperty(new ClientProperty<>(PropertyTypes.REDSTONE_MODE, RedstoneMode.IGNORE)); } - PortableGridContainerMenu(final int syncId, - final Inventory playerInventory, - final AbstractPortableGridBlockEntity portableGrid) { - super(Menus.INSTANCE.getPortableGrid(), syncId, playerInventory, portableGrid); - this.diskInventory = portableGrid.getDiskInventory(); + AbstractPortableGridContainerMenu( + final MenuType menuType, + final int syncId, + final Inventory playerInventory, + final DiskInventory diskInventory, + final Grid grid, + final EnergyStorage energyStorage + ) { + super(menuType, syncId, playerInventory, grid); + this.diskInventory = diskInventory; this.energyInfo = EnergyInfo.forServer( playerInventory.player, - portableGrid.getEnergyStorage()::getStored, - portableGrid.getEnergyStorage()::getCapacity + energyStorage::getStored, + energyStorage::getCapacity ); - addSlots(0); - registerProperty(new ServerProperty<>( - PropertyTypes.REDSTONE_MODE, - portableGrid::getRedstoneMode, - portableGrid::setRedstoneMode - )); } @Override diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGrid.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGrid.java new file mode 100644 index 000000000..6c8859166 --- /dev/null +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGrid.java @@ -0,0 +1,134 @@ +package com.refinedmods.refinedstorage2.platform.common.storage.portablegrid; + +import com.refinedmods.refinedstorage2.api.core.Action; +import com.refinedmods.refinedstorage2.api.grid.operations.GridOperations; +import com.refinedmods.refinedstorage2.api.grid.operations.NoopGridOperations; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridStorageChannelProvider; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcher; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcherManager; +import com.refinedmods.refinedstorage2.api.grid.watcher.GridWatcherManagerImpl; +import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; +import com.refinedmods.refinedstorage2.api.storage.Actor; +import com.refinedmods.refinedstorage2.api.storage.NoopStorage; +import com.refinedmods.refinedstorage2.api.storage.StateTrackedStorage; +import com.refinedmods.refinedstorage2.api.storage.Storage; +import com.refinedmods.refinedstorage2.api.storage.StorageState; +import com.refinedmods.refinedstorage2.api.storage.TrackedResourceAmount; +import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel; +import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType; +import com.refinedmods.refinedstorage2.platform.api.grid.Grid; +import com.refinedmods.refinedstorage2.platform.api.storage.channel.PlatformStorageChannelType; +import com.refinedmods.refinedstorage2.platform.api.support.resource.ItemResource; +import com.refinedmods.refinedstorage2.platform.common.Platform; +import com.refinedmods.refinedstorage2.platform.common.storage.DiskInventory; +import com.refinedmods.refinedstorage2.platform.common.storage.channel.StorageChannelTypes; + +import java.util.Collections; +import java.util.List; +import java.util.Set; +import javax.annotation.Nullable; + +class PortableGrid implements Grid, GridStorageChannelProvider { + private final EnergyStorage energyStorage; + private final DiskInventory diskInventory; + private final GridWatcherManager watchers = new GridWatcherManagerImpl(); + private final StateTrackedStorage.Listener diskListener; + @Nullable + private PortableGridStorage storage; + + PortableGrid(final EnergyStorage energyStorage, + final DiskInventory diskInventory, + final StateTrackedStorage.Listener diskListener) { + this.energyStorage = energyStorage; + this.diskInventory = diskInventory; + this.diskListener = diskListener; + } + + void updateStorage() { + watchers.detachAll(this); + this.storage = diskInventory.resolve(0) + .map(diskStorage -> StateTrackedStorage.of(diskStorage, diskListener)) + .map(PortableGridStorage::new) + .orElse(null); + watchers.attachAll(this); + } + + void activeChanged(final boolean active) { + watchers.activeChanged(active); + } + + StorageState getStorageState() { + if (storage == null) { + return StorageState.NONE; + } + if (!isGridActive()) { + return StorageState.INACTIVE; + } + return storage.getState(); + } + + @Override + public void addWatcher(final GridWatcher watcher, final Class actorType) { + energyStorage.extract(Platform.INSTANCE.getConfig().getPortableGrid().getOpenEnergyUsage(), Action.EXECUTE); + watchers.addWatcher(watcher, actorType, this); + } + + @Override + public void removeWatcher(final GridWatcher watcher) { + watchers.removeWatcher(watcher, this); + } + + @Override + @SuppressWarnings("unchecked") + public Storage getItemStorage() { + if (storage == null || storage.getStorageChannelType() != StorageChannelTypes.ITEM) { + return new NoopStorage<>(); + } + return (Storage) storage.getStorageChannel(); + } + + @Override + public boolean isGridActive() { + return energyStorage.getStored() > 0 && storage != null; + } + + @Override + @SuppressWarnings("unchecked") + public List> getResources(final StorageChannelType type, + final Class actorType) { + if (storage == null || storage.getStorageChannelType() != type) { + return Collections.emptyList(); + } + final StorageChannel casted = (StorageChannel) storage.getStorageChannel(); + return casted.getAll().stream().map(resource -> new TrackedResourceAmount<>( + resource, + casted.findTrackedResourceByActorType(resource.getResource(), actorType).orElse(null) + )).toList(); + } + + @Override + @SuppressWarnings("unchecked") + public GridOperations createOperations(final PlatformStorageChannelType storageChannelType, + final Actor actor) { + if (storage == null || storage.getStorageChannelType() != storageChannelType) { + return new NoopGridOperations<>(); + } + final StorageChannel casted = (StorageChannel) storage.getStorageChannel(); + final GridOperations operations = storageChannelType.createGridOperations(casted, actor); + return new PortableGridOperations<>(operations, energyStorage); + } + + @Override + public Set> getStorageChannelTypes() { + return storage == null ? Collections.emptySet() : Set.of(storage.getStorageChannelType()); + } + + @Override + @SuppressWarnings("unchecked") + public StorageChannel getStorageChannel(final StorageChannelType type) { + if (storage == null || type != storage.getStorageChannelType()) { + throw new IllegalArgumentException(); + } + return (StorageChannel) storage.getStorageChannel(); + } +} diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridBlockContainerMenu.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridBlockContainerMenu.java new file mode 100644 index 000000000..880c59258 --- /dev/null +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridBlockContainerMenu.java @@ -0,0 +1,39 @@ +package com.refinedmods.refinedstorage2.platform.common.storage.portablegrid; + +import com.refinedmods.refinedstorage2.platform.common.content.Menus; +import com.refinedmods.refinedstorage2.platform.common.support.RedstoneMode; +import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ClientProperty; +import com.refinedmods.refinedstorage2.platform.common.support.containermenu.PropertyTypes; +import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ServerProperty; + +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.world.entity.player.Inventory; + +public class PortableGridBlockContainerMenu extends AbstractPortableGridContainerMenu { + public PortableGridBlockContainerMenu(final int syncId, + final Inventory playerInventory, + final FriendlyByteBuf buf) { + super(Menus.INSTANCE.getPortableGridBlock(), syncId, playerInventory, buf); + registerProperty(new ClientProperty<>(PropertyTypes.REDSTONE_MODE, RedstoneMode.IGNORE)); + addSlots(0); + } + + PortableGridBlockContainerMenu(final int syncId, + final Inventory playerInventory, + final AbstractPortableGridBlockEntity portableGrid) { + super( + Menus.INSTANCE.getPortableGridBlock(), + syncId, + playerInventory, + portableGrid.getDiskInventory(), + portableGrid.getGrid(), + portableGrid.getEnergyStorage() + ); + registerProperty(new ServerProperty<>( + PropertyTypes.REDSTONE_MODE, + portableGrid::getRedstoneMode, + portableGrid::setRedstoneMode + )); + addSlots(0); + } +} diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridBlockItem.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridBlockItem.java new file mode 100644 index 000000000..879c85d66 --- /dev/null +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridBlockItem.java @@ -0,0 +1,201 @@ +package com.refinedmods.refinedstorage2.platform.common.storage.portablegrid; + +import com.refinedmods.refinedstorage2.api.core.Action; +import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; +import com.refinedmods.refinedstorage2.api.network.impl.energy.AbstractProxyEnergyStorage; +import com.refinedmods.refinedstorage2.api.network.impl.energy.EnergyStorageImpl; +import com.refinedmods.refinedstorage2.api.storage.StateTrackedStorage; +import com.refinedmods.refinedstorage2.api.storage.StorageState; +import com.refinedmods.refinedstorage2.platform.api.PlatformApi; +import com.refinedmods.refinedstorage2.platform.api.storage.StorageContainerItem; +import com.refinedmods.refinedstorage2.platform.api.storage.StorageRepository; +import com.refinedmods.refinedstorage2.platform.api.support.energy.AbstractEnergyBlockItem; +import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.SlotReference; +import com.refinedmods.refinedstorage2.platform.common.Platform; +import com.refinedmods.refinedstorage2.platform.common.storage.Disk; +import com.refinedmods.refinedstorage2.platform.common.storage.DiskInventory; +import com.refinedmods.refinedstorage2.platform.common.support.energy.CreativeEnergyStorage; +import com.refinedmods.refinedstorage2.platform.common.support.energy.ItemEnergyStorage; +import com.refinedmods.refinedstorage2.platform.common.util.ContainerUtil; + +import javax.annotation.Nullable; + +import net.minecraft.core.BlockPos; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResultHolder; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.state.BlockState; + +public class PortableGridBlockItem extends AbstractEnergyBlockItem { + private static final String TAG_DISK_INVENTORY = "diskinv"; + + private final PortableGridType type; + + public PortableGridBlockItem(final Block block, final PortableGridType type) { + super(block, new Item.Properties().stacksTo(1), PlatformApi.INSTANCE.getEnergyItemHelper()); + this.type = type; + } + + public static PortableGridBlockItemRenderInfo getRenderInfo(final ItemStack stack, + @Nullable final Level level) { + final boolean creative = stack.getItem() instanceof PortableGridBlockItem portableGridBlockItem + && portableGridBlockItem.type == PortableGridType.CREATIVE; + final boolean hasEnergy = creative || ItemEnergyStorage.getStored(stack) > 0; + final ItemStack diskStack = getDisk(stack); + final boolean active = hasEnergy && !diskStack.isEmpty(); + final Disk disk = new Disk( + diskStack.isEmpty() ? null : diskStack.getItem(), + getState(diskStack, active, level) + ); + return new PortableGridBlockItemRenderInfo(active, disk); + } + + private static StorageState getState(final ItemStack diskStack, + final boolean active, + @Nullable final Level level) { + if (diskStack.isEmpty() || !(diskStack.getItem() instanceof StorageContainerItem storageContainerItem)) { + return StorageState.NONE; + } + if (!active || level == null) { + return StorageState.INACTIVE; + } + final StorageRepository storageRepository = PlatformApi.INSTANCE.getStorageRepository(level); + return storageContainerItem.getInfo(storageRepository, diskStack) + .map(storageInfo -> StateTrackedStorage.computeState(storageInfo.capacity(), storageInfo.stored())) + .orElse(StorageState.INACTIVE); + } + + private static ItemStack getDisk(final ItemStack stack) { + if (stack.getTag() == null) { + return ItemStack.EMPTY; + } + if (!stack.getTag().contains(TAG_DISK_INVENTORY)) { + return ItemStack.EMPTY; + } + return ContainerUtil.getItemInSlot(stack.getTag().getCompound(TAG_DISK_INVENTORY), 0); + } + + static void setDiskInventory(final ItemStack stack, final DiskInventory diskInventory) { + stack.getOrCreateTag().put(TAG_DISK_INVENTORY, ContainerUtil.write(diskInventory)); + } + + public EnergyStorage createEnergyStorage(final ItemStack stack) { + final EnergyStorage energyStorage = new EnergyStorageImpl( + Platform.INSTANCE.getConfig().getPortableGrid().getEnergyCapacity() + ); + return PlatformApi.INSTANCE.asItemEnergyStorage(energyStorage, stack); + } + + @Override + protected boolean updateCustomBlockEntityTag( + final BlockPos pos, + final Level level, + @Nullable final Player player, + final ItemStack stack, + final BlockState blockState + ) { + final boolean result = super.updateCustomBlockEntityTag(pos, level, player, stack, blockState); + if (!level.isClientSide() + && level.getBlockEntity(pos) instanceof AbstractPortableGridBlockEntity portableGrid) { + final ItemStack diskStack = getDisk(stack); + portableGrid.getDiskInventory().setItem(0, diskStack); + } + return result; + } + + @Override + public InteractionResultHolder use(final Level level, final Player player, final InteractionHand hand) { + final ItemStack stack = player.getItemInHand(hand); + if (player instanceof ServerPlayer serverPlayer && level.getServer() != null) { + final SlotReference slotReference = PlatformApi.INSTANCE.createInventorySlotReference(player, hand); + final PortableGridEnergyStorage energyStorage = createEnergyStorageInternal(stack); + final DiskInventoryListenerImpl listener = new DiskInventoryListenerImpl(stack); + final DiskInventory diskInventory = createDiskInventory(stack, listener); + diskInventory.setStorageRepository(PlatformApi.INSTANCE.getStorageRepository(level)); + final PortableGrid portableGrid = new PortableGrid( + energyStorage, + diskInventory, + () -> { + } + ); + listener.portableGrid = portableGrid; + energyStorage.portableGrid = portableGrid; + portableGrid.updateStorage(); + Platform.INSTANCE.getMenuOpener().openMenu(serverPlayer, new PortableGridItemExtendedMenuProvider( + portableGrid, + energyStorage, + diskInventory, + slotReference + )); + } + return InteractionResultHolder.consume(stack); + } + + private PortableGridEnergyStorage createEnergyStorageInternal(final ItemStack stack) { + if (type == PortableGridType.CREATIVE) { + return new PortableGridEnergyStorage(CreativeEnergyStorage.INSTANCE); + } + return new PortableGridEnergyStorage(createEnergyStorage(stack)); + } + + private DiskInventory createDiskInventory(final ItemStack stack, final DiskInventoryListenerImpl listener) { + final DiskInventory diskInventory = new DiskInventory(listener, 1); + if (stack.getTag() != null && stack.getTag().contains(TAG_DISK_INVENTORY)) { + ContainerUtil.read(stack.getTag().getCompound(TAG_DISK_INVENTORY), diskInventory); + } + return diskInventory; + } + + private static class DiskInventoryListenerImpl implements DiskInventory.DiskListener { + private final ItemStack portableGridStack; + @Nullable + private PortableGrid portableGrid; + + private DiskInventoryListenerImpl(final ItemStack portableGridStack) { + this.portableGridStack = portableGridStack; + } + + @Override + public void onDiskChanged(final DiskInventory inventory, final int slot) { + final boolean stillLoading = portableGrid == null; + if (stillLoading) { + return; + } + setDiskInventory(portableGridStack, inventory); + final boolean wasActive = portableGrid.isGridActive(); + portableGrid.updateStorage(); + final boolean isActive = portableGrid.isGridActive(); + if (wasActive != isActive) { + portableGrid.activeChanged(isActive); + } + } + } + + private static class PortableGridEnergyStorage extends AbstractProxyEnergyStorage { + @Nullable + private PortableGrid portableGrid; + + private PortableGridEnergyStorage(final EnergyStorage energyStorage) { + super(energyStorage); + } + + @Override + public long extract(final long amount, final Action action) { + if (action == Action.EXECUTE && portableGrid != null) { + final boolean wasActive = portableGrid.isGridActive(); + final long extracted = super.extract(amount, action); + final boolean isActive = portableGrid.isGridActive(); + if (wasActive != isActive) { + portableGrid.activeChanged(isActive); + } + return extracted; + } + return super.extract(amount, action); + } + } +} diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridBlockItemRenderInfo.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridBlockItemRenderInfo.java new file mode 100644 index 000000000..434e3605f --- /dev/null +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridBlockItemRenderInfo.java @@ -0,0 +1,6 @@ +package com.refinedmods.refinedstorage2.platform.common.storage.portablegrid; + +import com.refinedmods.refinedstorage2.platform.common.storage.Disk; + +public record PortableGridBlockItemRenderInfo(boolean active, Disk disk) { +} diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridItem.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridItem.java deleted file mode 100644 index fa69b1a7a..000000000 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridItem.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.refinedmods.refinedstorage2.platform.common.storage.portablegrid; - -import net.minecraft.world.item.BlockItem; -import net.minecraft.world.item.Item; -import net.minecraft.world.level.block.Block; - -public class PortableGridItem extends BlockItem { - public PortableGridItem(final Block block) { - super(block, new Item.Properties().stacksTo(1)); - } -} diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridItemContainerMenu.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridItemContainerMenu.java new file mode 100644 index 000000000..995c5af9c --- /dev/null +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridItemContainerMenu.java @@ -0,0 +1,39 @@ +package com.refinedmods.refinedstorage2.platform.common.storage.portablegrid; + +import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; +import com.refinedmods.refinedstorage2.platform.api.PlatformApi; +import com.refinedmods.refinedstorage2.platform.api.grid.Grid; +import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.SlotReference; +import com.refinedmods.refinedstorage2.platform.common.content.Menus; +import com.refinedmods.refinedstorage2.platform.common.storage.DiskInventory; + +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.world.entity.player.Inventory; + +public class PortableGridItemContainerMenu extends AbstractPortableGridContainerMenu { + public PortableGridItemContainerMenu(final int syncId, + final Inventory playerInventory, + final FriendlyByteBuf buf) { + super(Menus.INSTANCE.getPortableGridItem(), syncId, playerInventory, buf); + this.disabledSlot = PlatformApi.INSTANCE.getSlotReference(buf).orElse(null); + addSlots(0); + } + + PortableGridItemContainerMenu(final int syncId, + final Inventory playerInventory, + final DiskInventory diskInventory, + final Grid grid, + final EnergyStorage energyStorage, + final SlotReference slotReference) { + super( + Menus.INSTANCE.getPortableGridItem(), + syncId, + playerInventory, + diskInventory, + grid, + energyStorage + ); + this.disabledSlot = slotReference; + addSlots(0); + } +} diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridItemExtendedMenuProvider.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridItemExtendedMenuProvider.java new file mode 100644 index 000000000..2f3f300b3 --- /dev/null +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridItemExtendedMenuProvider.java @@ -0,0 +1,54 @@ +package com.refinedmods.refinedstorage2.platform.common.storage.portablegrid; + +import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; +import com.refinedmods.refinedstorage2.platform.api.PlatformApi; +import com.refinedmods.refinedstorage2.platform.api.grid.Grid; +import com.refinedmods.refinedstorage2.platform.api.support.network.bounditem.SlotReference; +import com.refinedmods.refinedstorage2.platform.common.content.ContentNames; +import com.refinedmods.refinedstorage2.platform.common.storage.DiskInventory; +import com.refinedmods.refinedstorage2.platform.common.support.containermenu.ExtendedMenuProvider; + +import javax.annotation.Nullable; + +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.chat.Component; +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.AbstractContainerMenu; + +class PortableGridItemExtendedMenuProvider implements ExtendedMenuProvider { + private final Grid grid; + private final EnergyStorage energyStorage; + private final DiskInventory diskInventory; + private final SlotReference slotReference; + + PortableGridItemExtendedMenuProvider(final Grid grid, + final EnergyStorage energyStorage, + final DiskInventory diskInventory, + final SlotReference slotReference) { + this.grid = grid; + this.energyStorage = energyStorage; + this.diskInventory = diskInventory; + this.slotReference = slotReference; + } + + @Override + public void writeScreenOpeningData(final ServerPlayer player, final FriendlyByteBuf buf) { + PlatformApi.INSTANCE.writeGridScreenOpeningData(grid, buf); + buf.writeLong(energyStorage.getStored()); + buf.writeLong(energyStorage.getCapacity()); + PlatformApi.INSTANCE.writeSlotReference(slotReference, buf); + } + + @Override + public Component getDisplayName() { + return ContentNames.PORTABLE_GRID; + } + + @Nullable + @Override + public AbstractContainerMenu createMenu(final int syncId, final Inventory inventory, final Player player) { + return new PortableGridItemContainerMenu(syncId, inventory, diskInventory, grid, energyStorage, slotReference); + } +} diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridLootItemFunction.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridLootItemFunction.java index 441449776..d6335c96c 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridLootItemFunction.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridLootItemFunction.java @@ -3,9 +3,11 @@ import com.refinedmods.refinedstorage2.platform.common.content.LootFunctions; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.storage.loot.LootContext; import net.minecraft.world.level.storage.loot.functions.LootItemFunction; import net.minecraft.world.level.storage.loot.functions.LootItemFunctionType; +import net.minecraft.world.level.storage.loot.parameters.LootContextParams; public class PortableGridLootItemFunction implements LootItemFunction { @Override @@ -15,7 +17,10 @@ public LootItemFunctionType getType() { @Override public ItemStack apply(final ItemStack itemStack, final LootContext lootContext) { - // TODO: item representation of the portable grid + final BlockEntity blockEntity = lootContext.getParam(LootContextParams.BLOCK_ENTITY); + if (blockEntity instanceof AbstractPortableGridBlockEntity portableGrid) { + PortableGridBlockItem.setDiskInventory(itemStack, portableGrid.getDiskInventory()); + } return itemStack; } } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridOperations.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridOperations.java index 7b41fb300..196f95fdb 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridOperations.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridOperations.java @@ -9,11 +9,11 @@ import com.refinedmods.refinedstorage2.api.storage.InsertableStorage; import com.refinedmods.refinedstorage2.platform.common.Platform; -public class PortableGridOperations implements GridOperations { +class PortableGridOperations implements GridOperations { private final GridOperations delegate; private final EnergyStorage energyStorage; - public PortableGridOperations(final GridOperations delegate, final EnergyStorage energyStorage) { + PortableGridOperations(final GridOperations delegate, final EnergyStorage energyStorage) { this.delegate = delegate; this.energyStorage = energyStorage; } diff --git a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridScreen.java b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridScreen.java index d3b0787a8..eab859392 100644 --- a/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridScreen.java +++ b/refinedstorage2-platform-common/src/main/java/com/refinedmods/refinedstorage2/platform/common/storage/portablegrid/PortableGridScreen.java @@ -13,7 +13,7 @@ import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createIdentifier; -public class PortableGridScreen extends AbstractGridScreen { +public class PortableGridScreen extends AbstractGridScreen { private static final int DISK_SLOT_WIDTH = 30; private static final int DISK_SLOT_HEIGHT = 26; @@ -22,7 +22,9 @@ public class PortableGridScreen extends AbstractGridScreen new PortableGridBlockItem(Blocks.INSTANCE.getPortableGrid(), PortableGridType.NORMAL) { + @Override + public boolean allowNbtUpdateAnimation(final Player player, + final InteractionHand hand, + final ItemStack oldStack, + final ItemStack newStack) { + return AbstractModInitializer.allowNbtUpdateAnimation(oldStack, newStack); + } + }, + () -> new PortableGridBlockItem(Blocks.INSTANCE.getCreativePortableGrid(), PortableGridType.CREATIVE) { + @Override + public boolean allowNbtUpdateAnimation(final Player player, + final InteractionHand hand, + final ItemStack oldStack, + final ItemStack newStack) { + return AbstractModInitializer.allowNbtUpdateAnimation(oldStack, newStack); + } } ); registerUpgradeMappings(); @@ -350,6 +369,10 @@ private void registerEnergyItemProviders() { (stack, context) -> new EnergyStorageAdapter(controller.get().createEnergyStorage(stack)), controller.get() )); + EnergyStorage.ITEM.registerForItems( + (stack, context) -> new EnergyStorageAdapter(Items.INSTANCE.getPortableGrid().createEnergyStorage(stack)), + Items.INSTANCE.getPortableGrid() + ); } private void registerTickHandler() { diff --git a/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/storage/portablegrid/DiskLeds.java b/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/storage/portablegrid/DiskLeds.java new file mode 100644 index 000000000..8af6e045b --- /dev/null +++ b/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/storage/portablegrid/DiskLeds.java @@ -0,0 +1,21 @@ +package com.refinedmods.refinedstorage2.platform.fabric.storage.portablegrid; + +import com.refinedmods.refinedstorage2.api.storage.StorageState; + +import net.minecraft.client.resources.model.BakedModel; + +record DiskLeds( + BakedModel inactiveBaker, + BakedModel normalBaker, + BakedModel nearCapacityBaker, + BakedModel fullBaker +) { + BakedModel forState(final StorageState state) { + return switch (state) { + case INACTIVE -> inactiveBaker; + case NEAR_CAPACITY -> nearCapacityBaker; + case FULL -> fullBaker; + default -> normalBaker; + }; + } +} diff --git a/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/storage/portablegrid/PortableGridBakedModel.java b/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/storage/portablegrid/PortableGridBakedModel.java index 1fab9829e..3db00da0a 100644 --- a/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/storage/portablegrid/PortableGridBakedModel.java +++ b/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/storage/portablegrid/PortableGridBakedModel.java @@ -1,7 +1,10 @@ package com.refinedmods.refinedstorage2.platform.fabric.storage.portablegrid; +import com.refinedmods.refinedstorage2.api.storage.StorageState; import com.refinedmods.refinedstorage2.platform.common.storage.Disk; import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridBlock; +import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridBlockItem; +import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridBlockItemRenderInfo; import com.refinedmods.refinedstorage2.platform.common.support.direction.BiDirection; import com.refinedmods.refinedstorage2.platform.fabric.support.render.QuadRotators; import com.refinedmods.refinedstorage2.platform.fabric.support.render.QuadTranslator; @@ -12,6 +15,8 @@ import net.fabricmc.fabric.api.renderer.v1.model.ForwardingBakedModel; import net.fabricmc.fabric.api.renderer.v1.render.RenderContext; import net.fabricmc.fabric.api.rendering.data.v1.RenderAttachedBlockView; +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientLevel; import net.minecraft.client.resources.model.BakedModel; import net.minecraft.core.BlockPos; import net.minecraft.util.RandomSource; @@ -22,21 +27,25 @@ public class PortableGridBakedModel extends ForwardingBakedModel { private static final QuadTranslator MOVE_TO_DISK_LOCATION = new QuadTranslator(0, -12 / 16F, 9 / 16F); + private static final QuadTranslator MOVE_TO_DISK_LED_LOCATION = new QuadTranslator(0, -12 / 16F, 9 / 16F); private final BakedModel activeModel; private final BakedModel inactiveModel; private final Map diskModels; private final QuadRotators quadRotators; + private final DiskLeds diskLeds; - public PortableGridBakedModel(final BakedModel activeModel, - final BakedModel inactiveModel, - final Map diskModels, - final QuadRotators quadRotators) { + PortableGridBakedModel(final BakedModel activeModel, + final BakedModel inactiveModel, + final Map diskModels, + final QuadRotators quadRotators, + final DiskLeds diskLeds) { this.wrapped = inactiveModel; this.activeModel = activeModel; this.inactiveModel = inactiveModel; this.diskModels = diskModels; this.quadRotators = quadRotators; + this.diskLeds = diskLeds; } @Override @@ -48,7 +57,29 @@ public boolean isVanillaAdapter() { public void emitItemQuads(final ItemStack stack, final Supplier randomSupplier, final RenderContext context) { - inactiveModel.emitItemQuads(stack, randomSupplier, context); + final ClientLevel level = Minecraft.getInstance().level; + if (level == null) { + return; + } + final PortableGridBlockItemRenderInfo renderInfo = PortableGridBlockItem.getRenderInfo(stack, level); + (renderInfo.active() ? activeModel : inactiveModel).emitItemQuads(stack, randomSupplier, context); + if (renderInfo.disk().state() != StorageState.NONE) { + final BakedModel diskModel = diskModels.get(renderInfo.disk().item()); + if (diskModel == null) { + return; + } + context.pushTransform(MOVE_TO_DISK_LOCATION); + context.pushTransform(quadRotators.forDirection(BiDirection.WEST)); + diskModel.emitItemQuads(stack, randomSupplier, context); + context.popTransform(); + context.popTransform(); + + context.pushTransform(MOVE_TO_DISK_LED_LOCATION); + context.pushTransform(quadRotators.forDirection(BiDirection.WEST)); + diskLeds.forState(renderInfo.disk().state()).emitItemQuads(stack, randomSupplier, context); + context.popTransform(); + context.popTransform(); + } } @Override diff --git a/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/storage/portablegrid/PortableGridUnbakedModel.java b/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/storage/portablegrid/PortableGridUnbakedModel.java index 11c6136b8..8923d57d2 100644 --- a/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/storage/portablegrid/PortableGridUnbakedModel.java +++ b/refinedstorage2-platform-fabric/src/main/java/com/refinedmods/refinedstorage2/platform/fabric/storage/portablegrid/PortableGridUnbakedModel.java @@ -24,6 +24,10 @@ public class PortableGridUnbakedModel implements UnbakedModel { private static final ResourceLocation ACTIVE_MODEL = createIdentifier("block/portable_grid/active"); private static final ResourceLocation INACTIVE_MODEL = createIdentifier("block/portable_grid/inactive"); + private static final ResourceLocation INACTIVE_LED_MODEL = createIdentifier("block/disk/led_inactive"); + private static final ResourceLocation NORMAL_LED_MODEL = createIdentifier("block/disk/led_normal"); + private static final ResourceLocation NEAR_CAPACITY_LED_MODEL = createIdentifier("block/disk/led_near_capacity"); + private static final ResourceLocation FULL_LED_MODEL = createIdentifier("block/disk/led_full"); private final QuadRotators quadRotators; @@ -40,6 +44,10 @@ public Collection getDependencies() { public void resolveParents(final Function modelGetter) { modelGetter.apply(ACTIVE_MODEL).resolveParents(modelGetter); modelGetter.apply(INACTIVE_MODEL).resolveParents(modelGetter); + modelGetter.apply(INACTIVE_LED_MODEL).resolveParents(modelGetter); + modelGetter.apply(NORMAL_LED_MODEL).resolveParents(modelGetter); + modelGetter.apply(NEAR_CAPACITY_LED_MODEL).resolveParents(modelGetter); + modelGetter.apply(FULL_LED_MODEL).resolveParents(modelGetter); PlatformApi.INSTANCE.getStorageContainerItemHelper().getDiskModels().forEach( diskModel -> modelGetter.apply(diskModel).resolveParents(modelGetter) ); @@ -62,7 +70,13 @@ public BakedModel bake(final ModelBaker baker, requireNonNull(baker.bake(ACTIVE_MODEL, state)), requireNonNull(baker.bake(INACTIVE_MODEL, state)), diskModels, - quadRotators + quadRotators, + new DiskLeds( + requireNonNull(baker.bake(INACTIVE_LED_MODEL, state)), + requireNonNull(baker.bake(NORMAL_LED_MODEL, state)), + requireNonNull(baker.bake(NEAR_CAPACITY_LED_MODEL, state)), + requireNonNull(baker.bake(FULL_LED_MODEL, state)) + ) ); } } diff --git a/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/ModInitializer.java b/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/ModInitializer.java index 3fdf498d6..a09b377fe 100644 --- a/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/ModInitializer.java +++ b/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/ModInitializer.java @@ -13,6 +13,7 @@ import com.refinedmods.refinedstorage2.platform.common.content.RegistryCallback; import com.refinedmods.refinedstorage2.platform.common.grid.WirelessGridItem; import com.refinedmods.refinedstorage2.platform.common.iface.InterfacePlatformExternalStorageProviderFactory; +import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridBlockItem; import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridType; import com.refinedmods.refinedstorage2.platform.common.support.AbstractBaseBlock; import com.refinedmods.refinedstorage2.platform.common.support.packet.PacketIds; @@ -228,6 +229,22 @@ public boolean shouldCauseReequipAnimation(final ItemStack oldStack, final boolean slotChanged) { return AbstractModInitializer.allowNbtUpdateAnimation(oldStack, newStack); } + }, + () -> new PortableGridBlockItem(Blocks.INSTANCE.getPortableGrid(), PortableGridType.NORMAL) { + @Override + public boolean shouldCauseReequipAnimation(final ItemStack oldStack, + final ItemStack newStack, + final boolean slotChanged) { + return AbstractModInitializer.allowNbtUpdateAnimation(oldStack, newStack); + } + }, + () -> new PortableGridBlockItem(Blocks.INSTANCE.getCreativePortableGrid(), PortableGridType.CREATIVE) { + @Override + public boolean shouldCauseReequipAnimation(final ItemStack oldStack, + final ItemStack newStack, + final boolean slotChanged) { + return AbstractModInitializer.allowNbtUpdateAnimation(oldStack, newStack); + } } ); itemRegistry.register(eventBus); @@ -296,6 +313,11 @@ private void registerCapabilities(final RegisterCapabilitiesEvent event) { (stack, ctx) -> new EnergyStorageAdapter(controllerItem.get().createEnergyStorage(stack)), controllerItem.get() )); + event.registerItem( + Capabilities.EnergyStorage.ITEM, + (stack, ctx) -> new EnergyStorageAdapter(Items.INSTANCE.getPortableGrid().createEnergyStorage(stack)), + Items.INSTANCE.getPortableGrid() + ); } private void registerSounds(final IEventBus eventBus) { diff --git a/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/storage/portablegrid/DiskLedBakers.java b/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/storage/portablegrid/DiskLedBakers.java new file mode 100644 index 000000000..cfb3f61b9 --- /dev/null +++ b/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/storage/portablegrid/DiskLedBakers.java @@ -0,0 +1,20 @@ +package com.refinedmods.refinedstorage2.platform.forge.storage.portablegrid; + +import com.refinedmods.refinedstorage2.api.storage.StorageState; +import com.refinedmods.refinedstorage2.platform.forge.support.render.RotationTranslationModelBaker; + +record DiskLedBakers( + RotationTranslationModelBaker inactiveBaker, + RotationTranslationModelBaker normalBaker, + RotationTranslationModelBaker nearCapacityBaker, + RotationTranslationModelBaker fullBaker +) { + RotationTranslationModelBaker forState(final StorageState state) { + return switch (state) { + case INACTIVE -> inactiveBaker; + case NEAR_CAPACITY -> nearCapacityBaker; + case FULL -> fullBaker; + default -> normalBaker; + }; + } +} diff --git a/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/storage/portablegrid/PortableGridBakedModel.java b/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/storage/portablegrid/PortableGridBakedModel.java index 121a16686..2299919f3 100644 --- a/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/storage/portablegrid/PortableGridBakedModel.java +++ b/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/storage/portablegrid/PortableGridBakedModel.java @@ -1,10 +1,10 @@ package com.refinedmods.refinedstorage2.platform.forge.storage.portablegrid; import com.refinedmods.refinedstorage2.api.storage.StorageState; -import com.refinedmods.refinedstorage2.platform.common.content.Items; import com.refinedmods.refinedstorage2.platform.common.storage.Disk; -import com.refinedmods.refinedstorage2.platform.common.storage.FluidStorageType; import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridBlock; +import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridBlockItem; +import com.refinedmods.refinedstorage2.platform.common.storage.portablegrid.PortableGridBlockItemRenderInfo; import com.refinedmods.refinedstorage2.platform.common.support.direction.BiDirection; import com.refinedmods.refinedstorage2.platform.forge.support.render.DiskModelBaker; import com.refinedmods.refinedstorage2.platform.forge.support.render.ItemBakedModel; @@ -35,6 +35,7 @@ class PortableGridBakedModel extends BakedModelWrapper { private static final Vector3f MOVE_TO_DISK_LOCATION = new Vector3f(0, -12 / 16F, 9 / 16F); + private static final Vector3f MOVE_TO_DISK_LED_LOCATION = new Vector3f(0, -12 / 16F, 9 / 16F); private final LoadingCache> cache; private final PortableGridItemOverrides itemOverrides = new PortableGridItemOverrides(); @@ -42,7 +43,8 @@ class PortableGridBakedModel extends BakedModelWrapper { PortableGridBakedModel(final BakedModel baseModel, final RotationTranslationModelBaker activeModelBaker, final RotationTranslationModelBaker inactiveModelBaker, - final DiskModelBaker diskModelBaker) { + final DiskModelBaker diskModelBaker, + final DiskLedBakers diskLedBakers) { super(baseModel); this.cache = CacheBuilder.newBuilder().build(CacheLoader.from(cacheKey -> { final RotationTranslationModelBaker baseModelBaker = cacheKey.active @@ -62,6 +64,12 @@ class PortableGridBakedModel extends BakedModelWrapper { .rotate(BiDirection.WEST) .build()).getQuads(null, cacheKey.side(), RandomSource.create())); } + if (cacheKey.includeLed && cacheKey.disk.state() != StorageState.NONE) { + quads.addAll(diskLedBakers.forState(cacheKey.disk.state()).bake(TransformationBuilder.create() + .translate(MOVE_TO_DISK_LED_LOCATION) + .rotate(BiDirection.WEST) + .build()).getQuads(null, cacheKey.side(), RandomSource.create())); + } return quads; })); } @@ -84,7 +92,7 @@ public List getQuads(@Nullable final BlockState state, return super.getQuads(state, side, randomSource); } final boolean active = state.getValue(PortableGridBlock.ACTIVE); - return cache.getUnchecked(new CacheKey(side, direction, active, disk)); + return cache.getUnchecked(new CacheKey(side, direction, active, disk, false)); } @Override @@ -108,14 +116,21 @@ public BakedModel resolve(final BakedModel bakedModel, @Nullable final ClientLevel level, @Nullable final LivingEntity entity, final int seed) { - final Disk disk = new Disk( - Items.INSTANCE.getFluidStorageDisk(FluidStorageType.Variant.SIXTY_FOUR_B), - StorageState.NEAR_CAPACITY - ); - return itemCache.getUnchecked(new CacheKey(null, BiDirection.NORTH, true, disk)); + final PortableGridBlockItemRenderInfo renderInfo = PortableGridBlockItem.getRenderInfo(stack, level); + return itemCache.getUnchecked(new CacheKey( + null, + BiDirection.NORTH, + renderInfo.active(), + renderInfo.disk(), + true + )); } } - private record CacheKey(@Nullable Direction side, BiDirection direction, boolean active, Disk disk) { + private record CacheKey(@Nullable Direction side, + BiDirection direction, + boolean active, + Disk disk, + boolean includeLed) { } } diff --git a/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/storage/portablegrid/PortableGridUnbakedGeometry.java b/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/storage/portablegrid/PortableGridUnbakedGeometry.java index a5e6a89bf..e30b9a1a8 100644 --- a/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/storage/portablegrid/PortableGridUnbakedGeometry.java +++ b/refinedstorage2-platform-forge/src/main/java/com/refinedmods/refinedstorage2/platform/forge/storage/portablegrid/PortableGridUnbakedGeometry.java @@ -23,6 +23,10 @@ public class PortableGridUnbakedGeometry implements IUnbakedGeometry { private static final ResourceLocation ACTIVE_MODEL = createIdentifier("block/portable_grid/active"); private static final ResourceLocation INACTIVE_MODEL = createIdentifier("block/portable_grid/inactive"); + private static final ResourceLocation INACTIVE_LED_MODEL = createIdentifier("block/disk/led_inactive"); + private static final ResourceLocation NORMAL_LED_MODEL = createIdentifier("block/disk/led_normal"); + private static final ResourceLocation NEAR_CAPACITY_LED_MODEL = createIdentifier("block/disk/led_near_capacity"); + private static final ResourceLocation FULL_LED_MODEL = createIdentifier("block/disk/led_full"); PortableGridUnbakedGeometry() { } @@ -32,6 +36,10 @@ public void resolveParents(final Function modelG final IGeometryBakingContext context) { modelGetter.apply(ACTIVE_MODEL).resolveParents(modelGetter); modelGetter.apply(INACTIVE_MODEL).resolveParents(modelGetter); + modelGetter.apply(INACTIVE_LED_MODEL).resolveParents(modelGetter); + modelGetter.apply(NORMAL_LED_MODEL).resolveParents(modelGetter); + modelGetter.apply(NEAR_CAPACITY_LED_MODEL).resolveParents(modelGetter); + modelGetter.apply(FULL_LED_MODEL).resolveParents(modelGetter); PlatformApi.INSTANCE.getStorageContainerItemHelper().getDiskModels().forEach( diskModel -> modelGetter.apply(diskModel).resolveParents(modelGetter) ); @@ -48,7 +56,13 @@ public BakedModel bake(final IGeometryBakingContext context, requireNonNull(baker.bake(INACTIVE_MODEL, modelState, spriteGetter)), new RotationTranslationModelBaker(modelState, baker, spriteGetter, ACTIVE_MODEL), new RotationTranslationModelBaker(modelState, baker, spriteGetter, INACTIVE_MODEL), - new DiskModelBaker(modelState, baker, spriteGetter) + new DiskModelBaker(modelState, baker, spriteGetter), + new DiskLedBakers( + new RotationTranslationModelBaker(modelState, baker, spriteGetter, INACTIVE_LED_MODEL), + new RotationTranslationModelBaker(modelState, baker, spriteGetter, NORMAL_LED_MODEL), + new RotationTranslationModelBaker(modelState, baker, spriteGetter, NEAR_CAPACITY_LED_MODEL), + new RotationTranslationModelBaker(modelState, baker, spriteGetter, FULL_LED_MODEL) + ) ); } } diff --git a/refinedstorage2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/StateTrackedStorage.java b/refinedstorage2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/StateTrackedStorage.java index 980d7dd4d..4c902eff7 100644 --- a/refinedstorage2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/StateTrackedStorage.java +++ b/refinedstorage2-storage-api/src/main/java/com/refinedmods/refinedstorage2/api/storage/StateTrackedStorage.java @@ -35,7 +35,7 @@ private StorageState computeState() { return StorageState.NORMAL; } - private StorageState computeState(final long capacity, final long stored) { + public static StorageState computeState(final long capacity, final long stored) { final double fullness = stored / (double) capacity; if (fullness >= 1D) { return StorageState.FULL;