Skip to content

Commit

Permalink
SelectWorldScreen#worldSelected - ensure buttons aren't null
Browse files Browse the repository at this point in the history
Fixes #25, which occurred when the world list was filtered before buttons were initialised, and then those buttons' `active` fields were attempted to be set, causing a null pointer exception.
  • Loading branch information
Antikyth committed Oct 15, 2023
1 parent 1db544f commit 9f71551
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package io.github.antikyth.searchable.mixin.select_world;

import com.llamalad7.mixinextras.injector.WrapWithCondition;
import io.github.antikyth.searchable.Searchable;
import io.github.antikyth.searchable.accessor.TextFieldWidgetValidityAccessor;
import io.github.antikyth.searchable.config.SearchableConfig;
Expand All @@ -14,8 +15,10 @@
import io.github.antikyth.searchable.util.match.MatchManager;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.world.SelectWorldScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.text.Text;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
Expand All @@ -34,6 +37,15 @@ public abstract class SelectWorldScreenMixin extends Screen {
@Shadow
protected TextFieldWidget searchBox;

@Shadow
private ButtonWidget selectButton;
@Shadow
private ButtonWidget editButton;
@Shadow
private ButtonWidget recreateButton;
@Shadow
private ButtonWidget deleteButton;

protected SelectWorldScreenMixin(Text title) {
super(title);
}
Expand Down Expand Up @@ -92,4 +104,16 @@ private void onSearchBoxChange(String query, CallbackInfo ci) {

((TextFieldWidgetValidityAccessor) this.searchBox).searchable$setValidity(validityError);
}

/**
* Only update buttons' {@link ButtonWidget#active active} field if the button is not null.
*/
@WrapWithCondition(method = "worldSelected", at = @At(
value = "FIELD",
target = "net/minecraft/client/gui/widget/ButtonWidget.active : Z",
opcode = Opcodes.PUTFIELD
))
public boolean onWorldSelectedButtonActiveSet(ButtonWidget button, boolean buttonActive, boolean active, boolean deleteButtonActive) {
return button != null;
}
}

0 comments on commit 9f71551

Please sign in to comment.