Skip to content

Commit

Permalink
Change to Mixin.
Browse files Browse the repository at this point in the history
  • Loading branch information
Satxm committed Nov 18, 2024
1 parent ff4586b commit 9682b76
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 144 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Modified from [TheGlitch76/mcpnp](https://github.com/TheGlitch76/mcpnp) project

## What Can It Do - 它的作用

For the `Oline Mode` button, there are now three options:
For the `Online Mode` button, there are now three options:
- `Enable`: enable genuine verification, which will verify login information against the Mojang server database, only allowing players who login with a Microsoft account to join,
- `Disable`: not verify login information, allows offline players to join,
- `Disable + UUID Fixer`: Attempt to match the Mojang server user name with the player name for offline mode players to obtain a unique UUID, Meanwhile, UUIDs are retained for users logging in with Microsoft accounts, It can also prevent the loss of backpack and inventory items.
Expand Down
2 changes: 1 addition & 1 deletion fabric/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '1.7+'
id 'fabric-loom' version '1.8+'
}

sourceCompatibility = targetCompatibility = JavaVersion.VERSION_21
Expand Down
26 changes: 1 addition & 25 deletions fabric/src/main/java/io/github/satxm/mcwifipnp/MCWiFiPnP.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package io.github.satxm.mcwifipnp;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.fabricmc.fabric.api.client.screen.v1.Screens;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.PauseScreen;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.commands.BanIpCommands;
import net.minecraft.server.commands.BanListCommands;
Expand All @@ -29,7 +21,6 @@ public class MCWiFiPnP implements ModInitializer {
public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTING.register(this::onServerLoad);
ServerLifecycleEvents.SERVER_STOPPING.register(this::onServerStop);
ScreenEvents.AFTER_INIT.register(MCWiFiPnP::afterScreenInit);

CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
DeOpCommands.register(dispatcher);
Expand All @@ -40,25 +31,10 @@ public void onInitialize() {
BanPlayerCommands.register(dispatcher);
PardonCommand.register(dispatcher);
PardonIpCommand.register(dispatcher);
ForceOfflineCommand.register(dispatcher);
ForceOfflineCommand.register(dispatcher);
});
}

public static void afterScreenInit(Minecraft client, Screen screen, int i, int j) {
if (screen instanceof PauseScreen) {
for (AbstractWidget button : Screens.getButtons(screen)) {
if (button.getMessage().equals(Component.translatable("menu.shareToLan"))) {
Button newButton = Button.builder(Component.translatable("menu.shareToLan"), $ -> {
client.setScreen(new ShareToLanScreenNew(screen));
}).bounds(button.getX(), button.getY(), button.getWidth(), button.getHeight()).build();
newButton.active = button.active;
Screens.getButtons(screen).remove(button);
Screens.getButtons(screen).add(newButton);
}
}
}
}

private void onServerLoad(MinecraftServer server) {
MCWiFiPnPUnit.ReadingConfig(server);
}
Expand Down
40 changes: 0 additions & 40 deletions forge/src/main/java/io/github/satxm/mcwifipnp/MCWiFiPnP.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
package io.github.satxm.mcwifipnp;

import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.screens.PauseScreen;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.server.commands.BanIpCommands;
import net.minecraft.server.commands.BanListCommands;
import net.minecraft.server.commands.BanPlayerCommands;
Expand All @@ -18,7 +8,6 @@
import net.minecraft.server.commands.DeOpCommands;
import net.minecraft.server.commands.OpCommand;
import net.minecraft.server.commands.WhitelistCommand;
import net.minecraftforge.client.event.ScreenEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.server.ServerStartingEvent;
Expand All @@ -32,9 +21,6 @@ public class MCWiFiPnP {

public MCWiFiPnP() {
MinecraftForge.EVENT_BUS.register(this);
MinecraftForge.EVENT_BUS.addListener((final ScreenEvent.Init.Post evt) -> {
onAfterInitScreen(evt.getScreen().getMinecraft(), evt.getScreen(), evt.getListenersList(), evt::addListener, evt::removeListener);
});
}

@SubscribeEvent
Expand All @@ -60,30 +46,4 @@ public void onServerStopping(ServerStoppingEvent event) {
MCWiFiPnPUnit.CloseUPnPPort(event.getServer());
}

public static void onAfterInitScreen(Minecraft client, Screen screen, List<GuiEventListener> children,
Consumer<GuiEventListener> add, Consumer<GuiEventListener> remove) {
if (screen instanceof PauseScreen pauseScreen && screen.getClass() == PauseScreen.class) {
if (pauseScreen.showsPauseMenu()) {
findButton(children).ifPresent(button -> {
Button newButton = Button.builder(Component.translatable("menu.shareToLan"), $ -> {
client.setScreen(new ShareToLanScreenNew(screen));
}).bounds(button.getX(), button.getY(), button.getWidth(), button.getHeight()).build();
newButton.active = button.active;
remove.accept(button);
add.accept(newButton);
});
}
}
}

private static Optional<Button> findButton(List<GuiEventListener> widgets) {
for (GuiEventListener widget : widgets) {
if (widget instanceof Button button) {
if (button.getMessage().equals(Component.translatable("menu.shareToLan")))
return Optional.of(button);
}
}
return Optional.empty();
}

}
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ org.gradle.debug = false

