diff options
author | NotAPenguin <michiel.vandeginste@gmail.com> | 2024-09-02 23:17:17 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-02 23:17:17 +0200 |
commit | 1b820de08a05070909a267e17f033fcf58ac8710 (patch) | |
tree | 02831a025986a06b20f87e5bcc69d1e0c639a342 /src/main/java/gregtech/common/covers/CoverFluidLimiter.java | |
parent | afd3fd92b6a6ab9ab0d0dc3214e6bc8ff7a86c9b (diff) | |
download | GT5-Unofficial-1b820de08a05070909a267e17f033fcf58ac8710.tar.gz GT5-Unofficial-1b820de08a05070909a267e17f033fcf58ac8710.tar.bz2 GT5-Unofficial-1b820de08a05070909a267e17f033fcf58ac8710.zip |
The Great Renaming (#3014)
* move kekztech to a single root dir
* move detrav to a single root dir
* move gtnh-lanthanides to a single root dir
* move tectech and delete some gross reflection in gt++
* remove more reflection inside gt5u
* delete more reflection in gt++
* fix imports
* move bartworks and bwcrossmod
* fix proxies
* move galactigreg and ggfab
* move gtneioreplugin
* try to fix gt++ bee loader
* apply the rename rules to BW
* apply rename rules to bwcrossmod
* apply rename rules to detrav scanner mod
* apply rename rules to galacticgreg
* apply rename rules to ggfab
* apply rename rules to goodgenerator
* apply rename rules to gtnh-lanthanides
* apply rename rules to gt++
* apply rename rules to kekztech
* apply rename rules to kubatech
* apply rename rules to tectech
* apply rename rules to gt
apply the rename rules to gt
* fix tt import
* fix mui hopefully
* fix coremod except intergalactic
* rename assline recipe class
* fix a class name i stumbled on
* rename StructureUtility to GTStructureUtility to prevent conflict with structurelib
* temporary rename of GTTooltipDataCache to old name
* fix gt client/server proxy names
Diffstat (limited to 'src/main/java/gregtech/common/covers/CoverFluidLimiter.java')
-rw-r--r-- | src/main/java/gregtech/common/covers/CoverFluidLimiter.java | 202 |
1 files changed, 202 insertions, 0 deletions
diff --git a/src/main/java/gregtech/common/covers/CoverFluidLimiter.java b/src/main/java/gregtech/common/covers/CoverFluidLimiter.java new file mode 100644 index 0000000000..4669a3a442 --- /dev/null +++ b/src/main/java/gregtech/common/covers/CoverFluidLimiter.java @@ -0,0 +1,202 @@ +package gregtech.common.covers; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.nbt.NBTBase; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.common.util.ForgeDirection; +import net.minecraftforge.fluids.Fluid; +import net.minecraftforge.fluids.FluidTankInfo; +import net.minecraftforge.fluids.IFluidHandler; + +import com.google.common.io.ByteArrayDataInput; +import com.gtnewhorizons.modularui.api.screen.ModularWindow; +import com.gtnewhorizons.modularui.common.widget.TextWidget; + +import gregtech.api.gui.modularui.CoverUIBuildContext; +import gregtech.api.interfaces.ITexture; +import gregtech.api.interfaces.tileentity.ICoverable; +import gregtech.api.util.CoverBehaviorBase; +import gregtech.api.util.GTUtility; +import gregtech.api.util.ISerializableObject; +import gregtech.common.gui.modularui.widget.CoverDataControllerWidget; +import gregtech.common.gui.modularui.widget.CoverDataFollowerNumericWidget; +import io.netty.buffer.ByteBuf; + +/*** + * @author TrainerSnow#5086 + */ +public class CoverFluidLimiter extends CoverBehaviorBase<CoverFluidLimiter.FluidLimiterData> { + + public CoverFluidLimiter(ITexture coverTexture) { + super(FluidLimiterData.class, coverTexture); + } + + @Override + protected FluidLimiterData onCoverScrewdriverClickImpl(ForgeDirection side, int aCoverID, + FluidLimiterData aCoverVariable, ICoverable aTileEntity, EntityPlayer aPlayer, float aX, float aY, float aZ) { + if (aTileEntity instanceof IFluidHandler) { + adjustThreshold(aCoverVariable, !aPlayer.isSneaking()); + GTUtility.sendChatToPlayer(aPlayer, String.format("Threshold: %f", aCoverVariable.threshold)); + } + return aCoverVariable; + } + + @Override + protected boolean letsFluidInImpl(ForgeDirection side, int aCoverID, FluidLimiterData aCoverVariable, Fluid aFluid, + ICoverable aTileEntity) { + return allowsFluidIn(aCoverVariable, aTileEntity); + } + + @Override + protected boolean alwaysLookConnectedImpl(ForgeDirection side, int aCoverID, FluidLimiterData aCoverVariable, + ICoverable aTileEntity) { + return true; + } + + /* + * Helpers + */ + + private boolean allowsFluidIn(FluidLimiterData aCoverVariable, ICoverable c) { + if (c instanceof IFluidHandler) { + return aCoverVariable.threshold > getFillLevelInputSlots((IFluidHandler) c); + } + return false; + } + + private void adjustThreshold(FluidLimiterData coverVariable, boolean way) { + if (way) { + if ((coverVariable.threshold + 0.05f) > 1F) { + coverVariable.threshold = 0F; + return; + } + coverVariable.threshold += 0.05F; + } else { + if ((Math.abs(coverVariable.threshold) - 0.05F) < 0F) { + coverVariable.threshold = 1F; + return; + } + coverVariable.threshold -= 0.05F; + } + } + + private float getFillLevelInputSlots(IFluidHandler fh) { + FluidTankInfo[] tankInfo = fh.getTankInfo(ForgeDirection.UNKNOWN); + long tMax; + long tUsed; + if (tankInfo != null) { + // 0 Because we acces first slot only + FluidTankInfo inputSlot = tankInfo[0]; + if (inputSlot.fluid != null) { + tMax = inputSlot.capacity; + tUsed = inputSlot.fluid.amount; + return (float) tUsed / (float) tMax; + } + } + return 0F; + } + + /* + * Data + */ + + @Override + public FluidLimiterData createDataObject(int aLegacyData) { + return createDataObject(); + } + + @Override + public FluidLimiterData createDataObject() { + return new FluidLimiterData(1F); + } + + public static class FluidLimiterData implements ISerializableObject { + + private float threshold; + + public FluidLimiterData(float threshold) { + this.threshold = threshold; + } + + @Nonnull + @Override + public ISerializableObject copy() { + return new FluidLimiterData(threshold); + } + + @Nonnull + @Override + public NBTBase saveDataToNBT() { + NBTTagCompound tag = new NBTTagCompound(); + tag.setFloat("threshold", this.threshold); + return tag; + } + + @Override + public void writeToByteBuf(ByteBuf aBuf) { + aBuf.writeFloat(this.threshold); + } + + @Override + public void loadDataFromNBT(NBTBase aNBT) { + if (aNBT instanceof NBTTagCompound tag) { + this.threshold = tag.getFloat("threshold"); + } + } + + @Nonnull + @Override + public ISerializableObject readFromPacket(ByteArrayDataInput aBuf, @Nullable EntityPlayerMP aPlayer) { + this.threshold = aBuf.readFloat(); + return this; + } + } + + // GUI stuff + + @Override + public boolean hasCoverGUI() { + return true; + } + + @Override + public ModularWindow createWindow(CoverUIBuildContext buildContext) { + return new FluidLimiterUIFactory(buildContext).createWindow(); + } + + private class FluidLimiterUIFactory extends UIFactory { + + private static final int startX = 10; + private static final int startY = 25; + private static final int spaceX = 18; + private static final int spaceY = 18; + + public FluidLimiterUIFactory(CoverUIBuildContext buildContext) { + super(buildContext); + } + + @Override + protected void addUIWidgets(ModularWindow.Builder builder) { + builder.widget( + new CoverDataControllerWidget<>(this::getCoverData, this::setCoverData, CoverFluidLimiter.this) + .addFollower( + new CoverDataFollowerNumericWidget<>(), + coverData -> (double) Math.round(coverData.threshold * 100), + (coverData, val) -> { + coverData.threshold = val.floatValue() / 100; + return coverData; + }, + widget -> widget.setBounds(0, 100) + .setFocusOnGuiOpen(true) + .setPos(startX, startY + spaceY * 2 - 24) + .setSize(spaceX * 4 - 3, 12))) + .widget( + new TextWidget("Percent threshold").setDefaultColor(COLOR_TEXT_GRAY.get()) + .setPos(startX, startY + spaceY * 2 - 35)); + } + } +} |