-
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.
- Loading branch information
1 parent
5d3b24f
commit cfe7f54
Showing
10 changed files
with
223 additions
and
5 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
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
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { z } from "zod"; | ||
import type { Client } from "../Client"; | ||
import { Base } from "./Base"; | ||
import type { Player } from "./Player"; | ||
|
||
export class PlayerChatManager extends Base { | ||
public constructor(client: Client, private player: Player) { | ||
super(client); | ||
} | ||
|
||
/** | ||
* Send a message to the player | ||
* @param message Message to send | ||
* @returns New player instance | ||
*/ | ||
public async send(message: string) { | ||
return this.client.players.constructPlayer( | ||
( | ||
await this.client.post(`/players/${this.player.uuid}/chat`, { | ||
message, | ||
}) | ||
).data | ||
); | ||
} | ||
|
||
/** | ||
* Creates a message collector on the player this manager is for. | ||
* @returns Whether the collector was created or it already existed | ||
*/ | ||
public async createCollector() { | ||
const res = z | ||
.object({ | ||
status: z.union([ | ||
z.literal("collector-exists"), | ||
z.literal("collector-created"), | ||
]), | ||
}) | ||
.parse( | ||
await ( | ||
await this.client.post(`/players/${this.player.uuid}/chat/collector`) | ||
).data | ||
); | ||
|
||
switch (res.status) { | ||
case "collector-exists": | ||
return false; | ||
case "collector-created": | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* Returns the collected message | ||
* @returns Collected message | ||
*/ | ||
public async checkCollector() { | ||
return z | ||
.union([ | ||
z.object({ | ||
status: z.union([z.literal("no-collector"), z.literal("waiting")]), | ||
}), | ||
z.object({ | ||
status: z.literal("collected"), | ||
result: z.string(), | ||
}), | ||
]) | ||
.parse( | ||
(await this.client.get(`/players/${this.player.uuid}/chat/collector`)) | ||
.data | ||
); | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/tristansmp/elytra/events/ChatListener.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,33 @@ | ||
package com.tristansmp.elytra.events | ||
|
||
import com.tristansmp.elytra.Elytra | ||
import io.papermc.paper.event.player.AsyncChatEvent | ||
import net.kyori.adventure.text.TextComponent | ||
import org.bukkit.event.EventHandler | ||
import org.bukkit.event.Listener | ||
|
||
|
||
class ChatListener : Listener { | ||
@EventHandler | ||
fun onPlayerChat(event: AsyncChatEvent) { | ||
val needsCollection = Elytra.instance.mstore.get<Boolean>("cc:${event.player.uniqueId}:needs_collection") | ||
|
||
if (needsCollection == null || !needsCollection) { | ||
return | ||
} | ||
|
||
event.isCancelled = true | ||
|
||
try { | ||
val message = event.message() as TextComponent | ||
|
||
Elytra.instance.mstore.set("cc:${event.player.uniqueId}:results", message.content()) | ||
|
||
event.player.sendMessage("§d[Elytra] §aMessage collected.") | ||
|
||
Elytra.instance.mstore.remove("cc:${event.player.uniqueId}:needs_collection") | ||
} catch (e: ClassCastException) { | ||
event.player.sendMessage("§d[Elytra] §cMessage could not be collected.") | ||
} | ||
} | ||
} |
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,27 @@ | ||
package com.tristansmp.elytra.lib | ||
|
||
import java.util.* | ||
|
||
class MemoryStore { | ||
private val store = mutableMapOf<String, Any>() | ||
|
||
fun <T> get(key: String): T? { | ||
return store[key] as T? | ||
} | ||
|
||
fun <T : Any> set(key: String, value: T) { | ||
store[key] = value | ||
} | ||
|
||
fun remove(key: String) { | ||
store.remove(key) | ||
} | ||
|
||
init { | ||
Timer().scheduleAtFixedRate(object : TimerTask() { | ||
override fun run() { | ||
store.clear() | ||
} | ||
}, 300000, 300000) | ||
} | ||
} |
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