Skip to content

Commit

Permalink
Switch to using lava for Sky Steel recipe (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
62832 authored Nov 1, 2023
1 parent d1c0647 commit 963ce52
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ public void buildRecipes(Consumer<FinishedRecipe> consumer) {
MEGACells.makeId("transform/sky_steel_ingot"),
MEGAItems.SKY_STEEL_INGOT,
2,
TransformCircumstance.fluid(FluidTags.WATER),
Ingredient.of(ConventionTags.IRON_INGOT),
TransformCircumstance.fluid(FluidTags.LAVA),
Ingredient.of(AEItems.CERTUS_QUARTZ_CRYSTAL_CHARGED),
Ingredient.of(ConventionTags.IRON_INGOT),
Ingredient.of(AEBlocks.SKY_STONE_BLOCK));

InscriberRecipeBuilder.inscribe(AEItems.SINGULARITY, MEGAItems.ACCUMULATION_PROCESSOR_PRESS, 1)
Expand Down
8 changes: 2 additions & 6 deletions common/src/data/resources/megacells.data.mixins.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{
"required": true,
"minVersion": "0.8",
"minVersion": "0.8.5",
"package": "gripe._90.megacells.mixin.data",
"compatibilityLevel": "JAVA_17",
"mixins": ["TextureSlotAccessor"],
"injectors": {
"defaultRequire": 1
}
"mixins": ["TextureSlotAccessor"]
}
2 changes: 2 additions & 0 deletions common/src/main/java/gripe/_90/megacells/MEGACells.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public static void initCommon() {
PLATFORM.initCompression();
GridServices.register(DecompressionService.class, DecompressionService.class);

PLATFORM.initLavaTransform();

PLATFORM.addVillagerTrade(MEGAItems.SKY_STEEL_INGOT, 8, 3, 20);
PLATFORM.addVillagerTrade(MEGAItems.ACCUMULATION_PROCESSOR_PRESS, 40, 1, 50);
}
Expand Down
2 changes: 2 additions & 0 deletions common/src/main/java/gripe/_90/megacells/core/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public interface Platform {

void initCompression();

void initLavaTransform();

void addVillagerTrade(ItemLike item, int cost, int quantity, int xp);

BakedModel createWrappedCellModel(Item cell, BlockOrientation orientation);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package gripe._90.megacells.misc;

import java.util.HashSet;
import java.util.Set;

import net.minecraft.tags.FluidTags;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;

import appeng.recipes.transform.TransformRecipe;

public final class LavaTransformLogic {
private static final Set<Item> lavaCache = new HashSet<>();

public static boolean canTransformInLava(ItemEntity entity) {
return getLavaTransformableItems(entity.level())
.contains(entity.getItem().getItem());
}

@SuppressWarnings("resource")
public static boolean allIngredientsPresent(ItemEntity entity) {
var x = entity.getX();
var y = entity.getY();
var z = entity.getZ();
var level = entity.level();

var items = level.getEntities(null, new AABB(x - 1, y - 1, z - 1, x + 1, y + 1, z + 1)).stream()
.filter(e -> e instanceof ItemEntity && !e.isRemoved())
.map(e -> ((ItemEntity) e).getItem().getItem())
.toList();

for (var recipe : level.getRecipeManager().getAllRecipesFor(TransformRecipe.TYPE)) {
if (!recipe.circumstance.isFluidTag(FluidTags.LAVA)) {
continue;
}

if (recipe.ingredients.isEmpty()) {
continue;
}

return recipe.ingredients.stream().noneMatch(ingredient -> {
for (var stack : ingredient.getItems()) {
if (items.contains(stack.getItem())) {
return false;
}
}

return true;
});
}

return false;
}

private static Set<Item> getLavaTransformableItems(Level level) {
if (lavaCache.isEmpty()) {
for (var recipe : level.getRecipeManager().getAllRecipesFor(TransformRecipe.TYPE)) {
if (!recipe.circumstance.isFluidTag(FluidTags.LAVA)) {
continue;
}

for (var ingredient : recipe.ingredients) {
for (var stack : ingredient.getItems()) {
lavaCache.add(stack.getItem());
}

// Don't break here unlike AE2's TransformLogic, otherwise non-processed items will burn up
}
}
}

return lavaCache;
}

public static void clearCache() {
lavaCache.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package gripe._90.megacells.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import net.minecraft.core.BlockPos;
import net.minecraft.tags.FluidTags;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.level.Level;

import gripe._90.megacells.misc.LavaTransformLogic;

@Mixin(ItemEntity.class)
public abstract class ItemEntityMixin extends Entity {
public ItemEntityMixin(EntityType<?> entityType, Level level) {
super(entityType, level);
}

@Unique
private boolean mega$lavaImmune = false;

@Unique
private int mega$lavaTicks = 0;

@Inject(method = "fireImmune", at = @At("RETURN"), cancellable = true)
private void handleLavaTransform(CallbackInfoReturnable<Boolean> cir) {
cir.setReturnValue(cir.getReturnValue() || mega$lavaImmune);
}

@SuppressWarnings({"resource", "DataFlowIssue"})
@Inject(method = "tick", at = @At("RETURN"))
private void lavaTimeout(CallbackInfo ci) {
var self = (ItemEntity) (Object) this;

if (LavaTransformLogic.canTransformInLava(self)) {
var x = Mth.floor(getX());
var y = Mth.floor((getBoundingBox().minY + getBoundingBox().maxY) / 2);
var z = Mth.floor(getZ());
var state = level().getFluidState(new BlockPos(x, y, z));

if (state.is(FluidTags.LAVA)) {
mega$lavaImmune = mega$lavaTicks++ <= 200 || LavaTransformLogic.allIngredientsPresent(self);

if (mega$lavaTicks > 200 && LavaTransformLogic.allIngredientsPresent(self)) {
mega$lavaTicks = 0;
}
}
}
}
}
9 changes: 9 additions & 0 deletions common/src/main/resources/megacells.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"required": true,
"minVersion": "0.8.5",
"package": "gripe._90.megacells.mixin",
"mixins": ["ItemEntityMixin"],
"injectors": {
"defaultRequire": 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import gripe._90.megacells.core.Loaders;
import gripe._90.megacells.core.Platform;
import gripe._90.megacells.misc.CompressionService;
import gripe._90.megacells.misc.LavaTransformLogic;

public final class FabricPlatform implements Platform {
@Override
Expand All @@ -55,6 +56,14 @@ public void initCompression() {
});
}

@Override
public void initLavaTransform() {
ServerLifecycleEvents.SERVER_STARTED.register(server -> LavaTransformLogic.clearCache());
ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, resourceManager, success) -> {
if (success) LavaTransformLogic.clearCache();
});
}

@Override
public void addVillagerTrade(ItemLike item, int cost, int quantity, int xp) {
TradeOfferHelper.registerVillagerOffers(
Expand Down
1 change: 1 addition & 0 deletions fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"ae2:client": ["gripe._90.megacells.fabric.MEGACellsClient"],
"modmenu": ["gripe._90.megacells.integration.modmenu.ModMenuIntegration"]
},
"mixins": ["megacells.mixins.json"],

"depends": {
"minecraft": "$minecraftVersion",
Expand Down
1 change: 1 addition & 0 deletions forge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ loom {
}

forge {
mixinConfig("$modId.mixins.json")
mixinConfig("$modId.forge.mixins.json")
}
}
Expand Down
3 changes: 1 addition & 2 deletions forge/src/data/resources/megacells.data.forge.mixins.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"required": true,
"minVersion": "0.8",
"minVersion": "0.8.5",
"package": "gripe._90.megacells.mixin.data.forge",
"compatibilityLevel": "JAVA_17",
"mixins": ["MainMixin"],
"injectors": {
"defaultRequire": 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import gripe._90.megacells.core.Loaders;
import gripe._90.megacells.core.Platform;
import gripe._90.megacells.misc.CompressionService;
import gripe._90.megacells.misc.LavaTransformLogic;

public final class ForgePlatform implements Platform {
@Override
Expand Down Expand Up @@ -82,6 +83,14 @@ public void initCompression() {
});
}

@Override
public void initLavaTransform() {
MinecraftForge.EVENT_BUS.addListener((ServerStartedEvent event) -> LavaTransformLogic.clearCache());
MinecraftForge.EVENT_BUS.addListener((OnDatapackSyncEvent event) -> {
if (event.getPlayer() == null) LavaTransformLogic.clearCache();
});
}

@Override
public void addVillagerTrade(ItemLike item, int cost, int quantity, int xp) {
var offers = VillagerTrades.TRADES.computeIfAbsent(InitVillager.PROFESSION, k -> new Int2ObjectOpenHashMap<>());
Expand Down
3 changes: 1 addition & 2 deletions forge/src/main/resources/megacells.forge.mixins.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"required": true,
"minVersion": "0.8",
"minVersion": "0.8.5",
"package": "gripe._90.megacells.mixin.forge",
"compatibilityLevel": "JAVA_17",
"mixins": [],
"client": ["client.BuiltInModelHooksAccessor", "client.ModelBakeryMixin"],
"injectors": {
Expand Down

0 comments on commit 963ce52

Please sign in to comment.