[CHORE] Update orion and add chat suggestions for SDLink

This commit is contained in:
2024-05-01 23:41:14 +02:00
parent 9ffd14d9c1
commit e68ebaa5fa
6 changed files with 147 additions and 3 deletions

View File

@@ -0,0 +1,13 @@
package com.hypherionmc.craterlib.client.mentions;
/**
* Based on <a href="https://github.com/SarahIsWeird/MoreChatSuggestions/blob/main/src/main/java/com/sarahisweird/morechatsuggestions/SuggestionCondition.java">...</a>
*/
@FunctionalInterface
public interface MentionCondition {
boolean shouldAddMention(String currentWord);
MentionCondition ALWAYS = currentWord -> true;
}

View File

@@ -0,0 +1,47 @@
package com.hypherionmc.craterlib.client.mentions;
import com.hypherionmc.craterlib.nojang.resources.ResourceIdentifier;
import lombok.Getter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Based on <a href="https://github.com/SarahIsWeird/MoreChatSuggestions/blob/main/src/main/java/com/sarahisweird/morechatsuggestions/client/MoreChatSuggestions.java">...</a>
*/
public class MentionsController {
private static final Map<ResourceIdentifier, Collection<String>> mentions = new LinkedHashMap<>();
private static final Map<ResourceIdentifier, MentionCondition> mentionConditions = new LinkedHashMap<>();
@Getter
private static boolean lastMentionConditional = true;
public static void registerMention(ResourceIdentifier mentionClass, Collection<String> suggestions, MentionCondition condition) {
mentions.put(mentionClass, suggestions);
mentionConditions.put(mentionClass, condition);
}
public static Collection<String> getMentions(String currentWord) {
ArrayList<String> applicableMentions = new ArrayList<>();
lastMentionConditional = false;
mentionConditions.forEach((mention, condition) -> {
boolean shouldSuggest = condition.shouldAddMention(currentWord);
if (!shouldSuggest) return;
if (!lastMentionConditional && condition != MentionCondition.ALWAYS) {
lastMentionConditional = true;
}
applicableMentions.addAll(mentions.get(mention));
});
return applicableMentions;
}
public static boolean hasMentions() {
return !mentions.isEmpty();
}
}

View File

@@ -0,0 +1,84 @@
package com.hypherionmc.craterlib.mixin;
import com.hypherionmc.craterlib.client.mentions.MentionsController;
import net.minecraft.client.gui.components.CommandSuggestions;
import net.minecraft.client.gui.components.EditBox;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Final;
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.ModifyVariable;
import org.spongepowered.asm.mixin.injection.Slice;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.ArrayList;
import java.util.Collection;
/**
* @author HypherionSA
* Allow Users, Roles and Channels to be pingable from MC chat (Client Side)
*/
@Mixin(CommandSuggestions.class)
public abstract class ChatInputSuggestorMixin {
@Shadow
public abstract void showSuggestions(boolean p_93931_);
@Shadow @Final
EditBox input;
@Shadow
private static int getLastWordIndex(String p_93913_) {
return 0;
}
@Inject(
method = "updateCommandInfo",
at = @At(
value = "FIELD",
target = "Lnet/minecraft/client/gui/components/CommandSuggestions;pendingSuggestions:Ljava/util/concurrent/CompletableFuture;",
opcode = Opcodes.PUTFIELD,
shift = At.Shift.AFTER,
ordinal = 0
),
slice = @Slice(
from = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/gui/components/CommandSuggestions;getLastWordIndex(Ljava/lang/String;)I"
)
)
)
private void injectSuggestions(CallbackInfo ci) {
if (MentionsController.hasMentions() && MentionsController.isLastMentionConditional()) {
this.showSuggestions(true);
}
}
@SuppressWarnings("InvalidInjectorMethodSignature")
@ModifyVariable(method = "updateCommandInfo", at = @At(value = "STORE"), ordinal = 0, name = "collection")
private Collection<String> injectMentions(Collection<String> vanilla) {
if (!MentionsController.hasMentions())
return vanilla;
ArrayList<String> newSuggest = new ArrayList<>(vanilla);
String currentInput = this.input.getValue();
int currentCursorPosition = this.input.getCursorPosition();
String textBeforeCursor = currentInput.substring(0, currentCursorPosition);
int startOfCurrentWord = getLastWordIndex(textBeforeCursor);
String currentWord = textBeforeCursor.substring(startOfCurrentWord);
String finalWord = currentWord.replace("[", "").replace("]", "");
Collection<String> mentions = MentionsController.getMentions(finalWord);
if (!mentions.isEmpty()) {
mentions.forEach(m -> newSuggest.add("[" + m + "]"));
}
return newSuggest;
}
}

View File

@@ -6,6 +6,7 @@
"mixins": [
],
"client": [
"ChatInputSuggestorMixin",
"events.PlayerMixin",
"events.client.ClientLevelMixin",
"events.client.MinecraftMixin",

View File

@@ -56,7 +56,6 @@ jar {
processResources {
from project(":Common").sourceSets.main.resources
def buildProps = project.properties.clone()
println(project.version)
filesMatching(['fabric.mod.json']) {
expand buildProps

View File

@@ -3,11 +3,11 @@ plugins {
id 'com.github.johnrengelman.shadow' version '8.1.1' apply false
id "xyz.wagyourtail.unimined" version "1.1.0-SNAPSHOT" apply false
id "me.hypherionmc.modutils.modpublisher" version "1.0.23+"
id "com.hypherionmc.modutils.orion" version "1.0.7"
id "com.hypherionmc.modutils.orion" version "1.0.9"
id 'maven-publish'
}
orion {
orion.setup {
multiProject = true
enableMirrorMaven = true
enableReleasesMaven = true