Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom disk models #461

Merged
merged 5 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- Custom disk models. Fluid disks now have a different model.

## [2.0.0-milestone.3.2] - 2023-11-03

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ public MultiStorageInternalStorage(final Storage<T> delegate,
this.delegate = delegate;
this.storageChannelType = storageChannelType;
this.listener = listener;
this.state = getState();
this.state = computeState();
}

public StorageChannelType<T> getStorageChannelType() {
return storageChannelType;
}

public MultiStorageStorageState getState() {
public MultiStorageStorageState computeState() {
if (delegate instanceof LimitedStorage<?> limitedStorage) {
return getStateWithCapacity(limitedStorage.getCapacity());
return computeState(limitedStorage.getCapacity());
}
return MultiStorageStorageState.NORMAL;
}

private MultiStorageStorageState getStateWithCapacity(final long capacity) {
private MultiStorageStorageState computeState(final long capacity) {
final double fullness = (double) delegate.getStored() / capacity;
if (fullness >= 1D) {
return MultiStorageStorageState.FULL;
Expand All @@ -54,7 +54,7 @@ private MultiStorageStorageState getStateWithCapacity(final long capacity) {
}

private void checkStateChanged() {
final MultiStorageStorageState currentState = getState();
final MultiStorageStorageState currentState = computeState();
if (state != currentState) {
this.state = currentState;
notifyListener();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,21 @@ public long getEnergyUsage() {
return energyUsage + (energyUsagePerStorage * activeStorages);
}

public MultiStorageState createState() {
return MultiStorageState.of(cache.length, idx -> getState(cache[idx]));
public int getSize() {
return cache.length;
}

public MultiStorageStorageState getState(final int index) {
return computeState(cache[index]);
}

private MultiStorageStorageState getState(@Nullable final MultiStorageInternalStorage<?> internalStorage) {
private MultiStorageStorageState computeState(@Nullable final MultiStorageInternalStorage<?> internalStorage) {
if (internalStorage == null) {
return MultiStorageStorageState.NONE;
} else if (!isActive()) {
return MultiStorageStorageState.INACTIVE;
}
return internalStorage.getState();
return internalStorage.computeState();
}

@Override
Expand All @@ -193,10 +197,6 @@ public <T> Optional<Storage<T>> getStorageForChannel(final StorageChannelType<T>
return Optional.empty();
}

public int getSize() {
return cache.length;
}

private record StorageChange(boolean removed,
MultiStorageExposedStorage<?> exposedStorage,
MultiStorageInternalStorage<?> internalStorage) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,15 @@ void shouldInitializeMultipleTimes(

@Test
void testInitialState(@InjectNetworkStorageChannel final StorageChannel<String> networkStorage) {
// Act
final MultiStorageState states = sut.createState();

// Assert
assertThat(sut.getEnergyUsage()).isEqualTo(BASE_USAGE);
assertThat(sut.getFilterMode()).isEqualTo(FilterMode.BLOCK);
assertThat(networkStorage.getAll()).isEmpty();
assertThat(networkStorage.getStored()).isZero();
assertThat(states.getStates())
.hasSize(9)
.allMatch(state -> state == MultiStorageStorageState.NONE);
assertThat(sut.getSize()).isEqualTo(9);
for (int i = 0; i < 9; ++i) {
assertThat(sut.getState(i)).isEqualTo(MultiStorageStorageState.NONE);
}
}

@ParameterizedTest
Expand All @@ -155,20 +152,18 @@ void testState(final boolean active) {
sut.setProvider(provider);
sut.setActive(active);

final MultiStorageState state = sut.createState();

// Assert
assertThat(state.getState(0)).isEqualTo(MultiStorageStorageState.NONE);
assertThat(state.getState(1)).isEqualTo(MultiStorageStorageState.NONE);
assertThat(state.getState(2)).isEqualTo(
assertThat(sut.getState(0)).isEqualTo(MultiStorageStorageState.NONE);
assertThat(sut.getState(1)).isEqualTo(MultiStorageStorageState.NONE);
assertThat(sut.getState(2)).isEqualTo(
active ? MultiStorageStorageState.NORMAL : MultiStorageStorageState.INACTIVE);
assertThat(state.getState(3)).isEqualTo(
assertThat(sut.getState(3)).isEqualTo(
active ? MultiStorageStorageState.NORMAL : MultiStorageStorageState.INACTIVE);
assertThat(state.getState(4)).isEqualTo(MultiStorageStorageState.NONE);
assertThat(state.getState(5)).isEqualTo(
assertThat(sut.getState(4)).isEqualTo(MultiStorageStorageState.NONE);
assertThat(sut.getState(5)).isEqualTo(
active ? MultiStorageStorageState.NEAR_CAPACITY : MultiStorageStorageState.INACTIVE);
assertThat(state.getState(6)).isEqualTo(MultiStorageStorageState.NONE);
assertThat(state.getState(7)).isEqualTo(
assertThat(sut.getState(6)).isEqualTo(MultiStorageStorageState.NONE);
assertThat(sut.getState(7)).isEqualTo(
active ? MultiStorageStorageState.FULL : MultiStorageStorageState.INACTIVE);
assertThat(sut.getEnergyUsage()).isEqualTo(BASE_USAGE + (USAGE_PER_STORAGE * 4));
}
Expand Down Expand Up @@ -249,11 +244,10 @@ void shouldNotDetectStorageChangeInInvalidIndex() {
sut.onStorageChanged(9);

// Assert
final MultiStorageState states = sut.createState();

assertThat(states.getStates())
.hasSize(9)
.allMatch(state -> state == MultiStorageStorageState.NONE);
assertThat(sut.getSize()).isEqualTo(9);
for (int i = 0; i < 9; ++i) {
assertThat(sut.getState(i)).isEqualTo(MultiStorageStorageState.NONE);
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.refinedmods.refinedstorage2.platform.api.PlatformApi;

import java.util.List;
import java.util.UUID;
import javax.annotation.Nullable;

import net.minecraft.core.BlockPos;
Expand All @@ -16,12 +15,15 @@
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.apiguardian.api.API;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.1.4")
public abstract class AbstractStorageContainerBlockItem extends BlockItem {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractStorageContainerBlockItem.class);

protected final StorageContainerItemHelper helper;

protected AbstractStorageContainerBlockItem(
Expand Down Expand Up @@ -60,7 +62,11 @@ protected boolean updateCustomBlockEntityTag(final BlockPos pos,
private void updateBlockEntityTag(final BlockPos pos,
final Level level,
final ItemStack stack) {
helper.getId(stack).ifPresent(id -> updateBlockEntityWithStorageId(pos, level.getBlockEntity(pos), id));
if (level.getBlockEntity(pos) instanceof ItemTransferableStorageBlockEntity blockEntity) {
helper.transferToBlockEntity(stack, blockEntity);
} else {
LOGGER.warn("Storage could not be set, block entity does not exist yet at {}", pos);
}
}

@Override
Expand All @@ -84,6 +90,4 @@ public void appendHoverText(final ItemStack stack,

@Nullable
protected abstract ItemStack createSecondaryDisassemblyByproduct(int count);

protected abstract void updateBlockEntityWithStorageId(BlockPos pos, @Nullable BlockEntity blockEntity, UUID id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.refinedmods.refinedstorage2.platform.api.storage;

import java.util.UUID;
import javax.annotation.Nullable;

import org.apiguardian.api.API;

/**
* Implement this on a block entity that contains a storage (ID) that can be transferred to/from an item.
* The "storage block" is an example of such an item.
*/
@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.3")
public interface ItemTransferableStorageBlockEntity {
@Nullable
UUID getStorageId();

void modifyStorageIdAfterAlreadyInitialized(UUID id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import com.refinedmods.refinedstorage2.api.storage.StorageInfo;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.Set;
import java.util.function.LongFunction;
import javax.annotation.Nullable;

import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
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.item.TooltipFlag;
import net.minecraft.world.level.Level;
Expand All @@ -38,9 +41,13 @@ void appendToTooltip(ItemStack stack,
LongFunction<String> amountFormatter,
boolean hasCapacity);

// TODO: remove - leaky abstraction
Optional<UUID> getId(ItemStack stack);
void transferToBlockEntity(ItemStack stack, ItemTransferableStorageBlockEntity blockEntity);

// TODO: remove - leaky abstraction
void setId(ItemStack stack, UUID id);
void transferFromBlockEntity(ItemStack stack, ItemTransferableStorageBlockEntity blockEntity);

void registerDiskModel(Item item, ResourceLocation model);

Set<ResourceLocation> getDiskModels();

Map<Item, ResourceLocation> getDiskModelsByItem();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.refinedmods.refinedstorage2.platform.common.iface.InterfaceScreen;
import com.refinedmods.refinedstorage2.platform.common.importer.ImporterScreen;
import com.refinedmods.refinedstorage2.platform.common.networking.NetworkTransmitterScreen;
import com.refinedmods.refinedstorage2.platform.common.storage.FluidStorageType;
import com.refinedmods.refinedstorage2.platform.common.storage.ItemStorageType;
import com.refinedmods.refinedstorage2.platform.common.storage.diskdrive.DiskDriveScreen;
import com.refinedmods.refinedstorage2.platform.common.storage.externalstorage.ExternalStorageScreen;
import com.refinedmods.refinedstorage2.platform.common.storage.storageblock.FluidStorageBlockScreen;
Expand All @@ -34,11 +36,14 @@
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.MenuAccess;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;

import static com.refinedmods.refinedstorage2.platform.common.util.IdentifierUtil.createIdentifier;

public abstract class AbstractClientModInitializer {
protected static void registerScreens(final ScreenRegistration registration) {
registration.register(Menus.INSTANCE.getDiskDrive(), DiskDriveScreen::new);
Expand Down Expand Up @@ -85,6 +90,24 @@ protected static void handleInputEvents() {
}
}

protected static void registerDiskModels() {
final ResourceLocation diskModel = createIdentifier("block/disk/disk");
for (final ItemStorageType.Variant variant : ItemStorageType.Variant.values()) {
PlatformApi.INSTANCE.getStorageContainerItemHelper().registerDiskModel(
Items.INSTANCE.getItemStorageDisk(variant),
diskModel
);
}

final ResourceLocation fluidDiskModel = createIdentifier("block/disk/fluid_disk");
for (final FluidStorageType.Variant variant : FluidStorageType.Variant.values()) {
PlatformApi.INSTANCE.getStorageContainerItemHelper().registerDiskModel(
Items.INSTANCE.getFluidStorageDisk(variant),
fluidDiskModel
);
}
}

@FunctionalInterface
public interface ScreenRegistration {
<M extends AbstractContainerMenu, U extends Screen & MenuAccess<M>> void register(MenuType<? extends M> type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ private InteractionResult applyConfiguration(
return InteractionResult.SUCCESS;
}

@SuppressWarnings("deprecation") // Forge deprecates registry access this way
private void tryTransferUpgrades(final Player player, final ConfigurationCardTarget target, final CompoundTag tag) {
final ListTag upgradesTag = tag.getList(TAG_UPGRADES, Tag.TAG_STRING);
for (final Tag upgradeItemTag : upgradesTag) {
Expand Down Expand Up @@ -110,6 +111,7 @@ private InteractionResult configurationCardIsConfiguredForDifferentType(
return InteractionResult.CONSUME;
}

@SuppressWarnings("deprecation") // Forge deprecates registry access this way
private void writeConfiguration(final ItemStack stack,
final Player player,
final ConfigurationCardTarget target,
Expand All @@ -128,6 +130,7 @@ private CompoundTag createConfigTag(final ConfigurationCardTarget target) {
return tag;
}

@SuppressWarnings("deprecation") // Forge deprecates registry access this way
private ListTag createUpgradesTag(final ConfigurationCardTarget target) {
final ListTag tag = new ListTag();
target.getUpgradeItems().forEach(item -> {
Expand Down Expand Up @@ -181,11 +184,13 @@ public void appendHoverText(final ItemStack stack,
}

@Nullable
@SuppressWarnings("deprecation") // Forge deprecates registry access this way
private BlockEntityType<?> getConfiguredType(final CompoundTag tag) {
final ResourceLocation type = new ResourceLocation(tag.getString(TAG_TYPE));
return BuiltInRegistries.BLOCK_ENTITY_TYPE.get(type);
}

@SuppressWarnings("deprecation") // Forge deprecates registry access this way
private MutableComponent getConfiguredTypeTranslation(final BlockEntityType<?> type) {
final ResourceLocation typeId = BuiltInRegistries.BLOCK_ENTITY_TYPE.getKey(type);
if (typeId == null) {
Expand Down
Loading
Loading