aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/util/GT_Utility.java
diff options
context:
space:
mode:
authorGlease <4586901+Glease@users.noreply.github.com>2023-02-27 13:49:05 +0800
committerGitHub <noreply@github.com>2023-02-27 06:49:05 +0100
commit44824520ab2140f561a741bc486bc2f3459da2fe (patch)
tree5d81da726f4cb783a6b2369241a7613a2eeefff6 /src/main/java/gregtech/api/util/GT_Utility.java
parentf04ae276af1346fb8ca5a022b21f5f372960ae13 (diff)
downloadGT5-Unofficial-44824520ab2140f561a741bc486bc2f3459da2fe.tar.gz
GT5-Unofficial-44824520ab2140f561a741bc486bc2f3459da2fe.tar.bz2
GT5-Unofficial-44824520ab2140f561a741bc486bc2f3459da2fe.zip
implement save & load for single recipe lock (#1771)
* implement save & load for single recipe lock * fix fat finger * fix NPE * disable machine if old locked recipe is gone * address reviews * spotless
Diffstat (limited to 'src/main/java/gregtech/api/util/GT_Utility.java')
-rw-r--r--src/main/java/gregtech/api/util/GT_Utility.java56
1 files changed, 52 insertions, 4 deletions
diff --git a/src/main/java/gregtech/api/util/GT_Utility.java b/src/main/java/gregtech/api/util/GT_Utility.java
index 2134d94253..577364c0f7 100644
--- a/src/main/java/gregtech/api/util/GT_Utility.java
+++ b/src/main/java/gregtech/api/util/GT_Utility.java
@@ -24,6 +24,9 @@ import java.util.Map.Entry;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Supplier;
+import java.util.stream.Collector;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
import javax.annotation.Nullable;
@@ -82,6 +85,7 @@ import cofh.api.transport.IItemDuct;
import com.google.auto.value.AutoValue;
import com.google.common.base.Suppliers;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.gtnewhorizon.structurelib.alignment.IAlignment;
@@ -2421,7 +2425,7 @@ public class GT_Utility {
/**
* Return texture id from page and index, for use when determining hatches, but can also be precomputed from:
* (page<<7)+index
- *
+ *
* @param page 0 to 127 page
* @param index 0 to 127 texture index
* @return casing texture 0 to 16383
@@ -2436,7 +2440,7 @@ public class GT_Utility {
/**
* Return texture id from page and index, for use when determining hatches, but can also be precomputed from:
* (page<<7)+index
- *
+ *
* @param page 0 to 127 page
* @param startIndex 0 to 127 start texture index
* @param blockMeta meta of the block
@@ -2459,7 +2463,7 @@ public class GT_Utility {
/**
* Return texture id from item stack, unoptimized but readable?
- *
+ *
* @return casing texture 0 to 16383
*/
@Deprecated
@@ -2469,7 +2473,7 @@ public class GT_Utility {
/**
* Return texture id from item stack, unoptimized but readable?
- *
+ *
* @return casing texture 0 to 16383
*/
public static int getTextureId(Block blockFromBlock, byte metaFromBlock) {
@@ -4379,9 +4383,45 @@ public class GT_Utility {
return new ItemStack(aItem.getItem(), 0, aItem.getItemDamage());
}
+ public static Stream<NBTTagCompound> streamCompounds(NBTTagList list) {
+ if (list == null) return Stream.empty();
+ return IntStream.range(0, list.tagCount()).mapToObj(list::getCompoundTagAt);
+ }
+
+ public static boolean equals(ItemStack[] a, ItemStack[] b) {
+ // because stupid mojang didn't override equals for us
+ if (a == null && b == null) return true;
+ if ((a == null) != (b == null)) return false;
+ if (a.length != b.length) return false;
+ for (int i = 0; i < a.length; i++) {
+ if (!areStacksEqual(a[i], b[i], false)) return false;
+ }
+ return true;
+ }
+
+ /**
+ * Guava ImmutableMap variant of Collectors.toMap. Optimized for serial streams.
+ */
+ public static <T, K, U> Collector<T, ?, ImmutableMap<K, U>> toImmutableMapSerial(
+ Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) {
+ // petty type inference cannot work out the correct type parameter
+ return Collector.<T, ImmutableMap.Builder<K, U>, ImmutableMap<K, U>>of(
+ ImmutableMap::builder,
+ (b, t) -> b.put(keyMapper.apply(t), valueMapper.apply(t)),
+ (b1, b2) -> b1.putAll(b2.build()),
+ ImmutableMap.Builder::build);
+ }
+
@AutoValue
public abstract static class ItemId {
+ public static AutoValue_GT_Utility_ItemId create(NBTTagCompound tag) {
+ return new AutoValue_GT_Utility_ItemId(
+ Item.getItemById(tag.getShort("item")),
+ tag.getShort("meta"),
+ tag.hasKey("tag", Constants.NBT.TAG_COMPOUND) ? tag.getCompoundTag("tag") : null);
+ }
+
/** This method copies NBT, as it is mutable. */
public static ItemId create(ItemStack itemStack) {
NBTTagCompound nbt = itemStack.getTagCompound();
@@ -4406,6 +4446,14 @@ public class GT_Utility {
@Nullable
protected abstract NBTTagCompound nbt();
+
+ public NBTTagCompound writeToNBT() {
+ NBTTagCompound tag = new NBTTagCompound();
+ tag.setShort("item", (short) Item.getIdFromItem(item()));
+ tag.setShort("meta", (short) metaData());
+ if (nbt() != null) tag.setTag("tag", nbt());
+ return tag;
+ }
}
public static int getPlasmaFuelValueInEUPerLiterFromMaterial(Materials material) {