Skip to content

Commit

Permalink
Theoretical attempt at mitigating #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Vazkii committed Apr 9, 2024
1 parent 0f3129b commit 9685459
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public class ZetaGeneralConfig {
@Config(description = "Set to true to make the Zeta big worldgen features generate as spheres rather than unique shapes. It's faster, but won't look as cool")
public static boolean useFastWorldgen = false;

@Config(description = "Set to true to enable verbose logging of creative tab logic, for debugging purposes")
public static boolean enableCreativeVerboseLogging = false;

@Config(description = "Set to true to force all creative tab additions to be added to the end of the creative tabs rather than the middle, as a failsafe")
public static boolean forceCreativeTabAppends = false;

private ZetaGeneralConfig() {
// NO-OP
}
Expand Down
52 changes: 40 additions & 12 deletions src/main/java/org/violetmoon/zeta/registry/CreativeTabManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Supplier;

import org.violetmoon.zeta.Zeta;
import org.violetmoon.zeta.config.ZetaGeneralConfig;
import org.violetmoon.zeta.module.IDisableable;

import com.google.common.collect.HashMultimap;
Expand Down Expand Up @@ -115,7 +117,19 @@ public static void buildContents(BuildCreativeModeTabContentsEvent event) {

for(ItemLike item : add.appendToEnd)
acceptItem(event, item);


if(ZetaGeneralConfig.forceCreativeTabAppends) {
for(ItemSet itemset : add.appendInFront.keySet())
for(ItemLike item : itemset.items)
acceptItem(event, item);
for(ItemSet itemset : add.appendBehind.keySet())
for(ItemLike item : itemset.items)
acceptItem(event, item);

return;
}


MutableHashedLinkedMap<ItemStack, CreativeModeTab.TabVisibility> entries = event.getEntries();

Map<ItemSet, ItemLike> front = new LinkedHashMap<>(add.appendInFront);
Expand All @@ -129,13 +143,19 @@ public static void buildContents(BuildCreativeModeTabContentsEvent event) {

while(true) {
boolean missed = false;
logVerbose(() -> "front empty=" + front.isEmpty() + " / behind empty=" + behind.isEmpty());

if(!front.isEmpty())
missed = appendNextTo(tabKey, entries, front, false, failsafing);
if(!behind.isEmpty())
missed |= appendNextTo(tabKey, entries, behind, true, failsafing);

if(missed)
if(missed) {
int fMisses = misses;
logVerbose(() -> "Missed " + fMisses + "times out of " + failsafe);

misses++;
}

// arbitrary failsafe, should never happen
if(misses > failsafe) {
Expand All @@ -145,7 +165,7 @@ public static void buildContents(BuildCreativeModeTabContentsEvent event) {
if(misses > printThreshold)
failsafing = true;

if(front.isEmpty() && behind.isEmpty())
if(front.isEmpty() && behind.isEmpty())
return;
}
}
Expand All @@ -168,8 +188,9 @@ private static void acceptItem(BuildCreativeModeTabContentsEvent event, ItemLike
else
event.accept(item);
}

private static void addToEntries(ItemStack target, MutableHashedLinkedMap<ItemStack, CreativeModeTab.TabVisibility> entries, ItemLike item, boolean behind) {
logVerbose(() -> "adding target=" + item + " next to " + item + " with behind=" + behind);
if(!isItemEnabled(item))
return;

Expand All @@ -192,6 +213,7 @@ private static void addToEntries(ItemStack target, MutableHashedLinkedMap<ItemSt
* Returns true if the item needs to be tried again later
*/
private static boolean appendNextTo(ResourceKey<CreativeModeTab> tabKey, MutableHashedLinkedMap<ItemStack, CreativeModeTab.TabVisibility> entries, Map<ItemSet, ItemLike> map, boolean behind, boolean log) {
logVerbose(() -> "appendNextTo " + tabKey + " / behind=" + behind);
Collection<ItemSet> coll = map.keySet();
if(coll.isEmpty())
throw new RuntimeException("Tab collection is empty, this should never happen.");
Expand All @@ -205,18 +227,23 @@ private static boolean appendNextTo(ResourceKey<CreativeModeTab> tabKey, Mutable

map.remove(firstSet);

if(!isItemEnabled(firstSetItem) || target == null)
if(!isItemEnabled(firstSetItem) || target == null) {
logVerbose(() -> "hit early false return");
return false;
}

if(!itemLikeCache.containsKey(target))
itemLikeCache.put(target, target.asItem());
Item targetItem = itemLikeCache.get(target);

for(Entry<ItemStack, TabVisibility> entry : entries) {
ItemStack stack = entry.getKey();
Item item = stack.getItem();

if(!itemLikeCache.containsKey(target))
itemLikeCache.put(target, target.asItem());
Item targetItem = itemLikeCache.get(target);
logVerbose(() -> "Comparing item " + item + " to our target " + targetItem);

if(item == targetItem) {
logVerbose(() -> "Matched");
for(int i = 0; i < firstSet.items.size(); i++) {
int j = i;
if(!behind)
Expand All @@ -233,6 +260,11 @@ private static boolean appendNextTo(ResourceKey<CreativeModeTab> tabKey, Mutable
map.put(firstSet, target);
return true;
}

private static void logVerbose(Supplier<String> s) {
if(ZetaGeneralConfig.enableCreativeVerboseLogging)
Zeta.GLOBAL_LOG.warn(s.get());
}

private static class CreativeTabAdditions {

Expand All @@ -250,10 +282,6 @@ public ItemSet(ItemLike item) {
items.add(item);
}

public boolean isFresh() {
return items.size() == 1;
}

}

public interface AppendsUniquely extends ItemLike {
Expand Down

0 comments on commit 9685459

Please sign in to comment.