Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Jun 16, 2024
2 parents d1d1981 + 95b879f commit ccac3d9
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 177 deletions.
4 changes: 2 additions & 2 deletions build.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#Thu Apr 11 18:45:01 UTC 2024
#Tue Jun 11 13:34:58 UTC 2024
mapping_version=1.20.1
version=1.0
mod_name=Zeta
mc_version=1.20.1
mapping_channel=official
mod_id=zeta
build_number=17
build_number=20
dir_output=../Build Output/Zeta/
7 changes: 6 additions & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
- Made some minor tweaks to the creative tab addition logic to try and fix whatever is going on with the sporadic tabs disappearing/crashing bug
- mass recipe get can now also get tags
- recipe crawl event will ignore catalyst items
- Recipe crawl digestion will only target vanilla recipe types as to noc cause problems with unknown recipe types that could have some different interpretation
- moved recipe handler methods in recipe digest event

(sorry for triple release i messed up :/)
99 changes: 78 additions & 21 deletions src/main/java/org/violetmoon/zeta/event/play/ZRecipeCrawl.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package org.violetmoon.zeta.event.play;

import java.util.Collection;

import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.common.collect.Streams;
import net.minecraft.core.registries.BuiltInRegistries;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
import org.violetmoon.zeta.event.bus.IZetaPlayEvent;

import com.google.common.collect.Multimap;
Expand All @@ -17,32 +26,13 @@
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.ShapedRecipe;
import net.minecraft.world.item.crafting.ShapelessRecipe;
import org.violetmoon.zeta.util.RegistryUtil;

public abstract class ZRecipeCrawl implements IZetaPlayEvent {

public static class Reset extends ZRecipeCrawl { }
public static class Starting extends ZRecipeCrawl { }

public static class Digest extends ZRecipeCrawl {

private final Multimap<Item, ItemStack> digestion;
private final Multimap<Item, ItemStack> backwardsDigestion;

public Digest(Multimap<Item, ItemStack> digestion, Multimap<Item, ItemStack> backwardsDigestion) {
this.digestion = digestion;
this.backwardsDigestion = backwardsDigestion;
}

public boolean has(Item item, boolean backwards) {
return (backwards ? backwardsDigestion : digestion).containsKey(item);
}

public Collection<ItemStack> get(Item item, boolean backwards) {
return (backwards ? backwardsDigestion : digestion).get(item);
}

}

public static abstract class Visit<T extends Recipe<?>> extends ZRecipeCrawl {

public final T recipe;
Expand Down Expand Up @@ -98,4 +88,71 @@ public Misc(Recipe<?> recipe, RegistryAccess access) {
}

}

/**
* Fired after all recipes have been digested
*/
public static class Digest extends ZRecipeCrawl {

private final Multimap<Item, ItemStack> digestion;
private final Multimap<Item, ItemStack> backwardsDigestion;

public Digest(Multimap<Item, ItemStack> digestion, Multimap<Item, ItemStack> backwardsDigestion) {
this.digestion = digestion;
this.backwardsDigestion = backwardsDigestion;
}

public boolean has(Item item, boolean backwards) {
return (backwards ? backwardsDigestion : digestion).containsKey(item);
}

public Collection<ItemStack> get(Item item, boolean backwards) {
return (backwards ? backwardsDigestion : digestion).get(item);
}

/*
* Derivation list -> items to add and then derive (raw materials)
* Whitelist -> items to add and not derive from
* Blacklist -> items to ignore
*/

public void recursivelyFindCraftedItemsFromStrings(@Nullable Collection<String> derivationList, @Nullable Collection<String> whitelist, @Nullable Collection<String> blacklist, Consumer<Item> callback) {
List<Item> parsedDerivationList = derivationList == null ? null : RegistryUtil.massRegistryGet(derivationList, BuiltInRegistries.ITEM);
List<Item> parsedWhitelist = whitelist == null ? null : RegistryUtil.massRegistryGet(whitelist, BuiltInRegistries.ITEM);
List<Item> parsedBlacklist = blacklist == null ? null : RegistryUtil.massRegistryGet(blacklist, BuiltInRegistries.ITEM);

recursivelyFindCraftedItems(parsedDerivationList, parsedWhitelist, parsedBlacklist, callback);
}

public void recursivelyFindCraftedItems(@Nullable Collection<Item> derivationList, @Nullable Collection<Item> whitelist, @Nullable Collection<Item> blacklist, Consumer<Item> callback) {
Collection<Item> trueDerivationList = derivationList == null ? Lists.newArrayList() : derivationList;
Collection<Item> trueWhitelist = whitelist == null ? Lists.newArrayList() : whitelist;
Collection<Item> trueBlacklist = blacklist == null ? Lists.newArrayList() : blacklist;

Streams.concat(trueDerivationList.stream(), trueWhitelist.stream()).forEach(callback);

Set<Item> scanned = Sets.newHashSet(trueDerivationList);
List<Item> toScan = Lists.newArrayList(trueDerivationList);

while(!toScan.isEmpty()) {
Item scan = toScan.remove(0);

if(digestion.containsKey(scan)) {
for(ItemStack digestedStack : digestion.get(scan)) {
Item candidate = digestedStack.getItem();

if(!scanned.contains(candidate)) {
scanned.add(candidate);
toScan.add(candidate);

if(!trueBlacklist.contains(candidate))
callback.accept(candidate);
}
}
}
}
}

}

}
57 changes: 31 additions & 26 deletions src/main/java/org/violetmoon/zeta/util/ItemNBTHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,129 +14,134 @@
import net.minecraft.nbt.ListTag;
import net.minecraft.world.item.ItemStack;

