Skip to content

Commit

Permalink
Port to 1.20.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Flooflez committed Sep 26, 2023
1 parent 1a5ab0a commit f0f07cc
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 144 deletions.
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.20.1
yarn_mappings=1.20.1+build.1
loader_version=0.14.21
minecraft_version=1.20.2
yarn_mappings=1.20.2+build.1
loader_version=0.14.22

# Mod Properties
mod_version = beta-1.5.1-1.20.1
mod_version = 1.5.1-1.20.2
maven_group = alujjdnd.ngrok.lan
archives_base_name = ngrok-lan-expose-mod

# Dependencies
fabric_version=0.83.0+1.20.1
fabric_version=0.89.2+1.20.2
4 changes: 2 additions & 2 deletions remappedSrc/alujjdnd/ngrok/lan/NgrokLan.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import alujjdnd.ngrok.lan.command.ReloadJsonListsCommand;
import com.github.alexdlaird.ngrok.NgrokClient;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -40,7 +40,7 @@ public void onInitialize() {
AutoConfig.register(NLanConfig.class, JanksonConfigSerializer::new);

//register commands
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) ->
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) ->
{
LanOpCommand.register(dispatcher);
LanDeopCommand.register(dispatcher);
Expand Down
20 changes: 8 additions & 12 deletions remappedSrc/alujjdnd/ngrok/lan/NgrokServerInitialiser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -19,19 +19,15 @@ public class NgrokServerInitialiser implements DedicatedServerModInitializer
public static final Logger LOGGER = LoggerFactory.getLogger(NgrokLan.MODID);

@Override
// Initialize the differents parts of the mod when lauched on server
public void onInitializeServer()
{
public void onInitializeServer() {

LOGGER.info("World is Open to LAN with Ngrok LAN");

//register commands
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) ->
{
LanOpCommand.register(dispatcher);
LanDeopCommand.register(dispatcher);
LanWhitelistCommand.register(dispatcher);
ReloadJsonListsCommand.register(dispatcher);
});
// Initialize the differents parts of the mod when lauched on server
CommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess, environment) -> LanOpCommand.register(dispatcher)));
CommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess, environment) -> LanDeopCommand.register(dispatcher)));
CommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess, environment) -> LanWhitelistCommand.register(dispatcher)));
CommandRegistrationCallback.EVENT.register(((dispatcher, registryAccess, environment) -> ReloadJsonListsCommand.register(dispatcher)));
}

}
5 changes: 4 additions & 1 deletion remappedSrc/alujjdnd/ngrok/lan/command/LanDeopCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.text.Text;

import java.util.Collection;
import java.util.Iterator;

