Minestom/demo/src/main/java/net/minestom/demo/Main.java

134 lines
7.2 KiB
Java

package net.minestom.demo;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.minestom.demo.commands.*;
import net.minestom.server.Configuration;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandManager;
import net.minestom.server.event.player.PlayerChatEvent;
import net.minestom.server.event.server.ServerListPingEvent;
import net.minestom.server.extras.lan.OpenToLAN;
import net.minestom.server.extras.lan.OpenToLANConfig;
import net.minestom.server.extras.optifine.OptifineSupport;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.instance.block.rule.vanilla.RedstonePlacementRule;
import net.minestom.server.message.MessageSender;
import net.minestom.server.ping.ResponseData;
import net.minestom.server.registry.dynamic.chat.ChatDecoration;
import net.minestom.server.registry.dynamic.chat.ChatType;
import net.minestom.server.registry.dynamic.chat.ChatTypeBuilder;
import net.minestom.server.utils.identity.NamedAndIdentified;
import net.minestom.server.utils.time.TimeUnit;
import java.time.Duration;
public class Main {
public static void main(String[] args) {
MinecraftServer minecraftServer = MinecraftServer.init(Configuration.builder()
.setSystemChatType(ChatTypeBuilder.builder(ChatType.SYSTEM.key())
.chat(ChatDecoration.content("SYSTEM: %s", Style.style(NamedTextColor.AQUA)
.font(Key.key("minecraft:uniform")))))
.build());
BlockManager blockManager = MinecraftServer.getBlockManager();
blockManager.registerBlockPlacementRule(new RedstonePlacementRule());
CommandManager commandManager = MinecraftServer.getCommandManager();
commandManager.register(new TestCommand());
commandManager.register(new EntitySelectorCommand());
commandManager.register(new HealthCommand());
commandManager.register(new LegacyCommand());
commandManager.register(new DimensionCommand());
commandManager.register(new ShutdownCommand());
commandManager.register(new TeleportCommand());
commandManager.register(new PlayersCommand());
commandManager.register(new FindCommand());
commandManager.register(new PotionCommand());
commandManager.register(new TitleCommand());
commandManager.register(new BookCommand());
commandManager.register(new ShootCommand());
commandManager.register(new HorseCommand());
commandManager.register(new EchoCommand());
commandManager.register(new SummonCommand());
commandManager.register(new RemoveCommand());
commandManager.register(new GiveCommand());
commandManager.register(new SetBlockCommand());
commandManager.register(new AutoViewCommand());
commandManager.register(new SaveCommand());
commandManager.register(new GamemodeCommand());
commandManager.register(new CommandSignTest());
commandManager.register(new ChatPreviewCommand());
commandManager.setUnknownCommandCallback((sender, command) -> sender.sendMessage(Component.text("Unknown command", NamedTextColor.RED)));
MinecraftServer.getBenchmarkManager().enable(Duration.of(10, TimeUnit.SECOND));
MinecraftServer.getSchedulerManager().buildShutdownTask(() -> System.out.println("Good night"));
MinecraftServer.getGlobalEventHandler().addListener(ServerListPingEvent.class, event -> {
ResponseData responseData = event.getResponseData();
responseData.addEntry(NamedAndIdentified.named("The first line is separated from the others"));
responseData.addEntry(NamedAndIdentified.named("Could be a name, or a message"));
// on modern versions, you can obtain the player connection directly from the event
if (event.getConnection() != null) {
responseData.addEntry(NamedAndIdentified.named("IP test: " + event.getConnection().getRemoteAddress().toString()));
responseData.addEntry(NamedAndIdentified.named("Connection Info:"));
String ip = event.getConnection().getServerAddress();
responseData.addEntry(NamedAndIdentified.named(Component.text('-', NamedTextColor.DARK_GRAY)
.append(Component.text(" IP: ", NamedTextColor.GRAY))
.append(Component.text(ip != null ? ip : "???", NamedTextColor.YELLOW))));
responseData.addEntry(NamedAndIdentified.named(Component.text('-', NamedTextColor.DARK_GRAY)
.append(Component.text(" PORT: ", NamedTextColor.GRAY))
.append(Component.text(event.getConnection().getServerPort()))));
responseData.addEntry(NamedAndIdentified.named(Component.text('-', NamedTextColor.DARK_GRAY)
.append(Component.text(" VERSION: ", NamedTextColor.GRAY))
.append(Component.text(event.getConnection().getProtocolVersion()))));
}
responseData.addEntry(NamedAndIdentified.named(Component.text("Time", NamedTextColor.YELLOW)
.append(Component.text(": ", NamedTextColor.GRAY))
.append(Component.text(System.currentTimeMillis(), Style.style(TextDecoration.ITALIC)))));
// components will be converted the legacy section sign format so they are displayed in the client
responseData.addEntry(NamedAndIdentified.named(Component.text("You can use ").append(Component.text("styling too!", NamedTextColor.RED, TextDecoration.BOLD))));
// the data will be automatically converted to the correct format on response, so you can do RGB and it'll be downsampled!
// on legacy versions, colors will be converted to the section format so it'll work there too
responseData.setDescription(Component.text("This is a Minestom Server", TextColor.color(0x66b3ff)));
//responseData.setPlayersHidden(true);
}).addListener(PlayerChatEvent.class, e -> {
final MessageSender s = e.getSender();
e.setSender(new MessageSender(
s.displayName().clickEvent(ClickEvent.suggestCommand("/msg "+e.getPlayer().getUsername())).color(NamedTextColor.DARK_GREEN),
Component.text("Team Name").color(NamedTextColor.GOLD)
));
});
PlayerInit.init();
OptifineSupport.enable();
//VelocityProxy.enable("rBeJJ79W4MVU");
//BungeeCordProxy.enable();
//MojangAuth.init();
// useful for testing - we don't need to worry about event calls so just set this to a long time
OpenToLAN.open(new OpenToLANConfig().eventCallDelay(Duration.of(1, TimeUnit.DAY)));
minecraftServer.start("0.0.0.0", 25565);
// minecraftServer.start(java.net.UnixDomainSocketAddress.of("minestom-demo.sock"));
//Runtime.getRuntime().addShutdownHook(new Thread(MinecraftServer::stopCleanly));
}
}