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

Add chemical filters #762

Merged
merged 5 commits into from
Sep 15, 2024
Merged
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
@@ -1,7 +1,11 @@
package com.enderio.base.client.gui.screen;

import com.enderio.EnderIOBase;
import com.enderio.base.api.attachment.StoredEntityData;
import com.enderio.base.common.capability.EntityFilterCapability;
import com.enderio.base.common.capability.FluidFilterCapability;
import com.enderio.base.common.init.EIODataComponents;
import com.enderio.base.common.init.EIOItems;
import com.enderio.base.common.lang.EIOLang;
import com.enderio.base.common.menu.FluidFilterMenu;
import com.enderio.core.client.gui.screen.EIOScreen;
Expand All @@ -16,6 +20,7 @@
import net.minecraft.util.FastColor;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.client.extensions.common.IClientFluidTypeExtensions;
import net.neoforged.neoforge.fluids.FluidStack;
import org.apache.commons.lang3.NotImplementedException;
Expand Down Expand Up @@ -75,6 +80,25 @@ public void renderSlot(GuiGraphics pGuiGraphics, Slot pSlot) {
}
}

@Override
protected void renderTooltip(GuiGraphics guiGraphics, int x, int y) {
if (this.menu.getCarried().isEmpty() && this.hoveredSlot != null) {
ItemStack itemstack = this.hoveredSlot.getItem();
var capability = getMenu().getFilter();
if (hoveredSlot.index < capability.getEntries().size()) {
FluidStack value = capability.getEntries().get(hoveredSlot.index);
if (!value.isEmpty()) {
guiGraphics.renderTooltip(this.font, value.getHoverName(), x, y);
return;
}
}
if (itemstack.isEmpty()) {
return;
}
guiGraphics.renderTooltip(this.font, this.getTooltipFromContainerItem(itemstack), itemstack.getTooltipImage(), itemstack, x, y);
}
}

@Override
public ResourceLocation getBackgroundImage() {
return BG_TEXTURE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.fluids.FluidStack;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.function.Supplier;

Expand Down Expand Up @@ -150,6 +153,37 @@
return new Component(newEntities, nbt, invert);
}

@Override
public boolean equals(Object o) {
if (this == o)

Check failure on line 158 in enderio-base/src/main/java/com/enderio/base/common/capability/EntityFilterCapability.java

View workflow job for this annotation

GitHub Actions / runner / checkstyle

[checkstyle] reported by reviewdog 🐶 'if' construct must use '{}'s. Raw Output: /github/workspace/./enderio-base/src/main/java/com/enderio/base/common/capability/EntityFilterCapability.java:158:13: error: 'if' construct must use '{}'s. (com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck)
return true;
if (o == null || getClass() != o.getClass())

Check failure on line 160 in enderio-base/src/main/java/com/enderio/base/common/capability/EntityFilterCapability.java

View workflow job for this annotation

GitHub Actions / runner / checkstyle

[checkstyle] reported by reviewdog 🐶 'if' construct must use '{}'s. Raw Output: /github/workspace/./enderio-base/src/main/java/com/enderio/base/common/capability/EntityFilterCapability.java:160:13: error: 'if' construct must use '{}'s. (com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck)
return false;
Component component = (Component) o;
for (int i = 0; i < entities.size(); i++) {
if (!entities.get(i).equals(component.entities.get(i))) {
return false;
}
}
return nbt == component.nbt && invert == component.invert;
}

@Override
public int hashCode() {
return Objects.hash(hashList(entities), nbt, invert);
}

public static int hashList(List<StoredEntityData> list) {
int i = 0;

StoredEntityData stack;
for(Iterator<StoredEntityData> var2 = list.iterator(); var2.hasNext(); i = i * 31 + stack.hashCode()) {
stack = var2.next();
}

return i;
}

