Skip to content

Commit

Permalink
Backport to mc1.20.4
Browse files Browse the repository at this point in the history
  • Loading branch information
NotRyken committed Aug 4, 2024
1 parent ccb58d0 commit e7aa9bf
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ private void combineStacks() {
stack = stacks[i];
if (stack.isEmpty()) continue;
int stackSize = stack.getCount();
if (stackSize >= stack.getItem().getDefaultMaxStackSize()) continue;
if (stackSize >= stack.getItem().getMaxStackSize()) continue;
clickEvents.add(screenHelper.createClickEvent(inventorySlots[i], 0, ClickType.PICKUP));
for (int j = 0; j < i; j++) {
ItemStack targetStack = stacks[j];
if (targetStack.isEmpty()) continue;
if (targetStack.getCount() >= targetStack.getItem().getDefaultMaxStackSize()) continue;
if (ItemStack.isSameItemSameComponents(stack, targetStack)) {
int delta = targetStack.getItem().getDefaultMaxStackSize() - targetStack.getCount();
if (targetStack.getCount() >= targetStack.getItem().getMaxStackSize()) continue;
if (ItemStack.isSameItemSameTags(stack, targetStack)) {
int delta = targetStack.getItem().getMaxStackSize() - targetStack.getCount();
delta = Math.min(delta, stackSize);
stackSize -= delta;
targetStack.setCount(targetStack.getCount() + delta);
Expand Down Expand Up @@ -160,7 +160,7 @@ protected void sortOnClient(int[] sortedIds) {
stacks[id].getItem() == currentStack.getItem()
//&& stacks[id].getCount() == currentStack.getCount()
&& !doneSlashEmpty.get(slotCount + id)
&& ItemStack.isSameItemSameComponents(stacks[id], currentStack)
&& ItemStack.isSameItemSameTags(stacks[id], currentStack)
) {
// If the current stack and the target stack are completely equal, then we can skip this step in the chain
if (stacks[id].getCount() == currentStack.getCount()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@

package dev.terminalmc.clientsort.util;

import net.minecraft.client.Minecraft;
import net.minecraft.core.component.DataComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.item.*;
import net.minecraft.world.item.component.DyedItemColor;

import java.awt.*;
import java.util.Iterator;
Expand All @@ -40,30 +36,22 @@ public static int compareEqualItems(ItemStack a, ItemStack b) {

private static int compareEqualItems2(ItemStack a, ItemStack b) {
// compare names
if (hasCustomHoverName(a)) {
if (!hasCustomHoverName(b)) {
if (a.hasCustomHoverName()) {
if (!b.hasCustomHoverName()) {
return -1;
}
return compareEqualItems3(a, b);
}
if (hasCustomHoverName(b)) {
if (b.hasCustomHoverName()) {
return 1;
}
return compareEqualItems3(a, b);
}

private static boolean hasCustomHoverName(ItemStack itemStack) {
return itemStack.get(DataComponents.CUSTOM_NAME) != null;
}

private static int compareEqualItems3(ItemStack a, ItemStack b) {
// compare tooltips
Iterator<Component> tooltipsA = a.getTooltipLines(
Item.TooltipContext.of(Minecraft.getInstance().level),
null, TooltipFlag.Default.NORMAL).iterator();
Iterator<Component> tooltipsB = b.getTooltipLines(
Item.TooltipContext.of(Minecraft.getInstance().level),
null, TooltipFlag.Default.NORMAL).iterator();
Iterator<Component> tooltipsA = a.getTooltipLines(null, TooltipFlag.Default.NORMAL).iterator();
Iterator<Component> tooltipsB = b.getTooltipLines(null, TooltipFlag.Default.NORMAL).iterator();

while (tooltipsA.hasNext()) {
if (!tooltipsB.hasNext()) {
Expand All @@ -84,9 +72,9 @@ private static int compareEqualItems3(ItemStack a, ItemStack b) {
private static int compareEqualItems4(ItemStack a, ItemStack b) {
// compare special item properties
Item item = a.getItem();
if ((item.getDefaultInstance()).is(ItemTags.DYEABLE)) {
int colorA = DyedItemColor.getOrDefault(a, -6265536);
int colorB = DyedItemColor.getOrDefault(b, -6265536);
if (item instanceof DyeableLeatherItem dli) {
int colorA = dli.getColor(a);
int colorB = dli.getColor(b);
float[] hsbA = Color.RGBtoHSB(colorA >> 16 & 0xFF, colorA >> 8 & 0xFF, colorA & 0xFF, null);
float[] hsbB = Color.RGBtoHSB(colorB >> 16 & 0xFF, colorB >> 8 & 0xFF, colorB & 0xFF, null);
int cmp = Float.compare(hsbA[0], hsbB[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package dev.terminalmc.clientsort.util;

import com.google.common.base.Objects;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
Expand All @@ -28,28 +28,29 @@
// TODO: This appears to work but needs more testing.
public class StackMatcher {
private final @NotNull Item item;
private final @Nullable DataComponentMap components;
private final @Nullable CompoundTag nbt;

private StackMatcher(@NotNull Item item, @Nullable DataComponentMap components) {
private StackMatcher(@NotNull Item item, @Nullable CompoundTag nbt) {
this.item = item;
this.components = components;
this.nbt = nbt;
}

public static StackMatcher ignoreNbt(@NotNull ItemStack stack) {
return new StackMatcher(stack.getItem(), null);
}

public static StackMatcher of(@NotNull ItemStack stack) {
return new StackMatcher(stack.getItem(), stack.getComponents());
return new StackMatcher(stack.getItem(), stack.getTag());
}

@Override
public boolean equals(Object obj) {
if (obj instanceof StackMatcher matcher) {
return ItemStack.isSameItemSameComponents(item.getDefaultInstance(), matcher.item.getDefaultInstance());
return item == matcher.item && Objects.equal(nbt, matcher.nbt);
}
else if (obj instanceof ItemStack stack) {
return ItemStack.isSameItem(item.getDefaultInstance(), stack);
return item == stack.getItem() && ItemStack.isSameItemSameTags(
item.getDefaultInstance(), stack);
}
else if (obj instanceof Item objItem) {
return item == objItem;
Expand All @@ -59,6 +60,6 @@ else if (obj instanceof Item objItem) {

@Override
public int hashCode() {
return Objects.hashCode(item, components);
return Objects.hashCode(item, nbt);
}
}
36 changes: 18 additions & 18 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,38 @@ mod_license=Apache-2.0
mod_environment=client

# Java
java_version=21
java_versions_fabric=>=21
java_versions_neoforge=[21,)
java_version=17
java_versions_fabric=>=17
java_versions_neoforge=[17,)

# Minecraft
minecraft_version=1.20.6
minecraft_versions_fabric=>1.20.4 <1.21
minecraft_versions_neoforge=(1.20.4, 1.21)
minecraft_version=1.20.4
minecraft_versions_fabric=>1.20.2 <1.25
minecraft_versions_neoforge=(1.20.2, 1.20.5)

# Fabric https://fabricmc.net/develop/
fabric_loader_version=0.15.11
fabric_loader_versions=>=0.15.0
fabric_api_version=0.100.8+1.20.6
fabric_api_version=0.97.1+1.20.4
fabric_api_versions=*

# NeoForge https://projects.neoforged.net/neoforged/neoforge
neoforge_loader_versions=[1,)
neoforge_version=20.6.119
neoforge_versions=(20.4, 21)
neoforge_version=20.4.237
neoforge_versions=(20.2, 20.5)

# Cloth Config https://modrinth.com/mod/9s6osm5g/versions
clothconfig_version=14.0.126
clothconfig_versions_fabric=>13
clothconfig_versions_neoforge=(13,)
clothconfig_version=13.0.121
clothconfig_versions_fabric=>12
clothconfig_versions_neoforge=(12,)

# ModMenu https://modrinth.com/mod/mOgUt4GM/versions
modmenu_version=10.0.0
modmenu_versions=>9
modmenu_version=9.2.0
modmenu_versions=>8

# EMI https://modrinth.com/mod/fRiHVvU7/versions
emi_version_fabric=1.1.10+1.20.6+fabric
emi_version_neoforge=1.1.10+1.20.6+neoforge
emi_version_fabric=1.1.10+1.20.4+fabric
emi_version_neoforge=1.1.10+1.20.4+neoforge

# Links
homepage_url=https://modrinth.com/mod/K0AkAin6
Expand All @@ -65,12 +65,12 @@ curseforge_id=1049891
release_type=beta
# Fabric
release_mod_loaders_fabric=fabric,quilt
release_game_versions_fabric=1.20.6
release_game_versions_fabric=1.20.4
release_required_dep_ids_fabric_mr=P7dR8mSH,mOgUt4GM,9s6osm5g
release_required_dep_ids_fabric_cf=fabric-api,modmenu,cloth-config
# NeoForge
release_mod_loaders_neoforge=neoforge
release_game_versions_neoforge=1.20.6
release_game_versions_neoforge=1.20.4
release_required_dep_ids_neoforge_mr=9s6osm5g
release_required_dep_ids_neoforge_cf=cloth-config

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.client.event.ClientTickEvent;
import net.neoforged.neoforge.client.ConfigScreenHandler;
import net.neoforged.neoforge.client.event.RegisterKeyMappingsEvent;
import net.neoforged.neoforge.client.gui.IConfigScreenFactory;
import net.neoforged.neoforge.event.TickEvent;

@Mod(value = ClientSort.MOD_ID, dist = Dist.CLIENT)
@EventBusSubscriber(modid = ClientSort.MOD_ID, bus = EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
@Mod(value = ClientSort.MOD_ID)
@Mod.EventBusSubscriber(modid = ClientSort.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
public class ClientSortNeoForge {
public ClientSortNeoForge() {
// Config screen
ModLoadingContext.get().registerExtensionPoint(IConfigScreenFactory.class,
() -> (mc, parent) -> ConfigScreenProvider.getConfigScreen(parent));
ModLoadingContext.get().registerExtensionPoint(ConfigScreenHandler.ConfigScreenFactory.class,
() -> new ConfigScreenHandler.ConfigScreenFactory(
(mc, parent) -> ConfigScreenProvider.getConfigScreen(parent)
));

// Main initialization
ClientSort.init();
Expand All @@ -34,12 +35,14 @@ static void registerKeyMappingsEvent(RegisterKeyMappingsEvent event) {
event.register(ClientSort.SORT_KEY);
}

@EventBusSubscriber(modid = ClientSort.MOD_ID, value = Dist.CLIENT)
@Mod.EventBusSubscriber(modid = ClientSort.MOD_ID, value = Dist.CLIENT)
static class ClientEventHandler {
// Tick events
@SubscribeEvent
public static void clientTickEvent(ClientTickEvent.Post event) {
ClientSort.onEndTick(Minecraft.getInstance());
public static void clientTickEvent(TickEvent.ClientTickEvent event) {
if (event.phase.equals(TickEvent.Phase.END)) {
ClientSort.onEndTick(Minecraft.getInstance());
}
}
}
}

0 comments on commit e7aa9bf

Please sign in to comment.