// TBH this entire class could be Deprecated and removed
public final class ItemNBTHelper {

/** Checks if an ItemStack has a Tag Compound **/
@Deprecated(forRemoval = true) // Just use stack.hasTag()
public static boolean detectNBT(ItemStack stack) {
return stack.hasTag();
}

/** Tries to initialize an NBT Tag Compound in an ItemStack,
* this will not do anything if the stack already has a tag
* compound **/
@Deprecated(forRemoval = true) // Just use stack.getOrCreateTag()
public static void initNBT(ItemStack stack) {
if(!detectNBT(stack))
injectNBT(stack, new CompoundTag());
stack.getOrCreateTag();
}

/** Injects an NBT Tag Compound to an ItemStack, no checks
* are made previously **/
@Deprecated(forRemoval = true) // Just use stack.setTag(nbt)
public static void injectNBT(ItemStack stack, CompoundTag nbt) {
stack.setTag(nbt);
}

/** Gets the CompoundNBT in an ItemStack. Tries to init it
* previously in case there isn't one present **/
@Deprecated(forRemoval = true) // Just use stack.getOrCreateTag()
public static CompoundTag getNBT(ItemStack stack) {
initNBT(stack);
return stack.getTag();
return stack.getOrCreateTag();
}

// SETTERS ///////////////////////////////////////////////////////////////////

// All these force create a tag if it doesn't exist

public static void setBoolean(ItemStack stack, String tag, boolean b) {
getNBT(stack).putBoolean(tag, b);
stack.getOrCreateTag().putBoolean(tag, b);
}

public static void setByte(ItemStack stack, String tag, byte b) {
getNBT(stack).putByte(tag, b);
stack.getOrCreateTag().putByte(tag, b);
}

public static void setShort(ItemStack stack, String tag, short s) {
getNBT(stack).putShort(tag, s);
stack.getOrCreateTag().putShort(tag, s);
}

public static void setInt(ItemStack stack, String tag, int i) {
getNBT(stack).putInt(tag, i);
stack.getOrCreateTag().putInt(tag, i);
}

public static void setLong(ItemStack stack, String tag, long l) {
getNBT(stack).putLong(tag, l);
stack.getOrCreateTag().putLong(tag, l);
}

public static void setFloat(ItemStack stack, String tag, float f) {
getNBT(stack).putFloat(tag, f);
stack.getOrCreateTag().putFloat(tag, f);
}

public static void setDouble(ItemStack stack, String tag, double d) {
getNBT(stack).putDouble(tag, d);
stack.getOrCreateTag().putDouble(tag, d);
}

public static void setCompound(ItemStack stack, String tag, CompoundTag cmp) {
if(!tag.equalsIgnoreCase("ench")) // not override the enchantments
getNBT(stack).put(tag, cmp);
stack.getOrCreateTag().put(tag, cmp);
}

public static void setString(ItemStack stack, String tag, String s) {
getNBT(stack).putString(tag, s);
stack.getOrCreateTag().putString(tag, s);
}

public static void setList(ItemStack stack, String tag, ListTag list) {
getNBT(stack).put(tag, list);
stack.getOrCreateTag().put(tag, list);
}

// GETTERS ///////////////////////////////////////////////////////////////////


public static boolean verifyExistence(ItemStack stack, String tag) {
return !stack.isEmpty() && detectNBT(stack) && getNBT(stack).contains(tag);
return !stack.isEmpty() && stack.hasTag() && stack.getOrCreateTag().contains(tag);
}

@Deprecated
@Deprecated(forRemoval = true)
public static boolean verifyExistance(ItemStack stack, String tag) {
return verifyExistence(stack, tag);
}

public static boolean getBoolean(ItemStack stack, String tag, boolean defaultExpected) {
return verifyExistence(stack, tag) ? getNBT(stack).getBoolean(tag) : defaultExpected;
return verifyExistence(stack, tag) ? stack.getOrCreateTag().getBoolean(tag) : defaultExpected;
}

public static byte getByte(ItemStack stack, String tag, byte defaultExpected) {
return verifyExistence(stack, tag) ? getNBT(stack).getByte(tag) : defaultExpected;
return verifyExistence(stack, tag) ? stack.getOrCreateTag().getByte(tag) : defaultExpected;
}

public static short getShort(ItemStack stack, String tag, short defaultExpected) {
return verifyExistence(stack, tag) ? getNBT(stack).getShort(tag) : defaultExpected;
return verifyExistence(stack, tag) ? stack.getOrCreateTag().getShort(tag) : defaultExpected;
}

public static int getInt(ItemStack stack, String tag, int defaultExpected) {
return verifyExistence(stack, tag) ? getNBT(stack).getInt(tag) : defaultExpected;
return verifyExistence(stack, tag) ? stack.getOrCreateTag().getInt(tag) : defaultExpected;
}

public static long getLong(ItemStack stack, String tag, long defaultExpected) {
return verifyExistence(stack, tag) ? getNBT(stack).getLong(tag) : defaultExpected;
return verifyExistence(stack, tag) ? stack.getOrCreateTag().getLong(tag) : defaultExpected;
}

public static float getFloat(ItemStack stack, String tag, float defaultExpected) {
return verifyExistence(stack, tag) ? getNBT(stack).getFloat(tag) : defaultExpected;
return verifyExistence(stack, tag) ? stack.getOrCreateTag().getFloat(tag) : defaultExpected;
}

public static double getDouble(ItemStack stack, String tag, double defaultExpected) {
return verifyExistence(stack, tag) ? getNBT(stack).getDouble(tag) : defaultExpected;
return verifyExistence(stack, tag) ? stack.getOrCreateTag().getDouble(tag) : defaultExpected;
}

/** If nullifyOnFail is true it'll return null if it doesn't find any
* compounds, otherwise it'll return a new one. **/
public static CompoundTag getCompound(ItemStack stack, String tag, boolean nullifyOnFail) {
return verifyExistence(stack, tag) ? getNBT(stack).getCompound(tag) : nullifyOnFail ? null : new CompoundTag();
return verifyExistence(stack, tag) ? stack.getOrCreateTag().getCompound(tag) : nullifyOnFail ? null : new CompoundTag();
}

public static String getString(ItemStack stack, String tag, String defaultExpected) {
return verifyExistence(stack, tag) ? getNBT(stack).getString(tag) : defaultExpected;
return verifyExistence(stack, tag) ? stack.getOrCreateTag().getString(tag) : defaultExpected;
}

public static ListTag getList(ItemStack stack, String tag, int objtype, boolean nullifyOnFail) {
return verifyExistence(stack, tag) ? getNBT(stack).getList(tag, objtype) : nullifyOnFail ? null : new ListTag();
return verifyExistence(stack, tag) ? stack.getOrCreateTag().getList(tag, objtype) : nullifyOnFail ? null : new ListTag();
}

}
Loading

0 comments on commit ccac3d9

Please sign in to comment.