Skip to content

Commit fba2587

Browse files
committed
Inject into vanilla msg command
1 parent 4608c68 commit fba2587

10 files changed

+112
-37
lines changed

src/main/java/com/lx862/svrutil/Commands.java

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public static void register(CommandDispatcher<net.minecraft.server.command.Serve
2626
playsoundarea.register(dispatcher);
2727
rootCommand.register(dispatcher);
2828
lightblock.register(dispatcher);
29-
msg.register(dispatcher);
3029
opLevel.register(dispatcher);
3130
nether.register(dispatcher);
3231
overworld.register(dispatcher);

src/main/java/com/lx862/svrutil/SvrUtil.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
public class SvrUtil implements ModInitializer {
1313
public static final String[] motds = {"Have a great day!", "OwO What's this?", "made ya look", "Drink water!", "i use arch btw", "The mystery is of Wet and Dry. And where does the solution lie?", "Backup Regularly >.<", "I'll be back in a jiffy!", "Treasure everything before it's no longer there.", "3 Billion Devices runs Java!"};
14-
public static final HashMap<String, String> lastReply = new HashMap<>();
14+
public static final HashMap<String, Collection<String>> lastReply = new HashMap<>();
1515
public static final HashMap<UUID, Long> fakeTimeList = new HashMap<>();
1616

1717
@Override

src/main/java/com/lx862/svrutil/commands/msg.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
3232
.then(CommandManager.argument("target", EntityArgumentType.player())
3333
.then(CommandManager.argument("message", StringArgumentType.greedyString())
3434
.executes(context -> {
35-
String playerName = context.getSource().getName();
36-
ServerPlayerEntity target = EntityArgumentType.getPlayer(context, "target");
37-
String message = StringArgumentType.getString(context, "message");
38-
context.getSource().sendFeedback(Text.literal(String.format("§6[me §r-> §6%s]: §r%s", target.getGameProfile().getName(), message)), false);
39-
target.sendMessage(Text.literal(String.format("§6[%s §r-> §6me]: §r%s", playerName, message)), false);
40-
SvrUtil.lastReply.put(target.getGameProfile().getName(), playerName);
41-
SvrUtil.lastReply.put(playerName, target.getGameProfile().getName());
42-
target.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.MASTER, 1, 1);
43-
44-
if(afk.afkList.containsKey(target.getUuid()) && context.getSource().isExecutedByPlayer()) {
45-
context.getSource().getPlayer().sendMessageToClient(Text.literal("").append(target.getDisplayName()).append(" are AFK and may not be available at the moment.").formatted(Formatting.YELLOW), true);
46-
}
47-
48-
Commands.finishedExecution(context, defaultEntry);
35+
// String playerName = context.getSource().getName();
36+
// ServerPlayerEntity target = EntityArgumentType.getPlayer(context, "target");
37+
// String message = StringArgumentType.getString(context, "message");
38+
// context.getSource().sendFeedback(Text.literal(String.format("§6[me §r-> §6%s]: §r%s", target.getGameProfile().getName(), message)), false);
39+
// target.sendMessage(Text.literal(String.format("§6[%s §r-> §6me]: §r%s", playerName, message)), false);
40+
// SvrUtil.lastReply.put(target.getGameProfile().getName(), playerName);
41+
// SvrUtil.lastReply.put(playerName, target.getGameProfile().getName());
42+
// target.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.MASTER, 1, 1);
43+
//
44+
// if(afk.afkList.containsKey(target.getUuid()) && context.getSource().isExecutedByPlayer()) {
45+
// context.getSource().getPlayer().sendMessageToClient(Text.literal("").append(target.getDisplayName()).append(" are AFK and may not be available at the moment.").formatted(Formatting.YELLOW), true);
46+
// }
47+
//
48+
// Commands.finishedExecution(context, defaultEntry);
4949
return 1;
5050
}))
5151
)

src/main/java/com/lx862/svrutil/commands/r.java

+14-13
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
import net.minecraft.server.command.CommandManager;
1010
import net.minecraft.server.command.ServerCommandSource;
1111
import net.minecraft.server.network.ServerPlayerEntity;
12-
import net.minecraft.sound.SoundCategory;
13-
import net.minecraft.sound.SoundEvents;
1412
import net.minecraft.text.Text;
1513
import net.minecraft.util.Formatting;
1614

15+
import java.util.Collection;
16+
1717
public class r {
1818
private static final CommandEntry defaultEntry = new CommandEntry("r", 0, true);
1919

@@ -26,23 +26,24 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
2626
.then(CommandManager.argument("message", StringArgumentType.greedyString())
2727
.executes(context -> {
2828
String player = context.getSource().getName();
29-
ServerPlayerEntity target = context.getSource().getServer().getPlayerManager().getPlayer(SvrUtil.lastReply.get(player));
30-
if (target == null) {
29+
Collection<String> targets = SvrUtil.lastReply.get(player);
30+
if(targets == null || targets.isEmpty()) {
3131
context.getSource().sendFeedback(Text.literal("Either no one have replied to you, or the player is offline.").formatted(Formatting.GRAY), false);
3232
return 1;
3333
}
3434

35-
if (context.getSource().getWorld().getPlayerByUuid(target.getUuid()) == null) {
36-
context.getSource().sendFeedback(Text.literal("The player you were messaging with are now offline").formatted(Formatting.RED), false);
37-
return 1;
35+
for(String targetName : targets) {
36+
ServerPlayerEntity target = context.getSource().getServer().getPlayerManager().getPlayer(targetName);
37+
if (context.getSource().getWorld().getPlayerByUuid(target.getUuid()) == null) {
38+
context.getSource().sendFeedback(Text.literal(String.format("Cannot reply as %s is now offline")).formatted(Formatting.RED), false);
39+
continue;
40+
}
41+
42+
String message = StringArgumentType.getString(context, "message");
43+
context.getSource().getServer().getCommandManager().executeWithPrefix(context.getSource().getPlayer().getCommandSource(), String.format("msg %s %s", target.getGameProfile().getName(), message));
44+
Commands.finishedExecution(context, defaultEntry);
3845
}
3946

40-
String message = StringArgumentType.getString(context, "message");
41-
context.getSource().sendFeedback(Text.literal(String.format("§6[me §r-> §6%s]: §r%s", target.getGameProfile().getName(), message)), false);
42-
target.sendMessage(Text.literal(String.format("§6[%s §r-> §6me]: §r%s", player, message)), false);
43-
target.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.MASTER, 1, 1);
44-
SvrUtil.lastReply.put(target.getGameProfile().getName(), player);
45-
Commands.finishedExecution(context, defaultEntry);
4647
return 1;
4748
})));
4849
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.lx862.svrutil.feature;
2+
3+
import com.google.gson.JsonObject;
4+
import net.minecraft.server.MinecraftServer;
5+
6+
public class FancyMessageFeature extends Feature {
7+
8+
public FancyMessageFeature() {
9+
super("Fancy Message", "fancy_message");
10+
}
11+
12+
@Override
13+
public void readConfig(JsonObject jsonObject) {
14+
super.readConfig(jsonObject);
15+
}
16+
17+
@Override
18+
public JsonObject writeConfig() {
19+
JsonObject jsonObject = super.writeConfig();
20+
return jsonObject;
21+
}
22+
}

src/main/java/com/lx862/svrutil/feature/FeatureSet.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
public enum FeatureSet {
44
JOIN_MESSAGE(new JoinMessageFeature()),
55
HUNGER(new HungerFeature()),
6-
CUSTOM_MESSAGE(new CustomMessageFeature()),
7-
VANILLA_MECHANICS(new VanillaMechanicsFeature());
6+
TEXT_OVERRIDE(new TextOverrideFeature()),
7+
VANILLA_MECHANICS(new VanillaMechanicsFeature()),
8+
FANCY_MESSAGE(new FancyMessageFeature());
89

910
public final Feature feature;
1011

src/main/java/com/lx862/svrutil/feature/CustomMessageFeature.java src/main/java/com/lx862/svrutil/feature/TextOverrideFeature.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import com.google.gson.JsonObject;
55
import net.minecraft.text.Text;
66

7-
public class CustomMessageFeature extends Feature {
7+
public class TextOverrideFeature extends Feature {
88
private static Text whitelistedMessage = null;
99

10-
public CustomMessageFeature() {
11-
super("Custom Message", "custom_message");
10+
public TextOverrideFeature() {
11+
super("Text Override", "text_override");
1212
}
1313

1414
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.lx862.svrutil.mixin;
2+
3+
import com.lx862.svrutil.SvrUtil;
4+
import com.lx862.svrutil.commands.afk;
5+
import com.lx862.svrutil.feature.FeatureSet;
6+
import net.minecraft.command.argument.MessageArgumentType;
7+
import net.minecraft.server.command.MessageCommand;
8+
import net.minecraft.server.command.ServerCommandSource;
9+
import net.minecraft.server.network.ServerPlayerEntity;
10+
import net.minecraft.sound.SoundCategory;
11+
import net.minecraft.sound.SoundEvents;
12+
import net.minecraft.text.Text;
13+
import net.minecraft.util.Formatting;
14+
import org.spongepowered.asm.mixin.Mixin;
15+
import org.spongepowered.asm.mixin.injection.At;
16+
import org.spongepowered.asm.mixin.injection.Inject;
17+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
18+
19+
import java.util.Collection;
20+
import java.util.List;
21+
22+
@Mixin(MessageCommand.class)
23+
public class MessageCommandMixin {
24+
@Inject(method = "execute", at = @At("HEAD"), cancellable = true)
25+
private static void execute(ServerCommandSource source, Collection<ServerPlayerEntity> targets, MessageArgumentType.SignedMessage signedMessage, CallbackInfoReturnable<Integer> cir) {
26+
String playerName = source.getName();
27+
28+
SvrUtil.lastReply.put(source.getName(), targets.stream().map(e -> e.getGameProfile().getName()).toList());
29+
30+
for(ServerPlayerEntity player : targets) {
31+
SvrUtil.lastReply.put(player.getGameProfile().getName(), List.of(source.getName()));
32+
}
33+
34+
if(FeatureSet.FANCY_MESSAGE.feature.enabled) {
35+
signedMessage.decorate(source, message -> {
36+
for(ServerPlayerEntity target : targets) {
37+
Text msgContent = message.getContent();
38+
39+
source.sendFeedback(Text.literal(String.format("§6[me §r-> §6%s]: ", target.getGameProfile().getName())).append(msgContent), false);
40+
target.sendMessage(Text.literal(String.format("§6[%s §r-> §6me]: ", playerName)).append(msgContent), false);
41+
target.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, SoundCategory.MASTER, 1, 1);
42+
43+
if(afk.afkList.containsKey(target.getUuid()) && source.isExecutedByPlayer()) {
44+
source.getPlayer().sendMessageToClient(Text.literal("").append(target.getDisplayName()).append(" are AFK and may not be available at the moment.").formatted(Formatting.YELLOW), true);
45+
}
46+
}
47+
});
48+
cir.setReturnValue(1);
49+
}
50+
}
51+
}

src/main/java/com/lx862/svrutil/mixin/PlayerManagerMixin.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.lx862.svrutil.mixin;
22

33
import com.lx862.svrutil.SvrUtil;
4-
import com.lx862.svrutil.feature.CustomMessageFeature;
4+
import com.lx862.svrutil.feature.TextOverrideFeature;
55
import com.lx862.svrutil.feature.Feature;
66
import com.lx862.svrutil.feature.FeatureSet;
77
import com.mojang.authlib.GameProfile;
@@ -46,8 +46,8 @@ public void sendToDimension(Packet<?> packet, RegistryKey<World> dimension, Call
4646

4747
@Inject(method = "checkCanJoin", at = @At(value = "RETURN", ordinal = 1), cancellable = true)
4848
public void checkCanJoinWhitelist(SocketAddress address, GameProfile profile, CallbackInfoReturnable<Text> cir) {
49-
Feature featureSet = FeatureSet.CUSTOM_MESSAGE.feature;
50-
Text whitelistedMessage = ((CustomMessageFeature)featureSet).getWhitelistedMessage();
49+
Feature featureSet = FeatureSet.TEXT_OVERRIDE.feature;
50+
Text whitelistedMessage = ((TextOverrideFeature)featureSet).getWhitelistedMessage();
5151
if(featureSet.enabled && whitelistedMessage != null) {
5252
cir.setReturnValue(whitelistedMessage);
5353
cir.cancel();

src/main/resources/svrutil.mixins.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"mixins": [
77
"FallingBlockMixin",
88
"ItemFrameMixin",
9+
"MessageCommandMixin",
910
"PlayerManagerMixin"
1011
],
1112
"client": [

0 commit comments

Comments
 (0)