Skip to content

Commit

Permalink
Merge remote-tracking branch 'shedaniel/11.x-1.19.4' into 12.x-1.20
Browse files Browse the repository at this point in the history
  • Loading branch information
shedaniel committed Sep 18, 2023
2 parents 21d144a + 3dc4238 commit 001fc84
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import me.shedaniel.rei.api.common.category.CategoryIdentifier;
import me.shedaniel.rei.api.common.display.Display;
import me.shedaniel.rei.api.common.display.DisplayMerger;
import me.shedaniel.rei.api.common.entry.EntryIngredient;
import me.shedaniel.rei.api.common.util.Identifiable;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand All @@ -41,6 +42,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

@Environment(EnvType.CLIENT)
Expand Down Expand Up @@ -149,15 +151,25 @@ static <T extends Display> DisplayMerger<T> getContentMerger() {
@Override
public boolean canMerge(T first, T second) {
if (!first.getCategoryIdentifier().equals(second.getCategoryIdentifier())) return false;
if (!first.getInputEntries().equals(second.getInputEntries())) return false;
if (!first.getOutputEntries().equals(second.getOutputEntries())) return false;
if (!equals(first.getInputEntries(), second.getInputEntries())) return false;
if (!equals(first.getOutputEntries(), second.getOutputEntries())) return false;
return true;
}

@Override
public int hashOf(T display) {
return display.getCategoryIdentifier().hashCode() * 31 * 31 * 31 + display.getInputEntries().hashCode() * 31 * 31 + display.getOutputEntries().hashCode();
}

private boolean equals(List<EntryIngredient> l1, List<EntryIngredient> l2) {
if (l1.size() != l2.size()) return false;
Iterator<EntryIngredient> it1 = l1.iterator();
Iterator<EntryIngredient> it2 = l2.iterator();
while (it1.hasNext() && it2.hasNext()) {
if (!it1.next().equals(it2.next())) return false;
}
return true;
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import me.shedaniel.rei.api.client.registry.display.DisplayCategory;
import me.shedaniel.rei.api.common.category.CategoryIdentifier;
import me.shedaniel.rei.api.common.display.DisplayMerger;
import me.shedaniel.rei.api.common.entry.EntryIngredient;
import me.shedaniel.rei.api.common.entry.EntryStack;
import me.shedaniel.rei.api.common.entry.InputIngredient;
import me.shedaniel.rei.api.common.util.EntryStacks;
Expand All @@ -44,6 +45,7 @@
import net.minecraft.world.level.block.Blocks;
import org.jetbrains.annotations.Nullable;

import java.util.Iterator;
import java.util.List;

@Environment(EnvType.CLIENT)
Expand Down Expand Up @@ -89,6 +91,32 @@ public List<Widget> setupDisplay(DefaultCraftingDisplay<?> display, Rectangle bo
@Override
@Nullable
public DisplayMerger<DefaultCraftingDisplay<?>> getDisplayMerger() {
return DisplayCategory.getContentMerger();
return new DisplayMerger<>() {
@Override
public boolean canMerge(DefaultCraftingDisplay<?> first, DefaultCraftingDisplay<?> second) {
if (!first.getCategoryIdentifier().equals(second.getCategoryIdentifier())) return false;
if (!equals(first.getOrganisedInputEntries(3, 3), second.getInputEntries())) return false;
if (!equals(first.getOutputEntries(), second.getOutputEntries())) return false;
if (first.isShapeless() != second.isShapeless()) return false;
if (first.getWidth() != second.getWidth()) return false;
if (first.getHeight() != second.getHeight()) return false;
return true;
}

@Override
public int hashOf(DefaultCraftingDisplay<?> display) {
return display.getCategoryIdentifier().hashCode() * 31 * 31 * 31 + display.getOrganisedInputEntries(3, 3).hashCode() * 31 * 31 + display.getOutputEntries().hashCode();
}

private boolean equals(List<EntryIngredient> l1, List<EntryIngredient> l2) {
if (l1.size() != l2.size()) return false;
Iterator<EntryIngredient> it1 = l1.iterator();
Iterator<EntryIngredient> it2 = l2.iterator();
while (it1.hasNext() && it2.hasNext()) {
if (!it1.next().equals(it2.next())) return false;
}
return true;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public boolean canDeleteItems() {
@Override
public void appendModIdToTooltips(Tooltip components, String modId) {
final String modName = ClientHelper.getInstance().getModFromModId(modId);
if (this.preserveModId(components, modName)) return;

int i = 0;
Iterator<Tooltip.Entry> iterator = components.entries().iterator();
while (iterator.hasNext()) {
Expand All @@ -145,6 +147,14 @@ public void appendModIdToTooltips(Tooltip components, String modId) {
components.add(ClientHelper.getInstance().getFormattedModFromModId(modId));
}

private boolean preserveModId(Tooltip components, String modName) {
if (components.entries().isEmpty()) return false;
Tooltip.Entry lastEntry = components.entries().get(components.entries().size() - 1);

if (!lastEntry.isText()) return false;
return FormattingUtils.stripFormatting(lastEntry.getAsText().getString()).equalsIgnoreCase(modName);
}

@Override
public String getModFromModId(String modId) {
if (modId == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Wrapped)) return false;
Wrapped wrapped = (Wrapped) o;
return merger.canMerge(display, wrapped.display);
return hash == wrapped.hash && merger.canMerge(display, wrapped.display);
}

@Override
Expand Down

0 comments on commit 001fc84

Please sign in to comment.