diff --git a/TesseractAPI b/TesseractAPI index 50f48b034..fc2525b44 160000 --- a/TesseractAPI +++ b/TesseractAPI @@ -1 +1 @@ -Subproject commit 50f48b0347d5edf0ac7617f98f648a8b3140698c +Subproject commit fc2525b44077557704fd7dcadeede0df9489cf03 diff --git a/common/src/main/java/muramasa/antimatter/Ref.java b/common/src/main/java/muramasa/antimatter/Ref.java index a10bd052c..e758418f5 100644 --- a/common/src/main/java/muramasa/antimatter/Ref.java +++ b/common/src/main/java/muramasa/antimatter/Ref.java @@ -90,6 +90,8 @@ public class Ref { public static final String KEY_MACHINE_TEXTURE = "mt"; public static final String KEY_MACHINE_FLUIDS = "fl"; public static final String KEY_MACHINE_ENERGY = "en"; + public static final String KEY_MACHINE_RF = "rf"; + public static final String KEY_MACHINE_HEAT = "he"; public static final String KEY_MACHINE_RECIPE = "re"; public static final String KEY_MACHINE_COVER = "co"; public static final String KEY_MACHINE_INTERACT = "ci"; diff --git a/common/src/main/java/muramasa/antimatter/blockentity/BlockEntityMachine.java b/common/src/main/java/muramasa/antimatter/blockentity/BlockEntityMachine.java index aac4c96e6..9890ee8ab 100644 --- a/common/src/main/java/muramasa/antimatter/blockentity/BlockEntityMachine.java +++ b/common/src/main/java/muramasa/antimatter/blockentity/BlockEntityMachine.java @@ -65,6 +65,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import tesseract.api.gt.IEnergyHandler; +import tesseract.api.heat.IHeatHandler; import tesseract.api.item.ExtendedItemContainer; import tesseract.api.rf.IRFNode; @@ -115,6 +116,7 @@ public class BlockEntityMachine> extends BlockEn public Holder> fluidHandler = new Holder<>(FluidContainer.class, dispatch); public Holder, MachineCoverHandler> coverHandler = new Holder<>(ICoverHandler.class, dispatch, null); public Holder> energyHandler = new Holder<>(IEnergyHandler.class, dispatch); + public Holder heatHandler = new Holder<>(IHeatHandler.class, dispatch); public Holder> rfHandler = new Holder<>(IRFNode.class, dispatch); public Holder, MachineRecipeHandler> recipeHandler = new Holder<>(MachineRecipeHandler.class, dispatch, null); @@ -142,6 +144,9 @@ public BlockEntityMachine(Machine type, BlockPos pos, BlockState state) { energyHandler.set(() -> new MachineEnergyHandler<>((T) this, type.amps(), type.has(GENERATOR))); } } + if (type.has(HEAT)){ + heatHandler.set(() -> new DefaultHeatHandler(this, (int) (this.getMachineTier().getVoltage() * 4), type.has(GENERATOR) ? 0 : (int) this.getMachineTier().getVoltage(), type.has(GENERATOR) ? (int) this.getMachineTier().getVoltage() : 0)); + } if (type.has(RECIPE)) { recipeHandler.set(() -> new MachineRecipeHandler<>((T) this)); } @@ -250,6 +255,7 @@ public void serverTick(Level level, BlockPos pos, BlockState state) { itemHandler.ifPresent(MachineItemHandler::onUpdate); energyHandler.ifPresent(MachineEnergyHandler::onUpdate); rfHandler.ifPresent(MachineRFHandler::onUpdate); + heatHandler.ifPresent(handler -> handler.update(getMachineState() == MachineState.ACTIVE)); fluidHandler.ifPresent(MachineFluidHandler::onUpdate); coverHandler.ifPresent(MachineCoverHandler::onUpdate); this.recipeHandler.ifPresent(MachineRecipeHandler::onServerUpdate); @@ -629,10 +635,13 @@ public void load(CompoundTag tag) { if (tag.contains(Ref.KEY_MACHINE_ITEMS)) itemHandler.ifPresent(i -> i.deserialize(tag.getCompound(Ref.KEY_MACHINE_ITEMS))); if (tag.contains(Ref.KEY_MACHINE_ENERGY)) { - if (type.has(RF)) - rfHandler.ifPresent(e -> e.deserialize(tag.getCompound(Ref.KEY_MACHINE_ENERGY))); - else - energyHandler.ifPresent(e -> e.deserialize(tag.getCompound(Ref.KEY_MACHINE_ENERGY))); + energyHandler.ifPresent(e -> e.deserialize(tag.getCompound(Ref.KEY_MACHINE_ENERGY))); + } + if (tag.contains(Ref.KEY_MACHINE_RF)){ + rfHandler.ifPresent(e -> e.deserialize(tag.getCompound(Ref.KEY_MACHINE_RF))); + } + if (tag.contains(Ref.KEY_MACHINE_HEAT)){ + heatHandler.ifPresent(h -> h.deserialize(tag.getCompound(Ref.KEY_MACHINE_HEAT))); } if (tag.contains(Ref.KEY_MACHINE_COVER)) coverHandler.ifPresent(e -> e.deserialize(tag.getCompound(Ref.KEY_MACHINE_COVER))); @@ -657,10 +666,11 @@ public void saveAdditional(CompoundTag tag) { tag.putInt(Ref.KEY_MACHINE_STATE_D, disabledState.ordinal()); itemHandler.ifPresent(i -> tag.put(Ref.KEY_MACHINE_ITEMS, i.serialize(new CompoundTag()))); energyHandler.ifPresent(e -> tag.put(Ref.KEY_MACHINE_ENERGY, e.serialize(new CompoundTag()))); - rfHandler.ifPresent(e -> tag.put(Ref.KEY_MACHINE_ENERGY, e.serialize(new CompoundTag()))); + rfHandler.ifPresent(e -> tag.put(Ref.KEY_MACHINE_RF, e.serialize(new CompoundTag()))); coverHandler.ifPresent(e -> tag.put(Ref.KEY_MACHINE_COVER , e.serialize(new CompoundTag()))); fluidHandler.ifPresent(e -> tag.put(Ref.KEY_MACHINE_FLUIDS, e.serialize(new CompoundTag()))); recipeHandler.ifPresent(e -> tag.put(Ref.KEY_MACHINE_RECIPE, e.serialize())); + heatHandler.ifPresent(e -> tag.put(Ref.KEY_MACHINE_HEAT, e.serialize(new CompoundTag()))); } @NotNull diff --git a/common/src/main/java/muramasa/antimatter/blockentity/pipe/BlockEntityHeatPipe.java b/common/src/main/java/muramasa/antimatter/blockentity/pipe/BlockEntityHeatPipe.java index 379b73685..f526eb45d 100644 --- a/common/src/main/java/muramasa/antimatter/blockentity/pipe/BlockEntityHeatPipe.java +++ b/common/src/main/java/muramasa/antimatter/blockentity/pipe/BlockEntityHeatPipe.java @@ -21,32 +21,7 @@ public class BlockEntityHeatPipe> extends BlockEntityPipe< public BlockEntityHeatPipe(T type, BlockPos pos, BlockState state) { super(type, pos, state); - pipeCapHolder.set(() -> new DefaultHeatHandler(this, 800*type.conductivity, type.conductivity) { - @Override - public void update(boolean active) { - boolean doTransfer = true; - //Always called per frame - if (doTransfer) { - //Transfer 1 degree of power each quarter second. - HeatTransaction tx = extract(); - if (tx.isValid()) { - int3 mutPos = new int3(); - for (Direction dir : Ref.DIRS) { - if (connects(dir) && validate(dir)) { - mutPos.set(pos); - mutPos = mutPos.offset(1,dir); - BlockEntity ent = level.getBlockEntity(mutPos); - if (ent == null) continue; - TesseractCapUtils.getHeatHandler(ent, dir.getOpposite()).ifPresent(t -> t.insert(tx)); - } - } - tx.commit(); - this.currentHeat = Math.max(0, this.currentHeat - (int)(((float)this.temperaturesize)*0.01)); - } - } - } - }); } @Override @@ -64,48 +39,8 @@ protected boolean deregister() { return TesseractGraphWrappers.HEAT_CONTROLLER.remove(level, getBlockPos().asLong()); } - @Override - public HeatTransaction extract() { - return ((Optional) pipeCapHolder.nullSide()).map(IHeatHandler::extract).orElse(null); - } - - @Override - public void insert(HeatTransaction transaction) { - ((Optional) pipeCapHolder.nullSide()).ifPresent(t -> t.insert(transaction)); - } - - @Override - public int getHeat() { - return ((Optional) pipeCapHolder.nullSide()).map(IHeatHandler::getHeat).orElse(0); - } - - @Override - public int getHeatCap() { - return ((Optional) pipeCapHolder.nullSide()).map(IHeatHandler::getHeatCap).orElse(0); - } - - @Override - public int getTemperature() { - return ((Optional) pipeCapHolder.nullSide()).map(IHeatHandler::getTemperature).orElse(0); - } - - @Override - public void update(boolean active) { - ((Optional) pipeCapHolder.nullSide()).ifPresent(t -> t.update(active)); - } - @Override public int temperatureCoefficient() { return this.type.conductivity; } - - @Override - public CompoundTag serialize(CompoundTag nbt) { - return ((Optional) pipeCapHolder.nullSide()).map(h -> h.serialize(new CompoundTag())).orElse(null); - } - - @Override - public void deserialize(CompoundTag nbt) { - ((Optional) pipeCapHolder.nullSide()).ifPresent(h -> h.deserialize(nbt)); - } } diff --git a/common/src/main/java/muramasa/antimatter/capability/machine/DefaultHeatHandler.java b/common/src/main/java/muramasa/antimatter/capability/machine/DefaultHeatHandler.java index 497c0926a..7bb1ac2c8 100644 --- a/common/src/main/java/muramasa/antimatter/capability/machine/DefaultHeatHandler.java +++ b/common/src/main/java/muramasa/antimatter/capability/machine/DefaultHeatHandler.java @@ -5,11 +5,13 @@ import muramasa.antimatter.blockentity.BlockEntityBase; import muramasa.antimatter.capability.Dispatch; import muramasa.antimatter.blockentity.BlockEntityMachine; +import muramasa.antimatter.capability.IMachineHandler; +import muramasa.antimatter.machine.event.MachineEvent; import muramasa.antimatter.util.Utils; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.level.block.entity.BlockEntity; import tesseract.TesseractCapUtils; -import tesseract.api.heat.HeatTransaction; import tesseract.api.heat.IHeatHandler; import java.util.Optional; @@ -18,37 +20,77 @@ public class DefaultHeatHandler implements IHeatHandler, Dispatch.Sided tile; - public DefaultHeatHandler(BlockEntityBase tile, int heatCap, int temperatureSize) { + public DefaultHeatHandler(BlockEntityBase tile, int heatCap, int maxInput, int maxOutput) { this.heatCap = heatCap; this.tile = tile; - this.temperaturesize = temperatureSize; + this.temperaturesize = 100; + this.maxInput = maxInput; + this.maxOutput = maxOutput; } + @Override - public HeatTransaction extract() { - //if (this.currentHeat < this.temperaturesize) return new HeatTransaction(0,0, Utils.sink()); - if (this.currentHeat < this.temperaturesize) return new HeatTransaction(0,0, Utils.sink()); - return new HeatTransaction(this.temperaturesize, getTemperature(), this::sub); + public int insert(int heat, boolean simulate) { + if (!canInput()) return 0; + int insert = Math.min(maxInput, Math.min(heatCap - currentHeat, heat)); + if (!simulate) add(insert); + return insert; } @Override - public void insert(HeatTransaction transaction) { - int availableToInsert = transaction.available(); - availableToInsert = Math.min(heatCap - currentHeat, availableToInsert); - if (availableToInsert > 0) { - transaction.addData(availableToInsert, getTemperature(), this::add); - } + public int extract(int heat, boolean simulate) { + if (!canOutput()) return 0; + int extract = Math.min(maxOutput, Math.min(currentHeat, heat)); + if (!simulate) sub(extract); + return extract; + } + + @Override + public boolean canInput() { + return maxInput > 0; + } + + @Override + public boolean canOutput() { + return maxOutput > 0; + } + + @Override + public boolean canInput(Direction direction) { + return canInput(); + } + + @Override + public boolean canOutput(Direction direction) { + return canOutput(); + } + + @Override + public long getMaxInsert() { + return maxInput; + } + + @Override + public long getMaxExtract() { + return maxOutput; } - protected void sub(Integer temp) { + protected void sub(int temp) { this.currentHeat -= temp; + if (tile instanceof IMachineHandler machineHandler){ + machineHandler.onMachineEvent(MachineEvent.HEAT_DRAINED, temp); + } } - protected void add(Integer temp) { + protected void add(int temp) { this.currentHeat += temp; + if (tile instanceof IMachineHandler machineHandler){ + machineHandler.onMachineEvent(MachineEvent.HEAT_INPUTTED, temp); + } } @Override @@ -57,20 +99,20 @@ public int getTemperature() { } public void update(boolean active) { - boolean doTransfer = tile.getLevel().getGameTime() % 20 == 0; - - if (doTransfer) { - //Transfer 1 degree of power each second. - HeatTransaction tx = extract(); - if (tx.isValid()) { - Utils.entitiesAround(tile.getLevel(), tile.getBlockPos(), (dir, ent) -> TesseractCapUtils.getHeatHandler(ent, dir.getOpposite()).ifPresent(t -> t.insert(tx))); - tx.commit(); + + for (Direction dir : Ref.DIRS) { + if (canOutput(dir)) { + BlockEntity tile = this.tile.getLevel().getBlockEntity(this.tile.getBlockPos().relative(dir)); + if (tile == null) continue; + Optional handle = TesseractCapUtils.getHeatHandler(tile, dir.getOpposite()); + if (handle.map(h -> !h.canInput(dir.getOpposite())).orElse(true)) continue; + handle.ifPresent(eh -> Utils.transferHeat(this, eh)); } } - if (!active) { + /*if (!active) { this.currentHeat -= temperaturesize / 40; this.currentHeat = Math.max(0, this.currentHeat); - } + }*/ } @Override diff --git a/common/src/main/java/muramasa/antimatter/capability/machine/MachineEnergyHandler.java b/common/src/main/java/muramasa/antimatter/capability/machine/MachineEnergyHandler.java index 2174e3b92..0bde646ae 100644 --- a/common/src/main/java/muramasa/antimatter/capability/machine/MachineEnergyHandler.java +++ b/common/src/main/java/muramasa/antimatter/capability/machine/MachineEnergyHandler.java @@ -235,6 +235,7 @@ public void onUpdate() { BlockEntity tile = this.tile.getLevel().getBlockEntity(this.tile.getBlockPos().relative(dir)); if (tile == null) continue; Optional handle = TesseractCapUtils.getEnergyHandler(tile, dir.getOpposite()); + if (handle.map(h -> !h.canInput(dir.getOpposite())).orElse(true)) continue; handle.ifPresent(eh -> Utils.transferEnergy(this, eh)); } } diff --git a/common/src/main/java/muramasa/antimatter/pipe/BlockHeatPipe.java b/common/src/main/java/muramasa/antimatter/pipe/BlockHeatPipe.java index 04036bd20..5d7f6fd87 100644 --- a/common/src/main/java/muramasa/antimatter/pipe/BlockHeatPipe.java +++ b/common/src/main/java/muramasa/antimatter/pipe/BlockHeatPipe.java @@ -20,11 +20,11 @@ public BlockHeatPipe(T type, PipeSize size) { //@ParametersAreNotNullByDefault public void entityInside(BlockState state, Level worldIn, BlockPos pos, Entity entityIn) { super.entityInside(state, worldIn, pos, entityIn); - if (!(entityIn instanceof LivingEntity)) return; + /*if (!(entityIn instanceof LivingEntity)) return; BlockEntityHeatPipe pipe = (BlockEntityHeatPipe) worldIn.getBlockEntity(pos); int temp = pipe.getTemperature(); if (temp > 50) { entityIn.hurt(DamageSource.GENERIC, Mth.clamp((temp-10)/2, 2, 20)); - } + }*/ } } diff --git a/common/src/main/java/muramasa/antimatter/util/Utils.java b/common/src/main/java/muramasa/antimatter/util/Utils.java index 991a63bc6..faa161ad9 100644 --- a/common/src/main/java/muramasa/antimatter/util/Utils.java +++ b/common/src/main/java/muramasa/antimatter/util/Utils.java @@ -67,6 +67,7 @@ import org.jetbrains.annotations.Nullable; import tesseract.TesseractGraphWrappers; import tesseract.api.gt.IEnergyHandler; +import tesseract.api.heat.IHeatHandler; import tesseract.api.item.PlatformItemHandler; import java.awt.*; @@ -454,6 +455,18 @@ public static boolean transferEnergy(EnergyContainer from, PlatformEnergyManager return false; } + public static boolean transferHeat(IHeatHandler from, IHeatHandler to) { + int extracted = from.extract(Integer.MAX_VALUE, true); + if (extracted > 0) { + int inserted = to.insert(extracted, false); + if (inserted > 0) { + from.extract(inserted, false); + return true; + } + } + return false; + } + public static boolean addEnergy(IEnergyHandler to, long eu) { return to.insertEu(eu, false) > 0; }