Skip to content

Commit

Permalink
feat: custom disk models
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Dec 27, 2023
1 parent 4b462a8 commit f8fd21f
Show file tree
Hide file tree
Showing 15 changed files with 412 additions and 65 deletions.
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 @@ -4,13 +4,17 @@
import com.refinedmods.refinedstorage2.api.storage.StorageInfo;

import java.util.List;
import java.util.Map;
import java.util.Optional;
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 Down Expand Up @@ -40,4 +44,10 @@ void appendToTooltip(ItemStack stack,
void transferToBlockEntity(ItemStack stack, ItemTransferableStorageBlockEntity blockEntity);

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 @@ -7,8 +7,13 @@
import com.refinedmods.refinedstorage2.platform.api.storage.StorageContainerItemHelper;
import com.refinedmods.refinedstorage2.platform.api.storage.StorageRepository;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.function.LongFunction;
import javax.annotation.Nullable;
Expand All @@ -17,10 +22,12 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.item.ItemEntity;
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 @@ -31,6 +38,9 @@ public class StorageContainerItemHelperImpl implements StorageContainerItemHelpe
private static final Logger LOGGER = LoggerFactory.getLogger(StorageContainerItemHelperImpl.class);
private static final String TAG_ID = "id";

private final Map<Item, ResourceLocation> diskModelsByItem = new HashMap<>();
private final Set<ResourceLocation> diskModels = new HashSet<>();

@Override
public Optional<Storage<?>> resolve(final StorageRepository storageRepository, final ItemStack stack) {
return getId(stack).flatMap(storageRepository::get);
Expand Down Expand Up @@ -138,6 +148,22 @@ public void transferFromBlockEntity(final ItemStack stack, final ItemTransferabl
}
}

@Override
public void registerDiskModel(final Item item, final ResourceLocation model) {
diskModelsByItem.put(item, model);
diskModels.add(model);
}

@Override
public Set<ResourceLocation> getDiskModels() {
return diskModels;
}

@Override
public Map<Item, ResourceLocation> getDiskModelsByItem() {
return Collections.unmodifiableMap(diskModelsByItem);
}

private Optional<UUID> getId(final ItemStack stack) {
if (stack.hasTag() && stack.getTag() != null && stack.getTag().hasUUID(TAG_ID)) {
return Optional.of(stack.getTag().getUUID(TAG_ID));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
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;
Expand Down Expand Up @@ -88,9 +89,17 @@ protected AbstractDiskDriveBlockEntity(final BlockPos pos, final BlockState stat
getNode().setNormalizer(filter.createNormalizer());
}

public static boolean hasDisk(final CompoundTag tag, final int slot) {
return tag.contains(TAG_DISK_INVENTORY)
&& ContainerUtil.hasItemInSlot(tag.getCompound(TAG_DISK_INVENTORY), slot);
@Nullable
public static Item getDisk(final CompoundTag tag, final int slot) {
if (!tag.contains(TAG_DISK_INVENTORY)) {
return null;
}
final CompoundTag diskInventoryTag = tag.getCompound(TAG_DISK_INVENTORY);
if (!ContainerUtil.hasItemInSlot(diskInventoryTag, slot)) {
return null;
}
final ItemStack diskStack = ContainerUtil.getItemInSlot(diskInventoryTag, slot);
return diskStack.isEmpty() ? null : diskStack.getItem();
}

void updateDiskStateIfNecessaryInLevel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ public static CompoundTag write(final Container container) {

public static void read(final CompoundTag tag, final Container container) {
for (int i = 0; i < container.getContainerSize(); ++i) {
if (hasItemInSlot(tag, i)) {
final CompoundTag stackTag = tag.getCompound(getSlotKey(i));
readSlot(container, i, stackTag);
if (!hasItemInSlot(tag, i)) {
continue;
}
final ItemStack stack = getItemInSlot(tag, i);
if (stack.isEmpty()) {
continue;
}
container.setItem(i, stack);
}
}

Expand All @@ -36,10 +40,7 @@ public static boolean hasItemInSlot(final CompoundTag tag, final int slot) {
return tag.contains(getSlotKey(slot));
}

private static void readSlot(final Container container, final int slot, final CompoundTag stackTag) {
final ItemStack stack = ItemStack.of(stackTag);
if (!stack.isEmpty()) {
container.setItem(slot, stack);
}
public static ItemStack getItemInSlot(final CompoundTag tag, final int i) {
return ItemStack.of(tag.getCompound(getSlotKey(i)));
}
}
Loading

0 comments on commit f8fd21f

Please sign in to comment.