-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support External Heater of Immersive Engineering #16
- Loading branch information
Showing
8 changed files
with
186 additions
and
4 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
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
22 changes: 22 additions & 0 deletions
22
src/main/java/cech12/brickfurnace/compat/EventHandler.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,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); | ||
} | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
src/main/java/cech12/brickfurnace/compat/immersiveengineering/BrickFurnaceHeater.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,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) | ||
); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/cech12/brickfurnace/compat/immersiveengineering/ImmersiveEngineering.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,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); | ||
} | ||
} | ||
|
||
} |
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