-
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.
- Loading branch information
Showing
7 changed files
with
241 additions
and
2 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
1.21/src/main/java/net/rk4z/fabricord/mixins/PlayerManagerMixin.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,37 @@ | ||
package net.rk4z.fabricord.mixins; | ||
|
||
import net.minecraft.network.ClientConnection; | ||
import net.minecraft.server.PlayerManager; | ||
import net.minecraft.server.network.ConnectedClientData; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.rk4z.beacon.EventBus; | ||
import net.rk4z.fabricord.Fabricord; | ||
import net.rk4z.fabricord.events.*; | ||
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; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
@Mixin(PlayerManager.class) | ||
public class PlayerManagerMixin { | ||
@Inject(method = "onPlayerConnect", at = @At("RETURN")) | ||
public void onPlayerConnect(ClientConnection cc, ServerPlayerEntity player, ConnectedClientData ccData, CallbackInfo ci) { | ||
EventBus.callEventAsync(PlayerJoinEvent.get()); | ||
String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()); | ||
String message = timestamp + " > " + player.getName().getString() + " has joined the server."; | ||
|
||
Fabricord.INSTANCE.getLogContainer().add(message); | ||
} | ||
|
||
@Inject(method = "remove", at = @At("HEAD")) | ||
public void onPlayerDisconnect(ServerPlayerEntity player, CallbackInfo ci) { | ||
EventBus.callEventAsync(PlayerLeaveEvent.get()); | ||
String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()); | ||
String message = timestamp + " > " + player.getName().getString() + " has left the server."; | ||
|
||
Fabricord.INSTANCE.getLogContainer().add(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
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
77 changes: 77 additions & 0 deletions
77
1.21/src/main/kotlin/net/rk4z/fabricord/discord/DiscordMessageHandler.kt
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 net.rk4z.fabricord.discord | ||
|
||
import net.dv8tion.jda.api.entities.Role | ||
import net.kyori.adventure.text.Component | ||
import net.kyori.adventure.text.event.ClickEvent | ||
import net.kyori.adventure.text.format.TextColor | ||
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer | ||
import net.minecraft.server.network.ServerPlayerEntity | ||
import net.minecraft.text.Text | ||
import net.rk4z.beacon.Listener | ||
import net.rk4z.beacon.handler | ||
import net.rk4z.fabricord.Fabricord.logChannelID | ||
import net.rk4z.fabricord.events.DiscordMessageReceiveEvent | ||
import java.awt.Color | ||
import java.util.function.Consumer | ||
|
||
@Suppress("unused") | ||
class DiscordMessageHandler : Listener { | ||
|
||
val handleDiscordMessage = handler<DiscordMessageReceiveEvent> { event -> | ||
val channelId: String = logChannelID!! | ||
if (event.c.channel.id != channelId || event.c.author.isBot) { | ||
return@handler | ||
} | ||
|
||
val member = event.c.member | ||
val memberName = member?.user?.name ?: "Unknown Name" | ||
val memberId = member?.user?.id ?: "00000000000000000000" | ||
val idSuggest = "<@$memberId>" | ||
val highestRole = | ||
member?.roles?.stream()?.max(Comparator.comparingInt { obj: Role -> obj.position })?.orElse(null) | ||
val roleName = highestRole?.name | ||
val roleId = highestRole?.id | ||
val rIdSuggest = if (roleId != null) "<@&$roleId>" else null | ||
val roleColor = if (highestRole != null && highestRole.color != null) highestRole.color else Color.WHITE | ||
val kyoriRoleColor = TextColor.color( | ||
roleColor!!.red, roleColor.green, roleColor.blue | ||
) | ||
|
||
|
||
var componentMessage = Component.text("[", TextColor.color(0xFFFFFF)) | ||
.append(Component.text("Discord", TextColor.color(0x55CDFC))) | ||
|
||
componentMessage = if (roleName != null) { | ||
componentMessage.append(Component.text(" | ", TextColor.color(0xFFFFFF))) | ||
.append( | ||
Component.text(roleName, kyoriRoleColor) | ||
.clickEvent(ClickEvent.suggestCommand(rIdSuggest!!)) | ||
) | ||
.append(Component.text("]", TextColor.color(0xFFFFFF))) | ||
.append(Component.text(" ")) | ||
} else { | ||
componentMessage.append(Component.text("]", TextColor.color(0xFFFFFF))) | ||
.append(Component.text(" ")) | ||
} | ||
|
||
componentMessage = componentMessage.append( | ||
Component.text(memberName) | ||
.clickEvent(ClickEvent.suggestCommand(idSuggest)) | ||
) | ||
.append(Component.text(" » " + event.c.message.contentDisplay)) | ||
|
||
val json = GsonComponentSerializer.gson().serialize(componentMessage) | ||
|
||
val textMessage: Text? = Text.Serialization.fromJson(json, event.server.registryManager) | ||
|
||
event.server.playerManager.playerList.forEach( | ||
Consumer { player: ServerPlayerEntity -> | ||
player.sendMessage( | ||
textMessage, | ||
false | ||
) | ||
} | ||
) | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
1.21/src/main/kotlin/net/rk4z/fabricord/events/DiscordEvents.kt
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,23 @@ | ||
package net.rk4z.fabricord.events | ||
|
||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent | ||
import net.minecraft.server.MinecraftServer | ||
import net.rk4z.beacon.Event | ||
|
||
class DiscordMessageReceiveEvent(val c: MessageReceivedEvent, val server: MinecraftServer) : Event() { | ||
companion object { | ||
@JvmStatic | ||
fun get(event: MessageReceivedEvent, server: MinecraftServer): DiscordMessageReceiveEvent { | ||
return DiscordMessageReceiveEvent(event, server) | ||
} | ||
} | ||
} | ||
|
||
class DiscordMinecraftPlayerMentionEvent(val c: MessageReceivedEvent, val server: MinecraftServer) : Event() { | ||
companion object { | ||
@JvmStatic | ||
fun get(event: MessageReceivedEvent, server: MinecraftServer): DiscordMinecraftPlayerMentionEvent { | ||
return DiscordMinecraftPlayerMentionEvent(event, server) | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
1.21/src/main/kotlin/net/rk4z/fabricord/events/PlayerEvents.kt
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,23 @@ | ||
package net.rk4z.fabricord.events | ||
|
||
import net.rk4z.beacon.Event | ||
|
||
class PlayerJoinEvent: Event() { | ||
companion object { | ||
private val instance = PlayerJoinEvent() | ||
@JvmStatic | ||
fun get(): PlayerJoinEvent { | ||
return instance | ||
} | ||
} | ||
} | ||
|
||
class PlayerLeaveEvent: Event() { | ||
companion object { | ||
private val instance = PlayerLeaveEvent() | ||
@JvmStatic | ||
fun get(): PlayerLeaveEvent { | ||
return instance | ||
} | ||
} | ||
} |
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