From 8fc20065ee74fa949fa15b534a8d56b19e9cd480 Mon Sep 17 00:00:00 2001 From: trinsdar <30245301+Trinsdar@users.noreply.github.com> Date: Sat, 7 Oct 2023 17:44:28 -0400 Subject: [PATCH] started work on fluid detector --- .../gregtech/cover/CoverFluidDetector.java | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 common/src/main/java/muramasa/gregtech/cover/CoverFluidDetector.java diff --git a/common/src/main/java/muramasa/gregtech/cover/CoverFluidDetector.java b/common/src/main/java/muramasa/gregtech/cover/CoverFluidDetector.java new file mode 100644 index 000000000..51243c26d --- /dev/null +++ b/common/src/main/java/muramasa/gregtech/cover/CoverFluidDetector.java @@ -0,0 +1,145 @@ +package muramasa.gregtech.cover; + +import com.sun.jna.platform.win32.COM.IStream; +import earth.terrarium.botarium.common.fluid.base.FluidContainer; +import earth.terrarium.botarium.common.fluid.base.FluidHolder; +import muramasa.antimatter.blockentity.BlockEntityMachine; +import muramasa.antimatter.blockentity.pipe.BlockEntityPipe; +import muramasa.antimatter.capability.ICoverHandler; +import muramasa.antimatter.capability.IGuiHandler; +import muramasa.antimatter.cover.BaseCover; +import muramasa.antimatter.cover.CoverFactory; +import muramasa.antimatter.gui.ButtonOverlay; +import muramasa.antimatter.gui.event.GuiEvents; +import muramasa.antimatter.gui.event.IGuiEvent; +import muramasa.antimatter.machine.Tier; +import muramasa.antimatter.machine.event.IMachineEvent; +import muramasa.gregtech.data.GregTechData; +import muramasa.gregtech.data.SlotTypes; +import net.minecraft.core.Direction; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import tesseract.api.gt.IEnergyHandler; + +import java.util.stream.IntStream; +import java.util.stream.LongStream; +import java.util.stream.Stream; + +public class CoverFluidDetector extends BaseCover { + boolean inverted = false; + int outputRedstone = 0; + + private final CoverFluidFilter filter; + public CoverFluidDetector(@NotNull ICoverHandler source, @Nullable Tier tier, Direction side, CoverFactory factory) { + super(source, tier, side, factory); + this.filter = new CoverFluidFilter(source, null, side, GregTechData.COVER_FLUID_FILTER); + filter.onCreate(); + addGuiCallback(t -> { + t.addSwitchButton(79, 34, 16, 16, ButtonOverlay.TORCH_OFF, ButtonOverlay.TORCH_ON, h -> inverted, true, b -> "tooltip.gti.redstone_mode." + (b ? "inverted" : "normal")); + }); + } + + @Override + public boolean canPlace() { + return handler.getTile() instanceof BlockEntityMachine machine && machine.fluidHandler.side(side).isPresent(); + } + + @Override + public String getId() { + return "energy_detector"; + } + + @Override + public ResourceLocation getModel(String type, Direction dir) { + if (type.equals("pipe")) return PIPE_COVER_MODEL; + return getBasicModel(); + } + + @Override + public boolean hasGui() { + return true; + } + + @Override + public void onUpdate() { + if (handler.getTile().getLevel() == null || handler.getTile().getLevel().isClientSide) return; + if (handler.getTile() instanceof BlockEntityMachine machine && machine.fluidHandler.side(side).isPresent()){ + FluidContainer fluidContainer = machine.fluidHandler.side(side).get(); + int oldRedstone = outputRedstone; + long scale = IntStream.range(0, fluidContainer.getSize()).mapToLong(tankSlot -> { + FluidHolder fluidHolder = fluidContainer.getFluids().get(tankSlot); + if (filter.onTransfer(fluidHolder, true, true)) return 0; + return fluidContainer.getTankCapacity(tankSlot); + }).sum() / 15L; + long totalFluid = IntStream.range(0, fluidContainer.getSize()).mapToLong(tankSlot -> { + FluidHolder fluidHolder = fluidContainer.getFluids().get(tankSlot); + if (filter.onTransfer(fluidHolder, true, true)) return 0; + return fluidHolder.getFluidAmount(); + }).sum(); + if (scale > 0){ + outputRedstone = inverted ? (int) (15L - totalFluid / scale) : (int) (totalFluid / scale); + } else { + outputRedstone = inverted ? 15 : 0; + } + if (outputRedstone != oldRedstone){ + markAndNotifySource(); + } + } + } + + @Override + public int getWeakPower() { + return outputRedstone; + } + + @Override + public CompoundTag serialize() { + CompoundTag nbt = super.serialize(); + nbt.put("filter", filter.serialize()); + nbt.putBoolean("inverted", inverted); + return nbt; + } + + @Override + public void onGuiEvent(IGuiEvent event, Player playerEntity) { + if (event.getFactory() == GuiEvents.EXTRA_BUTTON){ + GuiEvents.GuiEvent ev = (GuiEvents.GuiEvent) event; + if (ev.data[1] == 0){ + inverted = !inverted; + if (handler.getTile() instanceof BlockEntityPipe pipe) pipe.onBlockUpdate(pipe.getBlockPos()); + if (handler.getTile() instanceof BlockEntityMachine machine) machine.onBlockUpdate(machine.getBlockPos()); + } + } + } + + @Override + public void deserialize(CompoundTag nbt) { + super.deserialize(nbt); + if (nbt.contains("filter")) { + filter.deserialize(nbt.getCompound("filter")); + } + inverted = nbt.getBoolean("inverted"); + } + @Override + public void onMachineEvent(IGuiHandler tile, IMachineEvent event, int... data) { + if (tile == this && event == SlotTypes.FILTERABLE){ + ItemStack slotStack = getInventory(SlotTypes.FILTERABLE).getItem(data[0]); + if (slotStack.isEmpty()){ + filter.clearFilter(); + } else { + filter.addInfoFromStack(slotStack); + } + } + super.onMachineEvent(tile, event, data); + } + + @Override + public void addInfoFromStack(ItemStack stack) { + super.addInfoFromStack(stack); + onMachineEvent(this, SlotTypes.FILTERABLE, 0); + } +}