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

feat: upgrading storage disks and storage blocks #659

Merged
merged 1 commit into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

- You can now upgrade Storage Disks and Storage Blocks to a higher tier by combining with a higher tier Storage Part. The original Storage Part will be returned.

### Changed

- Updated to Minecraft 1.21.1.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
Expand All @@ -35,6 +36,16 @@ protected AbstractStorageContainerBlockItem(
this.helper = helper;
}

@Override
public void inventoryTick(final ItemStack stack,
final Level level,
final Entity entity,
final int slotId,
final boolean isSelected) {
super.inventoryTick(stack, level, entity, slotId, isSelected);
helper.transferStorageIfNecessary(stack, level, entity, this::createStorage);
}

@Override
public InteractionResultHolder<ItemStack> use(final Level level, final Player player, final InteractionHand hand) {
final ItemStack stack = player.getItemInHand(hand);
Expand Down Expand Up @@ -76,13 +87,16 @@ public void appendHoverText(final ItemStack stack,
final TooltipFlag flag) {
super.appendHoverText(stack, context, tooltip, flag);
final StorageRepository storageRepository = RefinedStorageApi.INSTANCE.getClientStorageRepository();
helper.appendToTooltip(stack, storageRepository, tooltip, flag, this::formatAmount, hasCapacity());
helper.appendToTooltip(stack, storageRepository, tooltip, flag, this::formatAmount, getCapacity());
}

protected abstract boolean hasCapacity();
@Nullable
protected abstract Long getCapacity();

protected abstract String formatAmount(long amount);

protected abstract SerializableStorage createStorage(StorageRepository storageRepository);

protected abstract ItemStack createPrimaryDisassemblyByproduct(int count);

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ public void inventoryTick(final ItemStack stack,
final int slot,
final boolean selected) {
super.inventoryTick(stack, level, entity, slot, selected);
if (!level.isClientSide() && !helper.hasStorage(stack) && entity instanceof Player) {
final StorageRepository storageRepository = RefinedStorageApi.INSTANCE.getStorageRepository(level);
helper.setStorage(storageRepository, stack, createStorage(storageRepository));
}
helper.transferStorageIfNecessary(stack, level, entity, this::createStorage);
helper.loadStorageIfNecessary(stack, level, entity, this::createStorage);
}

@Override
Expand All @@ -64,10 +62,11 @@ public void appendHoverText(final ItemStack stack,
final TooltipFlag flag) {
super.appendHoverText(stack, context, tooltip, flag);
final StorageRepository storageRepository = RefinedStorageApi.INSTANCE.getClientStorageRepository();
helper.appendToTooltip(stack, storageRepository, tooltip, flag, this::formatAmount, hasCapacity());
helper.appendToTooltip(stack, storageRepository, tooltip, flag, this::formatAmount, getCapacity());
}

protected abstract boolean hasCapacity();
@Nullable
protected abstract Long getCapacity();

protected abstract String formatAmount(long amount);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
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.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
Expand All @@ -21,9 +23,15 @@
public interface StorageContainerItemHelper {
Optional<SerializableStorage> resolveStorage(StorageRepository storageRepository, ItemStack stack);

void setStorage(StorageRepository storageRepository, ItemStack stack, SerializableStorage storage);
void loadStorageIfNecessary(ItemStack stack,
Level level,
Entity entity,
Function<StorageRepository, SerializableStorage> factory);

boolean hasStorage(ItemStack stack);
void transferStorageIfNecessary(ItemStack stack,
Level level,
Entity entity,
Function<StorageRepository, SerializableStorage> factory);

Optional<StorageInfo> getInfo(StorageRepository storageRepository, ItemStack stack);

Expand All @@ -38,7 +46,7 @@ void appendToTooltip(ItemStack stack,
List<Component> tooltip,
TooltipFlag context,
LongFunction<String> amountFormatter,
boolean hasCapacity);
@Nullable Long capacity);

void transferToBlockEntity(ItemStack stack, StorageBlockEntity blockEntity);

Expand All @@ -49,4 +57,6 @@ void appendToTooltip(ItemStack stack,
Set<ResourceLocation> getDiskModels();

Map<Item, ResourceLocation> getDiskModelsByItem();

void markAsToTransfer(ItemStack from, ItemStack to);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public interface StorageRepository {
*/
Optional<SerializableStorage> removeIfEmpty(UUID id);

/**
* Removes a storage by ID.
*
* @param id the id
*/
void remove(UUID id);

/**
* Retrieves info for a given storage ID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@
import com.refinedmods.refinedstorage.common.security.SecurityManagerContainerMenu;
import com.refinedmods.refinedstorage.common.storage.FluidStorageVariant;
import com.refinedmods.refinedstorage.common.storage.ItemStorageVariant;
import com.refinedmods.refinedstorage.common.storage.StorageContainerUpgradeRecipe;
import com.refinedmods.refinedstorage.common.storage.StorageContainerUpgradeRecipeSerializer;
import com.refinedmods.refinedstorage.common.storage.StorageTypes;
import com.refinedmods.refinedstorage.common.storage.diskdrive.AbstractDiskDriveBlockEntity;
import com.refinedmods.refinedstorage.common.storage.diskdrive.DiskDriveBlock;
Expand Down Expand Up @@ -815,6 +817,42 @@ protected final void registerRecipeSerializers(final RegistryCallback<RecipeSeri
createIdentifier("upgrade_with_enchanted_book"),
UpgradeWithEnchantedBookRecipeSerializer::new
);
callback.register(
createIdentifier("storage_disk_upgrade"),
() -> new StorageContainerUpgradeRecipeSerializer<>(
ItemStorageVariant.values(),
to -> new StorageContainerUpgradeRecipe<>(
ItemStorageVariant.values(), to, Items.INSTANCE::getItemStorageDisk
)
)
);
callback.register(
createIdentifier("fluid_storage_disk_upgrade"),
() -> new StorageContainerUpgradeRecipeSerializer<>(
FluidStorageVariant.values(),
to -> new StorageContainerUpgradeRecipe<>(
FluidStorageVariant.values(), to, Items.INSTANCE::getFluidStorageDisk
)
)
);
callback.register(
createIdentifier("storage_block_upgrade"),
() -> new StorageContainerUpgradeRecipeSerializer<>(
ItemStorageVariant.values(),
to -> new StorageContainerUpgradeRecipe<>(
ItemStorageVariant.values(), to, Blocks.INSTANCE::getItemStorageBlock
)
)
);
callback.register(
createIdentifier("fluid_storage_block_upgrade"),
() -> new StorageContainerUpgradeRecipeSerializer<>(
FluidStorageVariant.values(),
to -> new StorageContainerUpgradeRecipe<>(
FluidStorageVariant.values(), to, Blocks.INSTANCE::getFluidStorageBlock
)
)
);
}

protected final void registerDataComponents(final RegistryCallback<DataComponentType<?>> callback) {
Expand All @@ -833,6 +871,12 @@ protected final void registerDataComponents(final RegistryCallback<DataComponent
.persistent(UUIDUtil.CODEC)
.networkSynchronized(UUIDUtil.STREAM_CODEC)
.build()));
DataComponents.INSTANCE.setStorageReferenceToBeTransferred(
callback.register(createIdentifier("storage_reference_to_be_transferred"),
() -> DataComponentType.<UUID>builder()
.persistent(UUIDUtil.CODEC)
.networkSynchronized(UUIDUtil.STREAM_CODEC)
.build()));
DataComponents.INSTANCE.setRegulatorUpgradeState(
callback.register(createIdentifier("regulator_upgrade_state"),
() -> DataComponentType.<RegulatorUpgradeState>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public final class DataComponents {
@Nullable
private Supplier<DataComponentType<UUID>> storageReference;
@Nullable
private Supplier<DataComponentType<UUID>> storageReferenceToBeTransferred;
@Nullable
private Supplier<DataComponentType<RegulatorUpgradeState>> regulatorUpgradeState;
@Nullable
private Supplier<DataComponentType<SecurityCardBoundPlayer>> securityCardBoundPlayer;
Expand Down Expand Up @@ -74,6 +76,14 @@ public void setStorageReference(@Nullable final Supplier<DataComponentType<UUID>
this.storageReference = supplier;
}

public DataComponentType<UUID> getStorageReferenceToBeTransferred() {
return requireNonNull(storageReferenceToBeTransferred).get();
}

public void setStorageReferenceToBeTransferred(@Nullable final Supplier<DataComponentType<UUID>> supplier) {
this.storageReferenceToBeTransferred = supplier;
}

public DataComponentType<RegulatorUpgradeState> getRegulatorUpgradeState() {
return requireNonNull(regulatorUpgradeState).get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public Optional<SerializableStorage> removeIfEmpty(final UUID id) {
throw new UnsupportedOperationException();
}

@Override
public void remove(final UUID id) {
throw new UnsupportedOperationException();
}

public void setInfo(final UUID id, final long stored, final long capacity) {
info.put(id, new StorageInfo(stored, capacity));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.refinedmods.refinedstorage.common.storage;

import com.refinedmods.refinedstorage.common.Platform;
import com.refinedmods.refinedstorage.common.content.Items;

import javax.annotation.Nullable;

public enum FluidStorageVariant {
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.item.Item;

public enum FluidStorageVariant implements StringRepresentable, StorageVariant {
SIXTY_FOUR_B("64b", 64L),
TWO_HUNDRED_FIFTY_SIX_B("256b", 256L),
THOUSAND_TWENTY_FOUR_B("1024b", 1024L),
Expand All @@ -29,6 +33,7 @@ public Long getCapacityInBuckets() {
return capacityInBuckets;
}

@Override
@Nullable
public Long getCapacity() {
if (capacityInBuckets == null) {
Expand All @@ -37,7 +42,17 @@ public Long getCapacity() {
return capacityInBuckets * Platform.INSTANCE.getBucketAmount();
}

public boolean hasCapacity() {
return capacityInBuckets != null;
@Nullable
@Override
public Item getStoragePart() {
if (this == CREATIVE) {
return null;
}
return Items.INSTANCE.getFluidStoragePart(this);
}

@Override
public String getSerializedName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.refinedmods.refinedstorage.common.storage;

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

import javax.annotation.Nullable;

public enum ItemStorageVariant {
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.item.Item;

public enum ItemStorageVariant implements StringRepresentable, StorageVariant {
ONE_K("1k", 1024L),
FOUR_K("4k", 1024 * 4L),
SIXTEEN_K("16k", 1024 * 4 * 4L),
Expand All @@ -22,12 +27,23 @@ public String getName() {
return name;
}

@Override
@Nullable
public Long getCapacity() {
return capacity;
}

public boolean hasCapacity() {
return capacity != null;
@Nullable
@Override
public Item getStoragePart() {
if (this == CREATIVE) {
return null;
}
return Items.INSTANCE.getItemStoragePart(this);
}

@Override
public String getSerializedName() {
return name;
}
}
Loading
Loading