-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
yet i still havent figured out how to add chat highlight hahaye
- Loading branch information
Showing
7 changed files
with
177 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/dev/intelligentcreations/pingme/inject/ServerPlayerEntityHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package dev.intelligentcreations.pingme.inject; | ||
|
||
import net.minecraft.network.MessageType; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Util; | ||
|
||
import java.util.UUID; | ||
|
||
public interface ServerPlayerEntityHelper { | ||
default void internalSendMessage(Text message, MessageType type, UUID sender) { | ||
} | ||
|
||
default void internalSendMessage(Text message, boolean actionBar) { | ||
this.internalSendMessage(message, actionBar ? MessageType.GAME_INFO : MessageType.CHAT, Util.NIL_UUID); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/dev/intelligentcreations/pingme/mixin/CommandSuggestorMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package dev.intelligentcreations.pingme.mixin; | ||
|
||
import com.mojang.brigadier.StringReader; | ||
import com.mojang.brigadier.suggestion.Suggestions; | ||
import com.mojang.brigadier.suggestion.SuggestionsBuilder; | ||
import dev.intelligentcreations.pingme.util.TextUtil; | ||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.client.gui.screen.CommandSuggestor; | ||
import net.minecraft.client.gui.widget.TextFieldWidget; | ||
import net.minecraft.client.network.ClientPlayNetworkHandler; | ||
import net.minecraft.command.CommandSource; | ||
import org.jetbrains.annotations.Nullable; | ||
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.callback.CallbackInfo; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.regex.Pattern; | ||
|
||
@Mixin(CommandSuggestor.class) | ||
public abstract class CommandSuggestorMixin { | ||
private static final Pattern COLON_PATTERN = Pattern.compile("(@)"); | ||
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("(\\s+)"); | ||
|
||
@Shadow @Final TextFieldWidget textField; | ||
|
||
@Shadow @Final private boolean slashOptional; | ||
|
||
@Shadow @Final private MinecraftClient client; | ||
|
||
@Shadow @Nullable private CompletableFuture<Suggestions> pendingSuggestions; | ||
|
||
@Shadow public abstract void showSuggestions(boolean narrateFirstSuggestion); | ||
|
||
@Inject( | ||
method = "refresh", | ||
at = @At("TAIL"), | ||
cancellable = true | ||
) | ||
public void onRefresh(CallbackInfo ci) { | ||
if (!this.client.isInSingleplayer()) { | ||
String message = this.textField.getText(); | ||
StringReader reader = new StringReader(message); | ||
boolean hasSlash = reader.canRead() && reader.peek() == '/'; | ||
if (hasSlash) reader.skip(); | ||
boolean isCommand = this.slashOptional || hasSlash; | ||
int cursor = this.textField.getCursor(); | ||
if (!isCommand) { | ||
String textUptoCursor = message.substring(0, cursor); | ||
int start = Math.max(TextUtil.getLastPattern(textUptoCursor, COLON_PATTERN) - 1, 0); | ||
int whitespace = TextUtil.getLastPattern(textUptoCursor, WHITESPACE_PATTERN); | ||
if (start < textUptoCursor.length() && start >= whitespace) { | ||
if (textUptoCursor.charAt(start) == '@') { | ||
List<String> playerNames = new ArrayList<>(); | ||
ClientPlayNetworkHandler networkHandler = this.client.getNetworkHandler(); | ||
if (networkHandler != null) { | ||
networkHandler.getPlayerList().forEach(entry -> | ||
playerNames.add("@" + entry.getProfile().getName()) | ||
); | ||
this.pendingSuggestions = CommandSource.suggestMatching(playerNames, new SuggestionsBuilder(textUptoCursor, start)); | ||
this.pendingSuggestions.thenRun(() -> { | ||
if (this.pendingSuggestions.isDone()) return; | ||
this.showSuggestions(false); | ||
}); | ||
ci.cancel(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/dev/intelligentcreations/pingme/util/TextUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dev.intelligentcreations.pingme.util; | ||
|
||
import com.google.common.base.Strings; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class TextUtil { | ||
public static int getLastPattern(String input, Pattern pattern){ | ||
if (Strings.isNullOrEmpty(input)) { | ||
return 0; | ||
} | ||
int i = 0; | ||
Matcher matcher = pattern.matcher(input); | ||
while (matcher.find()) { | ||
i = matcher.end(); | ||
} | ||
return i; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters