Skip to content

Commit

Permalink
Charger Machine (#158)
Browse files Browse the repository at this point in the history
* Add `getPlatformEnergyItem`

For retrieving items that stores native energy

* Add `ChargerMachine` that implements logic for charger

Almost a duplicate from battery buffer, follows same logic as 1.12.2

* Register `ChargerMachine` and its recipe

* Change Charger name to Turbo Charger

* Add texture for `ChargerMachine`

* Add rendering logic to `ChargerMachine`

Renders three states: idle, running, finished

* fix batterybuffer notification

---------

Co-authored-by: KilaBash <yefancy@foxmail.com>
  • Loading branch information
lukaslcf and Yefancy authored Jul 16, 2023
1 parent a0a6f3f commit 7d455b1
Show file tree
Hide file tree
Showing 20 changed files with 463 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public static IElectricItem getElectricItem(ItemStack itemStack) {
throw new AssertionError();
}

@ExpectPlatform
@Nullable
public static IPlatformEnergyStorage getPlatformEnergyItem(ItemStack itemStack) {
throw new AssertionError();
}

@ExpectPlatform
@Nullable
public static IEnergyContainer getEnergyContainer(Level level, BlockPos pos, @Nullable Direction side) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void onMachineLoad() {
checkOutputSubscription();
}

protected void checkOutputSubscription() {
public void checkOutputSubscription() {
if (getOutputVoltage() > 0 && getOutputAmperage() > 0) {
if (getEnergyStored() >= getOutputVoltage()) {
outputSubs = getMachine().subscribeServerTick(outputSubs, this::serverTick);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.gregtechceu.gtceu.client.renderer.machine;

import com.gregtechceu.gtceu.GTCEu;
import com.gregtechceu.gtceu.api.machine.MachineDefinition;
import com.gregtechceu.gtceu.api.machine.MetaMachine;
import com.gregtechceu.gtceu.common.machine.electric.ChargerMachine;
import com.gregtechceu.gtceu.config.ConfigHolder;
import com.lowdragmc.lowdraglib.client.bakedpipeline.FaceQuad;
import com.lowdragmc.lowdraglib.client.model.ModelFactory;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.client.resources.model.ModelState;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.RandomSource;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.function.Consumer;

/**
* @author lucifer_ll
* @date 2023/7/13
* @implNote ChargerRenderer
*/
public class ChargerRenderer extends TieredHullMachineRenderer{
public final static ResourceLocation CHARGER_IDLE = GTCEu.id("block/machines/charger/overlay_charger_idle");
public final static ResourceLocation CHARGER_RUNNING = GTCEu.id("block/machines/charger/overlay_charger_running");
public final static ResourceLocation CHARGER_RUNNING_EMISSIVE = GTCEu.id("block/machines/charger/overlay_charger_running_emissive");
public final static ResourceLocation CHARGER_FINISHED = GTCEu.id("block/machines/charger/overlay_charger_finished");
public final static ResourceLocation CHARGER_FINISHED_EMISSIVE = GTCEu.id("block/machines/charger/overlay_charger_finished_emissive");

public ChargerRenderer(int tier) {
super(tier, GTCEu.id("block/machine/hull_machine"));
}

@Override
@Environment(EnvType.CLIENT)
public void renderMachine(List<BakedQuad> quads, MachineDefinition definition, @Nullable MetaMachine machine, Direction frontFacing, @Nullable Direction side, RandomSource rand, Direction modelFacing, ModelState modelState) {
super.renderMachine(quads, definition, machine, frontFacing, side, rand, modelFacing, modelState);
var state = ChargerMachine.State.IDLE;
if (machine instanceof ChargerMachine charger) {
state = charger.getState();
}

if (side == frontFacing && modelFacing != null) {
var bakedFaces = new ArrayList<BakedQuad>();
switch (state) {
case IDLE -> bakedFaces.add(FaceQuad.bakeFace(modelFacing, ModelFactory.getBlockSprite(CHARGER_IDLE), modelState, -1, 0, false, true));
case RUNNING -> {
if (ConfigHolder.INSTANCE.client.machinesEmissiveTextures) {
bakedFaces.add(FaceQuad.bakeFace(modelFacing, ModelFactory.getBlockSprite(CHARGER_RUNNING), modelState, -1, 0, true, true));
bakedFaces.add(FaceQuad.bakeFace(modelFacing, ModelFactory.getBlockSprite(CHARGER_RUNNING_EMISSIVE), modelState, 0, 15, true, false));
} else {
bakedFaces.add(FaceQuad.bakeFace(modelFacing, ModelFactory.getBlockSprite(CHARGER_RUNNING), modelState, -1, 0, true, true));
}
}
case FINISHED -> {
if (ConfigHolder.INSTANCE.client.machinesEmissiveTextures) {
bakedFaces.add(FaceQuad.bakeFace(modelFacing, ModelFactory.getBlockSprite(CHARGER_FINISHED), modelState, -1, 0, true, true));
bakedFaces.add(FaceQuad.bakeFace(modelFacing, ModelFactory.getBlockSprite(CHARGER_FINISHED_EMISSIVE), modelState, 0, 15, true, false));
} else {
bakedFaces.add(FaceQuad.bakeFace(modelFacing, ModelFactory.getBlockSprite(CHARGER_FINISHED), modelState, -1, 0, true, true));
}
}
}
quads.addAll(bakedFaces);

}
}

@Override
@Environment(EnvType.CLIENT)
public void onPrepareTextureAtlas(ResourceLocation atlasName, Consumer<ResourceLocation> register) {
super.onPrepareTextureAtlas(atlasName, register);
if (atlasName.equals(TextureAtlas.LOCATION_BLOCKS)) {
register.accept(CHARGER_IDLE);
register.accept(CHARGER_RUNNING);
register.accept(CHARGER_RUNNING_EMISSIVE);
register.accept(CHARGER_FINISHED);
register.accept(CHARGER_FINISHED_EMISSIVE);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ public class GTMachines {

public final static MachineDefinition[] BATTERY_BUFFER_16 = registerBatteryBuffer(16);

public final static MachineDefinition[] CHARGER_4 = registerCharger(4);

public final static MachineDefinition[] PUMP = registerTieredMachines("pump", PumpMachine::new,
(tier, builder) -> builder
.rotationState(RotationState.NON_Y_AXIS)
Expand Down Expand Up @@ -1387,6 +1389,21 @@ public static MachineDefinition[] registerBatteryBuffer(int batterySlotSize){
ALL_TIERS);
}

public static MachineDefinition[] registerCharger(int itemSlotSize) {
return registerTieredMachines("charger_" + itemSlotSize + "x",
(holder, tier) -> new ChargerMachine(holder, tier, itemSlotSize),
(tier, builder) -> builder
.rotationState(RotationState.NON_Y_AXIS)
.renderer(() -> new ChargerRenderer(tier))
.langValue("%s %s%s".formatted(VOLTAGE_NAMES[tier], itemSlotSize, "x Turbo Charger"))
.tooltips(explosion())
.tooltips(Component.translatable("gtceu.universal.tooltip.item_storage_capacity", itemSlotSize),
Component.translatable("gtceu.universal.tooltip.voltage_in_out", GTValues.V[tier], GTValues.VNF[tier]),
Component.translatable("gtceu.universal.tooltip.amperage_in_till", itemSlotSize * ChargerMachine.AMPS_PER_ITEM))
.register(),
ALL_TIERS);
}

public static MultiblockMachineDefinition[] registerTieredMultis(String name,
BiFunction<IMachineBlockEntity, Integer, MultiblockControllerMachine> factory,
BiFunction<Integer, MultiblockMachineBuilder, MultiblockMachineDefinition> builder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.lowdragmc.lowdraglib.gui.widget.Widget;
import com.lowdragmc.lowdraglib.gui.widget.WidgetGroup;
import com.lowdragmc.lowdraglib.misc.ItemStackTransfer;
import com.lowdragmc.lowdraglib.syncdata.ISubscription;
import com.lowdragmc.lowdraglib.syncdata.annotation.Persisted;
import com.lowdragmc.lowdraglib.syncdata.field.ManagedFieldHolder;
import com.lowdragmc.lowdraglib.utils.Position;
Expand Down Expand Up @@ -51,6 +52,7 @@ public BatteryBufferMachine(IMachineBlockEntity holder, int tier, int inventoryS
this.isWorkingEnabled = true;
this.inventorySize = inventorySize;
this.batteryInventory = createBatteryInventory(args);
this.batteryInventory.setOnContentsChanged(energyContainer::checkOutputSubscription);
}

//////////////////////////////////////
Expand All @@ -72,11 +74,6 @@ protected ItemStackTransfer createBatteryInventory(Object... args) {
return itemTransfer;
}

@Override
public void onLoad() {
super.onLoad();
}

@Override
public int tintColor(int index) {
if (index == 2) {
Expand Down Expand Up @@ -186,7 +183,7 @@ public void serverTick() {

var voltage = getOutputVoltage();
var batteries = getNonEmptyBatteries();
if (batteries.size() > 0) {
if (!batteries.isEmpty()) {
//Prioritize as many packets as available of energy created
long internalAmps = Math.abs(Math.min(0, getInternalStorage() / voltage));
long genAmps = Math.max(0, batteries.size() - internalAmps);
Expand Down
Loading

0 comments on commit 7d455b1

Please sign in to comment.