Skip to content

Commit

Permalink
add ability to use block/fluid tags in multiblock patterns (#1030)
Browse files Browse the repository at this point in the history
  • Loading branch information
screret authored Mar 30, 2024
1 parent 40323c2 commit 57fe6c1
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/main/java/com/gregtechceu/gtceu/api/pattern/Predicates.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import com.gregtechceu.gtceu.api.machine.multiblock.IBatteryData;
import com.gregtechceu.gtceu.api.machine.multiblock.PartAbility;
import com.gregtechceu.gtceu.api.pattern.error.PatternStringError;
import com.gregtechceu.gtceu.api.pattern.predicates.PredicateBlocks;
import com.gregtechceu.gtceu.api.pattern.predicates.PredicateFluids;
import com.gregtechceu.gtceu.api.pattern.predicates.PredicateStates;
import com.gregtechceu.gtceu.api.pattern.predicates.SimplePredicate;
import com.gregtechceu.gtceu.api.pattern.predicates.*;
import com.gregtechceu.gtceu.api.pipenet.IPipeNode;
import com.gregtechceu.gtceu.api.recipe.GTRecipeType;
import com.gregtechceu.gtceu.common.block.BatteryBlock;
Expand All @@ -25,6 +22,7 @@
import com.lowdragmc.lowdraglib.utils.BlockInfo;
import com.tterrag.registrate.util.entry.RegistryEntry;
import net.minecraft.network.chat.Component;
import net.minecraft.tags.TagKey;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
Expand Down Expand Up @@ -62,10 +60,18 @@ public static TraceabilityPredicate blocks(IMachineBlock... blocks) {
return new TraceabilityPredicate(new PredicateBlocks(Arrays.stream(blocks).map(IMachineBlock::self).toArray(Block[]::new)));
}

public static TraceabilityPredicate blockTag(TagKey<Block> tag) {
return new TraceabilityPredicate(new PredicateBlockTag(tag));
}

public static TraceabilityPredicate fluids(Fluid... fluids) {
return new TraceabilityPredicate(new PredicateFluids(fluids));
}

public static TraceabilityPredicate fluidTag(TagKey<Fluid> tag) {
return new TraceabilityPredicate(new PredicateFluidTag(tag));
}

public static TraceabilityPredicate custom(Predicate<MultiblockState> predicate, Supplier<BlockInfo[]> candidates) {
return new TraceabilityPredicate(predicate, candidates);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.gregtechceu.gtceu.api.pattern.predicates;

import com.lowdragmc.lowdraglib.utils.BlockInfo;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.tags.TagKey;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;

public class PredicateBlockTag extends SimplePredicate {
public TagKey<Block> tag = null;

public PredicateBlockTag() {
super("tags");
}

public PredicateBlockTag(TagKey<Block> tag) {
this();
this.tag = tag;
buildPredicate();
}

@Override
public SimplePredicate buildPredicate() {
if (tag == null) {
predicate = state -> false;
candidates = () -> new BlockInfo[] {BlockInfo.fromBlock(Blocks.BARRIER)};
return this;
}
predicate = state -> state.getBlockState().is(tag);
candidates = () -> BuiltInRegistries.BLOCK.getTag(tag)
.stream()
.flatMap(HolderSet.Named::stream)
.map(Holder::value)
.map(BlockInfo::fromBlock)
.toArray(BlockInfo[]::new);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.gregtechceu.gtceu.api.pattern.predicates;

import com.lowdragmc.lowdraglib.utils.BlockInfo;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.tags.TagKey;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.material.Fluid;

public class PredicateFluidTag extends SimplePredicate {
public TagKey<Fluid> tag = null;

public PredicateFluidTag() {
super("tags");
}

public PredicateFluidTag(TagKey<Fluid> tag) {
this();
this.tag = tag;
buildPredicate();
}

@Override
public SimplePredicate buildPredicate() {
if (tag == null) {
predicate = state -> false;
candidates = () -> new BlockInfo[] {BlockInfo.fromBlock(Blocks.BARRIER)};
return this;
}
predicate = state -> state.getBlockState().getFluidState().is(tag);
candidates = () -> BuiltInRegistries.FLUID.getTag(tag)
.stream()
.flatMap(HolderSet.Named::stream)
.map(Holder::value)
.map(fluid -> BlockInfo.fromBlockState(fluid.defaultFluidState().createLegacyBlock()))
.toArray(BlockInfo[]::new);
return this;
}
}

0 comments on commit 57fe6c1

Please sign in to comment.