# Mod Properties
mod_id = mcwifipnp
mod_version = 1.7.3
mod_version = 1.7.4
mod_group_id = io.github.satxm.mcwifipnp

# Minecraft Properties
minecraft_version = 1.21

# Fabric Properties on https://fabricmc.net/develop/
fabric_loader_version = 0.15.11
fabric_loader_version = 0.16.7
fabric_version = 0.102.0+1.21

# Forge Properties on https://files.minecraftforge.net/net/minecraftforge/forge/promotions_slim.json
Expand All @@ -22,7 +22,7 @@ neo_version = 21.0.167

# Quilt Properties on https://lambdaurora.dev/tools/import_quilt.html
quilt_loader_version = 0.26.3
qfapi_version = 11.0.0-alpha.3+0.100.7-1.21
qfapi_version = 11.0.0-alpha.3+0.102.0-1.21
# QSL on https://maven.quiltmc.org/repository/release/org/quiltmc/qsl/maven-metadata.xml
qsl_version = 10.0.0-alpha.1+1.21

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
40 changes: 0 additions & 40 deletions neoforge/src/main/java/io/github/satxm/mcwifipnp/MCWiFiPnP.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
package io.github.satxm.mcwifipnp;

import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.screens.PauseScreen;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.server.commands.BanIpCommands;
import net.minecraft.server.commands.BanListCommands;
import net.minecraft.server.commands.BanPlayerCommands;
Expand All @@ -21,7 +11,6 @@
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.client.event.ScreenEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
import net.neoforged.neoforge.event.server.ServerStartingEvent;
Expand All @@ -34,9 +23,6 @@ public class MCWiFiPnP {
public MCWiFiPnP(IEventBus modEventBus) {
NeoForge.EVENT_BUS.register(this);
NeoForge.EVENT_BUS.addListener(this::onRegisterCommands);
NeoForge.EVENT_BUS.addListener((final ScreenEvent.Init.Post evt) -> {
onAfterInitScreen(evt.getScreen().getMinecraft(), evt.getScreen(), evt.getListenersList(), evt::addListener, evt::removeListener);
});
}

@SubscribeEvent
Expand All @@ -62,30 +48,4 @@ public void onServerStopping(ServerStoppingEvent event) {
MCWiFiPnPUnit.CloseUPnPPort(event.getServer());
}

public static void onAfterInitScreen(Minecraft client, Screen screen, List<GuiEventListener> children,
Consumer<GuiEventListener> add, Consumer<GuiEventListener> remove) {
if (screen instanceof PauseScreen pauseScreen && screen.getClass() == PauseScreen.class) {
if (pauseScreen.showsPauseMenu()) {
findButton(children).ifPresent(button -> {
Button newButton = Button.builder(Component.translatable("menu.shareToLan"), $ -> {
client.setScreen(new ShareToLanScreenNew(screen));
}).bounds(button.getX(), button.getY(), button.getWidth(), button.getHeight()).build();
newButton.active = button.active;
remove.accept(button);
add.accept(newButton);
});
}
}
}

private static Optional<Button> findButton(List<GuiEventListener> widgets) {
for (GuiEventListener widget : widgets) {
if (widget instanceof Button button) {
if (button.getMessage().equals(Component.translatable("menu.shareToLan")))
return Optional.of(button);
}
}
return Optional.empty();
}

}
24 changes: 0 additions & 24 deletions quilt/src/main/java/io/github/satxm/mcwifipnp/MCWiFiPnP.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
package io.github.satxm.mcwifipnp;

