Skip to content

Commit

Permalink
laser cables (#501)
Browse files Browse the repository at this point in the history
* feat: laser cables

* fix: remove laser recipe capability, recipes

* fix: add 16A dynamo hatch recipes.

* remove todo

* fix: steam machines only allow GT steam to be inserted.

* reviews
  • Loading branch information
screret authored Nov 1, 2023
1 parent 22c46e6 commit 90c57a6
Show file tree
Hide file tree
Showing 73 changed files with 1,694 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public abstract class PipeBlockEntity<PipeType extends Enum<PipeType> & IPipeTyp
@Persisted(key = "cover")
protected final PipeCoverContainer coverContainer;

@Setter
@Getter @Setter
@DescSynced
@Persisted
@RequireRerender
Expand Down Expand Up @@ -133,6 +133,17 @@ public void clearRemoved() {
coverContainer.onLoad();
}

@Override
public int getNumConnections() {
int count = 0;
int connections = getConnections();
while (connections > 0) {
count++;
connections = connections & (connections - 1);
}
return count;
}

@Nullable
public TickableSubscription subscribeServerTick(Runnable runnable) {
if (!isRemote()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,10 @@ public static ICleanroomReceiver getCleanroomReceiver(Level level, BlockPos pos,
public static IMaintenanceMachine getMaintenanceMachine(Level level, BlockPos pos, @Nullable Direction side) {
throw new AssertionError();
}

@ExpectPlatform
@Nullable
public static ILaserContainer getLaser(Level level, BlockPos pos, @Nullable Direction side) {
throw new AssertionError();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.gregtechceu.gtceu.api.capability;

/**
* It is its own separate interface to make piping work easier
*/
public interface ILaserContainer extends IEnergyContainer {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public class BlockStateRecipeCapability extends RecipeCapability<BlockState> {
protected BlockStateRecipeCapability() {
super("block_state", 0xFFABABAB, SerializerBlockState.INSTANCE);
}

@Override
public BlockState copyInner(BlockState content) {
return content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.gregtechceu.gtceu.api.item;

import com.gregtechceu.gtceu.api.block.PipeBlock;
import com.gregtechceu.gtceu.common.block.LaserPipeBlock;
import com.lowdragmc.lowdraglib.client.renderer.IItemRendererProvider;
import com.lowdragmc.lowdraglib.client.renderer.IRenderer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.color.item.ItemColor;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;

public class LaserPipeBlockItem extends PipeBlockItem implements IItemRendererProvider {

public LaserPipeBlockItem(PipeBlock block, Properties properties) {
super(block, properties);
}

@Override
public LaserPipeBlock getBlock() {
return (LaserPipeBlock) super.getBlock();
}

@Environment(EnvType.CLIENT)
public static ItemColor tintColor() {
return (itemStack, index) -> {
if (itemStack.getItem() instanceof LaserPipeBlockItem materialBlockItem) {
return materialBlockItem.getBlock().tinted(materialBlockItem.getBlock().defaultBlockState(), null, null, index);
}
return -1;
};
}

@Nullable
@Override
@Environment(EnvType.CLIENT)
public IRenderer getRenderer(ItemStack stack) {
return getBlock().getRenderer(getBlock().defaultBlockState());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class PartAbility {
public static final PartAbility TANK_VALVE = new PartAbility("tank_valve");
public static final PartAbility PASSTHROUGH_HATCH = new PartAbility("passthrough_hatch");
public static final PartAbility PARALLEL_HATCH = new PartAbility("parallel_hatch");
public static final PartAbility INPUT_LASER = new PartAbility("input_laser");
public static final PartAbility OUTPUT_LASER = new PartAbility("output_laser");

/**
* tier -> available blocks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.gregtechceu.gtceu.api.machine.trait.NotifiableFluidTank;
import com.gregtechceu.gtceu.api.recipe.GTRecipe;
import com.gregtechceu.gtceu.api.recipe.ingredient.FluidIngredient;
import com.gregtechceu.gtceu.common.data.GTMaterials;
import com.gregtechceu.gtceu.data.recipe.CustomTags;
import com.lowdragmc.lowdraglib.side.fluid.FluidStack;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -35,7 +36,7 @@ public List<Long> handleRecipeInner(IO io, GTRecipe recipe, List<Long> left, @Nu
long sum = left.stream().reduce(0L, Long::sum);
long realSum = (long) Math.ceil(sum * conversionRate);
if (realSum > 0) {
var steam = FluidIngredient.of(CustomTags.STEAM, realSum);
var steam = io == IO.IN ? FluidIngredient.of(CustomTags.STEAM, realSum) : FluidIngredient.of(GTMaterials.Steam.getFluid(realSum));
var list = new ArrayList<FluidIngredient>();
list.add(steam);
var leftSteam = steamTank.handleRecipeInner(io, recipe, list, slotName, simulate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.gregtechceu.gtceu.api.machine.trait.NotifiableFluidTank;
import com.gregtechceu.gtceu.api.machine.feature.ITieredMachine;
import com.gregtechceu.gtceu.common.data.GTMaterials;
import com.gregtechceu.gtceu.data.recipe.CustomTags;
import com.lowdragmc.lowdraglib.syncdata.annotation.Persisted;
import com.lowdragmc.lowdraglib.syncdata.field.ManagedFieldHolder;
import lombok.Getter;
Expand All @@ -31,7 +32,7 @@ public SteamMachine(IMachineBlockEntity holder, boolean isHighPressure, Object..
super(holder);
this.isHighPressure = isHighPressure;
this.steamTank = createSteamTank(args);
this.steamTank.setFilter(fluid -> GTMaterials.Steam.getFluid() == fluid.getFluid());
this.steamTank.setFilter(fluidStack -> fluidStack.getFluid().is(CustomTags.STEAM));
}

//////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;

public class NotifiableEnergyContainer extends NotifiableRecipeHandlerTrait<Long> implements IEnergyContainer {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.gregtechceu.gtceu.api.machine.trait;

import com.gregtechceu.gtceu.api.capability.GTCapabilityHelper;
import com.gregtechceu.gtceu.api.capability.ILaserContainer;
import com.gregtechceu.gtceu.api.machine.MetaMachine;
import com.lowdragmc.lowdraglib.syncdata.field.ManagedFieldHolder;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.entity.BlockEntity;

public class NotifiableLaserContainer extends NotifiableEnergyContainer implements ILaserContainer {

public static final ManagedFieldHolder MANAGED_FIELD_HOLDER = new ManagedFieldHolder(NotifiableEnergyContainer.class, NotifiableRecipeHandlerTrait.MANAGED_FIELD_HOLDER);

public NotifiableLaserContainer(MetaMachine machine, long maxCapacity, long maxInputVoltage, long maxInputAmperage, long maxOutputVoltage, long maxOutputAmperage) {
super(machine, maxCapacity, maxInputVoltage, maxInputAmperage, maxOutputVoltage, maxOutputAmperage);
}

public static NotifiableLaserContainer emitterContainer(MetaMachine machine, long maxCapacity, long maxOutputVoltage, long maxOutputAmperage) {
return new NotifiableLaserContainer(machine, maxCapacity, 0L, 0L, maxOutputVoltage, maxOutputAmperage);
}

public static NotifiableLaserContainer receiverContainer(MetaMachine machine, long maxCapacity, long maxInputVoltage, long maxInputAmperage) {
return new NotifiableLaserContainer(machine, maxCapacity, maxInputVoltage, maxInputAmperage, 0L, 0L);
}

@Override
public void serverTick() {
amps = 0;
if (getMachine().getLevel().isClientSide)
return;
if (getEnergyStored() < getOutputVoltage() || getOutputVoltage() <= 0 || getOutputAmperage() <= 0)
return;
long outputVoltage = getOutputVoltage();
long outputAmperes = Math.min(getEnergyStored() / outputVoltage, getOutputAmperage());
if (outputAmperes == 0) return;
long amperesUsed = 0;
for (Direction side : Direction.values()) {
if (!outputsEnergy(side)) continue;
BlockEntity tileEntity = getMachine().getLevel().getBlockEntity(getMachine().getPos().relative(side));
Direction oppositeSide = side.getOpposite();
ILaserContainer laserContainer = GTCapabilityHelper.getLaser(getMachine().getLevel(), getMachine().getPos().relative(side), oppositeSide);
if (tileEntity != null && laserContainer != null) {
if (laserContainer == null || !laserContainer.inputsEnergy(oppositeSide)) continue;
amperesUsed += laserContainer.acceptEnergyFromNetwork(oppositeSide, outputVoltage, outputAmperes - amperesUsed);
if (amperesUsed == outputAmperes) break;
}
}
if (amperesUsed > 0) {
setEnergyStored(getEnergyStored() - amperesUsed * outputVoltage);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.gregtechceu.gtceu.api.misc;

import com.gregtechceu.gtceu.api.capability.IEnergyContainer;
import com.gregtechceu.gtceu.api.capability.ILaserContainer;
import net.minecraft.core.Direction;

import java.util.List;

public class LaserContainerList implements ILaserContainer {

private final List<? extends ILaserContainer> energyContainerList;

public LaserContainerList(List<? extends ILaserContainer> energyContainerList) {
this.energyContainerList = energyContainerList;
}

@Override
public long acceptEnergyFromNetwork(Direction side, long voltage, long amperage) {
long amperesUsed = 0L;
List<? extends ILaserContainer> energyContainerList = this.energyContainerList;
for (ILaserContainer iEnergyContainer : energyContainerList) {
amperesUsed += iEnergyContainer.acceptEnergyFromNetwork(null, voltage, amperage);
if (amperage == amperesUsed) {
return amperesUsed;
}
}
return amperesUsed;
}

@Override
public long changeEnergy(long energyToAdd) {
long energyAdded = 0L;
List<? extends ILaserContainer> energyContainerList = this.energyContainerList;
for (ILaserContainer iEnergyContainer : energyContainerList) {
energyAdded += iEnergyContainer.changeEnergy(energyToAdd - energyAdded);
if (energyAdded == energyToAdd) {
return energyAdded;
}
}
return energyAdded;
}

@Override
public long getEnergyStored() {
long energyStored = 0L;
for (ILaserContainer iEnergyContainer : energyContainerList) {
energyStored += iEnergyContainer.getEnergyStored();
}
return energyStored;
}

@Override
public long getEnergyCapacity() {
long energyCapacity = 0L;
for (ILaserContainer iEnergyContainer : energyContainerList) {
energyCapacity += iEnergyContainer.getEnergyCapacity();
}
return energyCapacity;
}

@Override
public long getInputAmperage() {
return 1L;
}

@Override
public long getOutputAmperage() {
return 1L;
}

@Override
public long getInputVoltage() {
long inputVoltage = 0L;
for (ILaserContainer container : energyContainerList) {
inputVoltage += container.getInputVoltage() * container.getInputAmperage();
}
return inputVoltage;
}

@Override
public long getOutputVoltage() {
long outputVoltage = 0L;
for (ILaserContainer container : energyContainerList) {
outputVoltage += container.getOutputVoltage() * container.getOutputAmperage();
}
return outputVoltage;
}

@Override
public boolean inputsEnergy(Direction side) {
return true;
}

@Override
public boolean outputsEnergy(Direction side) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public interface IPipeNode<PipeType extends Enum<PipeType> & IPipeType<NodeDataT
*/
void setConnections(int connections);

int getConnections();

int getNumConnections();

/**
* set to block connection from the specific side
* @param side face
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.gregtechceu.gtceu.GTCEu;
import com.mojang.serialization.JsonOps;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
Expand Down Expand Up @@ -63,7 +64,7 @@ public BlockState fromNetwork(FriendlyByteBuf buf) {

@Override
public BlockState fromJson(JsonElement json) {
return BlockState.CODEC.decode(JsonOps.INSTANCE, json).get().map(Function.identity(), right -> null).getFirst();
return BlockState.CODEC.parse(JsonOps.INSTANCE, json).getOrThrow(false, GTCEu.LOGGER::error);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public void updateIndex() {
}
}

public ChatFormatting getCurrent() {
return codes[index];
}

@Override
public String toString() {
return codes[index].toString();
Expand Down
Loading

0 comments on commit 90c57a6

Please sign in to comment.