Skip to content

Fix crash when a keybinding with a category not known to MC gets added #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: jitpack
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.dimdev.rift.listener.client;

import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.command.CommandSource;

public interface LocalCommandAdder {
void registerLocalCommands(CommandDispatcher<CommandSource> dispatcher);
}
54 changes: 54 additions & 0 deletions src/main/java/org/dimdev/rift/mixin/hook/client/MixinGuiChat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.dimdev.rift.mixin.hook.client;

import com.mojang.brigadier.suggestion.Suggestion;
import net.minecraft.client.gui.GuiChat;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import com.mojang.brigadier.suggestion.Suggestions;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import net.minecraft.client.gui.GuiTextField;
import org.dimdev.rift.util.LocalCommandManager;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(GuiChat.class)
public class MixinGuiChat {

@Shadow GuiTextField inputField;

@Redirect(method="showSuggestions",
at=@At(value="INVOKE",
target="Ljava/util/concurrent/CompletableFuture;join()Ljava/lang/Object;",
remap=false)
)

// We need to declare this to return Object, not Suggestions, because the
// redirected method is type polymorphic and thus returns Object,
// not Suggestions, in its byte code definition.

public Object gotServerSideSuggestions(CompletableFuture<Suggestions> pendingSuggestions) {
Suggestions server=pendingSuggestions.join();
Suggestions local=LocalCommandManager.getSuggestions(inputField.getText());

if (local==null || local.isEmpty()) {
return server;
}

if (server.isEmpty()) {
return local;
}

if (!local.getRange().equals(server.getRange())) {
System.err.println("something wrong with ranges");
return server;
}

List<Suggestion> results=new ArrayList<>();
results.addAll(server.getList());
results.addAll(local.getList());
Suggestions result=new Suggestions(server.getRange(), results);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.dimdev.rift.mixin.hook.client;

import java.util.Map;
import net.minecraft.client.settings.KeyBinding;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(KeyBinding.class)
public class MixinKeyBinding {
@Shadow private static Map<String, Integer> CATEGORY_ORDER;
@Inject(method = "<init>", at = @At("RETURN"))
private void insertCategory(String description,
int keycode, String category, CallbackInfo ci) {
if (CATEGORY_ORDER.get(category)==null) {
CATEGORY_ORDER.put(category, CATEGORY_ORDER.size());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.dimdev.rift.mixin.hook.client;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.client.entity.EntityPlayerSP;
import org.dimdev.rift.util.LocalCommandManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(EntityPlayerSP.class)
public class MixinLocalCommand {
@Inject(method="sendChatMessage", at=@At("HEAD"), cancellable=true)
private void handleLocalCommand(String message, CallbackInfo callbackInfo) {
if (message.startsWith("/")) {
try {
LocalCommandManager.dispatchLocalCommand(message.substring(1));
callbackInfo.cancel();
} catch (CommandSyntaxException ex) {
// Don't do anything, it wasn't intended to be our command.
// Not cancelling callbackInfo will make MC send the command to the server.
}
}
}
}
67 changes: 67 additions & 0 deletions src/main/java/org/dimdev/rift/util/LocalCommandManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.dimdev.rift.util;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import java.util.concurrent.CompletableFuture;
import net.minecraft.command.CommandSource;
import org.dimdev.rift.listener.client.LocalCommandAdder;
import org.dimdev.riftloader.RiftLoader;

/**
*
* @author gbl
*/
public class LocalCommandManager {

private static LocalCommandManager instance;
private CommandDispatcher<CommandSource> dispatcher;
private CompletableFuture<Suggestions> suggestions;

private LocalCommandManager() {
}

public static LocalCommandManager getInstance() {
if (instance == null) {
instance=new LocalCommandManager();
instance.dispatcher=new CommandDispatcher<>();

for (LocalCommandAdder localCommandAdder: RiftLoader.instance.getListeners(LocalCommandAdder.class)) {
localCommandAdder.registerLocalCommands(instance.dispatcher);
}
}
return instance;
}

public static void dispatchLocalCommand(String s) throws CommandSyntaxException {
getInstance().dispatcher.execute(s, null);
}

private Suggestions getSuggestionsFor(String command) {
if (!command.startsWith("/")) {
return null;
}
// Don't just pass command; pass a stringreader that skips over the '/',
// or Suggestions.range won't match the server version.
StringReader reader=new StringReader(command);
reader.skip();
ParseResults<CommandSource> parse = dispatcher.parse(reader, null);
// We are losing the advantage of using a separate thread here,
// but the server commands, which imply a network exchange,
// need it much more than we do.
suggestions = dispatcher.getCompletionSuggestions(parse);
Suggestions result = suggestions.join();
return result;
}

public static Suggestions getSuggestions(String s) {
return getInstance().getSuggestionsFor(s);
}
}
5 changes: 4 additions & 1 deletion src/main/resources/mixins.rift.hooks.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@
"MixinItemTool"
],
"client": [
"client.MixinGuiChat",
"client.MixinLocalCommand",
"client.MixinMinecraft",
"client.MixinModelBakery",
"client.MixinNetHandlerPlayClient",
"client.MixinTileEntityRendererDispatcher",
"client.MixinRenderManager",
"client.MixinEntityPlayerSP",
"client.MixinGameSettings",
"client.MixinGuiIngame"
"client.MixinGuiIngame",
"client.MixinKeyBinding"
]
}