Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype Throttle #1636

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
Expand Up @@ -26,4 +26,6 @@ public class GregtechCapabilities {
@CapabilityInject(IFuelable.class)
public static Capability<IFuelable> CAPABILITY_FUELABLE = null;

@CapabilityInject(IThrottleable.class)
public static Capability<IThrottleable> CAPABILITY_THROTTLEABLE = null;
}
24 changes: 24 additions & 0 deletions src/main/java/gregtech/api/capability/IThrottle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package gregtech.api.capability;

public interface IThrottle {

public static final IThrottle FULL_THROTTLE = constantThrottle(100);

public static IThrottle constantThrottle(final int percentage) {
return () -> percentage;
}

int getThrottlePercentage();

default double calculateConsumptionMultiplier() {
return ((double) getThrottlePercentage()) / 100;
}

default double calculateDurationMultiplier() {
return ((double) getThrottlePercentage()) / 100;
}

default double calculateOutputVoltageMultiplier() {
return ((double) getThrottlePercentage()) / 100;
}
}
11 changes: 11 additions & 0 deletions src/main/java/gregtech/api/capability/IThrottleable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gregtech.api.capability;

public interface IThrottleable {

IThrottle getThrottle();

void setThrottle(IThrottle throttle);

// Metrics
long getRecipeOutputVoltage();
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static void init() {
registerCapabilityWithNoDefault(IControllable.class);
registerCapabilityWithNoDefault(IActiveOutputSide.class);
registerCapabilityWithNoDefault(IFuelable.class);
registerCapabilityWithNoDefault(IThrottleable.class);

registerCapabilityWithNoDefault(IWrenchItem.class);
registerCapabilityWithNoDefault(IScrewdriverItem.class);
Expand Down
27 changes: 21 additions & 6 deletions src/main/java/gregtech/api/capability/impl/FuelRecipeLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.LinkedHashMap;
import java.util.function.Supplier;

public class FuelRecipeLogic extends MTETrait implements IControllable, IFuelable {
public class FuelRecipeLogic extends MTETrait implements IControllable, IFuelable, IThrottleable {

public final FuelRecipeMap recipeMap;
protected FuelRecipe previousRecipe;
Expand All @@ -35,6 +35,8 @@ public class FuelRecipeLogic extends MTETrait implements IControllable, IFuelabl
private boolean workingEnabled = true;
private boolean wasActiveAndNeedsUpdate = false;

private IThrottle throttle = IThrottle.FULL_THROTTLE;

public FuelRecipeLogic(MetaTileEntity metaTileEntity, FuelRecipeMap recipeMap, Supplier<IEnergyContainer> energyContainer, Supplier<IMultipleTankHandler> fluidTank, long maxVoltage) {
super(metaTileEntity);
this.recipeMap = recipeMap;
Expand All @@ -43,6 +45,16 @@ public FuelRecipeLogic(MetaTileEntity metaTileEntity, FuelRecipeMap recipeMap, S
this.maxVoltage = maxVoltage;
}

@Override
public IThrottle getThrottle() {
return this.throttle;
}

@Override
public void setThrottle(final IThrottle throttle) {
this.throttle = throttle;
}

public long getRecipeOutputVoltage() {
return recipeOutputVoltage;
}
Expand Down Expand Up @@ -80,8 +92,8 @@ public Collection<IFuelInfo> getFuels() {
FuelRecipe recipe = findRecipe(tankContents);
if (recipe == null)
continue;
int amountPerRecipe = calculateFuelAmount(recipe);
int duration = calculateRecipeDuration(recipe);
int amountPerRecipe = (int) (calculateFuelAmount(recipe) * this.throttle.calculateConsumptionMultiplier());
int duration = (int) (calculateRecipeDuration(recipe) * this.throttle.calculateDurationMultiplier());
int fuelBurnTime = duration * fuelRemaining / amountPerRecipe;

FluidFuelInfo fuelInfo = (FluidFuelInfo) fuels.get(tankContents.getUnlocalizedName());
Expand All @@ -105,6 +117,9 @@ public <T> T getCapability(Capability<T> capability) {
if(capability == GregtechCapabilities.CAPABILITY_FUELABLE) {
return GregtechCapabilities.CAPABILITY_FUELABLE.cast(this);
}
if (capability == GregtechCapabilities.CAPABILITY_THROTTLEABLE) {
return GregtechCapabilities.CAPABILITY_THROTTLEABLE.cast(this);
}
return null;
}

Expand Down Expand Up @@ -171,10 +186,10 @@ private int tryAcquireNewRecipe(FluidStack fluidStack) {
}
}
if (currentRecipe != null && checkRecipe(currentRecipe)) {
int fuelAmountToUse = calculateFuelAmount(currentRecipe);
int fuelAmountToUse = (int) (calculateFuelAmount(currentRecipe) * this.throttle.calculateConsumptionMultiplier());
if (fluidStack.amount >= fuelAmountToUse) {
this.recipeDurationLeft = calculateRecipeDuration(currentRecipe);
this.recipeOutputVoltage = startRecipe(currentRecipe, fuelAmountToUse, recipeDurationLeft);
this.recipeDurationLeft = (int) (calculateRecipeDuration(currentRecipe) * this.throttle.calculateDurationMultiplier());
this.recipeOutputVoltage = (long) (startRecipe(currentRecipe, fuelAmountToUse, this.recipeDurationLeft) * this.throttle.calculateOutputVoltageMultiplier());
if (wasActiveAndNeedsUpdate) {
this.wasActiveAndNeedsUpdate = false;
} else {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/gregtech/common/covers/CoverBehaviors.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static void init() {
registerBehavior(37, new ResourceLocation(GTValues.MODID, "machine_controller"), MetaItems.COVER_MACHINE_CONTROLLER, CoverMachineController::new);
registerBehavior(38, new ResourceLocation(GTValues.MODID, "smart_filter"), MetaItems.SMART_FILTER, (tile, side) -> new CoverItemFilter(tile, side, "cover.smart_item_filter.title", Textures.SMART_FILTER_FILTER_OVERLAY, new SmartItemFilter()));
registerBehavior(39, new ResourceLocation(GTValues.MODID, "facade"), MetaItems.COVER_FACADE, CoverFacade::new);
registerBehavior(40, new ResourceLocation(GTValues.MODID, "throttle"), MetaItems.COVER_THROTTLE, CoverThrottle::new);
}

public static void registerBehavior(int coverNetworkId, ResourceLocation coverId, MetaValueItem placerItem, BiFunction<ICoverable, EnumFacing, CoverBehavior> behaviorCreator) {
Expand Down
139 changes: 139 additions & 0 deletions src/main/java/gregtech/common/covers/CoverThrottle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package gregtech.common.covers;

import codechicken.lib.raytracer.CuboidRayTraceResult;
import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.vec.Cuboid6;
import codechicken.lib.vec.Matrix4;
import gregtech.api.capability.GregtechCapabilities;
import gregtech.api.capability.IThrottle;
import gregtech.api.capability.IThrottleable;
import gregtech.api.cover.CoverBehavior;
import gregtech.api.cover.CoverWithUI;
import gregtech.api.cover.ICoverable;
import gregtech.api.gui.GuiTextures;
import gregtech.api.gui.ModularUI;
import gregtech.api.gui.widgets.ClickButtonWidget;
import gregtech.api.gui.widgets.ImageWidget;
import gregtech.api.gui.widgets.SimpleTextWidget;
import gregtech.api.render.Textures;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.MathHelper;

public class CoverThrottle extends CoverBehavior implements CoverWithUI, IThrottle, ITickable {

private int throttlePercentage;

public CoverThrottle(final ICoverable coverHolder, final EnumFacing attachedSide) {
super(coverHolder, attachedSide);
this.throttlePercentage = 100;
}

@Override
public int getThrottlePercentage() {
return this.throttlePercentage;
}

public void setThrottlePercentage(final int throttlePercentage) {
this.throttlePercentage = MathHelper.clamp(throttlePercentage, 20, 100);
this.coverHolder.markDirty();
}

public void adjustThrottle(final int change) {
setThrottlePercentage(this.throttlePercentage + change);
}

@Override
public boolean canAttach() {
return getThrottleable() != null;
}

@Override
public void onAttached(final ItemStack itemStack) {
super.onAttached(itemStack);
addThrottle();
}

@Override
public void onRemoved() {
super.onRemoved();
removeThrottle();
}

private IThrottleable getThrottleable() {
return this.coverHolder.getCapability(GregtechCapabilities.CAPABILITY_THROTTLEABLE, null);
}

private void addThrottle() {
IThrottleable throttleable = getThrottleable();
if (throttleable != null) {
throttleable.setThrottle(this);
}
}

private void removeThrottle() {
IThrottleable throttleable = getThrottleable();
if (throttleable != null) {
throttleable.setThrottle(IThrottle.FULL_THROTTLE);
}
}

@Override
public void update() {
if (this.coverHolder.getTimer() == 0) {
addThrottle();
}
}

private long getOutputVoltage() {
IThrottleable throttleable = getThrottleable();
return throttleable != null ? throttleable.getRecipeOutputVoltage() : 0;
}

@Override
public void renderCover(final CCRenderState renderState, final Matrix4 translation, final IVertexOperation[] pipeline, final Cuboid6 plateBox, final BlockRenderLayer layer) {
Textures.MACHINE_CONTROLLER_OVERLAY.renderSided(this.attachedSide, plateBox, renderState, pipeline, translation);
}

@Override
public EnumActionResult onScrewdriverClick(final EntityPlayer playerIn, final EnumHand hand, final CuboidRayTraceResult hitResult) {
if (!this.coverHolder.getWorld().isRemote) {
openUI((EntityPlayerMP) playerIn);
}
return EnumActionResult.SUCCESS;
}

@Override
public ModularUI createUI(EntityPlayer player) {
return ModularUI.defaultBuilder()
.label(10, 5, "cover.machine_controller.name")
.widget(new ClickButtonWidget(10, 20, 20, 20, "-10", data -> adjustThrottle(data.isShiftClick ? -100 : -10)))
.widget(new ClickButtonWidget(146, 20, 20, 20, "+10", data -> adjustThrottle(data.isShiftClick ? +100 : +10)))
.widget(new ClickButtonWidget(30, 20, 20, 20, "-1", data -> adjustThrottle(data.isShiftClick ? -5 : -1)))
.widget(new ClickButtonWidget(126, 20, 20, 20, "+1", data -> adjustThrottle(data.isShiftClick ? +5 : +1)))
.widget(new ImageWidget(50, 20, 76, 20, GuiTextures.DISPLAY))
.widget(new SimpleTextWidget(88, 30, "cover.conveyor.transfer_rate", 0xFFFFFF, () -> Integer.toString(getThrottlePercentage())))
.widget(new SimpleTextWidget(88, 60, "cover.conveyor.transfer_rate", 0xFFFFFF, () -> Long.toString(getOutputVoltage())))
.build(this, player);
}

@Override
public void writeToNBT(NBTTagCompound tagCompound) {
super.writeToNBT(tagCompound);
tagCompound.setInteger("ThrottlePercentage", this.throttlePercentage);
}

@Override
public void readFromNBT(NBTTagCompound tagCompound) {
super.readFromNBT(tagCompound);
this.throttlePercentage = tagCompound.getInteger("ThrottlePercentage");
}
}
1 change: 1 addition & 0 deletions src/main/java/gregtech/common/items/MetaItem1.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ public void registerSubItems() {
SMART_FILTER = addItem(103, "smart_item_filter");

COVER_MACHINE_CONTROLLER = addItem(730, "cover.controller");
COVER_THROTTLE = addItem(735, "cover.throttle");

COVER_ACTIVITY_DETECTOR = addItem(731, "cover.activity.detector").setInvisible();
COVER_FLUID_DETECTOR = addItem(732, "cover.fluid.detector").setInvisible();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/gregtech/common/items/MetaItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ private MetaItems() {
public static MetaItem<?>.MetaValueItem COVER_SHUTTER;
public static MetaItem<?>.MetaValueItem COVER_MACHINE_CONTROLLER;
public static MetaItem<?>.MetaValueItem COVER_FACADE;
public static MetaItem<?>.MetaValueItem COVER_THROTTLE;

public static MetaItem<?>.MetaValueItem COVER_ACTIVITY_DETECTOR;
public static MetaItem<?>.MetaValueItem COVER_FLUID_DETECTOR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,44 @@
import codechicken.lib.vec.Matrix4;
import gregtech.api.capability.IEnergyContainer;
import gregtech.api.capability.IMultipleTankHandler;
import gregtech.api.capability.IThrottle;
import gregtech.api.capability.impl.EnergyContainerList;
import gregtech.api.capability.impl.FluidTankList;
import gregtech.api.capability.impl.FuelRecipeLogic;
import gregtech.api.gui.Widget.ClickData;
import gregtech.api.metatileentity.MTETrait;
import gregtech.api.metatileentity.multiblock.IMultiblockPart;
import gregtech.api.metatileentity.multiblock.MultiblockAbility;
import gregtech.api.metatileentity.multiblock.MultiblockWithDisplayBase;
import gregtech.api.multiblock.PatternMatchContext;
import gregtech.api.recipes.machines.FuelRecipeMap;
import gregtech.api.render.Textures;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;

import static gregtech.api.gui.widgets.AdvancedTextWidget.withButton;
import static gregtech.api.gui.widgets.AdvancedTextWidget.withHoverTextTranslate;

import java.util.List;
import java.util.Map;

public abstract class FueledMultiblockController extends MultiblockWithDisplayBase {
public abstract class FueledMultiblockController extends MultiblockWithDisplayBase implements IThrottle {

protected final FuelRecipeMap recipeMap;
protected FuelRecipeLogic workableHandler;
protected IEnergyContainer energyContainer;
protected IMultipleTankHandler importFluidHandler;
protected int throttlePercentage = 100;

public FueledMultiblockController(ResourceLocation metaTileEntityId, FuelRecipeMap recipeMap, long maxVoltage) {
super(metaTileEntityId);
this.recipeMap = recipeMap;
this.workableHandler = createWorkable(maxVoltage);
this.workableHandler.setThrottle(this);
}

protected FuelRecipeLogic createWorkable(long maxVoltage) {
Expand All @@ -41,21 +51,45 @@ protected FuelRecipeLogic createWorkable(long maxVoltage) {
() -> importFluidHandler, maxVoltage);
}

@Override
public int getThrottlePercentage() {
return this.throttlePercentage;
}

@Override
protected void addDisplayText(List<ITextComponent> textList) {
super.addDisplayText(textList);
if (isStructureFormed()) {
ITextComponent throttleText = new TextComponentTranslation("gregtech.multiblock.large_boiler.throttle", this.throttlePercentage, 100);
withHoverTextTranslate(throttleText, "gregtech.multiblock.large_boiler.throttle.tooltip");
textList.add(throttleText);
ITextComponent buttonText = new TextComponentTranslation("gregtech.multiblock.large_boiler.throttle_modify");
buttonText.appendText(" ");
buttonText.appendSibling(withButton(new TextComponentString("[-]"), "sub"));
buttonText.appendText(" ");
buttonText.appendSibling(withButton(new TextComponentString("[+]"), "add"));
textList.add(buttonText);
if (!workableHandler.isWorkingEnabled()) {
textList.add(new TextComponentTranslation("gregtech.multiblock.work_paused"));
} else if (workableHandler.isActive()) {
textList.add(new TextComponentTranslation("gregtech.multiblock.running"));
textList.add(new TextComponentTranslation("gregtech.multiblock.generation_eu", workableHandler.getRecipeOutputVoltage()));
} else {
textList.add(new TextComponentTranslation("gregtech.multiblock.idling"));
textList.add(new TextComponentTranslation("gregtech.multiblock.generation_eu", this.workableHandler.getRecipeOutputVoltage()));
}
}
}

@Override
protected void handleDisplayClick(String componentData, ClickData clickData) {
super.handleDisplayClick(componentData, clickData);
int modifier = componentData.equals("add") ? 1 : -1;
int result = (clickData.isShiftClick ? 1 : 5) * modifier;
this.throttlePercentage = MathHelper.clamp(this.throttlePercentage + result, 20, 100);
markDirty();
}

@Override
protected void formStructure(PatternMatchContext context) {
super.formStructure(context);
Expand Down Expand Up @@ -101,4 +135,19 @@ public void renderMetaTileEntity(CCRenderState renderState, Matrix4 translation,
Textures.MULTIBLOCK_WORKABLE_OVERLAY.render(renderState, translation, pipeline, getFrontFacing(),
isStructureFormed() && workableHandler.isActive());
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound data) {
super.writeToNBT(data);
data.setInteger("ThrottlePercentage", this.throttlePercentage);
return data;
}

@Override
public void readFromNBT(NBTTagCompound data) {
super.readFromNBT(data);
if(data.hasKey("ThrottlePercentage")) {
this.throttlePercentage = data.getInteger("ThrottlePercentage");
}
}
}
Loading