Skip to content

Commit

Permalink
Support External Heater of Immersive Engineering #16
Browse files Browse the repository at this point in the history
  • Loading branch information
cech12 committed Nov 3, 2022
1 parent 3e283cb commit eeeb7ff
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Forge Recommended Versioning](https://mcforge.readthedocs.io/en/latest/conventions/versioning/).

## [1.18.2-1.7.1.0] - 2022-11-04
### Added
- Support for External Heater of Immersive Engineering #16

### Changed
- Update mod to Forge 1.18.2-40.1.0

## [1.18.2-1.7.0.1] - 2022-09-28
### Fixed
- Villager profession code reflection failed in productive environment (thanks to Vatruvius for the report) #15
Expand Down
12 changes: 12 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ buildscript {
repositories {
maven { url = 'https://maven.minecraftforge.net' }
mavenCentral()
maven {url 'https://repo.spongepowered.org/repository/maven-public/'} //only to use immersive engineering
}
dependencies {
classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '5.1.+', changing: true
classpath 'org.spongepowered:mixingradle:0.7-SNAPSHOT' //only to use immersive engineering
}
}

Expand All @@ -16,6 +18,7 @@ apply plugin: 'net.minecraftforge.gradle'
// Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.
apply plugin: 'eclipse'
apply plugin: 'maven-publish'
apply plugin: 'org.spongepowered.mixin' //only to use immersive engineering

version = "${minecraft_version}-${mod_version}"
group = "cech12.${mod_id}" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down Expand Up @@ -116,6 +119,11 @@ repositories {
name = "ModMaven"
url = "https://modmaven.k-4u.nl"
}
maven {
// Immersive Engineering
name = "Blamejared Maven"
url "https://maven.blamejared.com"
}
}

