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: chemical storage disks #15

Merged
merged 1 commit into from
Nov 28, 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

## Added

- Chemical Storage Disk
- Chemical support in the Grid.
- Chemical support in the Storage Monitor.
9 changes: 9 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ repositories {
includeGroup("mekanism")
}
}
maven {
name = "JEI"
url = uri("https://maven.blamejared.com/")
}
}

refinedarchitect {
Expand All @@ -38,12 +42,17 @@ base {
val refinedstorageVersion: String by project
val minecraftVersion: String by project
val mekanismVersion: String by project
val jeiVersion: String by project

dependencies {
api("com.refinedmods.refinedstorage:refinedstorage-neoforge:${refinedstorageVersion}")
compileOnlyApi("mekanism:Mekanism:${minecraftVersion}-${mekanismVersion}:api")
runtimeOnly("mekanism:Mekanism:${minecraftVersion}-${mekanismVersion}:all") {
exclude(group = "com.blamejared.crafttweaker")
}
runtimeOnly("mezz.jei:jei-${minecraftVersion}-neoforge:${jeiVersion}")
compileOnlyApi("mezz.jei:jei-${minecraftVersion}-common-api:${jeiVersion}")
testCompileOnly("mezz.jei:jei-${minecraftVersion}-common:${jeiVersion}")
compileOnlyApi("mezz.jei:jei-${minecraftVersion}-neoforge-api:${jeiVersion}")
}

1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ refinedarchitectVersion=0.20.0
refinedstorageVersion=2.0.0-milestone.4.10
# https://www.curseforge.com/minecraft/mc-mods/mekanism/files/all
mekanismVersion=10.7.7.64
jeiVersion=19.20.0.241
minecraftVersion=1.21.1
# Gradle
org.gradle.jvmargs=-Xmx1G
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.refinedmods.refinedstorage.mekanism;

import com.refinedmods.refinedstorage.common.api.RefinedStorageApi;
import com.refinedmods.refinedstorage.mekanism.chemical.ChemicalGridInsertionHint;
import com.refinedmods.refinedstorage.mekanism.chemical.ChemicalResource;
import com.refinedmods.refinedstorage.mekanism.chemical.ChemicalResourceRendering;
import com.refinedmods.refinedstorage.mekanism.chemical.ChemicalStorageVariant;

import net.minecraft.resources.ResourceLocation;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;

import static com.refinedmods.refinedstorage.mekanism.MekanismIntegrationIdentifierUtil.createMekanismIntegrationIdentifier;

public final class ClientModInitializer {
private ClientModInitializer() {
}

@SubscribeEvent
public static void onClientSetup(final FMLClientSetupEvent e) {
RefinedStorageApi.INSTANCE.registerResourceRendering(
ChemicalResource.class,
new ChemicalResourceRendering()
);
RefinedStorageApi.INSTANCE.addAlternativeGridInsertionHint(new ChemicalGridInsertionHint());
final ResourceLocation diskModel = createMekanismIntegrationIdentifier("block/disk/chemical_disk");
for (final ChemicalStorageVariant variant : ChemicalStorageVariant.values()) {
RefinedStorageApi.INSTANCE.getStorageContainerItemHelper().registerDiskModel(
Items.INSTANCE.getChemicalStorageDisk(variant),
diskModel
);
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/refinedmods/refinedstorage/mekanism/Items.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.refinedmods.refinedstorage.mekanism;

import com.refinedmods.refinedstorage.mekanism.chemical.ChemicalStorageVariant;

import java.util.EnumMap;
import java.util.Map;
import java.util.function.Supplier;

import net.minecraft.world.item.Item;

public final class Items {
public static final Items INSTANCE = new Items();

private final Map<ChemicalStorageVariant, Supplier<Item>> chemicalStorageParts =
new EnumMap<>(ChemicalStorageVariant.class);
private final Map<ChemicalStorageVariant, Supplier<Item>> chemicalStorageDisks =
new EnumMap<>(ChemicalStorageVariant.class);

private Items() {
}

public Item getChemicalStoragePart(final ChemicalStorageVariant variant) {
return chemicalStorageParts.get(variant).get();
}

public void setChemicalStoragePart(final ChemicalStorageVariant variant, final Supplier<Item> supplier) {
chemicalStorageParts.put(variant, supplier);
}

public Item getChemicalStorageDisk(final ChemicalStorageVariant variant) {
return chemicalStorageDisks.get(variant).get();
}

public void setChemicalStorageDisk(final ChemicalStorageVariant variant, final Supplier<Item> supplier) {
chemicalStorageDisks.put(variant, supplier);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.refinedmods.refinedstorage.mekanism;

import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;

public final class MekanismIntegrationIdentifierUtil {
public static final String MOD_ID = "refinedstorage_mekanism_integration";

private MekanismIntegrationIdentifierUtil() {
}

public static ResourceLocation createMekanismIntegrationIdentifier(final String value) {
return ResourceLocation.fromNamespaceAndPath(MOD_ID, value);
}

public static String createMekanismIntegrationTranslationKey(final String category, final String value) {
return String.format("%s.%s.%s", category, MOD_ID, value);
}

public static MutableComponent createMekanismIntegrationTranslation(final String category, final String value) {
return Component.translatable(createMekanismIntegrationTranslationKey(category, value));
}

public static MutableComponent createMekanismIntegrationTranslation(final String category,
final String value,
final Object... args) {
return Component.translatable(createMekanismIntegrationTranslationKey(category, value), args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.refinedmods.refinedstorage.mekanism;

import com.refinedmods.refinedstorage.api.core.Action;

public final class MekanismUtil {
private MekanismUtil() {
}

public static mekanism.api.Action toMekanismAction(final Action action) {
return switch (action) {
case EXECUTE -> mekanism.api.Action.EXECUTE;
case SIMULATE -> mekanism.api.Action.SIMULATE;
};
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,111 @@
package com.refinedmods.refinedstorage.mekanism;

import com.refinedmods.refinedstorage.common.util.IdentifierUtil;
import com.refinedmods.refinedstorage.common.api.RefinedStorageApi;
import com.refinedmods.refinedstorage.common.storage.StorageContainerUpgradeRecipe;
import com.refinedmods.refinedstorage.common.storage.StorageContainerUpgradeRecipeSerializer;
import com.refinedmods.refinedstorage.common.support.SimpleItem;
import com.refinedmods.refinedstorage.mekanism.chemical.ChemicalGridExtractionStrategy;
import com.refinedmods.refinedstorage.mekanism.chemical.ChemicalGridInsertionStrategy;
import com.refinedmods.refinedstorage.mekanism.chemical.ChemicalResourceContainerInsertStrategy;
import com.refinedmods.refinedstorage.mekanism.chemical.ChemicalResourceFactory;
import com.refinedmods.refinedstorage.mekanism.chemical.ChemicalResourceType;
import com.refinedmods.refinedstorage.mekanism.chemical.ChemicalStorageDiskItem;
import com.refinedmods.refinedstorage.mekanism.chemical.ChemicalStorageMonitorInsertionStrategy;
import com.refinedmods.refinedstorage.mekanism.chemical.ChemicalStorageVariant;

import mekanism.api.MekanismAPI;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.common.Mod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent;
import net.neoforged.neoforge.registries.DeferredRegister;

@Mod(ModInitializer.ID)
import static com.refinedmods.refinedstorage.mekanism.MekanismIntegrationIdentifierUtil.createMekanismIntegrationIdentifier;

@Mod(MekanismIntegrationIdentifierUtil.MOD_ID)
public final class ModInitializer {
public static final String ID = "refinedstorage_mekanism_integration";
public ModInitializer(final IEventBus eventBus) {
if (FMLEnvironment.dist == Dist.CLIENT) {
eventBus.addListener(ClientModInitializer::onClientSetup);
}
eventBus.addListener(this::setup);
eventBus.addListener(this::registerCreativeModeTabListener);

private static final Logger LOGGER = LoggerFactory.getLogger(ModInitializer.class);
final DeferredRegister<Item> itemRegistry = DeferredRegister.create(
BuiltInRegistries.ITEM,
MekanismIntegrationIdentifierUtil.MOD_ID
);
for (final ChemicalStorageVariant variant : ChemicalStorageVariant.values()) {
if (variant.getStoragePartId() != null) {
Items.INSTANCE.setChemicalStoragePart(
variant,
itemRegistry.register(variant.getStoragePartId().getPath(), SimpleItem::new)
);
}
Items.INSTANCE.setChemicalStorageDisk(
variant,
itemRegistry.register(variant.getStorageDiskId().getPath(), () -> new ChemicalStorageDiskItem(
RefinedStorageApi.INSTANCE.getStorageContainerItemHelper(),
variant
))
);
}
itemRegistry.register(eventBus);

public ModInitializer(final IEventBus eventBus) {
LOGGER.info(
"Refined Storage - Mekanism Integration has loaded. RS2 ModId: {}, Mekanism API version: {}",
IdentifierUtil.MOD_ID,
MekanismAPI.API_VERSION
final DeferredRegister<RecipeSerializer<?>> recipeSerializerRegistry = DeferredRegister.create(
BuiltInRegistries.RECIPE_SERIALIZER,
MekanismIntegrationIdentifierUtil.MOD_ID
);
recipeSerializerRegistry.register(
"chemical_storage_disk_upgrade",
() -> new StorageContainerUpgradeRecipeSerializer<>(
ChemicalStorageVariant.values(),
to -> new StorageContainerUpgradeRecipe<>(
ChemicalStorageVariant.values(), to, Items.INSTANCE::getChemicalStorageDisk
)
)
);
recipeSerializerRegistry.register(eventBus);
}

private void setup(final FMLCommonSetupEvent e) {
RefinedStorageApi.INSTANCE.getResourceTypeRegistry().register(
createMekanismIntegrationIdentifier("chemical"),
ChemicalResourceType.INSTANCE
);
RefinedStorageApi.INSTANCE.getAlternativeResourceFactories().add(new ChemicalResourceFactory());
RefinedStorageApi.INSTANCE.getStorageTypeRegistry().register(
createMekanismIntegrationIdentifier("chemical"),
ChemicalResourceType.STORAGE_TYPE
);
RefinedStorageApi.INSTANCE.addGridInsertionStrategyFactory(ChemicalGridInsertionStrategy::new);
RefinedStorageApi.INSTANCE.addGridExtractionStrategyFactory(ChemicalGridExtractionStrategy::new);
RefinedStorageApi.INSTANCE.addStorageMonitorInsertionStrategy(new ChemicalStorageMonitorInsertionStrategy());
RefinedStorageApi.INSTANCE.addResourceContainerInsertStrategy(new ChemicalResourceContainerInsertStrategy());
}

private void registerCreativeModeTabListener(final BuildCreativeModeTabContentsEvent e) {
final ResourceKey<CreativeModeTab> creativeModeTab = ResourceKey.create(
Registries.CREATIVE_MODE_TAB,
RefinedStorageApi.INSTANCE.getCreativeModeTabId()
);
if (!e.getTabKey().equals(creativeModeTab)) {
return;
}
for (final ChemicalStorageVariant variant : ChemicalStorageVariant.values()) {
if (variant.getStoragePartId() != null) {
e.accept(Items.INSTANCE.getChemicalStoragePart(variant));
}
}
for (final ChemicalStorageVariant variant : ChemicalStorageVariant.values()) {
e.accept(Items.INSTANCE.getChemicalStorageDisk(variant));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.refinedmods.refinedstorage.mekanism.chemical;

import com.refinedmods.refinedstorage.api.grid.operations.GridExtractMode;
import com.refinedmods.refinedstorage.api.grid.operations.GridOperations;
import com.refinedmods.refinedstorage.common.api.grid.Grid;
import com.refinedmods.refinedstorage.common.api.grid.strategy.GridExtractionStrategy;
import com.refinedmods.refinedstorage.common.api.support.resource.PlatformResourceKey;

import javax.annotation.Nullable;

import mekanism.api.chemical.Chemical;
import mekanism.api.chemical.ChemicalStack;
import mekanism.api.chemical.IChemicalHandler;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;

import static com.refinedmods.refinedstorage.mekanism.MekanismUtil.toMekanismAction;

public class ChemicalGridExtractionStrategy implements GridExtractionStrategy {
private final AbstractContainerMenu menu;
private final GridOperations gridOperations;

public ChemicalGridExtractionStrategy(final AbstractContainerMenu containerMenu,
final ServerPlayer player,
final Grid grid) {
this.menu = containerMenu;
this.gridOperations = grid.createOperations(ChemicalResourceType.INSTANCE, player);
}

@Override
public boolean onExtract(final PlatformResourceKey resource,
final GridExtractMode extractMode,
final boolean cursor) {
if (resource instanceof ChemicalResource chemicalResource && isChemicalContainerOnCursor()) {
extractWithContainerOnCursor(chemicalResource, extractMode);
return true;
}
return false;
}

@Nullable
private IChemicalHandler getChemicalStorage(final ItemStack stack) {
return stack.getCapability(ChemicalResourceType.ITEM_CAPABILITY);
}

private void extractWithContainerOnCursor(final ChemicalResource chemicalResource,
final GridExtractMode mode) {
gridOperations.extract(chemicalResource, mode, (resource, amount, action, source) -> {
if (!(resource instanceof ChemicalResource(Chemical chemical))) {
return 0;
}
final IChemicalHandler destination = getChemicalStorage(menu.getCarried());
if (destination == null) {
return 0;
}
final ChemicalStack toInsert = new ChemicalStack(chemical, amount);
final ChemicalStack remainder = destination.insertChemical(toInsert, toMekanismAction(action));
return amount - remainder.getAmount();
});
}

private boolean isChemicalContainerOnCursor() {
return getChemicalStorage(menu.getCarried()) != null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.refinedmods.refinedstorage.mekanism.chemical;

import com.refinedmods.refinedstorage.common.Platform;
import com.refinedmods.refinedstorage.common.api.RefinedStorageApi;
import com.refinedmods.refinedstorage.common.api.grid.GridInsertionHint;
import com.refinedmods.refinedstorage.common.support.tooltip.MouseClientTooltipComponent;

import java.util.Optional;

import mekanism.api.Action;
import mekanism.api.chemical.ChemicalStack;
import net.minecraft.client.gui.screens.inventory.tooltip.ClientTooltipComponent;
import net.minecraft.world.item.ItemStack;

import static com.refinedmods.refinedstorage.mekanism.chemical.ChemicalResource.ofChemicalStack;

public class ChemicalGridInsertionHint implements GridInsertionHint {
@Override
public Optional<ClientTooltipComponent> getHint(final ItemStack stack) {
return Optional.ofNullable(stack.getCapability(ChemicalResourceType.ITEM_CAPABILITY))
.map(handler -> handler.extractChemical(Long.MAX_VALUE, Action.SIMULATE))
.filter(result -> !result.isEmpty())
.map(this::createComponent);
}

private ClientTooltipComponent createComponent(final ChemicalStack result) {
final ChemicalResource chemicalResource = ofChemicalStack(result);
final String amount = result.getAmount() == Platform.INSTANCE.getBucketAmount()
? null
: RefinedStorageApi.INSTANCE.getResourceRendering(ChemicalResource.class).formatAmount(result.getAmount());
return MouseClientTooltipComponent.resource(MouseClientTooltipComponent.Type.RIGHT, chemicalResource, amount);
}
}
Loading
Loading