Skip to content

Commit

Permalink
Merge pull request #1 from maxomatic458/lan-world-1.19+
Browse files Browse the repository at this point in the history
Lan world 1.19+
  • Loading branch information
maxomatic458 authored Jul 19, 2023
2 parents 6b5e47f + 8f99c58 commit 47baaa1
Show file tree
Hide file tree
Showing 16 changed files with 369 additions and 140 deletions.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Playit.gg Minecraft mod

This is an **un**official server-side mod that allows you to make a modded server (fabric and forge) public using the [Playit.gg](https://playit.gg) network, without having to port forward.
**Disclaimer**: This is an unofficial mod and is not affiliated with Playit.gg.

This mod will make your server, or singleplayer world public using the [Playit.gg](https://playit.gg) network, without having to port forward.

## Installation

Expand All @@ -9,8 +11,31 @@ This is an **un**official server-side mod that allows you to make a modded serve
2. Put the downloaded jar file into your mods folder

3. Follow the instructions in your chat/server console
(If you are in a singleplayer world run ``/playit open-lan``)

5. You will get a static IP and Port, that anyone can use to connect to your server

## Commands
- ``/playit``
- ``open-lan``, make your singleplayer world public
- ``agent``
- ``status`` get the current status of your playit connection
- ``restart`` restart the playit connection-manager
- ``reset`` unbind the mod from your playit.gg account
- ``shutdown`` stop the connection to playit (existing connections will stay open, until closed)
- ``set-secret`` manually set the secret of the playit mod

- ``prop``
- ``set <option> <value>`` set a config option
- ``get <option> <value>`` get a config option
- ``tunnel get-address`` get the address of the mods playit tunnel (the address your players use to connect with)
- ``account guest-login-link`` generate a link to connect the mod to a playit.gg guest account

**config options**

``mc-timeout-secs`` timeout in seconds for the tunnel channel (default 30s)

4. You will get a static IP and Port, that anyone can use to connect to your server
``autostart`` whether playit should automatically start with your server or singleplayer world (default server: true, client: false)

### Other Links
* [Playit bukkit plugin](https://www.spigotmc.org/resources/playit-gg.105566/) (this mod is basically a forge translation of this plugin)
Expand Down
2 changes: 1 addition & 1 deletion fabric/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.19+build.4
loader_version=0.14.21

# Mod Properties
mod_version=1.2.0
mod_version=1.3.0
maven_group=gg.playit
archives_base_name=playit_fabric

Expand Down
89 changes: 49 additions & 40 deletions fabric/src/main/java/gg/playit/playitfabric/PlayitCommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gg.playit.playitfabric;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
Expand All @@ -11,26 +10,32 @@
import gg.playit.api.ApiError;
import gg.playit.playitfabric.utils.ChatColor;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;

import static net.minecraft.server.command.CommandManager.*;

import java.io.IOException;
import org.slf4j.Logger;

public class PlayitCommand {

private static PlayitFabric playitFabric;
public PlayitFabric playitFabric;
private static final Logger log = LogUtils.getLogger();

public PlayitCommand(PlayitFabric playitFabric) {
PlayitCommand.playitFabric = playitFabric;
this.playitFabric = playitFabric;
}

public void register(CommandDispatcher<ServerCommandSource> dispatcher) {

dispatcher.register(literal("playit")
.requires(source -> source.hasPermissionLevel(3))
.requires(source ->
(source.hasPermissionLevel(3) && source.getServer().isDedicated())
||
(!source.getServer().isDedicated()
&& source.getPlayer().getUuid() == source.getServer().getHostProfile().getId())
)
.then(literal("open-lan")
.executes(ctx -> openLan(ctx.getSource()))
)
.then(literal("agent")
.then(literal("status")
.executes(ctx -> getStatus(ctx.getSource()))
Expand Down Expand Up @@ -89,17 +94,26 @@ public void register(CommandDispatcher<ServerCommandSource> dispatcher) {
);
}

private static int getStatus(ServerCommandSource source) {
ServerPlayerEntity player = source.getPlayer();
private int openLan(ServerCommandSource source) {
if (playitFabric.server.isDedicated()) {
source.sendError(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + "this command is only available in singleplayer"));
return 0;
}

playitFabric.makeLanPublic();
return 0;
}

private int getStatus(ServerCommandSource source) {
PlayitManager manager = playitFabric.playitManager;

if (manager == null) {
String currentSecret = playitFabric.config.CFG_AGENT_SECRET_KEY;
if (currentSecret == null || currentSecret.isEmpty()) {
player.sendMessage(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + "Secret key not set"));
source.sendError(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + "Secret key not set"));
return 0;
}
player.sendMessage(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + "playit status: offline (or shutting down)"));
source.sendError(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + "playit status: offline (or shutting down)"));
return 0;
}
String message = switch (manager.state()) {
Expand All @@ -119,22 +133,22 @@ private static int getStatus(ServerCommandSource source) {
default -> "unknown";
};

player.sendMessage(Text.literal(ChatColor.BLUE + "playit status: " + ChatColor.RESET + message));
source.sendFeedback(Text.literal(ChatColor.BLUE + "playit status: " + ChatColor.RESET + message), false);
return 0;
}

private static int restart(ServerCommandSource source) {
private int restart(ServerCommandSource source) {
playitFabric.resetConnection(null);
return 0;
}

private static int reset(ServerCommandSource source) {
private int reset(ServerCommandSource source) {
playitFabric.config.setAgentSecret("");
playitFabric.resetConnection(null);
return 0;
}

private static int shutdown(ServerCommandSource source) {
private int shutdown(ServerCommandSource source) {
synchronized (playitFabric.managerSync) {
if (playitFabric.playitManager != null) {
playitFabric.playitManager.shutdown();
Expand All @@ -144,15 +158,13 @@ private static int shutdown(ServerCommandSource source) {
return 0;
}

private static int setSecret(ServerCommandSource source, String secret) {
private int setSecret(ServerCommandSource source, String secret) {
playitFabric.config.setAgentSecret(secret);
playitFabric.resetConnection(null);
return 0;
}

private static int getProp(ServerCommandSource source, String prop) {
ServerPlayerEntity player = source.getPlayer();

private int getProp(ServerCommandSource source, String prop) {
String valueCfg;
String valueCurrent;
switch (prop) {
Expand All @@ -172,16 +184,14 @@ private static int getProp(ServerCommandSource source, String prop) {
}

if (valueCfg == null || valueCfg.isEmpty() || valueCurrent == null || valueCurrent.isEmpty()) {
player.sendMessage(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + prop + "is not set"));
source.sendError(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + prop + "is not set"));
return 0;
}
player.sendMessage(Text.literal(ChatColor.BLUE + prop + ChatColor.RESET + " current: " + valueCurrent + ", config: " + valueCfg));
source.sendFeedback(Text.literal(ChatColor.BLUE + prop + ChatColor.RESET + " current: " + valueCurrent + ", config: " + valueCfg), false);
return 0;
}

private static int setProp(ServerCommandSource source, String prop, Object value) {
ServerPlayerEntity player = source.getPlayer();

private int setProp(ServerCommandSource source, String prop, Object value) {
switch (prop) {
case "mc-timeout-sec" -> {
playitFabric.config.setConnectionTimeoutSeconds((Integer) value);
Expand All @@ -191,49 +201,47 @@ private static int setProp(ServerCommandSource source, String prop, Object value
}

default -> {
player.sendMessage(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + "Unknown property " + prop));
source.sendError(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + "Unknown property " + prop));
return 0;
}
}

player.sendMessage(Text.literal("set " + ChatColor.BLUE + prop + ChatColor.RESET + " to " + ChatColor.GREEN + value));
source.sendFeedback(Text.literal("set " + ChatColor.BLUE + prop + ChatColor.RESET + " to " + ChatColor.GREEN + value), false);
if (prop == "mc-timeout-sec") {
player.sendMessage(Text.literal("run " + ChatColor.GREEN + "/playit agent restart" + ChatColor.RESET + " to apply changes"));
source.sendFeedback(Text.literal("run " + ChatColor.GREEN + "/playit agent restart" + ChatColor.RESET + " to apply changes"), false);
return 0;
}
player.sendMessage(Text.literal("changes will be applied on next restart"));
source.sendFeedback(Text.literal("changes will be applied on next restart"), false);
return 0;
}

private static int getTunnelAddress(ServerCommandSource source) {
ServerPlayerEntity player = source.getPlayer();
private int getTunnelAddress(ServerCommandSource source) {
PlayitManager playitManager = playitFabric.playitManager;

if (playitManager != null) {
var address = playitManager.getAddress();
if (address != null) {
player.sendMessage(Text.literal(ChatColor.BLUE + "tunnel address: " + ChatColor.RESET + address));
source.sendFeedback(Text.literal(ChatColor.BLUE + "tunnel address: " + ChatColor.RESET + address), false);
return 0;
}
player.sendMessage(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + "tunnel address is not set"));
source.sendError(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + "tunnel address is not set"));
return 0;
}

player.sendMessage(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + "tunnel is not running"));
source.sendError(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + "tunnel is not running"));
return 0;

}

private static int getGuestLoginLink(ServerCommandSource source) {
ServerPlayerEntity player = source.getPlayer();
private int getGuestLoginLink(ServerCommandSource source) {
String secret = playitFabric.config.CFG_AGENT_SECRET_KEY;

if (secret == null || secret.isEmpty()) {
player.sendMessage(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + "secret is not set"));
source.sendError(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + "secret is not set"));
return 0;
}

player.sendMessage(Text.literal("preparing login link..."));
source.sendFeedback(Text.literal("preparing login link..."), false);

new Thread(() -> {
try {
Expand All @@ -243,11 +251,12 @@ private static int getGuestLoginLink(ServerCommandSource source) {
var url = "https://playit.gg/login/guest-account/" + session;
log.info("generated login url: " + url);

player.sendMessage(Text.literal("generated login url"));
player.sendMessage(Text.literal(ChatColor.BLUE + "URL: " + ChatColor.RESET + url));
source.sendFeedback(Text.literal("generated login url"), false);
source.sendFeedback(Text.literal(ChatColor.BLUE + "URL: " + ChatColor.RESET + url), false);

} catch (ApiError e) {
log.warn("failed to create guest secret: " + e);
player.sendMessage(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + "failed to create guest secret: " + e.getMessage()));
source.sendError(Text.literal(ChatColor.RED + "ERROR: " + ChatColor.RESET + "failed to create guest secret: " + e.getMessage()));
} catch (IOException e) {
log.error("failed to create guest secret: " + e);
}
Expand Down
Loading

0 comments on commit 47baaa1

Please sign in to comment.