Skip to content

Commit

Permalink
Implement Quantum Storage Controller Network (#1963)
Browse files Browse the repository at this point in the history
Co-authored-by: Serenibyss <dane@strandboge.com>
  • Loading branch information
ghzdude and serenibyss authored Oct 26, 2024
1 parent 067d32f commit ebc0338
Show file tree
Hide file tree
Showing 47 changed files with 1,620 additions and 54 deletions.
5 changes: 5 additions & 0 deletions src/main/java/gregtech/api/capability/GregtechDataCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ public static int assignId() {
public static final int UPDATE_ITEM_COUNT = assignId();
public static final int UPDATE_FLUID_AMOUNT = assignId();

// Quantum Storage Controller
public static final int UPDATE_CONTROLLER_POS = assignId();
public static final int REMOVE_CONTROLLER = assignId();
public static final int LOCATE_CONTROLLER = assignId();

// Detector Covers
public static final int UPDATE_INVERTED = assignId();

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/gregtech/api/capability/IDualHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gregtech.api.capability;

import net.minecraftforge.items.IItemHandler;

public interface IDualHandler {

boolean hasFluidTanks();

boolean hasItemHandlers();

IMultipleTankHandler getFluidTanks();

IItemHandler getItemHandlers();
}
34 changes: 34 additions & 0 deletions src/main/java/gregtech/api/capability/IQuantumController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package gregtech.api.capability;

import net.minecraft.util.math.BlockPos;
import net.minecraftforge.common.capabilities.ICapabilityProvider;

// ICapabilityProvider is needed because getCapability is called in the quantum proxy against this interface
public interface IQuantumController extends ICapabilityProvider {

/**
* Constructs the network upon placement and when storages are added/removed
* <br />
*/
void rebuildNetwork();

/**
* Return whether this storage block can connect. Can be used to implement a maximum distance from controller for
* example.
*/
boolean canConnect(IQuantumStorage<?> storage);

BlockPos getPos();

IDualHandler getHandler();

boolean isPowered();

long getEnergyUsage();

int getCount(IQuantumStorage.Type type);

long getTypeEnergy(IQuantumStorage<?> storage);

void updateHandler();
}
77 changes: 77 additions & 0 deletions src/main/java/gregtech/api/capability/IQuantumStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package gregtech.api.capability;

import gregtech.api.cover.CoverableView;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;

import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;

import org.jetbrains.annotations.Nullable;

public interface IQuantumStorage<T> extends CoverableView {

Type getType();

void setConnected(IQuantumController controller);

void setDisconnected();

BlockPos getControllerPos();

@Nullable
IQuantumController getQuantumController();

BlockPos getPos();

default boolean isConnected() {
// use controllerPos here because it is synced
// on both sides, where controller is not
return getControllerPos() != null;
}

default void tryFindNetwork() {
for (EnumFacing facing : EnumFacing.VALUES) {
var offset = getPos().offset(facing);
var state = getWorld().getBlockState(offset);
if (state.getBlock().isAir(state, getWorld(), offset)) continue;
MetaTileEntity mte;
if (getNeighbor(facing) instanceof IGregTechTileEntity gtte) {
mte = gtte.getMetaTileEntity();
} else {
continue;
}

IQuantumController candidate = null;
if (mte instanceof IQuantumStorage<?>storage) {
if (storage.isConnected()) {
IQuantumController controller = storage.getQuantumController();
if (controller != null && controller.canConnect(this)) {
candidate = controller;
}
}
} else if (mte instanceof IQuantumController quantumController) {
if (quantumController.canConnect(this)) {
candidate = quantumController;
}
}
if (candidate != null) {
candidate.rebuildNetwork();
return;
}
}
}

T getTypeValue();

enum Type {

ITEM,
FLUID,
EXTENDER,
PROXY,
ENERGY;

public static final Type[] VALUES = values();
}
}
2 changes: 0 additions & 2 deletions src/main/java/gregtech/api/gui/widgets/ClickButtonWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;

import com.google.common.base.Preconditions;
import org.lwjgl.input.Mouse;

import java.util.Arrays;
Expand Down Expand Up @@ -64,7 +63,6 @@ public ClickButtonWidget setDisplayFunction(Supplier<Boolean> displayFunction) {
}

public ClickButtonWidget setTooltipText(String tooltipText, Object... args) {
Preconditions.checkNotNull(tooltipText, "tooltipText");
this.tooltipText = tooltipText;
this.tooltipArgs = args;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,33 @@
public class BlockPosHighlightRenderer {

private static BlockPos posHighLight;
private static long hlEndTime;
private static long duration;
private static long offset;
private static long start;

public static void renderBlockBoxHighLight(BlockPos blockpos, long durTimeMillis) {
posHighLight = blockpos;
hlEndTime = System.currentTimeMillis() + durTimeMillis;
duration = durTimeMillis;
offset = 1500;
start = System.currentTimeMillis();
}

public static void renderBlockBoxHighLight(BlockPos blockpos, long durTimeMillis, long offsetTimeMillis) {
posHighLight = blockpos;
duration = durTimeMillis;
offset = offsetTimeMillis;
start = System.currentTimeMillis();
}

public static void renderWorldLastEvent(RenderWorldLastEvent evt) {
if (posHighLight != null) {
long time = System.currentTimeMillis();
if (time > hlEndTime) {
if (time > duration + start) {
posHighLight = null;
hlEndTime = 0;
duration = 0;
return;
}
if (((time / 500) & 1) == 0) {
if (time % offset >= offset / 2) {
return;
}
EntityPlayerSP p = Minecraft.getMinecraft().player;
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/gregtech/client/renderer/texture/Textures.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@ public class Textures {
"casings/pipe/machine_casing_grate");
public static final SimpleOverlayRenderer HIGH_POWER_CASING = new SimpleOverlayRenderer(
"casings/computer/high_power_casing");
public static final SimpleOverlayRenderer QUANTUM_CASING = new SimpleOverlayRenderer(
"casings/quantum/quantum_casing");
public static final SimpleOverlayRenderer QUANTUM_CONTROLLER_FRONT_INACTIVE = new SimpleOverlayRenderer(
"casings/quantum/controller_front_inactive");
public static final SimpleOverlayRenderer QUANTUM_CONTROLLER_FRONT_ACTIVE = new SimpleOverlayRenderer(
"casings/quantum/controller_front_active");
public static final SimpleOverlayRenderer QUANTUM_CONTROLLER_ACTIVE = new SimpleOverlayRenderer(
"casings/quantum/controller_active");
public static final SimpleOverlayRenderer QUANTUM_CONTROLLER_INACTIVE = new SimpleOverlayRenderer(
"casings/quantum/controller_inactive");
public static final SimpleOverlayRenderer QUANTUM_PROXY_INACTIVE = new SimpleOverlayRenderer(
"casings/quantum/proxy_inactive");
public static final SimpleOverlayRenderer QUANTUM_PROXY_ACTIVE = new SimpleOverlayRenderer(
"casings/quantum/proxy_active");
public static final SimpleOverlayRenderer QUANTUM_EXTENDER = new SimpleOverlayRenderer("casings/quantum/extender");
public static final SimpleOverlayRenderer QUANTUM_EXTENDER_ACTIVE = new SimpleOverlayRenderer(
"casings/quantum/extender_active");

public static final SimpleOverlayRenderer QUANTUM_INDICATOR = new SimpleOverlayRenderer(
"casings/quantum/quantum_indicator_disconnected");

public static final SimpleOverlayRenderer QUANTUM_INDICATOR_CONNECTED = new SimpleOverlayRenderer(
"casings/quantum/quantum_indicator_connected");

public static final SimpleOverlayRenderer QUANTUM_INDICATOR_POWERED = new SimpleOverlayRenderer(
"casings/quantum/quantum_indicator_powered");

// Simple Sided Cube Renderers
public static final SimpleSidedCubeRenderer STEAM_CASING_BRONZE = new SimpleSidedCubeRenderer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import gregtech.api.gui.resources.TextTexture;
import gregtech.api.metatileentity.ITieredMetaTileEntity;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.util.TextFormattingUtil;
import gregtech.client.renderer.texture.Textures;
import gregtech.client.renderer.texture.cube.SimpleSidedCubeRenderer.RenderSide;
import gregtech.client.utils.RenderUtil;
import gregtech.common.ConfigHolder;
import gregtech.common.metatileentities.storage.MetaTileEntityQuantumChest;
import gregtech.common.metatileentities.storage.MetaTileEntityQuantumStorage;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
Expand Down Expand Up @@ -69,10 +69,10 @@ public void registerIcons(TextureMap textureMap) {
.registerSprite(new ResourceLocation("gregtech:blocks/overlay/machine/overlay_screen_glass"));
}

public <T extends MetaTileEntity & ITieredMetaTileEntity> void renderMachine(CCRenderState renderState,
Matrix4 translation,
IVertexOperation[] pipeline,
T mte) {
public <T extends MetaTileEntityQuantumStorage<?> & ITieredMetaTileEntity> void renderMachine(CCRenderState renderState,
Matrix4 translation,
IVertexOperation[] pipeline,
T mte) {
EnumFacing frontFacing = mte.getFrontFacing();
int tier = mte.getTier();
Textures.renderFace(renderState, translation, pipeline, frontFacing, glassBox, glassTexture,
Expand All @@ -81,6 +81,10 @@ public <T extends MetaTileEntity & ITieredMetaTileEntity> void renderMachine(CCR
TextureAtlasSprite hullTexture = Textures.VOLTAGE_CASINGS[tier]
.getSpriteOnSide(RenderSide.bySide(EnumFacing.NORTH));

if (mte.isConnected()) {
hullTexture = Textures.QUANTUM_CASING.getParticleSprite();
}

for (var facing : boxFacingMap.keySet()) {
// do not render the box at the front face when "facing" is "frontFacing"
if (facing == frontFacing) continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IElectricItem;
import gregtech.api.capability.IEnergyContainer;
import gregtech.api.capability.IQuantumController;
import gregtech.api.capability.IQuantumStorage;
import gregtech.api.capability.IWorkable;
import gregtech.api.capability.impl.FluidTankList;
import gregtech.api.items.metaitem.stats.IItemBehaviour;
Expand Down Expand Up @@ -284,6 +286,30 @@ else if (metaTileEntity instanceof IDataInfoProvider)
list.addAll(provider.getDataInfo());
}

// quantum storage
if (metaTileEntity instanceof IQuantumController quantumController) {
list.add(new TextComponentTranslation("behavior.tricorder.divider"));
long eut = quantumController.getEnergyUsage(); // eu per 10 ticks
int tier = GTUtility.getTierByVoltage(eut / 10);
list.add(new TextComponentTranslation("behavior.tricorder.quantum_controller.usage",
TextFormatting.RED + String.format("%.1f", eut / 10d) + TextFormatting.RESET,
GTValues.VNF[tier]));
var handler = quantumController.getHandler();
list.add(new TextComponentTranslation("behavior.tricorder.quantum_controller.connected_items",
TextFormatting.RED.toString() + handler.getItemHandlers().getSlots()));
list.add(new TextComponentTranslation("behavior.tricorder.quantum_controller.connected_fluids",
TextFormatting.RED.toString() + handler.getFluidTanks().getTanks()));
} else if (metaTileEntity instanceof IQuantumStorage<?>storage) {
var qcontrollor = storage.getQuantumController();
if (qcontrollor != null) {
long eut = qcontrollor.getTypeEnergy(storage);

list.add(new TextComponentTranslation("behavior.tricorder.divider"));
list.add(new TextComponentTranslation("behavior.tricorder.quantum_storage.usage",
TextFormatting.RED + String.format("%.1f", eut / 10d)));
}
}

} else if (tileEntity instanceof IPipeTile) {
// pipes need special name handling
IPipeTile<?, ?> pipeTile = (IPipeTile<?, ?>) tileEntity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@
import gregtech.common.metatileentities.storage.MetaTileEntityCreativeTank;
import gregtech.common.metatileentities.storage.MetaTileEntityDrum;
import gregtech.common.metatileentities.storage.MetaTileEntityQuantumChest;
import gregtech.common.metatileentities.storage.MetaTileEntityQuantumExtender;
import gregtech.common.metatileentities.storage.MetaTileEntityQuantumProxy;
import gregtech.common.metatileentities.storage.MetaTileEntityQuantumStorageController;
import gregtech.common.metatileentities.storage.MetaTileEntityQuantumTank;
import gregtech.common.metatileentities.storage.MetaTileEntityWorkbench;
import gregtech.common.pipelike.fluidpipe.longdistance.MetaTileEntityLDFluidEndpoint;
Expand Down Expand Up @@ -222,6 +225,9 @@ public class MetaTileEntities {
public static final MetaTileEntityRotorHolder[] ROTOR_HOLDER = new MetaTileEntityRotorHolder[6]; // HV, EV, IV, LuV, ZPM, UV
public static final MetaTileEntityMufflerHatch[] MUFFLER_HATCH = new MetaTileEntityMufflerHatch[GTValues.UV + 1]; // LV-UV
public static final MetaTileEntityFusionReactor[] FUSION_REACTOR = new MetaTileEntityFusionReactor[3];
public static MetaTileEntityQuantumStorageController QUANTUM_STORAGE_CONTROLLER;
public static MetaTileEntityQuantumProxy QUANTUM_STORAGE_PROXY;
public static MetaTileEntityQuantumExtender QUANTUM_STORAGE_EXTENDER;
public static final MetaTileEntityQuantumChest[] QUANTUM_CHEST = new MetaTileEntityQuantumChest[10];
public static final MetaTileEntityQuantumTank[] QUANTUM_TANK = new MetaTileEntityQuantumTank[10];
public static final MetaTileEntityBuffer[] BUFFER = new MetaTileEntityBuffer[3];
Expand Down Expand Up @@ -977,6 +983,14 @@ public static void init() {
PUMP[2] = registerMetaTileEntity(1532, new MetaTileEntityPump(gregtechId("pump.hv"), 3));
PUMP[3] = registerMetaTileEntity(1533, new MetaTileEntityPump(gregtechId("pump.ev"), 4));

// Quantum Storage Network 1757 - 1759
QUANTUM_STORAGE_CONTROLLER = registerMetaTileEntity(1757,
new MetaTileEntityQuantumStorageController(gregtechId("quantum_storage_controller")));
QUANTUM_STORAGE_PROXY = registerMetaTileEntity(1758,
new MetaTileEntityQuantumProxy(gregtechId("quantum_storage_proxy")));
QUANTUM_STORAGE_EXTENDER = registerMetaTileEntity(1759,
new MetaTileEntityQuantumExtender(gregtechId("quantum_storage_extender")));

// Super / Quantum Chests, IDs 1560-1574
for (int i = 0; i < 5; i++) {
String voltageName = GTValues.VN[i + 1].toLowerCase();
Expand Down
Loading

0 comments on commit ebc0338

Please sign in to comment.