Remove PlayLimitModule
This commit is contained in:
1 parent
e65d626b75
commit
d0e188a362
6 files changed
-127
No files matched your search
@@ -52,7 +52,6 @@ public class SFCraftInitializer extends GuiceModInitializer {
|
|||||||
registerFeature(WelcomeModule.class);
|
registerFeature(WelcomeModule.class);
|
||||||
registerFeature(OfflineExemptModule.class);
|
registerFeature(OfflineExemptModule.class);
|
||||||
registerFeature(ElytraSpeedMeterModule.class);
|
registerFeature(ElytraSpeedMeterModule.class);
|
||||||
registerFeature(PlayLimitModule.class);
|
|
||||||
registerFeature(BackModule.class);
|
registerFeature(BackModule.class);
|
||||||
registerFeature(ManagementModule.class);
|
registerFeature(ManagementModule.class);
|
||||||
registerFeature(AFKModule.class);
|
registerFeature(AFKModule.class);
|
||||||
|
|||||||
@@ -9,17 +9,6 @@ import java.util.concurrent.ThreadLocalRandom;
|
|||||||
|
|
||||||
public class SFConfig {
|
public class SFConfig {
|
||||||
public boolean enableOfflineExempt = true;
|
public boolean enableOfflineExempt = true;
|
||||||
public int maintainceStartHour = 13;
|
|
||||||
public int maintainceEndHour = 17;
|
|
||||||
public boolean disableTemporarily;
|
|
||||||
public String domain = "localhost";
|
public String domain = "localhost";
|
||||||
public String serverSecret = RandomStringUtils.random(32);
|
public String serverSecret = RandomStringUtils.random(32);
|
||||||
|
|
||||||
public boolean isClosed(Clock clock) {
|
|
||||||
if (disableTemporarily) return false;
|
|
||||||
var hourOfDay = ZonedDateTime.now(clock).getHour();
|
|
||||||
var startHr = maintainceStartHour;
|
|
||||||
var endHr = maintainceEndHour;
|
|
||||||
return startHr < hourOfDay && hourOfDay < endHr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -28,9 +28,6 @@ public class LegacyQueryHandlerMixin {
|
|||||||
@Redirect(method = "channelRead", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/handler/LegacyQueryHandler;getResponse(Lnet/minecraft/network/QueryableServer;)Ljava/lang/String;"))
|
@Redirect(method = "channelRead", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/handler/LegacyQueryHandler;getResponse(Lnet/minecraft/network/QueryableServer;)Ljava/lang/String;"))
|
||||||
private String getResponse(QueryableServer server) {
|
private String getResponse(QueryableServer server) {
|
||||||
var version = server.getVersion();
|
var version = server.getVersion();
|
||||||
if (address instanceof InetSocketAddress address) {
|
|
||||||
version = Helper.getVersionString(address);
|
|
||||||
}
|
|
||||||
return String.format(
|
return String.format(
|
||||||
Locale.ROOT,
|
Locale.ROOT,
|
||||||
"§1\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d",
|
"§1\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d",
|
||||||
|
|||||||
@@ -1,97 +0,0 @@
|
|||||||
package io.ib67.sfcraft.module;
|
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
|
||||||
import com.mojang.brigadier.CommandDispatcher;
|
|
||||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
|
||||||
import com.mojang.brigadier.context.CommandContext;
|
|
||||||
import io.ib67.sfcraft.util.Helper;
|
|
||||||
import io.ib67.sfcraft.util.SFConsts;
|
|
||||||
import io.ib67.sfcraft.ServerModule;
|
|
||||||
import io.ib67.sfcraft.callback.SFCallbacks;
|
|
||||||
import io.ib67.sfcraft.config.SFConfig;
|
|
||||||
import io.ib67.sfcraft.geoip.GeoIPService;
|
|
||||||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
|
|
||||||
import net.minecraft.command.CommandRegistryAccess;
|
|
||||||
import net.minecraft.network.ClientConnection;
|
|
||||||
import net.minecraft.server.MinecraftServer;
|
|
||||||
import net.minecraft.server.ServerMetadata;
|
|
||||||
import net.minecraft.server.command.CommandManager;
|
|
||||||
import net.minecraft.server.command.ServerCommandSource;
|
|
||||||
import net.minecraft.text.Text;
|
|
||||||
import net.minecraft.util.Colors;
|
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
public class PlayLimitModule extends ServerModule {
|
|
||||||
private final Set<String> motdRequestedIps = new HashSet<>();
|
|
||||||
@Inject
|
|
||||||
GeoIPService geoIPService;
|
|
||||||
@Inject
|
|
||||||
SFConfig config;
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onInitialize() {
|
|
||||||
SFCallbacks.PRE_LOGIN.register(this::onPreLogin);
|
|
||||||
SFCallbacks.MOTD.register(this::onMotd);
|
|
||||||
CommandRegistrationCallback.EVENT.register(this::registerCommand);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment registrationEnvironment) {
|
|
||||||
dispatcher.register(LiteralArgumentBuilder.<ServerCommandSource>literal("unblockserver")
|
|
||||||
.requires(i -> isEnabled())
|
|
||||||
.requires(it -> it.hasPermissionLevel(2) || SFConsts.COMMAND_UNBLOCKSERVER.hasPermission(it.getPlayer()))
|
|
||||||
.executes(this::unblockServer));
|
|
||||||
}
|
|
||||||
|
|
||||||
public int unblockServer(CommandContext<ServerCommandSource> ctx) {
|
|
||||||
var state = config.disableTemporarily;
|
|
||||||
config.disableTemporarily = !state;
|
|
||||||
ctx.getSource().sendMessage(Text.of("Current state: " + !state));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
private ServerMetadata onMotd(MinecraftServer server, ClientConnection connection) {
|
|
||||||
var ip = connection.getAddressAsString(false);
|
|
||||||
var cached = motdRequestedIps.contains(ip);
|
|
||||||
if (!cached) {
|
|
||||||
motdRequestedIps.add(ip);
|
|
||||||
}
|
|
||||||
var originalMetadata = server.getServerMetadata();
|
|
||||||
if (originalMetadata == null) return null;
|
|
||||||
String version = null;
|
|
||||||
if (connection.getAddress() instanceof InetSocketAddress address) {
|
|
||||||
version = Helper.getVersionString(address);
|
|
||||||
}
|
|
||||||
if (version != null && isEnabled()) {
|
|
||||||
return new ServerMetadata(
|
|
||||||
originalMetadata.description(),
|
|
||||||
originalMetadata.players(),
|
|
||||||
Optional.of(new ServerMetadata.Version(version, 0)),
|
|
||||||
cached ? Optional.empty() : originalMetadata.favicon(),
|
|
||||||
originalMetadata.secureChatEnforced()
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
return originalMetadata;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void onPreLogin(String currentPlayer, ClientConnection connection, Consumer<Text> disconnect, boolean offline) {
|
|
||||||
if (isEnabled() && connection.getAddress() instanceof InetSocketAddress address) {
|
|
||||||
try {
|
|
||||||
var clock = geoIPService.clockOf(address.getAddress());
|
|
||||||
if (config.isClosed(clock) && !"iceBear67".equals(currentPlayer)) {
|
|
||||||
final var msg = "§c§l服务器尚未开放\n\n§f服务器维护时间: §l%1$d~%2$d\n§c你的下午不值得浪费在游戏上,晚点再来吧!";
|
|
||||||
disconnect.accept(Text.of(String.format(msg, config.maintainceStartHour, config.maintainceEndHour)));
|
|
||||||
}
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
System.err.println("Failed to locate " + address.getAddress());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -52,7 +52,6 @@ public class WelcomeModule extends ServerModule {
|
|||||||
player.sendMessage(Text.of(" - 服务器内已有的设施均为其他玩家的财产,(如可以)使用时请注意礼貌"));
|
player.sendMessage(Text.of(" - 服务器内已有的设施均为其他玩家的财产,(如可以)使用时请注意礼貌"));
|
||||||
player.sendMessage(Text.literal(" - 服务器内有一些非原版特性,可以点击此条消息查看 [特性列表]")
|
player.sendMessage(Text.literal(" - 服务器内有一些非原版特性,可以点击此条消息查看 [特性列表]")
|
||||||
.styled(s -> s.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://wiki.sfclub.cc/s/sfcraft-features"))));
|
.styled(s -> s.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://wiki.sfclub.cc/s/sfcraft-features"))));
|
||||||
player.sendMessage(Text.of(" - 工作日下午 " + config.maintainceStartHour + " 至 " + config.maintainceEndHour + "点 为服务器维护时间 (防沉迷)"));
|
|
||||||
player.sendMessage(Text.of("§c - 邀请新成员即等同为其作担保"));
|
player.sendMessage(Text.of("§c - 邀请新成员即等同为其作担保"));
|
||||||
player.sendMessage(Text.of("§7如果您想邀请朋友加入服务器, 可以向管理员提出申请§7 (需备注正/盗版/小号)"));
|
player.sendMessage(Text.of("§7如果您想邀请朋友加入服务器, 可以向管理员提出申请§7 (需备注正/盗版/小号)"));
|
||||||
player.sendMessage(Text.of("§7此外, 本服务器无密码登录,盗版账号安全责任自负"));
|
player.sendMessage(Text.of("§7此外, 本服务器无密码登录,盗版账号安全责任自负"));
|
||||||
|
|||||||
@@ -77,20 +77,6 @@ public class Helper {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getVersionString(InetSocketAddress address) {
|
|
||||||
var geoIp = SFCraft.getInjector().getInstance(GeoIPService.class);
|
|
||||||
var config = SFCraft.getInjector().getInstance(SFConfig.class);
|
|
||||||
try {
|
|
||||||
var clock = geoIp.clockOf(address.getAddress());
|
|
||||||
if (config.isClosed(clock)) {
|
|
||||||
return "防沉迷: " + config.maintainceStartHour + "~" + config.maintainceEndHour;
|
|
||||||
}
|
|
||||||
} catch (GeoIp2Exception ignored) {
|
|
||||||
return "Not Available!";
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public static Optional<String> getConfigResource(Path root, String resource) {
|
public static Optional<String> getConfigResource(Path root, String resource) {
|
||||||
if (Files.exists(root)) {
|
if (Files.exists(root)) {
|
||||||
|
|||||||
Reference in new issue
Block a user