Skip to content

Commit

Permalink
processing cable wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Lothrazar committed Oct 4, 2024
1 parent ccbd4f1 commit e3fccc4
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.lothrazar.storagenetwork.api;

import com.lothrazar.storagenetwork.block.main.TileMain;
import net.minecraft.core.Direction;

/**
* Only expose this capability if you want your cable/block to auto-export and import blocks controlled by the networks main. You could quite as well just expose {@link IConnectable} and do the
* exporting/importing in your own update() method.
* <p>
* If you indeed want to add another exporting/importing cable in the style of the integrated ones, this might be for you. In all other cases, this is probably not what you want.
*/
public interface IConnectableItemProcessing {



Direction facingInventory();

/**
* Storages with a higher priority (== lower number) are processed first. You probably want to add a way to configure the priority of your storage.
*
* @return Return the priority here
*/
int getPriority();

void setPriority(int value);

/**
* Called every tick to see if an operation should be processed now, i.e. this can be used to add cooldown times or disable operations via redstone signal.
*
* @param connectablePos
* The position of your block, including the world
* @param main
* The network main. Use this to e.g. query amount of items.
* @return Whether or not this IConnectableLink should be processed this tick.
*/
boolean runNow(TileMain main);

/**
* TODO:
*
* get list of recipe outputs, for requests
*
* @return
*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.lothrazar.storagenetwork.block.cable.processing;

import com.lothrazar.storagenetwork.block.cable.BlockCable;
import com.lothrazar.storagenetwork.registry.SsnRegistry;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;

public class BlockCableProcess extends BlockCable {

public BlockCableProcess() {
super();
}

@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level world, BlockState state, BlockEntityType<T> type) {
return createTickerHelper(type, SsnRegistry.Tiles.PROCESS_KABEL.get(), world.isClientSide ? TileCableProcess::clientTick : TileCableProcess::serverTick);
}

@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new TileCableProcess(pos, state);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.lothrazar.storagenetwork.block.cable.processing;

import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;

//Not done but planned:
//{A}Text box input for priority (all)
//{B}CLONE settings between multiple cables
public class ProcessRequestModel {

//EXPORTING:from network to inventory (to start crafting, add ingredients) . also default state
//IMPORTING: from inventory to network (after crafting is done)
public enum ProcessStatus {
HALTED, IMPORTING, EXPORTING;
}

private static final String PREFIX = "sn_process_";
//you can request more than 64
private int count;
private boolean alwaysActive = false;
private ProcessStatus status = ProcessStatus.EXPORTING;
// current stack (in order of filters) being exported or imported
private int stackIndex;
private Direction inputFace = Direction.UP; // processingTop IE top of furnace
private Direction outputFace = Direction.DOWN; // processingBottom IE pulling output from the furnace

public int getCount() {
return count;
}

public void reduceCount() {
if (count > 0) {
count--;
}
}

public void setCount(int countRequested) {
if (count <= 0 && countRequested > 0) {
//if we are going from zero to non zero, kickstart the thing
setStatus(ProcessStatus.EXPORTING);
}
this.count = countRequested;
}

public void readFromNBT(CompoundTag compound) {
this.count = compound.getInt(PREFIX + "count");
this.status = ProcessStatus.values()[compound.getInt(PREFIX + "status")];
this.alwaysActive = compound.getBoolean(PREFIX + "always");
this.stackIndex = compound.getInt(PREFIX + "stack");
this.inputFace = Direction.values()[compound.getInt(PREFIX + "in")];
this.outputFace = Direction.values()[compound.getInt(PREFIX + "out")];
}

public CompoundTag writeToNBT(CompoundTag compound) {
compound.putInt(PREFIX + "count", count);
compound.putInt(PREFIX + "status", status.ordinal());
compound.putBoolean(PREFIX + "always", alwaysActive);
compound.putInt(PREFIX + "stack", stackIndex);
compound.putInt(PREFIX + "in", this.inputFace.ordinal());
compound.putInt(PREFIX + "out", this.outputFace.ordinal());
return compound;
}

public ProcessStatus getStatus() {
return status;
}

public void setStatus(ProcessStatus status) {
if (status == this.status) {
return;
}
// reset stack index on status change
this.status = status;
this.stackIndex = 0;
}

public boolean isAlwaysActive() {
return alwaysActive;
}

public void setAlwaysActive(boolean alwaysActive) {
this.alwaysActive = alwaysActive;
}

public int getStackIndex() {
return stackIndex;
}

public void increaseStackIndex() {
this.stackIndex++;
}

public Direction getInputFace() {
return inputFace;
}

public void setInputFace(Direction inputFace) {
this.inputFace = inputFace;
}

public Direction getOutputFace() {
return outputFace;
}

public void setOutputFace(Direction outputFace) {
this.outputFace = outputFace;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.lothrazar.storagenetwork.block.cable.processing;

import com.lothrazar.storagenetwork.block.TileCableWithFacing;
import com.lothrazar.storagenetwork.capability.CapabilityConnectableLink;
import com.lothrazar.storagenetwork.registry.SsnRegistry;
import com.lothrazar.storagenetwork.registry.StorageNetworkCapabilities;
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.state.BlockState;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;

public class TileCableProcess extends TileCableWithFacing {

protected CapabilityConnectableLink itemStorage;
private ProcessRequestModel processModel = new ProcessRequestModel();

public TileCableProcess(BlockPos pos, BlockState state) {
super(SsnRegistry.Tiles.PROCESS_KABEL.get(), pos, state);
this.itemStorage = new CapabilityConnectableLink(this);
}

@Override
public void load(CompoundTag compound) {
super.load(compound);
this.itemStorage.deserializeNBT(compound.getCompound("capability"));
this.processModel.readFromNBT(compound);
}

@Override
public void saveAdditional(CompoundTag compound) {
super.saveAdditional(compound);
compound.put("capability", itemStorage.serializeNBT());
this.processModel.writeToNBT(compound);
}

@Override
public void setDirection(Direction direction) {
super.setDirection(direction);
this.itemStorage.setInventoryFace(direction);
}

@Override
public <T> LazyOptional<T> getCapability(Capability<T> capability, Direction facing) {
if (capability == StorageNetworkCapabilities.CONNECTABLE_ITEM_STORAGE_CAPABILITY) {
LazyOptional<CapabilityConnectableLink> cap = LazyOptional.of(() -> itemStorage);
return cap.cast();
}
return super.getCapability(capability, facing);
}

public static void clientTick(Level level, BlockPos blockPos, BlockState blockState, TileCableProcess tile) {}

public static <E extends BlockEntity> void serverTick(Level level, BlockPos blockPos, BlockState blockState, TileCableProcess tile) {
tile.refreshInventoryDirection();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.lothrazar.storagenetwork.capability;

import java.util.concurrent.Callable;
import com.lothrazar.storagenetwork.StorageNetworkMod;
import com.lothrazar.storagenetwork.api.IConnectable;
import com.lothrazar.storagenetwork.api.IConnectableItemProcessing;
import com.lothrazar.storagenetwork.block.main.TileMain;
import com.lothrazar.storagenetwork.capability.handler.FilterItemStackHandler;
import com.lothrazar.storagenetwork.capability.handler.UpgradesItemStackHandler;
import com.lothrazar.storagenetwork.registry.StorageNetworkCapabilities;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.common.util.INBTSerializable;

public class CapabilityConnectableProcessing implements INBTSerializable<CompoundTag>, IConnectableItemProcessing {

public static final int DEFAULT_ITEMS_PER = 4;
public static final int IO_DEFAULT_SPEED = 30; // TODO CONFIG

public static class Factory implements Callable<IConnectableItemProcessing> {

@Override
public IConnectableItemProcessing call() throws Exception {
return new CapabilityConnectableProcessing();
}
}

private Direction inventoryFace;
public final IConnectable connectable;
public final UpgradesItemStackHandler upgrades = new UpgradesItemStackHandler();
private final FilterItemStackHandler filters = new FilterItemStackHandler(9);
private final FilterItemStackHandler filtersOut = new FilterItemStackHandler(1);
private int priority;

CapabilityConnectableProcessing() {
connectable = new CapabilityConnectable();
}

public CapabilityConnectableProcessing(BlockEntity tile) {
connectable = tile.getCapability(StorageNetworkCapabilities.CONNECTABLE_CAPABILITY, null).orElse(null);
}

public void setInventoryFace(Direction inventoryFace) {
this.inventoryFace = inventoryFace;
}

@Override
public CompoundTag serializeNBT() {
CompoundTag result = new CompoundTag();
result.putInt("prio", priority);
result.put("upgrades", this.upgrades.serializeNBT());
result.put("filtersIn", this.filters.serializeNBT());
result.put("filtersOut", this.filtersOut.serializeNBT());
if (inventoryFace != null) {
result.putString("inventoryFace", inventoryFace.toString());
}
// result.putBoolean("needsRedstone", this.needsRedstone());
return result;
}

@Override
public void deserializeNBT(CompoundTag nbt) {
priority = nbt.getInt("prio");
CompoundTag upgrades = nbt.getCompound("upgrades");
if (upgrades != null) {
this.upgrades.deserializeNBT(upgrades);
}
CompoundTag filters = nbt.getCompound("filters");
if (filters != null) {
this.filters.deserializeNBT(filters);
}
if (nbt.contains("inventoryFace")) {
inventoryFace = Direction.byName(nbt.getString("inventoryFace"));
}
// this.needsRedstone(nbt.getBoolean("needsRedstone"));
}

@Override
public int getPriority() {
return priority;
}

@Override
public void setPriority(int value) {
this.priority = value;
}

@Override
public Direction facingInventory() {
return this.inventoryFace;
}

@Override
public boolean runNow(TileMain main) {
StorageNetworkMod.log("Run Now processing cable ");
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public FilterItemStackHandler() {
super(FILTER_SIZE);
}

public FilterItemStackHandler(int size) {
super(size);
}

public void setMatchOreDict(boolean ores) {
this.tags = ores;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ public void onHit(PlayerInteractEvent.LeftClickBlock event) {

Level level = event.getLevel();
if (!level.isClientSide) {
return;
return; // dont save client data; server side only from here on
}
Player player = event.getEntity();
if (!player.isCrouching()) {
return; // match with 1.12 pr, only put facades when crouching
}
ItemStack held = player.getItemInHand(event.getHand());
TileCable cable = TileCable.getTileCable(level, event.getPos());
if (cable != null) {
Expand Down
Loading

0 comments on commit e3fccc4

Please sign in to comment.