Skip to content

Commit

Permalink
updated antimatter, changed heat system a bit to be like gt6, changed…
Browse files Browse the repository at this point in the history
… how rf handlers are saved
  • Loading branch information
Trinsdar committed Oct 11, 2023
1 parent 474b697 commit f0fbfed
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 98 deletions.
2 changes: 2 additions & 0 deletions common/src/main/java/muramasa/antimatter/Ref.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -115,6 +116,7 @@ public class BlockEntityMachine<T extends BlockEntityMachine<T>> extends BlockEn
public Holder<FluidContainer, MachineFluidHandler<T>> fluidHandler = new Holder<>(FluidContainer.class, dispatch);
public Holder<ICoverHandler<?>, MachineCoverHandler<T>> coverHandler = new Holder<>(ICoverHandler.class, dispatch, null);
public Holder<IEnergyHandler, MachineEnergyHandler<T>> energyHandler = new Holder<>(IEnergyHandler.class, dispatch);
public Holder<IHeatHandler, DefaultHeatHandler> heatHandler = new Holder<>(IHeatHandler.class, dispatch);
public Holder<IRFNode, MachineRFHandler<T>> rfHandler = new Holder<>(IRFNode.class, dispatch);
public Holder<MachineRecipeHandler<?>, MachineRecipeHandler<T>> recipeHandler = new Holder<>(MachineRecipeHandler.class, dispatch, null);

Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)));
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,7 @@ public class BlockEntityHeatPipe<T extends HeatPipe<T>> 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
Expand All @@ -64,48 +39,8 @@ protected boolean deregister() {
return TesseractGraphWrappers.HEAT_CONTROLLER.remove(level, getBlockPos().asLong());
}

@Override
public HeatTransaction extract() {
return ((Optional<IHeatHandler>) pipeCapHolder.nullSide()).map(IHeatHandler::extract).orElse(null);
}

@Override
public void insert(HeatTransaction transaction) {
((Optional<IHeatHandler>) pipeCapHolder.nullSide()).ifPresent(t -> t.insert(transaction));
}

@Override
public int getHeat() {
return ((Optional<IHeatHandler>) pipeCapHolder.nullSide()).map(IHeatHandler::getHeat).orElse(0);
}

@Override
public int getHeatCap() {
return ((Optional<IHeatHandler>) pipeCapHolder.nullSide()).map(IHeatHandler::getHeatCap).orElse(0);
}

@Override
public int getTemperature() {
return ((Optional<IHeatHandler>) pipeCapHolder.nullSide()).map(IHeatHandler::getTemperature).orElse(0);
}

@Override
public void update(boolean active) {
((Optional<IHeatHandler>) pipeCapHolder.nullSide()).ifPresent(t -> t.update(active));
}

@Override
public int temperatureCoefficient() {
return this.type.conductivity;
}

@Override
public CompoundTag serialize(CompoundTag nbt) {
return ((Optional<IHeatHandler>) pipeCapHolder.nullSide()).map(h -> h.serialize(new CompoundTag())).orElse(null);
}

@Override
public void deserialize(CompoundTag nbt) {
((Optional<IHeatHandler>) pipeCapHolder.nullSide()).ifPresent(h -> h.deserialize(nbt));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,37 +20,77 @@ public class DefaultHeatHandler implements IHeatHandler, Dispatch.Sided<IHeatHan

public final int heatCap;
public final int temperaturesize;
public final int maxInput, maxOutput;
protected int currentHeat;

public final BlockEntityBase<?> 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
Expand All @@ -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<IHeatHandler> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ public void onUpdate() {
BlockEntity tile = this.tile.getLevel().getBlockEntity(this.tile.getBlockPos().relative(dir));
if (tile == null) continue;
Optional<IEnergyHandler> handle = TesseractCapUtils.getEnergyHandler(tile, dir.getOpposite());
if (handle.map(h -> !h.canInput(dir.getOpposite())).orElse(true)) continue;
handle.ifPresent(eh -> Utils.transferEnergy(this, eh));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}*/
}
}
13 changes: 13 additions & 0 deletions common/src/main/java/muramasa/antimatter/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit f0fbfed

Please sign in to comment.