mirror of
https://github.com/Shiewk/SModeration.git
synced 2026-04-29 06:34:17 +02:00
Move SModeration for Paper to root project
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package de.shiewk.smoderation.paper.input;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.title.Title;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static de.shiewk.smoderation.paper.SModerationPaper.CHAT_PREFIX;
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
import static net.kyori.adventure.text.format.NamedTextColor.GRAY;
|
||||
|
||||
public class ChatInput {
|
||||
private final Player player;
|
||||
private final Component prompt;
|
||||
private final Consumer<Component> action;
|
||||
private int remainingTicks;
|
||||
|
||||
private ChatInput(@NotNull Player player, @NotNull Component prompt, @NotNull Consumer<Component> action, int remainingSeconds){
|
||||
Objects.requireNonNull(action);
|
||||
Objects.requireNonNull(prompt);
|
||||
Objects.requireNonNull(player);
|
||||
this.player = player;
|
||||
this.prompt = prompt;
|
||||
this.action = action;
|
||||
this.remainingTicks = remainingSeconds * 20 + 1;
|
||||
}
|
||||
|
||||
static void tickAll() {
|
||||
runningInputs.values().forEach(ChatInput::tick);
|
||||
}
|
||||
|
||||
void tick(){
|
||||
remainingTicks--;
|
||||
if (remainingTicks <= 0){
|
||||
runningInputs.remove(player);
|
||||
return;
|
||||
}
|
||||
if (remainingTicks % 20 == 0){
|
||||
player.showTitle(Title.title(
|
||||
text().content(getRemainingTicks() / 20 + " seconds").color(GRAY).build(),
|
||||
getPrompt(),
|
||||
Title.Times.times(Duration.ZERO, Duration.ofSeconds(2), Duration.ZERO)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
final static ConcurrentHashMap<Player, ChatInput> runningInputs = new ConcurrentHashMap<>();
|
||||
|
||||
public static void prompt(Player player, Consumer<Component> consumer, Component prompt, int timeSeconds){
|
||||
runningInputs.put(player, new ChatInput(player, prompt, consumer, timeSeconds));
|
||||
player.sendMessage(CHAT_PREFIX.append(prompt));
|
||||
}
|
||||
|
||||
public Component getPrompt() {
|
||||
return prompt;
|
||||
}
|
||||
|
||||
public Consumer<Component> getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public int getRemainingTicks() {
|
||||
return remainingTicks;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package de.shiewk.smoderation.paper.input;
|
||||
|
||||
import com.destroystokyo.paper.event.server.ServerTickStartEvent;
|
||||
import io.papermc.paper.event.player.AsyncChatEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import static de.shiewk.smoderation.paper.input.ChatInput.runningInputs;
|
||||
|
||||
public class ChatInputListener implements Listener {
|
||||
|
||||
|
||||
@EventHandler
|
||||
public void onAsyncChat(AsyncChatEvent event){
|
||||
final ChatInput input = runningInputs.remove(event.getPlayer());
|
||||
if (input != null){
|
||||
event.setCancelled(true);
|
||||
input.getAction().accept(event.message());
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler public void onPlayerQuit(PlayerQuitEvent event){
|
||||
runningInputs.remove(event.getPlayer());
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.MONITOR) public void onServerTickStart(ServerTickStartEvent event){
|
||||
ChatInput.tickAll();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user