-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Nassim Jahnke <[email protected]>
- Loading branch information
1 parent
40689b6
commit 80dc92e
Showing
31 changed files
with
1,722 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
dependencies { | ||
minecraft("com.mojang:minecraft:1.20.5-rc2") | ||
mappings("net.fabricmc:yarn:1.20.5-rc2+build.2:v2") | ||
|
||
modImplementation("net.fabricmc.fabric-api:fabric-api:0.97.4+1.20.5") | ||
modImplementation("com.terraformersmc:modmenu:9.0.0") | ||
} | ||
|
||
tasks.compileJava { | ||
options.release.set(21) | ||
} |
119 changes: 119 additions & 0 deletions
119
viafabric-mc1205/src/main/java/com/viaversion/fabric/mc1205/ViaFabric.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* This file is part of ViaFabric - https://github.com/ViaVersion/ViaFabric | ||
* Copyright (C) 2018-2024 ViaVersion and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.viaversion.fabric.mc1205; | ||
|
||
import com.google.common.util.concurrent.ThreadFactoryBuilder; | ||
import com.mojang.brigadier.arguments.StringArgumentType; | ||
import com.mojang.brigadier.builder.LiteralArgumentBuilder; | ||
import com.mojang.brigadier.builder.RequiredArgumentBuilder; | ||
import com.viaversion.fabric.common.config.VFConfig; | ||
import com.viaversion.fabric.common.platform.FabricInjector; | ||
import com.viaversion.fabric.common.protocol.HostnameParserProtocol; | ||
import com.viaversion.fabric.common.util.JLoggerToLog4j; | ||
import com.viaversion.fabric.mc1205.commands.VFCommandHandler; | ||
import com.viaversion.fabric.mc1205.platform.FabricPlatform; | ||
import com.viaversion.fabric.mc1205.platform.VFLoader; | ||
import com.viaversion.viaversion.ViaManagerImpl; | ||
import com.viaversion.viaversion.api.Via; | ||
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion; | ||
import io.netty.channel.DefaultEventLoop; | ||
import io.netty.channel.EventLoop; | ||
import net.fabricmc.api.EnvType; | ||
import net.fabricmc.api.ModInitializer; | ||
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; | ||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; | ||
import net.fabricmc.loader.api.FabricLoader; | ||
import net.minecraft.command.CommandSource; | ||
import org.apache.logging.log4j.LogManager; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.logging.Logger; | ||
|
||
public class ViaFabric implements ModInitializer { | ||
public static final Logger JLOGGER = new JLoggerToLog4j(LogManager.getLogger("ViaFabric")); | ||
public static final ExecutorService ASYNC_EXECUTOR; | ||
public static final EventLoop EVENT_LOOP; | ||
public static final CompletableFuture<Void> INIT_FUTURE = new CompletableFuture<>(); | ||
public static VFConfig config; | ||
|
||
static { | ||
ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("ViaFabric-%d").build(); | ||
ASYNC_EXECUTOR = Executors.newFixedThreadPool(8, factory); | ||
EVENT_LOOP = new DefaultEventLoop(factory); | ||
EVENT_LOOP.submit(INIT_FUTURE::join); // https://github.com/ViaVersion/ViaFabric/issues/53 ugly workaround code but works tm | ||
} | ||
|
||
public static <S extends CommandSource> LiteralArgumentBuilder<S> command(String commandName) { | ||
return LiteralArgumentBuilder.<S>literal(commandName) | ||
.then( | ||
RequiredArgumentBuilder | ||
.<S, String>argument("args", StringArgumentType.greedyString()) | ||
.executes(((VFCommandHandler) Via.getManager().getCommandHandler())::execute) | ||
.suggests(((VFCommandHandler) Via.getManager().getCommandHandler())::suggestion) | ||
) | ||
.executes(((VFCommandHandler) Via.getManager().getCommandHandler())::execute); | ||
} | ||
|
||
@Override | ||
public void onInitialize() { | ||
FabricPlatform platform = new FabricPlatform(); | ||
|
||
Via.init(ViaManagerImpl.builder() | ||
.injector(new FabricInjector()) | ||
.loader(new VFLoader()) | ||
.commandHandler(new VFCommandHandler()) | ||
.platform(platform).build()); | ||
|
||
platform.init(); | ||
|
||
ViaManagerImpl manager = (ViaManagerImpl) Via.getManager(); | ||
manager.init(); | ||
|
||
HostnameParserProtocol.INSTANCE.initialize(); | ||
HostnameParserProtocol.INSTANCE.register(Via.getManager().getProviders()); | ||
ProtocolVersion.register(-2, "AUTO"); | ||
|
||
FabricLoader.getInstance().getEntrypoints("viafabric:via_api_initialized", Runnable.class).forEach(Runnable::run); | ||
|
||
registerCommandsV1(); | ||
|
||
config = new VFConfig(FabricLoader.getInstance().getConfigDir().resolve("ViaFabric") | ||
.resolve("viafabric.yml").toFile()); | ||
|
||
manager.onServerLoaded(); | ||
|
||
INIT_FUTURE.complete(null); | ||
} | ||
|
||
|
||
private void registerCommandsV1() { | ||
try { | ||
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated, env) -> dispatcher.register(command("viaversion"))); | ||
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated, env) -> dispatcher.register(command("viaver"))); | ||
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated, env) -> dispatcher.register(command("vvfabric"))); | ||
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) { | ||
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(command("viafabricclient"))); | ||
} | ||
} catch (NoClassDefFoundError ignored) { | ||
JLOGGER.info("Couldn't register command as Fabric Commands V1 isn't installed"); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
viafabric-mc1205/src/main/java/com/viaversion/fabric/mc1205/ViaFabricClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* This file is part of ViaFabric - https://github.com/ViaVersion/ViaFabric | ||
* Copyright (C) 2018-2024 ViaVersion and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.viaversion.fabric.mc1205; | ||
|
||
import com.viaversion.fabric.mc1205.gui.ViaConfigScreen; | ||
import net.fabricmc.api.ClientModInitializer; | ||
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; | ||
import net.fabricmc.fabric.api.client.screen.v1.Screens; | ||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.client.gui.screen.ButtonTextures; | ||
import net.minecraft.client.gui.screen.multiplayer.MultiplayerScreen; | ||
import net.minecraft.client.gui.widget.ButtonWidget; | ||
import net.minecraft.client.gui.widget.TexturedButtonWidget; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Identifier; | ||
|
||
public class ViaFabricClient implements ClientModInitializer { | ||
@Override | ||
public void onInitializeClient() { | ||
registerGui(); | ||
} | ||
|
||
private void registerGui() { | ||
try { | ||
ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> { | ||
if (!(screen instanceof MultiplayerScreen)) return; | ||
ButtonWidget enableClientSideViaVersion = new TexturedButtonWidget(scaledWidth / 2 + 113, 10, | ||
40, 20, // Size | ||
new ButtonTextures(new Identifier("viafabric", "widget_unfocused"), new Identifier("viafabric", "widget_focused")), | ||
it -> MinecraftClient.getInstance().setScreen(new ViaConfigScreen(screen)), | ||
Text.translatable("gui.via_button")); | ||
if (ViaFabric.config.isHideButton()) enableClientSideViaVersion.visible = false; | ||
Screens.getButtons(screen).add(enableClientSideViaVersion); | ||
}); | ||
} catch (NoClassDefFoundError ignored) { | ||
ViaFabric.JLOGGER.info("Couldn't register screen handler as Fabric Screen isn't installed"); | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
viafabric-mc1205/src/main/java/com/viaversion/fabric/mc1205/commands/NMSCommandSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* This file is part of ViaFabric - https://github.com/ViaVersion/ViaFabric | ||
* Copyright (C) 2018-2024 ViaVersion and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.viaversion.fabric.mc1205.commands; | ||
|
||
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; | ||
import net.minecraft.command.CommandSource; | ||
import net.minecraft.entity.Entity; | ||
import net.minecraft.registry.DynamicRegistryManager; | ||
import net.minecraft.server.command.ServerCommandSource; | ||
import net.minecraft.text.MutableText; | ||
import net.minecraft.text.Text; | ||
import com.viaversion.viaversion.api.command.ViaCommandSender; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.UUID; | ||
|
||
public class NMSCommandSender implements ViaCommandSender { | ||
private final CommandSource source; | ||
|
||
public NMSCommandSender(CommandSource source) { | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
public boolean hasPermission(String s) { | ||
// https://gaming.stackexchange.com/questions/138602/what-does-op-permission-level-do | ||
return source.hasPermissionLevel(3); | ||
} | ||
|
||
public static MutableText fromLegacy(String legacy) { | ||
return Text.Serialization.fromJson(legacy, DynamicRegistryManager.EMPTY); | ||
} | ||
|
||
@Override | ||
public void sendMessage(String s) { | ||
if (source instanceof ServerCommandSource) { | ||
((ServerCommandSource) source).sendFeedback(() -> fromLegacy(s), false); | ||
} else if (source instanceof FabricClientCommandSource) { | ||
((FabricClientCommandSource) source).sendFeedback(fromLegacy(s)); | ||
} | ||
} | ||
|
||
@Override | ||
public UUID getUUID() { | ||
if (source instanceof ServerCommandSource) { | ||
Entity entity = ((ServerCommandSource) source).getEntity(); | ||
if (entity != null) return entity.getUuid(); | ||
} else if (source instanceof FabricClientCommandSource) { | ||
return ((FabricClientCommandSource) source).getPlayer().getUuid(); | ||
} | ||
return UUID.nameUUIDFromBytes(getName().getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
if (source instanceof ServerCommandSource) { | ||
return ((ServerCommandSource) source).getName(); | ||
} else if (source instanceof FabricClientCommandSource) { | ||
return ((FabricClientCommandSource) source).getPlayer().getName().getString(); | ||
} | ||
return "?"; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
viafabric-mc1205/src/main/java/com/viaversion/fabric/mc1205/commands/VFCommandHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* This file is part of ViaFabric - https://github.com/ViaVersion/ViaFabric | ||
* Copyright (C) 2018-2024 ViaVersion and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.viaversion.fabric.mc1205.commands; | ||
|
||
import com.viaversion.fabric.common.commands.subs.LeakDetectSubCommand; | ||
import com.mojang.brigadier.arguments.StringArgumentType; | ||
import com.mojang.brigadier.context.CommandContext; | ||
import com.mojang.brigadier.suggestion.Suggestions; | ||
import com.mojang.brigadier.suggestion.SuggestionsBuilder; | ||
import net.minecraft.command.CommandSource; | ||
import com.viaversion.viaversion.commands.ViaCommandHandler; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class VFCommandHandler extends ViaCommandHandler { | ||
{ | ||
try { | ||
registerSubCommand(new LeakDetectSubCommand()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public int execute(CommandContext<? extends CommandSource> ctx) { | ||
String[] args = new String[0]; | ||
try { | ||
args = StringArgumentType.getString(ctx, "args").split(" "); | ||
} catch (IllegalArgumentException ignored) { | ||
} | ||
onCommand( | ||
new NMSCommandSender(ctx.getSource()), | ||
args | ||
); | ||
return 1; | ||
} | ||
|
||
public CompletableFuture<Suggestions> suggestion(CommandContext<? extends CommandSource> ctx, SuggestionsBuilder builder) { | ||
String[] args; | ||
try { | ||
args = StringArgumentType.getString(ctx, "args").split(" ", -1); | ||
} catch (IllegalArgumentException ignored) { | ||
args = new String[]{""}; | ||
} | ||
String[] pref = args.clone(); | ||
pref[pref.length - 1] = ""; | ||
String prefix = String.join(" ", pref); | ||
onTabComplete(new NMSCommandSender(ctx.getSource()), args) | ||
.stream() | ||
.map(it -> { | ||
SuggestionsBuilder b = new SuggestionsBuilder(builder.getInput(), prefix.length() + builder.getStart()); | ||
b.suggest(it); | ||
return b; | ||
}) | ||
.forEach(builder::add); | ||
return builder.buildFuture(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
viafabric-mc1205/src/main/java/com/viaversion/fabric/mc1205/gui/ModMenuConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* This file is part of ViaFabric - https://github.com/ViaVersion/ViaFabric | ||
* Copyright (C) 2018-2024 ViaVersion and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.viaversion.fabric.mc1205.gui; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.terraformersmc.modmenu.api.ConfigScreenFactory; | ||
import com.terraformersmc.modmenu.api.ModMenuApi; | ||
|
||
import java.util.Map; | ||
|
||
public class ModMenuConfig implements ModMenuApi { | ||
@Override | ||
public ConfigScreenFactory<?> getModConfigScreenFactory() { | ||
return ViaConfigScreen::new; | ||
} | ||
|
||
@Override | ||
public Map<String, ConfigScreenFactory<?>> getProvidedConfigScreenFactories() { | ||
return ImmutableMap.of("viafabric", getModConfigScreenFactory()); | ||
} | ||
} |
Oops, something went wrong.