public class LanDeopCommand {
private static final SimpleCommandExceptionType ALREADY_DEOPPED_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.deop.failed"));
Expand All @@ -38,7 +39,9 @@ private static int deop(ServerCommandSource source, Collection<GameProfile> targ
if (playerManager.isOperator(gameProfile)) {
playerManager.removeFromOperators(gameProfile);
++i;
source.sendFeedback(new TranslatableText("commands.deop.success", targets.iterator().next().getName()), true);
source.sendFeedback(() -> {
return Text.translatable("commands.deop.success", new Object[]{((GameProfile) targets.iterator().next()).getName()});
}, true);
}
}

Expand Down
9 changes: 6 additions & 3 deletions remappedSrc/alujjdnd/ngrok/lan/command/LanOpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
import net.minecraft.server.PlayerManager;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.TranslatableText;
import net.minecraft.text.Text;

import java.util.Collection;
import java.util.Iterator;

public class LanOpCommand {
private static final SimpleCommandExceptionType ALREADY_OPPED_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.op.failed"));
private static final SimpleCommandExceptionType ALREADY_OPPED_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.op.failed"));

public LanOpCommand() {
}
Expand Down Expand Up @@ -43,7 +44,9 @@ private static int op(ServerCommandSource source, Collection<GameProfile> target
if (!playerManager.isOperator(gameProfile)) {
playerManager.addToOperators(gameProfile);
++i;
source.sendFeedback(new TranslatableText("commands.op.success", targets.iterator().next().getName()), true);
source.sendFeedback(() -> {
return Text.translatable("commands.op.success", new Object[]{((GameProfile) targets.iterator().next()).getName()});
}, true);
}
}

Expand Down
52 changes: 37 additions & 15 deletions remappedSrc/alujjdnd/ngrok/lan/command/LanWhitelistCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package alujjdnd.ngrok.lan.command;

import com.google.common.collect.Lists;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
Expand All @@ -12,16 +13,19 @@
import net.minecraft.server.WhitelistEntry;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Texts;
import net.minecraft.text.TranslatableText;
import net.minecraft.text.Text;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class LanWhitelistCommand {
private static final SimpleCommandExceptionType ALREADY_ON_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.whitelist.alreadyOn"));
private static final SimpleCommandExceptionType ALREADY_OFF_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.whitelist.alreadyOff"));
private static final SimpleCommandExceptionType ADD_FAILED_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.whitelist.add.failed"));
private static final SimpleCommandExceptionType REMOVE_FAILED_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("commands.whitelist.remove.failed"));
private static final SimpleCommandExceptionType ALREADY_ON_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.whitelist.alreadyOn"));
private static final SimpleCommandExceptionType ALREADY_OFF_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.whitelist.alreadyOff"));
private static final SimpleCommandExceptionType ADD_FAILED_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.whitelist.add.failed"));
private static final SimpleCommandExceptionType REMOVE_FAILED_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("commands.whitelist.remove.failed"));

public LanWhitelistCommand() {
}
Expand Down Expand Up @@ -55,8 +59,8 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {

private static int executeReload(ServerCommandSource source) {
source.getServer().getPlayerManager().reloadWhitelist();
source.sendFeedback(new TranslatableText("commands.whitelist.reloaded"), true);
source.getServer().kickNonWhitelistedPlayers(source);
source.sendFeedback(() -> Text.translatable("commands.whitelist.reloaded"), true);
kickNonWhitelistedPlayers(source);
return 1;
}

Expand All @@ -68,7 +72,7 @@ private static int executeAdd(ServerCommandSource source, Collection<GameProfile
if (!whitelist.isAllowed(gameProfile)) {
WhitelistEntry whitelistEntry = new WhitelistEntry(gameProfile);
whitelist.add(whitelistEntry);
source.sendFeedback(new TranslatableText("commands.whitelist.add.success", Texts.toText(gameProfile)), true);
source.sendFeedback(() -> Text.translatable("commands.whitelist.add.success", Texts.toText(gameProfile)), true);
++i;
}
}
Expand All @@ -88,15 +92,15 @@ private static int executeRemove(ServerCommandSource source, Collection<GameProf
if (whitelist.isAllowed(gameProfile)) {
WhitelistEntry whitelistEntry = new WhitelistEntry(gameProfile);
whitelist.remove(whitelistEntry);
source.sendFeedback(new TranslatableText("commands.whitelist.remove.success", Texts.toText(gameProfile)), true);
source.sendFeedback(() -> Text.translatable("commands.whitelist.remove.success", Texts.toText(gameProfile)), true);
++i;
}
}

if (i == 0) {
throw REMOVE_FAILED_EXCEPTION.create();
} else {
source.getServer().kickNonWhitelistedPlayers(source);
kickNonWhitelistedPlayers(source);
return i;
}
}
Expand All @@ -107,8 +111,8 @@ private static int executeOn(ServerCommandSource source) throws CommandSyntaxExc
throw ALREADY_ON_EXCEPTION.create();
} else {
playerManager.setWhitelistEnabled(true);
source.sendFeedback(new TranslatableText("commands.whitelist.enabled"), true);
source.getServer().kickNonWhitelistedPlayers(source);
source.sendFeedback(() -> Text.translatable("commands.whitelist.enabled"), true);
kickNonWhitelistedPlayers(source);
return 1;
}
}
Expand All @@ -119,19 +123,37 @@ private static int executeOff(ServerCommandSource source) throws CommandSyntaxEx
throw ALREADY_OFF_EXCEPTION.create();
} else {
playerManager.setWhitelistEnabled(false);
source.sendFeedback(new TranslatableText("commands.whitelist.disabled"), true);
source.sendFeedback(() -> Text.translatable("commands.whitelist.disabled"), true);
return 1;
}
}