import net.fabricmc.fabric.api.client.screen.v1.Screens;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import org.quiltmc.loader.api.ModContainer;
import org.quiltmc.qsl.base.api.entrypoint.ModInitializer;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.PauseScreen;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.commands.BanIpCommands;
import net.minecraft.server.commands.BanListCommands;
Expand All @@ -30,7 +22,6 @@ public class MCWiFiPnP implements ModInitializer {
public void onInitialize(ModContainer mod) {
ServerLifecycleEvents.SERVER_STOPPING.register(this::onServerLoad);
ServerLifecycleEvents.SERVER_STOPPING.register(this::onServerStop);
ScreenEvents.AFTER_INIT.register(MCWiFiPnP::afterScreenInit);

CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
DeOpCommands.register(dispatcher);
Expand All @@ -45,21 +36,6 @@ public void onInitialize(ModContainer mod) {
});
}

public static void afterScreenInit(Minecraft client, Screen screen, int x, int y) {
if (screen instanceof PauseScreen) {
for (AbstractWidget button : Screens.getButtons(screen)) {
if (button.getMessage().equals(Component.translatable("menu.shareToLan"))) {
Button newButton = Button.builder(Component.translatable("menu.shareToLan"), $ -> {
client.setScreen(new ShareToLanScreenNew(screen));
}).bounds(button.getX(), button.getY(), button.getWidth(), button.getHeight()).build();
newButton.active = button.active;
Screens.getButtons(screen).remove(button);
Screens.getButtons(screen).add(newButton);
}
}
}
}

private void onServerLoad(MinecraftServer server) {
MCWiFiPnPUnit.ReadingConfig(server);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/github/satxm/mcwifipnp/MCWiFiPnPUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class MCWiFiPnPUnit {
public static Config getConfig(MinecraftServer server) {
return Objects.requireNonNull(configMap.get(server), "no config for server???");
}

public static void OpenToLan() {
Minecraft client = Minecraft.getInstance();
IntegratedServer server = client.getSingleplayerServer();
Expand Down Expand Up @@ -148,7 +148,7 @@ public static void ReadingConfig(MinecraftServer server) {
Path location = server.getWorldPath(LevelResource.ROOT).resolve("mcwifipnp.json");
MCWiFiPnPUnit.Config cfg;
try {
cfg = gson.fromJson(new String(Files.readAllBytes(location),"utf-8"), MCWiFiPnPUnit.Config.class);
cfg = gson.fromJson(new String(Files.readAllBytes(location), "utf-8"), MCWiFiPnPUnit.Config.class);
cfg.location = location;
} catch (IOException | JsonParseException e) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.satxm.mcwifipnp.mixin;

import net.minecraft.client.gui.layouts.LayoutElement;
import net.minecraft.client.gui.layouts.GridLayout;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import java.util.List;

@Mixin(GridLayout.class)
public interface AccessorGridLayout {
@Accessor
List<LayoutElement> getChildren();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.github.satxm.mcwifipnp.mixin;

import io.github.satxm.mcwifipnp.ShareToLanScreenNew;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.layouts.GridLayout;
import net.minecraft.client.gui.layouts.LayoutElement;
import net.minecraft.client.gui.screens.PauseScreen;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentContents;
import net.minecraft.network.chat.contents.TranslatableContents;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.Arrays;
import java.util.List;

@Mixin(PauseScreen.class)
public abstract class MixinPauseScreen extends Screen {
protected MixinPauseScreen(Component title) {
super(title);
}

@Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/layouts/GridLayout;visitWidgets(Ljava/util/function/Consumer;)V"), method = "createPauseMenu", locals = LocalCapture.CAPTURE_FAILSOFT)
public void buttonOverride(CallbackInfo ci, GridLayout gridlayout, GridLayout.RowHelper helper) {
if (gridlayout != null) {
final List<LayoutElement> buttons = ((AccessorGridLayout) gridlayout).getChildren();
for (int i = 0; i < buttons.size(); i++) {
LayoutElement widget = buttons.get(i);
boolean isShareToLan = this.buttonHasText(widget, "menu.shareToLan");
if (widget instanceof Button button) {
if (isShareToLan) {
Button newButton = Button.builder(Component.translatable("menu.shareToLan"), b -> {
this.minecraft.setScreen(new ShareToLanScreenNew(this));
}).bounds(button.getX(), button.getY(), button.getWidth(), button.getHeight()).build();
newButton.active = this.minecraft.hasSingleplayerServer() && !this.minecraft.getSingleplayerServer().isPublished();
buttons.set(i,newButton);
}
}

}
}
}

private static boolean buttonHasText(LayoutElement element, String... translationKeys) {
if (element instanceof Button button) {
Component component = button.getMessage();
ComponentContents textContent = component.getContents();

return textContent instanceof TranslatableContents && Arrays.stream(translationKeys)
.anyMatch(s -> ((TranslatableContents) textContent).getKey().equals(s));
}
return false;
}
}
Loading

0 comments on commit 9682b76

Please sign in to comment.