Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package de.srendi.advancedperipherals.common.addons.computercraft.peripheral;

import dan200.computercraft.api.lua.LuaFunction;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.BlockEntityPeripheralOwner;
import de.srendi.advancedperipherals.common.blocks.base.BaseDetectorEntity;
import de.srendi.advancedperipherals.lib.peripherals.BasePeripheral;

public abstract class BaseDetectorPeripheral<E extends BaseDetectorEntity> extends BasePeripheral<BlockEntityPeripheralOwner<E>> {
protected BaseDetectorPeripheral(String type, E tileEntity) {
super(type, new BlockEntityPeripheralOwner<>(tileEntity));
}

@LuaFunction
public final long getMaxTransferRate() {
return owner.tileEntity.getMaxTransferRate();
}

@LuaFunction
public final long getTransferRateLimit() {
return owner.tileEntity.getTransferRateLimit();
}

@LuaFunction
public final void setTransferRateLimit(long transferRate) {
owner.tileEntity.setTransferRateLimit(transferRate);
}

@LuaFunction
public final long getTransferRate() {
return owner.tileEntity.getTransferRate();
}

@LuaFunction
public final String getLastTransferedId() {
return owner.tileEntity.getLastTransferedId();
}

@LuaFunction
public final String getReadyTransferId() {
return owner.tileEntity.getReadyTransferId();
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,18 @@
package de.srendi.advancedperipherals.common.addons.computercraft.peripheral;

import dan200.computercraft.api.lua.LuaFunction;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.BlockEntityPeripheralOwner;
import de.srendi.advancedperipherals.common.blocks.blockentities.EnergyDetectorEntity;
import de.srendi.advancedperipherals.common.configuration.APConfig;
import de.srendi.advancedperipherals.lib.peripherals.BasePeripheral;

public class EnergyDetectorPeripheral extends BasePeripheral<BlockEntityPeripheralOwner<EnergyDetectorEntity>> {
public class EnergyDetectorPeripheral extends BaseDetectorPeripheral<EnergyDetectorEntity> {

public static final String PERIPHERAL_TYPE = "energy_detector";
public static final String TYPE = "energy_detector";

public EnergyDetectorPeripheral(EnergyDetectorEntity tileEntity) {
super(PERIPHERAL_TYPE, new BlockEntityPeripheralOwner<>(tileEntity));
super(TYPE, tileEntity);
}

@Override
public boolean isEnabled() {
return APConfig.PERIPHERALS_CONFIG.enableEnergyDetector.get();
}

@LuaFunction(mainThread = true)
public final int getTransferRateLimit() {
return owner.tileEntity.storageProxy.getMaxTransferRate();
}

@LuaFunction(mainThread = true)
public final void setTransferRateLimit(long transferRate) {
transferRate = Math.max(0, Math.min(APConfig.PERIPHERALS_CONFIG.energyDetectorMaxFlow.get(), transferRate));
owner.tileEntity.storageProxy.setMaxTransferRate((int) transferRate);
}

@LuaFunction(mainThread = true)
public final int getTransferRate() {
return owner.tileEntity.transferRate;
}
}
Original file line number Diff line number Diff line change
@@ -1,43 +1,18 @@
package de.srendi.advancedperipherals.common.addons.computercraft.peripheral;

import dan200.computercraft.api.lua.LuaFunction;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.BlockEntityPeripheralOwner;
import de.srendi.advancedperipherals.common.blocks.blockentities.FluidDetectorEntity;
import de.srendi.advancedperipherals.common.configuration.APConfig;
import de.srendi.advancedperipherals.lib.peripherals.BasePeripheral;
import net.minecraftforge.registries.ForgeRegistries;

public class FluidDetectorPeripheral extends BasePeripheral<BlockEntityPeripheralOwner<FluidDetectorEntity>> {
public class FluidDetectorPeripheral extends BaseDetectorPeripheral<FluidDetectorEntity> {

public static final String TYPE = "fluid_detector";

public FluidDetectorPeripheral(FluidDetectorEntity tileEntity) {
super(TYPE, new BlockEntityPeripheralOwner<>(tileEntity));
super(TYPE, tileEntity);
}

@Override
public boolean isEnabled() {
return APConfig.PERIPHERALS_CONFIG.enableFluidDetector.get();
}

@LuaFunction(mainThread = true)
public final int getTransferRateLimit() {
return owner.tileEntity.storageProxy.getMaxTransferRate();
}

@LuaFunction(mainThread = true)
public final String getTransferedFluid() {
return ForgeRegistries.FLUIDS.getKey(owner.tileEntity.lastFlowedLiquid.getFluid()).toString();
}

@LuaFunction(mainThread = true)
public final void setTransferRateLimit(long transferRate) {
transferRate = Math.min(APConfig.PERIPHERALS_CONFIG.fluidDetectorMaxFlow.get(), transferRate);
owner.tileEntity.storageProxy.setMaxTransferRate((int) transferRate);
}

@LuaFunction(mainThread = true)
public final int getTransferRate() {
return owner.tileEntity.transferRate;
}
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,18 @@
package de.srendi.advancedperipherals.common.addons.computercraft.peripheral;

import dan200.computercraft.api.lua.LuaFunction;
import de.srendi.advancedperipherals.common.addons.computercraft.owner.BlockEntityPeripheralOwner;
import de.srendi.advancedperipherals.common.blocks.blockentities.GasDetectorEntity;
import de.srendi.advancedperipherals.common.configuration.APConfig;
import de.srendi.advancedperipherals.lib.peripherals.BasePeripheral;

public class GasDetectorPeripheral extends BasePeripheral<BlockEntityPeripheralOwner<GasDetectorEntity>> {
public class GasDetectorPeripheral extends BaseDetectorPeripheral<GasDetectorEntity> {

public static final String TYPE = "gas_detector";

public GasDetectorPeripheral(GasDetectorEntity tileEntity) {
super(TYPE, new BlockEntityPeripheralOwner<>(tileEntity));
super(TYPE, tileEntity);
}

@Override
public boolean isEnabled() {
return APConfig.PERIPHERALS_CONFIG.enableGasDetector.get();
}

@LuaFunction(mainThread = true)
public final int getTransferRateLimit() {
return owner.tileEntity.storageProxy.getMaxTransferRate();
}

@LuaFunction(mainThread = true)
public final String getTransferedGas() {
return owner.tileEntity.lastFlowedGas.getRaw().getRegistryName().toString();
}

@LuaFunction(mainThread = true)
public final void setTransferRateLimit(long transferRate) {
transferRate = Math.min(APConfig.PERIPHERALS_CONFIG.gasDetectorMaxFlow.get(), transferRate);
owner.tileEntity.storageProxy.setMaxTransferRate((int) transferRate);
}

@LuaFunction(mainThread = true)
public final int getTransferRate() {
return owner.tileEntity.transferRate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,5 @@
import static net.minecraftforge.common.capabilities.CapabilityManager.get;

public class MekanismCapabilities {

public static final Capability<IGasHandler> GAS_HANDLER = get(new CapabilityToken<>() {
});


public static final Capability<IGasHandler> GAS_HANDLER = get(new CapabilityToken<>() {});
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.srendi.advancedperipherals.common.blocks.base;

import de.srendi.advancedperipherals.common.blocks.blockentities.EnergyDetectorEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.entity.BlockEntity;
Expand Down Expand Up @@ -33,11 +32,6 @@ public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState sta
@Override
public void onNeighborChange(BlockState state, LevelReader level, BlockPos pos, BlockPos neighbor) {
super.onNeighborChange(state, level, pos, neighbor);

BlockEntity blockEntity = level.getBlockEntity(pos);

if(blockEntity instanceof EnergyDetectorEntity energyDetector)
energyDetector.invalidateStorages();

// BlockEntity blockEntity = level.getBlockEntity(pos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package de.srendi.advancedperipherals.common.blocks.base;

import de.srendi.advancedperipherals.common.util.proxy.IStorageProxy;
import de.srendi.advancedperipherals.lib.peripherals.BasePeripheral;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* @param <T> The storage handle type
* @param <S> The storage proxy type, must implements/extends both {@code <T>} and {@link IStorageProxy}
* @param <P> The peripheral type, must extends {@link BasePeripheral}
*/
public abstract class BaseDetectorEntity<T, S extends IStorageProxy, P extends BasePeripheral<?>> extends PeripheralBlockEntity<P> {

private static final String RATE_LIMIT_TAG = "RateLimit";

private final Capability<T> capability;
// proxy that will forward X to the output but limit it to maxTransferRate
private final S proxy = createProxy();
private volatile long transferRate = 0;
private LazyOptional<S> inputStorageCap = LazyOptional.empty();
private LazyOptional<T> zeroStorageCap = LazyOptional.empty();

protected BaseDetectorEntity(BlockEntityType<?> tileEntityType, BlockPos pos, BlockState state, Capability<T> capability) {
super(tileEntityType, pos, state);
this.capability = capability;
}

@NotNull
protected abstract S createProxy();

@NotNull
protected abstract T getZeroStorage();

@NotNull
protected S getStorageProxy() {
return this.proxy;
}

/**
* @return the transfered amount of stuff in the last tick
*/
public long getTransferRate() {
return this.transferRate;
}

/**
* @return the possible maximum transfered amount
*/
public long getMaxTransferRate() {
return this.proxy.getMaxTransferRate();
}

/**
* @return the max amount of stuff can be transfered in a tick
*/
public long getTransferRateLimit() {
return this.proxy.getTransferRate();
}

/**
* @param rate the max amount of stuff can be transfered in a tick
*/
public void setTransferRateLimit(long rate) {
if (this.proxy.getTransferRate() != rate) {
this.proxy.setTransferRate(rate);
this.setChanged();
}
}

/**
* @return the ID of last transfered stuff
*/
@Nullable
public String getLastTransferedId() {
return this.proxy.getLastTransferedId();
}

/**
* @return the ID of ready transfered stuff
*/
@Nullable
public String getReadyTransferId() {
return this.proxy.getReadyTransferId();
}

public Direction getInputDirection() {
return this.getBlockState().getValue(BaseBlock.ORIENTATION).front();
}

public Direction getOutputDirection() {
return this.getBlockState().getValue(BaseBlock.ORIENTATION).front().getOpposite();
}

@NotNull
@Override
public <U> LazyOptional<U> getCapability(@NotNull Capability<U> cap, @Nullable Direction direction) {
Direction inputDirection = this.getInputDirection();
Direction outputDirection = this.getOutputDirection();
if (cap == this.capability) {
if (direction == inputDirection) {
if (!this.inputStorageCap.isPresent()) {
this.inputStorageCap = LazyOptional.of(this::getStorageProxy);
}
return this.inputStorageCap.cast();
} else if (direction == outputDirection) {
if (!this.zeroStorageCap.isPresent()) {
this.zeroStorageCap = LazyOptional.of(this::getZeroStorage);
}
return this.zeroStorageCap.cast();
}
}
return super.getCapability(cap, direction);
}

@Override
public void saveAdditional(@NotNull CompoundTag compound) {
super.saveAdditional(compound);
compound.putLong(RATE_LIMIT_TAG, this.getTransferRateLimit());
}

@Override
public void load(@NotNull CompoundTag nbt) {
this.proxy.setTransferRate(nbt.getLong(RATE_LIMIT_TAG));
super.load(nbt);
}

@Override
public <T extends BlockEntity> void handleTick(Level level, BlockState state, BlockEntityType<T> type) {
if (!level.isClientSide) {
this.transferRate = this.proxy.getAndResetTransfered();
}
}

@NotNull
public LazyOptional<? extends T> getOutputStorage() {
Direction outputDirection = this.getOutputDirection();
BlockEntity be = level.getBlockEntity(worldPosition.relative(outputDirection));
return be == null ? LazyOptional.empty() : be.getCapability(this.capability, outputDirection.getOpposite());
}
}
Loading