Skip to content

Commit a0050cb

Browse files
committed
Updated to version 1.1.0
- Added `__on_chat_message('message', 'type', 'entity')` event handler (by ReplaceItem) - All `__on_twitch` events will now pass an entity as first parameter if the player is online or its nickname if it's offline - Updated `sctwitch_event_test.sc` script - Created `on_message_event_test.sc` script
1 parent c296eff commit a0050cb

File tree

11 files changed

+224
-139
lines changed

11 files changed

+224
-139
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ loader_version = 0.10.6+build.214
77
carpet_core_version = 1.4.15+v201102
88

99
#Mod properties
10-
mod_version = 1.0.2
10+
mod_version = 1.1.0
1111
maven_group = it.multicoredev.sti
1212
archives_base_name = sctwitch
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package de.replaceitem.discarpet;
2+
3+
import net.minecraft.text.Text;
4+
5+
import java.util.UUID;
6+
7+
public interface EventInteface {
8+
void onChatMessage(Text text, UUID senderUuid);
9+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package de.replaceitem.discarpet;
2+
3+
import net.minecraft.entity.Entity;
4+
import net.minecraft.server.world.ServerWorld;
5+
import net.minecraft.text.LiteralText;
6+
import net.minecraft.text.Text;
7+
import net.minecraft.text.TranslatableText;
8+
9+
import java.util.Arrays;
10+
import java.util.UUID;
11+
12+
import carpet.CarpetServer;
13+
import carpet.script.CarpetEventServer;
14+
import carpet.script.value.EntityValue;
15+
import carpet.script.value.NullValue;
16+
import carpet.script.value.StringValue;
17+
18+
public class ScarpetEvents {
19+
public static final CarpetEventServer.Event CHAT_MESSAGE = new CarpetEventServer.Event("chat_message", 3, false) {
20+
public void onChatMessage(Text text, UUID senderUuid) {
21+
this.handler.call(
22+
() -> {
23+
String message;
24+
String type;
25+
if (text instanceof TranslatableText) {
26+
type = ((TranslatableText) text).getKey();
27+
message = text.getString();
28+
} else if (text instanceof LiteralText) {
29+
type = text.getClass().getName();
30+
message = (text).getString();
31+
} else {
32+
message = null;
33+
type = text.getClass().getName();
34+
System.out.println("Unknown chat event type: " + type);
35+
}
36+
37+
Entity e = null;
38+
for (ServerWorld w : CarpetServer.minecraft_server.getWorlds()) {
39+
if (w.getEntity(senderUuid) != null) e = w.getEntity(senderUuid);
40+
}
41+
42+
return Arrays.asList(
43+
new StringValue(message),
44+
new StringValue(type),
45+
e != null ? new EntityValue(e) : new NullValue()
46+
);
47+
48+
},
49+
() -> {
50+
if (senderUuid != null)
51+
for (ServerWorld w : CarpetServer.minecraft_server.getWorlds()) {
52+
if (w.getEntity(senderUuid) != null)
53+
return w.getEntity(senderUuid).getCommandSource();
54+
}
55+
return CarpetServer.minecraft_server.getCommandSource();
56+
}
57+
);
58+
}
59+
};
60+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package de.replaceitem.discarpet.mixins;
2+
3+
import net.minecraft.text.Text;
4+
5+
import org.spongepowered.asm.mixin.Mixin;
6+
7+
import java.util.UUID;
8+
9+
import carpet.script.CarpetEventServer;
10+
import de.replaceitem.discarpet.EventInteface;
11+
12+
@Mixin(CarpetEventServer.Event.class)
13+
public class CarpetEvent_Mixin implements EventInteface {
14+
public void onChatMessage(Text text, UUID senderUuid) {
15+
}
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package de.replaceitem.discarpet.mixins;
2+
3+
4+
import de.replaceitem.discarpet.EventInteface;
5+
import de.replaceitem.discarpet.ScarpetEvents;
6+
7+
import net.minecraft.server.MinecraftServer;
8+
import net.minecraft.text.Text;
9+
import org.spongepowered.asm.mixin.Mixin;
10+
import org.spongepowered.asm.mixin.injection.At;
11+
import org.spongepowered.asm.mixin.injection.Inject;
12+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
13+
14+
import java.util.UUID;
15+
16+
@Mixin(MinecraftServer.class)
17+
public class MinecraftChat_Mixin {
18+
@Inject(at = @At("RETURN"),method = "sendSystemMessage(Lnet/minecraft/text/Text;Ljava/util/UUID;)V")
19+
public void redirectChatToScarpet(Text message, UUID senderUuid, CallbackInfo ci) {
20+
((EventInteface) ScarpetEvents.CHAT_MESSAGE).onChatMessage(message, senderUuid);
21+
}
22+
}

src/main/java/it/multicoredev/sti/ScTwitch.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import carpet.CarpetExtension;
1717
import carpet.CarpetServer;
18+
import carpet.script.CarpetExpression;
1819
import carpet.script.CarpetScriptServer;
1920
import carpet.script.bundled.BundledModule;
2021
import it.multicoredev.sti.config.Config;
@@ -34,6 +35,7 @@ public class ScTwitch implements CarpetExtension {
3435
static {
3536
CarpetServer.manageExtension(new ScTwitch());
3637
CarpetScriptServer.registerBuiltInScript(sctwitchDefaultScript("sctwitch_event_test", false));
38+
CarpetScriptServer.registerBuiltInScript(sctwitchDefaultScript("chat_message_event_test", false));
3739
}
3840

3941
public static BundledModule sctwitchDefaultScript(String scriptName, boolean isLibrary) {
@@ -99,4 +101,4 @@ public void onReload(MinecraftServer server) {
99101
public void onServerClosed(MinecraftServer server) {
100102
stopSockets();
101103
}
102-
}
104+
}

src/main/java/it/multicoredev/sti/scarpet/ScarpetTwitchEvents.java

Lines changed: 38 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import carpet.CarpetServer;
1010
import carpet.script.CarpetEventServer.Event;
11+
import carpet.script.value.EntityValue;
1112
import carpet.script.value.ListValue;
1213
import carpet.script.value.NumericValue;
1314
import carpet.script.value.StringValue;
@@ -18,171 +19,123 @@ public class ScarpetTwitchEvents extends Event {
1819
public static ScarpetTwitchEvents TWITCH_SUBSCRIPTION = new ScarpetTwitchEvents("twitch_subscription", 8, false) {
1920
@Override
2021
public void onTwitchEvent(String playerName, TwitchEvent event) {
22+
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(playerName);
2123
handler.call(
2224
() -> Arrays.asList(
23-
new StringValue(playerName),
24-
new StringValue(event.getNickname()),
25-
new StringValue(event.getMsg()),
26-
new NumericValue(event.getSubscriptionTier()),
27-
new NumericValue(event.getSubscriptionMonths()),
28-
new NumericValue(event.isResubbed()),
29-
new NumericValue(event.getSubscriptionStreakMonths()),
30-
new NumericValue(event.isGifted())
25+
player != null ? new EntityValue(player) : new StringValue(playerName),
26+
new StringValue(event.getNickname()),
27+
new StringValue(event.getMsg()),
28+
new NumericValue(event.getSubscriptionTier()),
29+
new NumericValue(event.getSubscriptionMonths()),
30+
new NumericValue(event.isResubbed()),
31+
new NumericValue(event.getSubscriptionStreakMonths()),
32+
new NumericValue(event.isGifted())
3133
),
32-
() -> {
33-
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(playerName);
34-
if (player != null) {
35-
return player.getCommandSource();
36-
} else {
37-
return CarpetServer.minecraft_server.getCommandSource();
38-
}
39-
}
34+
() -> player != null ? player.getCommandSource() : CarpetServer.minecraft_server.getCommandSource()
4035
);
4136
}
4237
};
4338
public static ScarpetTwitchEvents TWITCH_DONATION = new ScarpetTwitchEvents("twitch_donation", 6, false) {
4439
@Override
4540
public void onTwitchEvent(String playerName, TwitchEvent event) {
41+
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(playerName);
4642
handler.call(
4743
() -> Arrays.asList(
48-
new StringValue(playerName),
49-
new StringValue(event.getNickname()),
50-
new StringValue(event.getMsg()),
51-
new NumericValue(event.getDonationAmount()),
52-
new StringValue(event.getFormattedAmount()),
53-
new StringValue(event.getDonationCurrency())
44+
player != null ? new EntityValue(player) : new StringValue(playerName),
45+
new StringValue(event.getNickname()),
46+
new StringValue(event.getMsg()),
47+
new NumericValue(event.getDonationAmount()),
48+
new StringValue(event.getFormattedAmount()),
49+
new StringValue(event.getDonationCurrency())
5450
),
55-
() -> {
56-
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(playerName);
57-
if (player != null) {
58-
return player.getCommandSource();
59-
} else {
60-
return CarpetServer.minecraft_server.getCommandSource();
61-
}
62-
}
51+
() -> player != null ? player.getCommandSource() : CarpetServer.minecraft_server.getCommandSource()
6352
);
6453
}
6554
};
6655
public static ScarpetTwitchEvents TWITCH_FOLLOW = new ScarpetTwitchEvents("twitch_follow", 2, false) {
6756
@Override
6857
public void onTwitchEvent(String playerName, TwitchEvent event) {
58+
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(playerName);
6959
handler.call(
7060
() -> Arrays.asList(
71-
new StringValue(playerName),
72-
new StringValue(event.getNickname())
61+
player != null ? new EntityValue(player) : new StringValue(playerName),
62+
new StringValue(event.getNickname())
7363
),
74-
() -> {
75-
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(playerName);
76-
if (player != null) {
77-
return player.getCommandSource();
78-
} else {
79-
return CarpetServer.minecraft_server.getCommandSource();
80-
}
81-
}
64+
() -> player != null ? player.getCommandSource() : CarpetServer.minecraft_server.getCommandSource()
8265
);
8366
}
8467
};
8568
public static ScarpetTwitchEvents TWITCH_BITS = new ScarpetTwitchEvents("twitch_bits", 4, false) {
8669
@Override
8770
public void onTwitchEvent(String playerName, TwitchEvent event) {
71+
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(playerName);
8872
handler.call(
8973
() -> Arrays.asList(
90-
new StringValue(playerName),
74+
player != null ? new EntityValue(player) : new StringValue(playerName),
9175
new StringValue(event.getNickname()),
9276
new StringValue(event.getMsg()),
9377
new NumericValue(event.getDonationAmount())
9478
),
95-
() -> {
96-
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(playerName);
97-
if (player != null) {
98-
return player.getCommandSource();
99-
} else {
100-
return CarpetServer.minecraft_server.getCommandSource();
101-
}
102-
}
79+
() -> player != null ? player.getCommandSource() : CarpetServer.minecraft_server.getCommandSource()
10380
);
10481
}
10582
};
10683
public static ScarpetTwitchEvents TWITCH_RAID = new ScarpetTwitchEvents("twitch_raid", 3, false) {
10784
@Override
10885
public void onTwitchEvent(String playerName, TwitchEvent event) {
86+
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(playerName);
10987
handler.call(
11088
() -> Arrays.asList(
111-
new StringValue(playerName),
89+
player != null ? new EntityValue(player) : new StringValue(playerName),
11290
new StringValue(event.getNickname()),
11391
new NumericValue(event.getRaiderCount())
11492
),
115-
() -> {
116-
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(playerName);
117-
if (player != null) {
118-
return player.getCommandSource();
119-
} else {
120-
return CarpetServer.minecraft_server.getCommandSource();
121-
}
122-
}
93+
() -> player != null ? player.getCommandSource() : CarpetServer.minecraft_server.getCommandSource()
12394
);
12495
}
12596
};
12697
public static ScarpetTwitchEvents TWITCH_HOST = new ScarpetTwitchEvents("twitch_host", 3, false) {
12798
@Override
12899
public void onTwitchEvent(String playerName, TwitchEvent event) {
100+
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(playerName);
129101
handler.call(
130102
() -> Arrays.asList(
131-
new StringValue(playerName),
103+
player != null ? new EntityValue(player) : new StringValue(playerName),
132104
new StringValue(event.getNickname()),
133105
new NumericValue(event.getViewerCount())
134106
),
135-
() -> {
136-
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(playerName);
137-
if (player != null) {
138-
return player.getCommandSource();
139-
} else {
140-
return CarpetServer.minecraft_server.getCommandSource();
141-
}
142-
}
107+
() -> player != null ? player.getCommandSource() : CarpetServer.minecraft_server.getCommandSource()
143108
);
144109
}
145110
};
146111
public static ScarpetTwitchEvents TWITCH_SUBSCRIPTION_GIFT = new ScarpetTwitchEvents("twitch_subscription_gift", 4, false) {
147112
@Override
148113
public void onTwitchEvent(String playerName, TwitchEvent event) {
114+
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(playerName);
149115
handler.call(
150116
() -> Arrays.asList(
151-
new StringValue(playerName),
117+
player != null ? new EntityValue(player) : new StringValue(playerName),
152118
new StringValue(event.getNickname()),
153119
new NumericValue(event.getSubscriptionTier()),
154120
new NumericValue(event.getDonationAmount())
155121
),
156-
() -> {
157-
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(playerName);
158-
if (player != null) {
159-
return player.getCommandSource();
160-
} else {
161-
return CarpetServer.minecraft_server.getCommandSource();
162-
}
163-
}
122+
() -> player != null ? player.getCommandSource() : CarpetServer.minecraft_server.getCommandSource()
164123
);
165124
}
166125
};
167126
public static ScarpetTwitchEvents TWITCH_CHAT_MESSAGE = new ScarpetTwitchEvents("twitch_chat_message", 5, false) {
168127
@Override
169128
public void onTwitchEvent(String playerName, TwitchEvent event) {
129+
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(playerName);
170130
handler.call(
171131
() -> Arrays.asList(
172-
new StringValue(playerName),
132+
player != null ? new EntityValue(player) : new StringValue(playerName),
173133
new StringValue(event.getNickname()),
174134
new StringValue(event.getMsg()),
175135
new ListValue(getBadges(event)),
176136
new NumericValue(event.getSubscriptionMonths())
177137
),
178-
() -> {
179-
ServerPlayerEntity player = CarpetServer.minecraft_server.getPlayerManager().getPlayer(playerName);
180-
if (player != null) {
181-
return player.getCommandSource();
182-
} else {
183-
return CarpetServer.minecraft_server.getCommandSource();
184-
}
185-
}
138+
() -> player != null ? player.getCommandSource() : CarpetServer.minecraft_server.getCommandSource()
186139
);
187140
}
188141

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
__on_chat_message(message, type, entity) ->
2+
if(message~'^ \n'==null,
3+
print(' \
4+
__on_chat_message(message, type, entity)\
5+
- message: ' + replace(message,'\\\n','\\\n ') + '\
6+
- type: ' + type + '\
7+
- entity: ' + entity)
8+
)

0 commit comments

Comments
 (0)