Skip to content

Commit

Permalink
Fix bug breaking creative search, close #1
Browse files Browse the repository at this point in the history
  • Loading branch information
NotRyken committed Jun 24, 2024
1 parent d9a9c96 commit 5941c47
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 58 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug breaking creative search when operator tab was enabled
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class ClientSort {
translationKey("key", "sort"), InputConstants.Type.MOUSE,
InputConstants.MOUSE_BUTTON_MIDDLE, translationKey("key_group"));

public static boolean searchOrderUpdated = false;

public static int lastUpdatedSlot = -1;

public static int cooldown = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.minecraft.client.multiplayer.CommonListenerCookie;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket;
import net.minecraft.network.protocol.game.ClientboundLoginPacket;
import net.minecraft.network.protocol.game.ClientboundSetCarriedItemPacket;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -38,13 +39,18 @@ protected MixinClientPacketListener(Minecraft client, Connection connection, Com
super(client, connection, connectionState);
}

@Inject(method = "handleLogin", at = @At("HEAD"))
private void onLogin(ClientboundLoginPacket packet, CallbackInfo ci) {
ClientSort.searchOrderUpdated = false;
}

@Inject(method = "handleSetCarriedItem", at = @At("HEAD"))
public void onHeldItemChangeBegin(ClientboundSetCarriedItemPacket packet, CallbackInfo callbackInfo) {
public void onHeldItemChangeBegin(ClientboundSetCarriedItemPacket packet, CallbackInfo ci) {
InteractionManager.triggerSend(InteractionManager.TriggerType.HELD_ITEM_CHANGE);
}

@Inject(method = "handleContainerSetSlot", at = @At("RETURN"))
public void onGuiSlotUpdateBegin(ClientboundContainerSetSlotPacket packet, CallbackInfo callbackInfo) {
public void onGuiSlotUpdateBegin(ClientboundContainerSetSlotPacket packet, CallbackInfo ci) {
ClientSort.lastUpdatedSlot = packet.getSlot();
InteractionManager.triggerSend(InteractionManager.TriggerType.CONTAINER_SLOT_UPDATE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.mojang.authlib.GameProfile;
import dev.terminalmc.clientsort.network.InteractionManager;
import dev.terminalmc.clientsort.util.CreativeSearchOrder;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.client.player.LocalPlayer;
Expand All @@ -28,6 +29,8 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import static dev.terminalmc.clientsort.ClientSort.searchOrderUpdated;

@Mixin(LocalPlayer.class)
public abstract class MixinLocalPlayer extends AbstractClientPlayer {
public MixinLocalPlayer(ClientLevel world, GameProfile profile) {
Expand All @@ -38,4 +41,12 @@ public MixinLocalPlayer(ClientLevel world, GameProfile profile) {
public void onContainerClosed(CallbackInfo callbackInfo) {
InteractionManager.clear();
}

@Inject(method = "setPermissionLevel", at = @At("RETURN"))
public void onSetPermissionLevel(int level, CallbackInfo ci) {
if (!searchOrderUpdated) {
CreativeSearchOrder.refreshItemSearchPositionLookup();
searchOrderUpdated = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import dev.terminalmc.clientsort.config.Config;
import dev.terminalmc.clientsort.inventory.sort.SortMode;
import dev.terminalmc.clientsort.util.CreativeSearchOrder;
import me.shedaniel.clothconfig2.api.ConfigBuilder;
import me.shedaniel.clothconfig2.api.ConfigCategory;
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
Expand Down Expand Up @@ -106,7 +107,10 @@ else if (val > 100) return Optional.of(
sort.addEntry(eb.startBooleanToggle(localized("option", "optimized_creative_sorting"),
options.optimizedCreativeSorting)
.setDefaultValue(Config.Options.defaultOptimizedCreativeSorting)
.setSaveConsumer(val -> options.optimizedCreativeSorting = val)
.setSaveConsumer(val -> {
options.optimizedCreativeSorting = val;
CreativeSearchOrder.refreshItemSearchPositionLookup();
})
.build());

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,18 @@
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.world.flag.FeatureFlagSet;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.item.ItemStack;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class CreativeSearchOrder {
private static FeatureFlagSet lastFeatureSet = null;
private static final Object2IntMap<StackMatcher> stackToSearchPositionLookup = new Object2IntOpenHashMap<>();
static {
stackToSearchPositionLookup.defaultReturnValue(Integer.MAX_VALUE);
Expand All @@ -53,40 +52,42 @@ public static int getStackSearchPosition(ItemStack stack) {
return pos;
}

// Called on config change and when the feature set changes (on world join)
// Called when the feature set changes (on world join)
public static void refreshItemSearchPositionLookup() {
if (Config.get().options.optimizedCreativeSorting) {
Minecraft client = Minecraft.getInstance();
if (client.level == null) {
Minecraft mc = Minecraft.getInstance();
if (mc.level == null || mc.player == null) {
return;
}
FeatureFlagSet enabledFeatures = client.level.enabledFeatures();
FeatureFlagSet enabledFeatures = mc.level.enabledFeatures();

if (stackToSearchPositionLookup.isEmpty() || !Objects.equals(enabledFeatures, lastFeatureSet)) {
CreativeModeTabs.tryRebuildTabContents(enabledFeatures, true, client.level.registryAccess());
Collection<ItemStack> displayStacks = new ArrayList<>(CreativeModeTabs.searchTab().getDisplayItems());
new Thread(() -> {
Lock lock = stackToSearchPositionLookupLock.writeLock();
lock.lock();
stackToSearchPositionLookup.clear();
if (displayStacks.isEmpty()) {
lock.unlock();
return;
}
LocalPlayer player = mc.player;
boolean op = mc.options.operatorItemsTab().get() && player.canUseGameMasterBlocks();

int i = 0;
for (ItemStack stack : displayStacks) {
StackMatcher plainMatcher = StackMatcher.ignoreNbt(stack);
if (!stack.hasFoil() || !stackToSearchPositionLookup.containsKey(plainMatcher)) {
stackToSearchPositionLookup.put(plainMatcher, i);
i++;
}
stackToSearchPositionLookup.put(StackMatcher.of(stack), i);
i++;
}
lock.unlock();
}, "Mouse Wheelie: creative search stack position lookup builder").start();
}
CreativeModeTabs.tryRebuildTabContents(enabledFeatures, !op, mc.level.registryAccess());

Collection<ItemStack> displayStacks = new ArrayList<>(CreativeModeTabs.searchTab().getDisplayItems());
new Thread(() -> {
Lock lock = stackToSearchPositionLookupLock.writeLock();
lock.lock();
stackToSearchPositionLookup.clear();
if (displayStacks.isEmpty()) {
lock.unlock();
return;
}

int i = 0;
for (ItemStack stack : displayStacks) {
StackMatcher plainMatcher = StackMatcher.ignoreNbt(stack);
if (!stack.hasFoil() || !stackToSearchPositionLookup.containsKey(plainMatcher)) {
stackToSearchPositionLookup.put(plainMatcher, i);
i++;
}
stackToSearchPositionLookup.put(StackMatcher.of(stack), i);
i++;
}
lock.unlock();
}, "Mouse Wheelie: creative search stack position lookup builder").start();

} else {
Lock lock = stackToSearchPositionLookupLock.writeLock();
Expand Down
20 changes: 10 additions & 10 deletions common/src/main/resources/clientsort.mixins.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"required": true,
"minVersion": "0.8",
"package": "dev.terminalmc.clientsort.mixin",
"refmap": "${mod_id}.refmap.json",
"compatibilityLevel": "JAVA_${java_version}",
"mixins": [
"required" : true,
"minVersion" : "0.8",
"package" : "dev.terminalmc.clientsort.mixin",
"refmap" : "${mod_id}.refmap.json",
"compatibilityLevel" : "JAVA_${java_version}",
"mixins" : [
"MixinSlot"
],
"client": [
"client" : [
"KeyMappingAccessor",
"MixinAbstractContainerScreen",
"MixinClientPacketListener",
"MixinCreativeSlot",
"MixinLocalPlayer"
],
"server": [
"server" : [
],
"injectors": {
"defaultRequire": 1
"injectors" : {
"defaultRequire" : 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

package dev.terminalmc.clientsort;

import dev.terminalmc.clientsort.util.CreativeSearchOrder;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;

public class ClientSortFabric implements ClientModInitializer {
@Override
Expand All @@ -20,10 +18,6 @@ public void onInitializeClient() {
// Tick events
ClientTickEvents.END_CLIENT_TICK.register(ClientSort::onEndTick);

// Game join events
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) ->
CreativeSearchOrder.refreshItemSearchPositionLookup());

// Main initialization
ClientSort.init();
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Neo/Forge version ranges: https://maven.apache.org/enforcer/enforcer-rules/versionRanges.html

# Project
mod_version=0.2.0
mod_version=0.3.0
mod_group=dev.terminalmc
mod_id=clientsort
mod_name=ClientSort
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
package dev.terminalmc.clientsort;

import dev.terminalmc.clientsort.screen.ConfigScreenProvider;
import dev.terminalmc.clientsort.util.CreativeSearchOrder;
import net.minecraft.client.Minecraft;
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.ClientPlayerNetworkEvent;
import net.neoforged.neoforge.client.event.ClientTickEvent;
import net.neoforged.neoforge.client.event.RegisterKeyMappingsEvent;
import net.neoforged.neoforge.client.gui.IConfigScreenFactory;
Expand All @@ -38,12 +36,6 @@ static void registerKeyMappingsEvent(RegisterKeyMappingsEvent event) {

@EventBusSubscriber(modid = ClientSort.MOD_ID, value = Dist.CLIENT)
static class ClientEventHandler {
// Game join events
@SubscribeEvent
public static void loginEvent(ClientPlayerNetworkEvent.LoggingIn event) {
CreativeSearchOrder.refreshItemSearchPositionLookup();
}

// Tick events
@SubscribeEvent
public static void clientTickEvent(ClientTickEvent.Post event) {
Expand Down

0 comments on commit 5941c47

Please sign in to comment.