generated from FabricMC/fabric-example-mod
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add chat listener and "assert chat" command
- Loading branch information
Showing
9 changed files
with
148 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.github.misode.packtest; | ||
|
||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import org.apache.commons.compress.utils.Lists; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
public class ChatListener { | ||
|
||
private static final List<ChatListener> listeners = Lists.newArrayList(); | ||
|
||
public static void broadcast(ServerPlayer player, Component chatMessage) { | ||
Message message = new Message(player.getName().getString(), chatMessage.getString()); | ||
ChatListener.listeners.forEach(l -> l.messages.add(message)); | ||
} | ||
|
||
public ChatListener() { | ||
ChatListener.listeners.add(this); | ||
} | ||
|
||
public final List<Message> messages = Lists.newArrayList(); | ||
|
||
public void stop() { | ||
ChatListener.listeners.remove(this); | ||
} | ||
|
||
public List<String> filter(Predicate<Message> predicate) { | ||
return this.messages.stream() | ||
.filter(predicate) | ||
.map(m -> m.content) | ||
.toList(); | ||
} | ||
|
||
public void reset() { | ||
this.messages.clear(); | ||
} | ||
|
||
public record Message(String player, String content) {} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package io.github.misode.packtest; | ||
|
||
import net.minecraft.gametest.framework.GameTestInfo; | ||
|
||
public interface PackTestHelper { | ||
boolean packtest$isFinalCheckAdded(); | ||
GameTestInfo packtest$getInfo(); | ||
} |
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,6 @@ | ||
package io.github.misode.packtest; | ||
|
||
public interface PackTestInfo { | ||
void packtest$setChatListener(ChatListener listener); | ||
ChatListener packtest$getChatListener(); | ||
} |
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
24 changes: 23 additions & 1 deletion
24
src/main/java/io/github/misode/packtest/mixin/GameTestInfoMixin.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 |
---|---|---|
@@ -1,19 +1,41 @@ | ||
package io.github.misode.packtest.mixin; | ||
|
||
import io.github.misode.packtest.ChatListener; | ||
import io.github.misode.packtest.PackTestInfo; | ||
import net.minecraft.gametest.framework.GameTestInfo; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
/** | ||
* Adds chat listener field and accessors. Removes the listener when finishing. | ||
* Prevents crash when test has already started. | ||
*/ | ||
@Mixin(GameTestInfo.class) | ||
public abstract class GameTestInfoMixin { | ||
public class GameTestInfoMixin implements PackTestInfo { | ||
|
||
@Unique | ||
private ChatListener chatListener; | ||
|
||
@Override | ||
public void packtest$setChatListener(ChatListener chatListener) { | ||
this.chatListener = chatListener; | ||
} | ||
|
||
@Override | ||
public ChatListener packtest$getChatListener() { | ||
return this.chatListener; | ||
} | ||
|
||
@Inject(method = "startTest", cancellable = true, at = @At(value = "INVOKE", target = "Ljava/lang/IllegalStateException;<init>(Ljava/lang/String;)V")) | ||
private void startTest(CallbackInfo ci) { | ||
ci.cancel(); | ||
} | ||
|
||
@Inject(method = "finish", at = @At("HEAD")) | ||
private void finish(CallbackInfo ci) { | ||
this.chatListener.stop(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/io/github/misode/packtest/mixin/ServerPlayerMixin.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,21 @@ | ||
package io.github.misode.packtest.mixin; | ||
|
||
import io.github.misode.packtest.ChatListener; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.server.level.ServerPlayer; | ||
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; | ||
|
||
/** | ||
* Logs all chat messages sent to players | ||
*/ | ||
@Mixin(ServerPlayer.class) | ||
public class ServerPlayerMixin { | ||
|
||
@Inject(method = "sendSystemMessage(Lnet/minecraft/network/chat/Component;Z)V", at = @At("HEAD")) | ||
private void sendSystemMessage(Component message, boolean bl, CallbackInfo ci) { | ||
ChatListener.broadcast((ServerPlayer)(Object)this, message); | ||
} | ||
} |
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