From ab56f6dc19b6e23f1480235eb2da3f50f793e75e Mon Sep 17 00:00:00 2001 From: Skye Date: Thu, 20 Apr 2023 19:34:28 +0900 Subject: [PATCH] feat: add way to stop e4mc --- gradle.properties | 2 +- .../mixins/ServerNetworkIoMixin.java | 10 ++- .../vg/skye/e4mc_minecraft/E4mcClient.kt | 77 ++++++++++++++++++- .../skye/e4mc_minecraft/E4mcRelayHandler.kt | 28 +++++-- .../assets/e4mc_quilt/lang/el_gr.json | 5 +- .../assets/e4mc_quilt/lang/en_us.json | 5 +- .../assets/e4mc_quilt/lang/et_ee.json | 5 +- 7 files changed, 117 insertions(+), 15 deletions(-) diff --git a/gradle.properties b/gradle.properties index 3087581..fee6aae 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,5 +3,5 @@ org.gradle.jvmargs=-Xmx2G mod.name=e4mc mod.id=e4mc_minecraft -mod.version=2.1.0 +mod.version=3.0.0 mod.group=vg.skye \ No newline at end of file diff --git a/src/main/java/vg/skye/e4mc_minecraft/mixins/ServerNetworkIoMixin.java b/src/main/java/vg/skye/e4mc_minecraft/mixins/ServerNetworkIoMixin.java index 2c9f0f5..efede88 100644 --- a/src/main/java/vg/skye/e4mc_minecraft/mixins/ServerNetworkIoMixin.java +++ b/src/main/java/vg/skye/e4mc_minecraft/mixins/ServerNetworkIoMixin.java @@ -13,6 +13,7 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import vg.skye.e4mc_minecraft.E4mcClient; import vg.skye.e4mc_minecraft.E4mcRelayHandler; import javax.annotation.Nullable; @@ -22,7 +23,6 @@ import java.net.InetAddress; @Mixin(ServerNetworkIo.class) public abstract class ServerNetworkIoMixin { private static final ThreadLocal initializingE4mc = ThreadLocal.withInitial(() -> false); - private static E4mcRelayHandler e4mcHandler = null; @Shadow public abstract void bind(@Nullable InetAddress address, int port) throws IOException; @@ -40,7 +40,7 @@ public abstract class ServerNetworkIoMixin { } } else { E4mcRelayHandler handler = new E4mcRelayHandler(); - e4mcHandler = handler; + E4mcClient.HANDLER = handler; handler.connect(); } } @@ -56,8 +56,10 @@ public abstract class ServerNetworkIoMixin { } @Inject(method = "stop", at = @At("HEAD")) - private void bind(CallbackInfo ci) { - e4mcHandler.close(); + private void stop(CallbackInfo ci) { + if (E4mcClient.HANDLER != null) { + E4mcClient.HANDLER.close(); + } } diff --git a/src/main/kotlin/vg/skye/e4mc_minecraft/E4mcClient.kt b/src/main/kotlin/vg/skye/e4mc_minecraft/E4mcClient.kt index dde418d..5a53a8c 100644 --- a/src/main/kotlin/vg/skye/e4mc_minecraft/E4mcClient.kt +++ b/src/main/kotlin/vg/skye/e4mc_minecraft/E4mcClient.kt @@ -2,27 +2,100 @@ package vg.skye.e4mc_minecraft //#if FABRIC==1 import net.fabricmc.api.ModInitializer +//#if MC>=11904 +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback +//#else +//$$ import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback +//#endif //#else //$$ import net.minecraftforge.fml.common.Mod +//$$ import net.minecraftforge.eventbus.api.SubscribeEvent +//$$ import net.minecraftforge.event.RegisterCommandsEvent +//#endif +import net.minecraft.server.command.CommandManager.* +//#if MC>=11904 +import net.minecraft.text.Text +//#elseif FABRIC==1 +//$$ import net.minecraft.text.TranslatableText +//#else +//$$ import net.minecraft.network.chat.TranslatableComponent //#endif import org.slf4j.Logger import org.slf4j.LoggerFactory + //#if FORGE==1 //$$ @Mod("e4mc_minecraft") +//$$ @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE) //$$ object E4mcClient { //#else object E4mcClient : ModInitializer { //#endif const val NAME = "e4mc" const val ID = "e4mc_minecraft" - const val VERSION = "2.1.0" + const val VERSION = "3.0.0" @JvmField val LOGGER: Logger = LoggerFactory.getLogger("e4mc") + @JvmField + var HANDLER: E4mcRelayHandler? = null //#if FABRIC==1 override fun onInitialize() { - // nothing needed + //#if MC>=11904 + CommandRegistrationCallback.EVENT.register { dispatcher, _, _ -> + //#else + //$$ CommandRegistrationCallback.EVENT.register { dispatcher, _ -> + //#endif + dispatcher.register(literal("e4mc") + .then( + literal("stop") + .executes { context -> + if (HANDLER != null) { + HANDLER!!.close() + HANDLER = null + //#if MC>=11904 + context.source.sendMessage(Text.translatable("text.e4mc_minecraft.closeServer")) + //#else + //$$ context.source.sendFeedback(TranslatableText("text.e4mc_minecraft.closeServer"), false) + //#endif + } else { + //#if MC>=11904 + context.source.sendMessage(Text.translatable("text.e4mc_minecraft.serverAlreadyClosed")) + //#else + //$$ context.source.sendFeedback(TranslatableText("text.e4mc_minecraft.serverAlreadyClosed"), false) + //#endif + } + 1 + } + )) + } } + //#else + //$$ @SubscribeEvent + //$$ fun onRegisterCommandEvent(event: RegisterCommandsEvent) { + //$$ val commandDispatcher = event.getDispatcher() + //$$ commandDispatcher.register(literal("e4mc") + //$$ .then( + //$$ literal("stop") + //$$ .executes { context -> + //$$ if (HANDLER != null) { + //$$ HANDLER!!.close() + //$$ HANDLER = null + //$$ //#if MC>=11904 + //$$ context.source.sendSuccess(Component.translatable("text.e4mc_minecraft.closeServer"), false) + //$$ //#else + //$$ //$$ context.source.sendSuccess(TranslatableComponent("text.e4mc_minecraft.closeServer"), false) + //$$ //#endif + //$$ } else { + //$$ //#if MC>=11904 + //$$ context.source.sendFailure(Component.translatable("text.e4mc_minecraft.serverAlreadyClosed")) + //$$ //#else + //$$ //$$ context.source.sendFailure(TranslatableComponent("text.e4mc_minecraft.serverAlreadyClosed")) + //$$ //#endif + //$$ } + //$$ 1 + //$$ } + //$$ )) + //$$ } //#endif } diff --git a/src/main/kotlin/vg/skye/e4mc_minecraft/E4mcRelayHandler.kt b/src/main/kotlin/vg/skye/e4mc_minecraft/E4mcRelayHandler.kt index 368ea15..d1e62ca 100644 --- a/src/main/kotlin/vg/skye/e4mc_minecraft/E4mcRelayHandler.kt +++ b/src/main/kotlin/vg/skye/e4mc_minecraft/E4mcRelayHandler.kt @@ -122,28 +122,46 @@ class E4mcRelayHandler: WebSocketClient(URI("wss://ingress.e4mc.link")) { private fun createMessage(domain: String): Text { //#if MC>=11900 return Text.translatable( - "text.e4mc_quilt.domainAssigned", + "text.e4mc_minecraft.domainAssigned", Text.literal(domain).styled { it .withClickEvent(ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, domain)) .withColor(Formatting.GREEN) .withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.translatable("chat.copy.click"))) } + ).append( + Text.translatable("text.e4mc_minecraft.clickToStop").styled { + it + .withClickEvent(ClickEvent(ClickEvent.Action.RUN_COMMAND, "/e4mc stop")) + .withColor(Formatting.GRAY) + } ) //#elseif FABRIC==1 - //$$ return TranslatableText("text.e4mc_quilt.domainAssigned", LiteralText(domain).styled { + //$$ return TranslatableText("text.e4mc_minecraft.domainAssigned", LiteralText(domain).styled { //$$ return@styled it //$$ .withClickEvent(ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, domain)) //$$ .withColor(Formatting.GREEN) //$$ .withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, TranslatableText("chat.copy.click"))) - //$$ }) + //$$ }).append( + //$$ TranslatableText("text.e4mc_minecraft.clickToStop").styled { + //$$ return@styled it + //$$ .withClickEvent(ClickEvent(ClickEvent.Action.RUN_COMMAND, "/e4mc stop")) + //$$ .withColor(Formatting.GRAY) + //$$ } + //$$ ) //#else - //$$ return TranslatableComponent("text.e4mc_quilt.domainAssigned", TextComponent(domain).withStyle { + //$$ return TranslatableComponent("text.e4mc_minecraft.domainAssigned", TextComponent(domain).withStyle { //$$ return@withStyle it //$$ .withClickEvent(ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, domain)) //$$ .withColor(ChatFormatting.GREEN) //$$ .withHoverEvent(HoverEvent(HoverEvent.Action.SHOW_TEXT, TranslatableComponent("chat.copy.click"))) - //$$ }) + //$$ }).append( + //$$ TranslatableComponent("text.e4mc_minecraft.clickToStop").withStyle { + //$$ return@withStyle it + //$$ .withClickEvent(ClickEvent(ClickEvent.Action.RUN_COMMAND, "/e4mc stop")) + //$$ .withColor(ChatFormatting.GRAY) + //$$ } + //$$ ) //#endif } diff --git a/src/main/resources/assets/e4mc_quilt/lang/el_gr.json b/src/main/resources/assets/e4mc_quilt/lang/el_gr.json index 620f478..7149f46 100644 --- a/src/main/resources/assets/e4mc_quilt/lang/el_gr.json +++ b/src/main/resources/assets/e4mc_quilt/lang/el_gr.json @@ -1,3 +1,6 @@ { - "text.e4mc_quilt.domainAssigned": "Τοπικό παιχνίδι εξυπηρετήται στη διεύθηνση [%s]" + "text.e4mc_minecraft.domainAssigned": "Τοπικό παιχνίδι εξυπηρετήται στη διεύθηνση [%s]", + "text.e4mc_minecraft.closeServer": "Το τοπικό παιχνίδι δεν εξυπηρετήται δημόσια πλέον", + "text.e4mc_minecraft.serverAlreadyClosed": "Το τοπικό παιχνίδι δεν εξυπηρετήται δημόσια", + "text.e4mc_minecraft.clickToStop": " (Πατήστε εδώ για απενεργοποίηση)" } \ No newline at end of file diff --git a/src/main/resources/assets/e4mc_quilt/lang/en_us.json b/src/main/resources/assets/e4mc_quilt/lang/en_us.json index e8c2b95..84e4747 100644 --- a/src/main/resources/assets/e4mc_quilt/lang/en_us.json +++ b/src/main/resources/assets/e4mc_quilt/lang/en_us.json @@ -1,3 +1,6 @@ { - "text.e4mc_quilt.domainAssigned": "Local game hosted on domain [%s]" + "text.e4mc_minecraft.domainAssigned": "Local game hosted on domain [%s]", + "text.e4mc_minecraft.closeServer": "Local game no longer publicly hosted", + "text.e4mc_minecraft.serverAlreadyClosed": "Local game not publicly hosted", + "text.e4mc_minecraft.clickToStop": " (Click here to stop)" } diff --git a/src/main/resources/assets/e4mc_quilt/lang/et_ee.json b/src/main/resources/assets/e4mc_quilt/lang/et_ee.json index 6a893f8..6f7870f 100644 --- a/src/main/resources/assets/e4mc_quilt/lang/et_ee.json +++ b/src/main/resources/assets/e4mc_quilt/lang/et_ee.json @@ -1,3 +1,6 @@ { - "text.e4mc_quilt.domainAssigned": "Kohalik mäng on üles seatud domeenil [%s]" + "text.e4mc_minecraft.domainAssigned": "Kohalik mäng on üles seatud domeenil [%s]", + "text.e4mc_minecraft.closeServer": "Kohalik mäng ei ole enam avalikult hostitud", + "text.e4mc_minecraft.serverAlreadyClosed": "Kohalik mäng ei ole avalikult hostitud", + "text.e4mc_minecraft.clickToStop": " (Klõpsa lõpetamiseks)" } \ No newline at end of file