dependencies {
Expand All @@ -128,6 +136,10 @@ dependencies {
compileOnly fg.deobf("mezz.jei:jei-${project.jei_version}")
// at runtime, use the full JEI jar
runtimeOnly fg.deobf("mezz.jei:jei-${project.jei_version}")

//Immersive Engineering
runtimeOnly fg.deobf("blusunrize.immersiveengineering:ImmersiveEngineering:${project.immersive_engineering_version}")
compileOnly fg.deobf("blusunrize.immersiveengineering:ImmersiveEngineering:${project.immersive_engineering_version}:api")
}

// Example for how to get properties into the manifest for reading by the runtime..
Expand Down
7 changes: 5 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false

mod_id=brickfurnace
mod_version=1.7.0.1
mod_version=1.7.1.0
minecraft_version=1.18.2
forge_version=40.0.31
forge_version=40.1.0

# jei
jei_version=1.18.2:9.7.0.192

# immersive engineering
immersive_engineering_version=1.18.2-8.0.1-147

#Curseforge
curseforge_id=363593
curseforge_release=release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cech12.brickfurnace.config.ServerConfig;
import net.minecraft.core.BlockPos;
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.AbstractFurnaceBlock;
import net.minecraft.world.level.block.Blocks;
Expand Down Expand Up @@ -152,7 +153,7 @@ private int getTotalCookTime() {
return (int) (rec.getCookingTime() * ServerConfig.COOK_TIME_FACTOR.get());
}

protected AbstractCookingRecipe getRecipe() {
public AbstractCookingRecipe getRecipe() {
ItemStack input = this.getItem(INPUT);
if (input.isEmpty() || input == failedMatch) {
return null;
Expand All @@ -177,4 +178,8 @@ protected AbstractCookingRecipe getRecipe() {
}
}

public ContainerData getContainerData() {
return this.dataAccess;
}

}
22 changes: 22 additions & 0 deletions src/main/java/cech12/brickfurnace/compat/EventHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cech12.brickfurnace.compat;

import cech12.brickfurnace.compat.immersiveengineering.ImmersiveEngineering;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.common.Mod;

import static cech12.brickfurnace.BrickFurnaceMod.MOD_ID;

@Mod.EventBusSubscriber(modid= MOD_ID)
public class EventHandler {

@SubscribeEvent
public static void onCapabilitiesAttachBlockEntity(AttachCapabilitiesEvent<BlockEntity> event) {
if (ModList.get().isLoaded("immersiveengineering")) {
ImmersiveEngineering.onCapabilitiesAttachBlockEntity(event);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cech12.brickfurnace.compat.immersiveengineering;

import blusunrize.immersiveengineering.api.tool.ExternalHeaterHandler;
import cech12.brickfurnace.blockentity.AbstractBrickFurnaceBlockEntity;
import cech12.brickfurnace.blockentity.BrickFurnaceBlockEntity;
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.AbstractCookingRecipe;
import net.minecraft.world.level.block.AbstractFurnaceBlock;
import net.minecraft.world.level.block.state.BlockState;

import static net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity.*;

public class BrickFurnaceHeater implements ExternalHeaterHandler.IExternalHeatable {
private static final int FULLY_HEATED_LIT_TIME = 200;
private final BrickFurnaceBlockEntity furnace;
private long blockedUntilGameTime = 0;

public BrickFurnaceHeater(BrickFurnaceBlockEntity furnace) {
this.furnace = furnace;
}

boolean canCook() {
ItemStack input = furnace.getItem(AbstractBrickFurnaceBlockEntity.INPUT);
if (input.isEmpty()) return false;
AbstractCookingRecipe recipe = furnace.getRecipe();
if (recipe == null) return false;
ItemStack outStack = recipe.getResultItem();
if (outStack.isEmpty()) return false;
ItemStack existingOutput = furnace.getItem(2);
if (existingOutput.isEmpty()) return true;
if (!existingOutput.sameItem(outStack)) return false;
int stackSize = existingOutput.getCount() + outStack.getCount();
return stackSize <= furnace.getMaxStackSize() && stackSize <= outStack.getMaxStackSize();
}

@Override
public int doHeatTick(int energyAvailable, boolean redstone) {
long now = furnace.getLevel().getGameTime();
if (now < blockedUntilGameTime) return 0;
int energyConsumed = 0;
boolean canCook = canCook();
if (canCook || redstone) {
ContainerData furnaceData = furnace.getContainerData();
int burnTime = furnaceData.get(DATA_LIT_TIME);
if (burnTime < FULLY_HEATED_LIT_TIME) {
final int heatEnergyRatio = Math.max(1, ExternalHeaterHandler.defaultFurnaceEnergyCost);
if (burnTime == 0 && energyAvailable < heatEnergyRatio) {
// Turn off completely for one second if furnace goes out due to insufficient power to prevent fast
// on/off cycling on weak power sources
blockedUntilGameTime = now + 20;
return 0;
}
int heatAttempt = Math.min(4, FULLY_HEATED_LIT_TIME-burnTime);
int energyToUse = Math.min(energyAvailable, heatAttempt*heatEnergyRatio);
int heat = energyToUse/heatEnergyRatio;
if (heat > 0) {
furnaceData.set(DATA_LIT_TIME, burnTime+heat);
energyConsumed += heat*heatEnergyRatio;
setFurnaceActive();
}
}
// Speed up once fully charged
if (canCook && furnaceData.get(DATA_LIT_TIME) >= FULLY_HEATED_LIT_TIME&&furnaceData.get(DATA_COOKING_PROGRESS) < BURN_TIME_STANDARD-1) {
int energyToUse = ExternalHeaterHandler.defaultFurnaceSpeedupCost;
if (energyAvailable - energyConsumed > energyToUse) {
energyConsumed += energyToUse;
furnaceData.set(DATA_COOKING_PROGRESS, furnaceData.get(DATA_COOKING_PROGRESS)+1);
}
}
}
return energyConsumed;
}

public void setFurnaceActive() {
BlockState oldState = furnace.getBlockState();
if (!oldState.getValue(AbstractFurnaceBlock.LIT)) {
furnace.getLevel().setBlockAndUpdate(
furnace.getBlockPos(), oldState.setValue(AbstractFurnaceBlock.LIT, true)
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cech12.brickfurnace.compat.immersiveengineering;

import blusunrize.immersiveengineering.api.tool.ExternalHeaterHandler;
import blusunrize.immersiveengineering.api.utils.CapabilityUtils;
import cech12.brickfurnace.BrickFurnaceMod;
import cech12.brickfurnace.blockentity.BrickFurnaceBlockEntity;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.AttachCapabilitiesEvent;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Objects;
import java.util.function.Supplier;

public class ImmersiveEngineering {

public static void onCapabilitiesAttachBlockEntity(AttachCapabilitiesEvent<BlockEntity> event) {
if (event.getObject() instanceof BrickFurnaceBlockEntity furnace) {
event.addCapability(new ResourceLocation(BrickFurnaceMod.MOD_ID, "brick_furnace_heater"),
new SimpleCapProvider<>(() -> ExternalHeaterHandler.CAPABILITY, new BrickFurnaceHeater(furnace))
);
}
}

public record SimpleCapProvider<T>(Supplier<Capability<T>> cap, LazyOptional<T> value) implements ICapabilityProvider {
public SimpleCapProvider(Supplier<Capability<T>> cap, T value)
{
this(cap, CapabilityUtils.constantOptional(value));
}

@Nonnull
@Override
public <T2> LazyOptional<T2> getCapability(@Nonnull Capability<T2> cap, @Nullable Direction side)
{
return Objects.requireNonNull(this.cap.get()).orEmpty(cap, value);
}
}

}
8 changes: 7 additions & 1 deletion src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ license="The MIT License (MIT)"
[[dependencies.brickfurnace]]
modId="forge"
mandatory=true
versionRange="[40.0.31,)"
versionRange="[40.1.0,)"
ordering="NONE"
side="BOTH"
[[dependencies.brickfurnace]]
Expand All @@ -29,4 +29,10 @@ license="The MIT License (MIT)"
mandatory=false
versionRange="[9.7.0.192,)"
ordering="NONE"
side="BOTH"
[[dependencies.brickfurnace]]
modId="immersiveengineering"
mandatory=false
versionRange="[1.18.2-8.0.1,)"
ordering="NONE"
side="BOTH"

0 comments on commit eeeb7ff

Please sign in to comment.