diff --git a/src/main/java/gregtech/api/capability/GregtechCapabilities.java b/src/main/java/gregtech/api/capability/GregtechCapabilities.java index 7e9d215470..faa42fbf83 100644 --- a/src/main/java/gregtech/api/capability/GregtechCapabilities.java +++ b/src/main/java/gregtech/api/capability/GregtechCapabilities.java @@ -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; } diff --git a/src/main/java/gregtech/api/capability/IThrottle.java b/src/main/java/gregtech/api/capability/IThrottle.java new file mode 100755 index 0000000000..e29de62388 --- /dev/null +++ b/src/main/java/gregtech/api/capability/IThrottle.java @@ -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; + } +} diff --git a/src/main/java/gregtech/api/capability/IThrottleable.java b/src/main/java/gregtech/api/capability/IThrottleable.java new file mode 100755 index 0000000000..934cc069f6 --- /dev/null +++ b/src/main/java/gregtech/api/capability/IThrottleable.java @@ -0,0 +1,11 @@ +package gregtech.api.capability; + +public interface IThrottleable { + + IThrottle getThrottle(); + + void setThrottle(IThrottle throttle); + + // Metrics + long getRecipeOutputVoltage(); +} diff --git a/src/main/java/gregtech/api/capability/SimpleCapabilityManager.java b/src/main/java/gregtech/api/capability/SimpleCapabilityManager.java index 42c545c200..699c929855 100644 --- a/src/main/java/gregtech/api/capability/SimpleCapabilityManager.java +++ b/src/main/java/gregtech/api/capability/SimpleCapabilityManager.java @@ -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); diff --git a/src/main/java/gregtech/api/capability/impl/FuelRecipeLogic.java b/src/main/java/gregtech/api/capability/impl/FuelRecipeLogic.java index 8da167c8b9..eb41005ddd 100755 --- a/src/main/java/gregtech/api/capability/impl/FuelRecipeLogic.java +++ b/src/main/java/gregtech/api/capability/impl/FuelRecipeLogic.java @@ -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; @@ -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; @@ -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; } @@ -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()); @@ -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; } @@ -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 { diff --git a/src/main/java/gregtech/common/covers/CoverBehaviors.java b/src/main/java/gregtech/common/covers/CoverBehaviors.java index 58b4b04a50..c533245441 100644 --- a/src/main/java/gregtech/common/covers/CoverBehaviors.java +++ b/src/main/java/gregtech/common/covers/CoverBehaviors.java @@ -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) { diff --git a/src/main/java/gregtech/common/covers/CoverThrottle.java b/src/main/java/gregtech/common/covers/CoverThrottle.java new file mode 100755 index 0000000000..e8228b6dd1 --- /dev/null +++ b/src/main/java/gregtech/common/covers/CoverThrottle.java @@ -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"); + } +} diff --git a/src/main/java/gregtech/common/items/MetaItem1.java b/src/main/java/gregtech/common/items/MetaItem1.java index bb683e7e55..999464e537 100644 --- a/src/main/java/gregtech/common/items/MetaItem1.java +++ b/src/main/java/gregtech/common/items/MetaItem1.java @@ -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(); diff --git a/src/main/java/gregtech/common/items/MetaItems.java b/src/main/java/gregtech/common/items/MetaItems.java index 7dc01a3865..7189a67909 100644 --- a/src/main/java/gregtech/common/items/MetaItems.java +++ b/src/main/java/gregtech/common/items/MetaItems.java @@ -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; diff --git a/src/main/java/gregtech/common/metatileentities/multi/electric/generator/FueledMultiblockController.java b/src/main/java/gregtech/common/metatileentities/multi/electric/generator/FueledMultiblockController.java index a10a64767d..34424af4e0 100644 --- a/src/main/java/gregtech/common/metatileentities/multi/electric/generator/FueledMultiblockController.java +++ b/src/main/java/gregtech/common/metatileentities/multi/electric/generator/FueledMultiblockController.java @@ -5,9 +5,11 @@ 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; @@ -15,24 +17,32 @@ 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) { @@ -41,10 +51,24 @@ 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()) { @@ -52,10 +76,20 @@ protected void addDisplayText(List<ITextComponent> textList) { 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); @@ -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"); + } + } } diff --git a/src/main/resources/assets/gregtech/models/item/metaitems/cover.throttle.json b/src/main/resources/assets/gregtech/models/item/metaitems/cover.throttle.json new file mode 100755 index 0000000000..d95fd60ffe --- /dev/null +++ b/src/main/resources/assets/gregtech/models/item/metaitems/cover.throttle.json @@ -0,0 +1,6 @@ +{ + "parent": "item/generated", + "textures": { + "layer0": "gregtech:items/metaitems/cover.controller" + } +}