-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
And support in the grid and storage monitor.
- Loading branch information
1 parent
91cee5b
commit 4b60d5c
Showing
60 changed files
with
1,632 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/com/refinedmods/refinedstorage/mekanism/ClientModInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
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 net.neoforged.bus.api.SubscribeEvent; | ||
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; | ||
|
||
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()); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/refinedmods/refinedstorage/mekanism/Items.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/refinedmods/refinedstorage/mekanism/MekanismIntegrationIdentifierUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/refinedmods/refinedstorage/mekanism/MekanismUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} | ||
} |
111 changes: 99 additions & 12 deletions
111
src/main/java/com/refinedmods/refinedstorage/mekanism/ModInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...java/com/refinedmods/refinedstorage/mekanism/chemical/ChemicalGridExtractionStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...main/java/com/refinedmods/refinedstorage/mekanism/chemical/ChemicalGridInsertionHint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.