private static int executeList(ServerCommandSource source) {
String[] strings = source.getServer().getPlayerManager().getWhitelistedNames();
if (strings.length == 0) {
source.sendFeedback(new TranslatableText("commands.whitelist.none"), false);
source.sendFeedback(() -> Text.translatable("commands.whitelist.none"), false);
} else {
source.sendFeedback(new TranslatableText("commands.whitelist.list", strings.length, String.join(", ", strings)), false);
source.sendFeedback(() -> Text.translatable("commands.whitelist.list"), false);
}

return strings.length;
}
private static void kickNonWhitelistedPlayers(ServerCommandSource source){
PlayerManager playerManager = source.getServer().getPlayerManager();
if (playerManager.isWhitelistEnabled()) {
Whitelist whitelist = playerManager.getWhitelist();
List<ServerPlayerEntity> list = Lists.newArrayList(playerManager.getPlayerList());

for (ServerPlayerEntity serverPlayerEntity : list) {
GameProfile profile = serverPlayerEntity.getGameProfile();
if(!source.getServer().isHost(profile)){
if (!whitelist.isAllowed(profile) && !playerManager.isOperator(profile)) {
serverPlayerEntity.networkHandler.disconnect(Text.translatable("multiplayer.disconnect.not_whitelisted"));
}
}

}

}
}
}
42 changes: 12 additions & 30 deletions remappedSrc/alujjdnd/ngrok/lan/command/ReloadJsonListsCommand.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
package alujjdnd.ngrok.lan.command;

import alujjdnd.ngrok.lan.NgrokLan;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.minecraft.client.MinecraftClient;
import net.minecraft.command.CommandSource;
import net.minecraft.command.argument.GameProfileArgumentType;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.ServerConfigHandler;
import net.minecraft.server.*;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.TranslatableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

public class ReloadJsonListsCommand {

//check if player has permission (same permission level as /op and /deop)
//run loadJson();
//get source.getServer() from command

private static final SimpleCommandExceptionType LOAD_JSON_EXCEPTION = new SimpleCommandExceptionType(new TranslatableText("text.info.ngroklan.reload.message"));
private static final SimpleCommandExceptionType LOAD_JSON_EXCEPTION = new SimpleCommandExceptionType(Text.translatable("text.info.ngroklan.reload.message"));

public ReloadJsonListsCommand() {
}
Expand All @@ -34,30 +25,21 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
}

private static int loadJson(CommandContext<ServerCommandSource> serverCommandSourceCommandContext) throws CommandSyntaxException {
MinecraftServer server = serverCommandSourceCommandContext.getSource().getServer();

boolean bl3 = ServerConfigHandler.convertOperators(server);
PlayerManager playerManager = serverCommandSourceCommandContext.getSource().getServer().getPlayerManager();

boolean bl4 = ServerConfigHandler.convertWhitelist(server);
Whitelist whitelist = playerManager.getWhitelist();
OperatorList opList = playerManager.getOpList();

try {
whitelist.load();
opList.load();

if(! (bl3 && bl4) ){
MinecraftClient.getInstance().inGameHud.getChatHud().addMessage(Text.translatable("text.info.ngroklan.reload.success").styled(style -> style.withColor(Formatting.GREEN) ));
} catch (Exception e) {
throw LOAD_JSON_EXCEPTION.create();
}
else{
MinecraftClient.getInstance().inGameHud.getChatHud().addMessage( new TranslatableText("text.info.ngroklan.reload.success"));
}

return 1;
}




private static void sleepFiveSeconds() {
try {
Thread.sleep(5000L);
} catch (InterruptedException ignored) {
}
}
}
Loading

0 comments on commit f0f07cc

Please sign in to comment.