Initial commit
This commit is contained in:
@ -0,0 +1,45 @@
|
||||
package eu.mhsl.minenet.minigames.message;
|
||||
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.timer.TaskSchedule;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class Countdown {
|
||||
public Countdown(Class<? extends TranslatableMessage> messageType) {
|
||||
}
|
||||
public CompletableFuture<Void> countdown(Audience targets, int from, Consumer<CountdownModifier> modifier) {
|
||||
CompletableFuture<Void> future = new CompletableFuture<>();
|
||||
AtomicInteger count = new AtomicInteger(from);
|
||||
MinecraftServer.getSchedulerManager().submitTask(() -> {
|
||||
if(count.get() <= 0) {
|
||||
future.complete(null);
|
||||
return TaskSchedule.stop();
|
||||
}
|
||||
try {
|
||||
CountdownModifier countdownModifier = new CountdownModifier(count.getAndDecrement());
|
||||
modifier.accept(countdownModifier);
|
||||
countdownModifier.message.send(targets);
|
||||
System.out.println("SEND TITLE" + System.currentTimeMillis());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return TaskSchedule.seconds(1);
|
||||
});
|
||||
return future;
|
||||
}
|
||||
|
||||
|
||||
public static class CountdownModifier {
|
||||
public TranslatableMessage message;
|
||||
public final int timeLeft;
|
||||
public CountdownModifier(int timeLeft) {
|
||||
this.timeLeft = timeLeft;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
31
src/main/java/eu/mhsl/minenet/minigames/message/Icon.java
Normal file
31
src/main/java/eu/mhsl/minenet/minigames/message/Icon.java
Normal file
@ -0,0 +1,31 @@
|
||||
package eu.mhsl.minenet.minigames.message;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
|
||||
public enum Icon {
|
||||
SCIENCE("\uD83E\uDDEA", NamedTextColor.LIGHT_PURPLE),
|
||||
STAR("\u2606", NamedTextColor.GOLD),
|
||||
CHAT("\u276F\u276F", NamedTextColor.WHITE),
|
||||
SUCCESS("\u2714", NamedTextColor.GREEN),
|
||||
ERROR("\u274C", NamedTextColor.RED);
|
||||
|
||||
private final String symbol;
|
||||
private final NamedTextColor color;
|
||||
Icon(String symbol, NamedTextColor color) {
|
||||
this.symbol = symbol;
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public String getSymbol() {
|
||||
return symbol;
|
||||
}
|
||||
|
||||
public NamedTextColor getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public Component getComponent() {
|
||||
return Component.text(getSymbol(), getColor());
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package eu.mhsl.minenet.minigames.message;
|
||||
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.minestom.server.entity.Player;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
//TODO maybe async large batches
|
||||
public interface Sendable {
|
||||
void send(Player p);
|
||||
|
||||
default void send(Audience players) {
|
||||
players.forEachAudience(audience -> {
|
||||
this.send((Player) audience);
|
||||
});
|
||||
}
|
||||
|
||||
default void send(List<Player> players) {
|
||||
players.forEach(this::send);
|
||||
}
|
||||
|
||||
default void send(Set<Player> players) {
|
||||
players.forEach(this::send);
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package eu.mhsl.minenet.minigames.message;
|
||||
|
||||
import eu.mhsl.minenet.minigames.message.component.Translatable;
|
||||
import eu.mhsl.minenet.minigames.message.component.TranslatedComponent;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.ComponentBuilder;
|
||||
import net.kyori.adventure.text.ComponentLike;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.minestom.server.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class TranslatableMessage implements Sendable {
|
||||
int indention = 0;
|
||||
List<ComponentLike> chain = new ArrayList<>();
|
||||
|
||||
public TranslatableMessage() {
|
||||
|
||||
}
|
||||
|
||||
public TranslatableMessage appendStatic(Component component) {
|
||||
chain.add(component);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TranslatableMessage appendStatic(String text) {
|
||||
chain.add(Component.text(text));
|
||||
return this;
|
||||
}
|
||||
|
||||
public TranslatableMessage appendTranslated(String mapId) {
|
||||
chain.add(new TranslatedComponent(mapId));
|
||||
return this;
|
||||
}
|
||||
|
||||
public TranslatableMessage list(List<String> list) {
|
||||
list.forEach(s -> {
|
||||
chain.add(Component.text(s));
|
||||
newLine();
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
public TranslatableMessage pipe() {
|
||||
chain.add(Component.text(" | ", NamedTextColor.DARK_GRAY));
|
||||
return this;
|
||||
}
|
||||
|
||||
public TranslatableMessage quote(String text) {
|
||||
chain.add(Component.text("'" + text + "'"));
|
||||
return this;
|
||||
}
|
||||
|
||||
public TranslatableMessage indent(int amount) {
|
||||
this.indention += amount;
|
||||
this.newLine();
|
||||
return this;
|
||||
}
|
||||
|
||||
public TranslatableMessage newLine() {
|
||||
chain.add(Component.text(" ".repeat(indention) + "\n"));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public Component build(Player p) {
|
||||
ComponentBuilder out = Component.text();
|
||||
chain.forEach(componentLike -> {
|
||||
if(componentLike instanceof Translatable t) t.compute(p);
|
||||
|
||||
out.append(componentLike);
|
||||
});
|
||||
return out.build();
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package eu.mhsl.minenet.minigames.message.component;
|
||||
|
||||
import net.minestom.server.entity.Player;
|
||||
|
||||
public interface Translatable {
|
||||
void compute(Player p);
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package eu.mhsl.minenet.minigames.message.component;
|
||||
|
||||
import eu.mhsl.minenet.minigames.lang.Languages;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.ComponentLike;
|
||||
import net.minestom.server.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class TranslatedComponent implements ComponentLike, Translatable {
|
||||
private String mapId;
|
||||
private String result;
|
||||
|
||||
public TranslatedComponent(String mapId) {
|
||||
this.mapId = mapId;
|
||||
}
|
||||
|
||||
public void compute(Player p) {
|
||||
result = Languages.getInstance().getLanguage(p).getEntry(mapId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Component asComponent() {
|
||||
return Component.text(result);
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package eu.mhsl.minenet.minigames.message.type;
|
||||
|
||||
import eu.mhsl.minenet.minigames.message.TranslatableMessage;
|
||||
import net.minestom.server.entity.Player;
|
||||
|
||||
public class ActionBarMessage extends TranslatableMessage {
|
||||
@Override
|
||||
public void send(Player p) {
|
||||
p.sendActionBar(build(p));
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package eu.mhsl.minenet.minigames.message.type;
|
||||
|
||||
import eu.mhsl.minenet.minigames.message.Icon;
|
||||
import eu.mhsl.minenet.minigames.message.TranslatableMessage;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.minestom.server.entity.Player;
|
||||
|
||||
public class ChatMessage extends TranslatableMessage {
|
||||
public ChatMessage(Icon icon) {
|
||||
appendStatic(icon.getComponent());
|
||||
pipe();
|
||||
}
|
||||
|
||||
public void send(Player p) {
|
||||
p.sendMessage(build(p));
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package eu.mhsl.minenet.minigames.message.type;
|
||||
|
||||
import eu.mhsl.minenet.minigames.message.TranslatableMessage;
|
||||
import net.minestom.server.entity.Player;
|
||||
|
||||
import javax.management.RuntimeErrorException;
|
||||
|
||||
public class SubtitleMessage extends TranslatableMessage {
|
||||
@Override
|
||||
public void send(Player p) {
|
||||
throw new RuntimeErrorException(new Error("Cannot send subtitle-only to player"));
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package eu.mhsl.minenet.minigames.message.type;
|
||||
|
||||
import eu.mhsl.minenet.minigames.message.TranslatableMessage;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.title.Title;
|
||||
import net.kyori.adventure.title.TitlePart;
|
||||
import net.minestom.server.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.UnknownNullability;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class TitleMessage extends TranslatableMessage {
|
||||
private Title.Times times;
|
||||
private SubtitleMessage subtitle = new SubtitleMessage();
|
||||
|
||||
public TitleMessage() {
|
||||
times = Title.Times.times(Duration.ZERO, Duration.ofSeconds(1), Duration.ZERO);
|
||||
}
|
||||
public TitleMessage(Duration stay) {
|
||||
times = Title.Times.times(Duration.ZERO, stay, Duration.ZERO);
|
||||
}
|
||||
public TitleMessage(Duration stay, Duration fade) {
|
||||
times = Title.Times.times(fade, stay, fade);
|
||||
}
|
||||
|
||||
public void setTimes(Title.Times times) {
|
||||
this.times = times;
|
||||
}
|
||||
|
||||
public TranslatableMessage subtitle(Consumer<SubtitleMessage> callback) {
|
||||
this.subtitle = new SubtitleMessage();
|
||||
callback.accept(subtitle);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(Player p) {
|
||||
Audience.audience(p).showTitle(new Title() {
|
||||
@Override
|
||||
public @NotNull Component title() {
|
||||
return build(p);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Component subtitle() {
|
||||
return subtitle.build(p);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Times times() {
|
||||
return times;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> @UnknownNullability T part(@NotNull TitlePart<T> part) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user