public record Slot(int index, StoredEntityData entity) {
public static final Codec<Slot> CODEC = RecordCodecBuilder.create(
p_331695_ -> p_331695_.group(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.OptionalInt;
Expand Down Expand Up @@ -48,7 +49,6 @@ private Component getComponent() {
@Override
public void setInverted(Boolean inverted) {
container.set(componentType, this.getComponent().withInvert(inverted));

}

@Override
Expand Down Expand Up @@ -148,7 +148,18 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(fluids.hashCode(), nbt, invert);
return Objects.hash(hashStackList(fluids), nbt, invert);
}

public static int hashStackList(List<FluidStack> list) {
int i = 0;

FluidStack stack;
for(Iterator<FluidStack> var2 = list.iterator(); var2.hasNext(); i = i * 31 + FluidStack.hashFluidAndComponents(stack)) {
stack = var2.next();
}

return i;
}

public record Slot(int index, FluidStack fluid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(items.hashCode(), nbt, invert);
return Objects.hash(ItemStack.hashStackList(items), nbt, invert);
}

public record Slot(int index, ItemStack item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void setInverted(Boolean inverted) {

@Override
public void clicked(int pSlotId, int pButton, ClickType pClickType, Player pPlayer) {
if (pSlotId < capability.getEntries().size()) {
if (pSlotId < capability.getEntries().size() && pSlotId >= 0) {
if (!capability.getEntries().get(pSlotId).isEmpty()) {
capability.setEntry(pSlotId, FluidStack.EMPTY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

public class FluidFilterSlot extends Slot {

private static Container emptyInventory = new SimpleContainer(0);
private static Container EMPTY_INVENTORY = new SimpleContainer(0);
private final Consumer<FluidStack> consumer;

public FluidFilterSlot(Consumer<FluidStack> consumer, int pSlot, int pX, int pY) {
super(emptyInventory, pSlot, pX, pY);
super(EMPTY_INVENTORY, pSlot, pX, pY);
this.consumer = consumer;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"item.enderio.chemical_filter": "Chemical Filter",
"item.enderio.conduit.chemical": "Chemical Conduit",
"item.enderio.conduit.chemical.multi": "Allows multiple chemical types to be transported on the same line",
"item.enderio.conduit.dense_me": "Dense ME Conduit",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "enderio:item/chemical_filter"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"neoforge:conditions": [
{
"type": "neoforge:mod_loaded",
"modid": "mekanism"
}
],
"parent": "minecraft:recipes/root",
"criteria": {
"has_ingredient": {
"conditions": {
"items": [
{
"items": "minecraft:paper"
}
]
},
"trigger": "minecraft:inventory_changed"
},
"has_the_recipe": {
"conditions": {
"recipe": "enderio:mek_chemical_filter"
},
"trigger": "minecraft:recipe_unlocked"
}
},
"requirements": [
[
"has_the_recipe",
"has_ingredient"
]
],
"rewards": {
"recipes": [
"enderio:mek_chemical_filter"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"neoforge:conditions": [
{
"type": "neoforge:mod_loaded",
"modid": "mekanism"
}
],
"type": "minecraft:crafting_shaped",
"category": "misc",
"key": {
"O": {
"tag": "c:ingots/osmium"
},
"P": [
{
"item": "minecraft:paper"
},
{
"item": "enderio:black_paper"
}
]
},
"pattern": [
" P ",
"POP",
" P "
],
"result": {
"count": 1,
"id": "enderio:chemical_filter"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.enderio.modconduits.mods.laserio;

import com.direwolf20.laserio.common.containers.customhandler.FilterBasicHandler;
import com.direwolf20.laserio.common.items.cards.BaseCard;
import com.direwolf20.laserio.setup.LaserIODataComponents;
import com.enderio.base.common.capability.IFilterCapability;
import com.enderio.modconduits.mods.mekanism.ChemicalFilter;
import com.enderio.modconduits.mods.mekanism.MekanismModule;
import mekanism.api.chemical.ChemicalStack;
import mekanism.api.chemical.IChemicalHandler;
import net.minecraft.world.item.ItemStack;

import java.util.ArrayList;
import java.util.List;

public class LaserChemicalFilter implements IFilterCapability<ChemicalStack>, ChemicalFilter {

private final ItemStack container;

public LaserChemicalFilter(ItemStack cardItem) {
this.container = BaseCard.getFilter(cardItem);
}

@Override
public void setNbt(Boolean nbt) {
if (!nbt) {
container.remove(LaserIODataComponents.FILTER_COMPARE);
} else {
container.set(LaserIODataComponents.FILTER_COMPARE, nbt);
}
}

@Override
public boolean isNbt() {
return container.getOrDefault(LaserIODataComponents.FILTER_COMPARE, false);
}

@Override
public void setInverted(Boolean inverted) {
if (!inverted) {
container.remove(LaserIODataComponents.FILTER_ALLOW);
} else {
container.set(LaserIODataComponents.FILTER_ALLOW, false);
}
}

@Override
public boolean isInvert() {
return !container.getOrDefault(LaserIODataComponents.FILTER_ALLOW, true);
}

@Override
public List<ChemicalStack> getEntries() {
List<ChemicalStack> filteredChemicals = new ArrayList();
FilterBasicHandler filterSlotHandler = new FilterBasicHandler(15, container);

for(int i = 0; i < (filterSlotHandler).getSlots(); ++i) {
ItemStack stack = filterSlotHandler.getStackInSlot(i);
if (!stack.isEmpty()) {
IChemicalHandler capability = stack.getCapability(MekanismModule.Capabilities.Item.CHEMICAL);
if (capability != null) {

for(int tank = 0; tank < capability.getChemicalTanks(); ++tank) {
var chemical = capability.getChemicalInTank(tank);
if (!chemical.isEmpty()) {
filteredChemicals.add(chemical);
}
}
}
}
}

return filteredChemicals;
}

@Override
public void setEntry(int index, ChemicalStack entry) {

}

@Override
public boolean test(ChemicalStack boxedChemicalStack) {
for (ChemicalStack stack : getEntries()) {
if (ChemicalStack.isSameChemical(stack, boxedChemicalStack)) {
return !isInvert();
}
}
return isInvert();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.enderio.modconduits.mods.laserio;

import com.direwolf20.laserio.common.containers.customhandler.FilterCountHandler;
import com.direwolf20.laserio.common.containers.customhandler.FilterBasicHandler;
import com.direwolf20.laserio.common.items.cards.BaseCard;
import com.direwolf20.laserio.setup.LaserIODataComponents;
import com.enderio.base.api.filter.FluidStackFilter;
Expand Down Expand Up @@ -54,7 +54,7 @@ public boolean isInvert() {
@Override
public List<FluidStack> getEntries() {
List<FluidStack> filteredFluids = new ArrayList();
FilterCountHandler filterSlotHandler = new FilterCountHandler(15, container);
FilterBasicHandler filterSlotHandler = new FilterBasicHandler(15, container);

for(int i = 0; i < (filterSlotHandler).getSlots(); ++i) {
ItemStack itemStack = filterSlotHandler.getStackInSlot(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.direwolf20.laserio.setup.Registration;
import com.enderio.base.api.filter.ResourceFilter;
import com.enderio.base.api.integration.Integration;
import com.enderio.base.api.integration.IntegrationManager;
import com.enderio.base.api.integration.IntegrationWrapper;
import com.enderio.base.common.init.EIOCapabilities;
import com.enderio.modconduits.ModdedConduits;
import net.minecraft.world.item.ItemStack;
Expand All @@ -20,6 +22,8 @@ public class LaserIOIntegration implements Integration {
public static ICapabilityProvider<ItemStack, Void, ResourceFilter> FLUID_FILTER_PROVIDER =
(stack, v) -> new LaserFluidFilter(stack);

public static final IntegrationWrapper<MekansimIntegration> MEK_LASER_IO_INTEGRATION = IntegrationManager.wrapper("mekanism", () -> MekansimIntegration::new, ModdedConduits.modEventBus);

@Override
public void addEventListener(IEventBus modEventBus, IEventBus forgeEventBus) {
modEventBus.addListener(this::registerCapEvent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.enderio.modconduits.mods.laserio;

import com.direwolf20.laserio.common.containers.customhandler.FilterCountHandler;
import com.direwolf20.laserio.common.containers.customhandler.FilterBasicHandler;
import com.direwolf20.laserio.common.items.cards.BaseCard;
import com.direwolf20.laserio.setup.LaserIODataComponents;
import com.enderio.base.api.filter.ItemStackFilter;
Expand Down Expand Up @@ -48,7 +48,7 @@ public boolean isInvert() {

@Override
public List<ItemStack> getEntries() {
FilterCountHandler handler = new FilterCountHandler(15, container);
FilterBasicHandler handler = new FilterBasicHandler(15, container);
List<ItemStack> list = NonNullList.withSize(15, ItemStack.EMPTY);
for(int i = 0; i < 15; ++i) {
ItemStack itemStack = handler.getStackInSlot(i);
Expand Down
Loading