diff options
Diffstat (limited to 'src/main/java/gregtech/api')
-rw-r--r-- | src/main/java/gregtech/api/interfaces/IHatchElement.java | 47 | ||||
-rw-r--r-- | src/main/java/gregtech/api/util/GT_HatchElementBuilder.java | 64 |
2 files changed, 92 insertions, 19 deletions
diff --git a/src/main/java/gregtech/api/interfaces/IHatchElement.java b/src/main/java/gregtech/api/interfaces/IHatchElement.java index 46deb5383c..22dbbdf013 100644 --- a/src/main/java/gregtech/api/interfaces/IHatchElement.java +++ b/src/main/java/gregtech/api/interfaces/IHatchElement.java @@ -1,10 +1,9 @@ package gregtech.api.interfaces; +import com.google.common.collect.ImmutableList; import com.gtnewhorizon.structurelib.structure.IStructureElement; import gregtech.api.interfaces.metatileentity.IMetaTileEntity; import gregtech.api.interfaces.tileentity.IGregTechTileEntity; -import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_EnhancedMultiBlockBase; -import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_MultiBlockBase; import gregtech.api.util.GT_StructureUtility; import gregtech.api.util.IGT_HatchAdder; @@ -60,6 +59,7 @@ public interface IHatchElement<T> { .anyOf(this) .casingIndex(aCasingIndex) .dot(aDot) + .continueIfSuccess() .build(); } @@ -70,8 +70,51 @@ public interface IHatchElement<T> { .casingIndex(aCasingIndex) .dot(aDot) .shouldSkip(aShouldSkip) + .continueIfSuccess() .build(); } + + default <T2 extends T> IHatchElement<T2> or(IHatchElement<? super T2> fallback) { + return new HatchElementEither<>(this, fallback); + } +} + +class HatchElementEither<T> implements IHatchElement<T> { + private final IHatchElement<? super T> first, second; + private ImmutableList<? extends Class<? extends IMetaTileEntity>> mMteClasses; + private String name; + + HatchElementEither(IHatchElement<? super T> first, IHatchElement<? super T> second) { + this.first = first; + this.second = second; + } + + @Override + public List<? extends Class<? extends IMetaTileEntity>> mteClasses() { + if (mMteClasses == null) + mMteClasses = ImmutableList.<Class<? extends IMetaTileEntity>>builder() + .addAll(first.mteClasses()) + .addAll(second.mteClasses()) + .build(); + return mMteClasses; + } + + @Override + public IGT_HatchAdder<? super T> adder() { + return ((t, te, i) -> first.adder().apply(t, te, i) || second.adder().apply(t, te, i)); + } + + @Override + public String name() { + if (name == null) + name = first.name() + " or " + second.name(); + return name; + } + + @Override + public long count(T t) { + return first.count(t) + second.count(t); + } } class HatchElement<T> implements IHatchElement<T> { diff --git a/src/main/java/gregtech/api/util/GT_HatchElementBuilder.java b/src/main/java/gregtech/api/util/GT_HatchElementBuilder.java index 91d4d9a156..be6236b94f 100644 --- a/src/main/java/gregtech/api/util/GT_HatchElementBuilder.java +++ b/src/main/java/gregtech/api/util/GT_HatchElementBuilder.java @@ -9,14 +9,11 @@ import gnu.trove.set.hash.TIntHashSet; import gregtech.api.interfaces.IHatchElement; import gregtech.api.interfaces.metatileentity.IMetaTileEntity; import gregtech.api.interfaces.tileentity.IGregTechTileEntity; -import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_EnhancedMultiBlockBase; -import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_MultiBlockBase; import gregtech.common.blocks.GT_Item_Machines; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.ChatComponentText; import net.minecraft.util.ChatComponentTranslation; import net.minecraft.util.IChatComponent; import net.minecraft.world.World; @@ -24,7 +21,6 @@ import net.minecraft.world.World; import java.util.*; import java.util.function.*; import java.util.stream.Collectors; -import java.util.stream.Stream; import static com.gtnewhorizon.structurelib.structure.StructureUtility.ofBlock; import static com.gtnewhorizon.structurelib.structure.StructureUtility.ofChain; @@ -37,10 +33,11 @@ public class GT_HatchElementBuilder<T> { private int mCasingIndex = -1; private int mDot = -1; private BiPredicate<? super T, ? super IGregTechTileEntity> mShouldSkip; - private Function<? super T, ? extends Predicate<ItemStack>> mHatchItemFilter; + private BiFunction<? super T, ItemStack, ? extends Predicate<ItemStack>> mHatchItemFilter; private Supplier<String> mHatchItemType; private Predicate<? super T> mReject, mBuiltinReject; private boolean mCacheHint; + private boolean mNoStop; private GT_HatchElementBuilder() { } @@ -60,7 +57,7 @@ public class GT_HatchElementBuilder<T> { if (elements == null || elements.length == 0) throw new IllegalArgumentException(); return adder(Arrays.stream(elements).map(e -> e.adder().rebrand()).reduce(IGT_HatchAdder::orElse).get()) .hatchClasses(Arrays.stream(elements).map(IHatchElement::mteClasses).flatMap(Collection::stream).collect(Collectors.toList())) - .cacheHint(() -> Arrays.stream(elements).map(IHatchElement::name).collect(Collectors.joining(" or ", "of type ", ""))); + .cacheHint(() -> Arrays.stream(elements).map(IHatchElement::name).sorted().collect(Collectors.joining(" or ", "of type ", ""))); } /** @@ -102,7 +99,7 @@ public class GT_HatchElementBuilder<T> { .collect(Collectors.toList()))) .shouldReject(obj -> elements.entrySet().stream().allMatch(e-> e.getKey().count(obj) >= e.getValue().longValue())) .shouldSkip((BiPredicate<? super T, ? super IGregTechTileEntity> & Builtin) (c, t) -> t != null && list.stream().anyMatch(clazz -> clazz.isInstance(t.getMetaTileEntity()))) - .cacheHint(() -> elements.keySet().stream().map(IHatchElement::name).collect(Collectors.joining(" or ", "of type ", ""))); + .cacheHint(() -> elements.keySet().stream().map(IHatchElement::name).sorted().collect(Collectors.joining(" or ", "of type ", ""))); } //endregion @@ -151,14 +148,27 @@ public class GT_HatchElementBuilder<T> { public GT_HatchElementBuilder<T> hatchItemFilter(Function<? super T, ? extends Predicate<ItemStack>> aHatchItemFilter) { if (aHatchItemFilter == null) throw new IllegalArgumentException(); - mHatchItemFilter = aHatchItemFilter; + mHatchItemFilter = (t, s) -> aHatchItemFilter.apply(t); return this; } public GT_HatchElementBuilder<T> hatchItemFilterAnd(Function<? super T, ? extends Predicate<ItemStack>> aHatchItemFilter) { if (aHatchItemFilter == null) throw new IllegalArgumentException(); - Function<? super T, ? extends Predicate<ItemStack>> tOldFilter = mHatchItemFilter; - mHatchItemFilter = t -> tOldFilter.apply(t).and(aHatchItemFilter.apply(t)); + BiFunction<? super T, ItemStack, ? extends Predicate<ItemStack>> tOldFilter = mHatchItemFilter; + mHatchItemFilter = (t, s) -> tOldFilter.apply(t, s).and(aHatchItemFilter.apply(t)); + return this; + } + + public GT_HatchElementBuilder<T> hatchItemFilter(BiFunction<? super T, ItemStack, ? extends Predicate<ItemStack>> aHatchItemFilter) { + if (aHatchItemFilter == null) throw new IllegalArgumentException(); + mHatchItemFilter = aHatchItemFilter; + return this; + } + + public GT_HatchElementBuilder<T> hatchItemFilterAnd(BiFunction<? super T, ItemStack, ? extends Predicate<ItemStack>> aHatchItemFilter) { + if (aHatchItemFilter == null) throw new IllegalArgumentException(); + BiFunction<? super T, ItemStack, ? extends Predicate<ItemStack>> tOldFilter = mHatchItemFilter; + mHatchItemFilter = (t, s) -> tOldFilter.apply(t, s).and(aHatchItemFilter.apply(t, s)); return this; } @@ -183,12 +193,23 @@ public class GT_HatchElementBuilder<T> { return this; } // endregion + + public GT_HatchElementBuilder<T> continueIfSuccess() { + mNoStop = true; + return this; + } + + public GT_HatchElementBuilder<T> stopIfSuccess() { + mNoStop = false; + return this; + } // endregion // region intermediate public GT_HatchElementBuilder<T> hatchClass(Class<? extends IMetaTileEntity> clazz) { return hatchItemFilter(c -> is -> clazz.isInstance(GT_Item_Machines.getMetaTileEntity(is))) - .cacheHint(() -> "of class " + clazz.getSimpleName()); + .cacheHint(() -> "of class " + clazz.getSimpleName()) + .shouldSkip((BiPredicate<? super T, ? super IGregTechTileEntity> & Builtin) (c, t) -> clazz.isInstance(t.getMetaTileEntity())); } @SafeVarargs @@ -199,13 +220,14 @@ public class GT_HatchElementBuilder<T> { public final GT_HatchElementBuilder<T> hatchClasses(List<? extends Class<? extends IMetaTileEntity>> classes) { List<? extends Class<? extends IMetaTileEntity>> list = new ArrayList<>(classes); return hatchItemFilter(obj -> GT_StructureUtility.filterByMTEClass(list)) - .cacheHint(() -> list.stream().map(Class::getSimpleName).collect(Collectors.joining(" or ", "of class ", ""))) + .cacheHint(() -> list.stream().map(Class::getSimpleName).sorted().collect(Collectors.joining(" or ", "of class ", ""))) .shouldSkip((BiPredicate<? super T, ? super IGregTechTileEntity> & Builtin) (c, t) -> t != null && list.stream().anyMatch(clazz -> clazz.isInstance(t.getMetaTileEntity()))); } public GT_HatchElementBuilder<T> hatchId(int aId) { return hatchItemFilter(c -> is -> GT_Utility.isStackValid(is) && is.getItem() instanceof GT_Item_Machines && is.getItemDamage() == aId) - .cacheHint(() -> "of id " + aId); + .cacheHint(() -> "of id " + aId) + .shouldSkip((BiPredicate<? super T, ? super IGregTechTileEntity> & Builtin) (c, t) -> t != null && t.getMetaTileID() == aId); } public GT_HatchElementBuilder<T> hatchIds(int... aIds) { @@ -213,7 +235,8 @@ public class GT_HatchElementBuilder<T> { if (aIds.length == 1) return hatchId(aIds[0]); TIntCollection coll = aIds.length < 16 ? new TIntArrayList(aIds) : new TIntHashSet(aIds); return hatchItemFilter(c -> is -> GT_Utility.isStackValid(is) && is.getItem() instanceof GT_Item_Machines && coll.contains(is.getItemDamage())) - .cacheHint(() -> Arrays.stream(coll.toArray()).mapToObj(String::valueOf).collect(Collectors.joining(" or ", "of id ", ""))); + .cacheHint(() -> Arrays.stream(coll.toArray()).sorted().mapToObj(String::valueOf).collect(Collectors.joining(" or ", "of id ", ""))) + .shouldSkip((BiPredicate<? super T, ? super IGregTechTileEntity> & Builtin) (c, t) -> t != null && coll.contains(t.getMetaTileID())); } //endregion @@ -276,8 +299,15 @@ public class GT_HatchElementBuilder<T> { private String getHint() { if (mHint != null) return mHint; String tHint = mHatchItemType.get(); - if (mCacheHint) + if (tHint == null) return "?"; + // TODO move this to some .lang instead of half ass it into the crappy gt lang file + tHint = GT_LanguageManager.addStringLocalization("Hatch_Type_" + tHint.replace(' ', '_'), tHint); + if (mCacheHint) { mHint = tHint; + if (mHint != null) + // yeet the getter, since its product is retrieved and cached + mHatchItemType = null; + } return tHint; } @@ -291,13 +321,13 @@ public class GT_HatchElementBuilder<T> { if (!StructureLibAPI.isBlockTriviallyReplaceable(world, x, y, z, actor)) return PlaceResult.REJECT; if (mReject != null && mReject.test(t)) return PlaceResult.REJECT; - ItemStack taken = s.takeOne(mHatchItemFilter.apply(t), true); + ItemStack taken = s.takeOne(mHatchItemFilter.apply(t, trigger), true); if (GT_Utility.isStackInvalid(taken)) { String type = getHint(); chatter.accept(new ChatComponentTranslation("GT5U.autoplace.error.no_hatch", type)); return PlaceResult.REJECT; } - return StructureUtility.survivalPlaceBlock(taken, ItemStackPredicate.NBTMode.IGNORE, null, true, world, x, y, z, s, actor) == PlaceResult.ACCEPT ? PlaceResult.ACCEPT_STOP : PlaceResult.REJECT; + return StructureUtility.survivalPlaceBlock(taken, ItemStackPredicate.NBTMode.IGNORE, null, true, world, x, y, z, s, actor) == PlaceResult.ACCEPT ? (mNoStop ? PlaceResult.ACCEPT : PlaceResult.ACCEPT_STOP) : PlaceResult.REJECT; } }; } |