Skip to content

Commit

Permalink
Update to 1.20.4, bug fixes, add port-reusing, address detecting auto…
Browse files Browse the repository at this point in the history
…host provider, make resource pack generation path configurable
  • Loading branch information
Patbox committed Dec 7, 2023
1 parent 68c812f commit 9a08324
Show file tree
Hide file tree
Showing 26 changed files with 623 additions and 102 deletions.
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ org.gradle.jvmargs=-Xmx2G

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.20.3-rc1
yarn_mappings=1.20.3-rc1+build.1
minecraft_version=1.20.4
yarn_mappings=1.20.4+build.1
loader_version=0.15.0

#Fabric api
fabric_version=0.90.11+1.20.3
fabric_version=0.91.1+1.20.4

maven_group = eu.pb4

mod_version = 0.7.0
mod_version = 0.7.1

minecraft_version_supported = ">=1.20.3-"

Expand Down
1 change: 1 addition & 0 deletions polymer-autohost/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
compileOnly (project(path: ':polymer-virtual-entity', configuration: 'namedElements'))
localRuntime (project(path: ':polymer-virtual-entity', configuration: 'namedElements'))
//modLocalRuntime("xyz.nucleoid:server-translations-api:2.0.0-beta.2+1.19.4-pre2")
api include('io.netty:netty-codec-http:4.1.82.Final')

project(":polymer-core").afterEvaluate {
testmodImplementation project(":polymer-core").sourceSets.testmod.output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import com.google.gson.JsonObject;
import eu.pb4.polymer.autohost.impl.AutoHost;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import net.minecraft.network.ClientConnection;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.function.Supplier;

Expand All @@ -26,7 +28,14 @@ static ResourcePackDataProvider getActive() {
static <T> void register(Identifier identifier, Supplier<ResourcePackDataProvider> providerCreator) {
AutoHost.TYPES.put(identifier, providerCreator);
};
Collection<MinecraftServer.ServerResourcePackProperties> getProperties();
default Collection<MinecraftServer.ServerResourcePackProperties> getProperties(ClientConnection connection) {
return getProperties();
};

@Deprecated
default Collection<MinecraftServer.ServerResourcePackProperties> getProperties() {
return List.of();
};

static MinecraftServer.ServerResourcePackProperties createProperties(@Nullable UUID uuid, String address, @Nullable String hash) {
return new MinecraftServer.ServerResourcePackProperties(uuid, address, hash, AutoHost.config.require || PolymerResourcePackUtils.isRequired(), AutoHost.message);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package eu.pb4.polymer.autohost.impl;

import eu.pb4.polymer.autohost.api.ResourcePackDataProvider;
import eu.pb4.polymer.autohost.impl.providers.NettyProvider;
import eu.pb4.polymer.autohost.impl.providers.EmptyProvider;
import eu.pb4.polymer.autohost.impl.providers.WebServerProvider;
import eu.pb4.polymer.autohost.impl.providers.StandaloneWebServerProvider;
import eu.pb4.polymer.common.impl.CommonImpl;
import eu.pb4.polymer.common.impl.CommonImplUtils;
import eu.pb4.polymer.common.impl.CommonNetworkHandlerExt;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import net.fabricmc.api.ModInitializer;
import net.minecraft.network.packet.s2c.common.ResourcePackSendS2CPacket;
Expand All @@ -22,17 +24,32 @@

public class AutoHost implements ModInitializer {
public static final Map<Identifier, Supplier<ResourcePackDataProvider>> TYPES = new HashMap<>();
public static AutoHostConfig config;
public static Text message;
public static Text disconnectMessage;
public static AutoHostConfig config = new AutoHostConfig();
public static Text message = Text.empty();
public static Text disconnectMessage = Text.empty();

public static ResourcePackDataProvider provider = EmptyProvider.INSTANCE;

public static void init(MinecraftServer server) {
var config = CommonImpl.loadConfig("auto-host", AutoHostConfig.class);
AutoHost.config = config;

if (!config.enabled) {
return;
}

try {
AutoHost.message = Text.Serialization.fromJsonTree(AutoHost.config.message);
} catch (Exception e) {
AutoHost.message = null;
}

try {
AutoHost.disconnectMessage = Text.Serialization.fromJsonTree(AutoHost.config.disconnectMessage);
} catch (Exception e) {
AutoHost.disconnectMessage = Text.literal("This server requires resource pack enabled to play!");
}

var type = TYPES.get(Identifier.tryParse(config.type));


Expand All @@ -51,8 +68,6 @@ public static void init(MinecraftServer server) {

CommonImpl.saveConfig("auto-host", config);


//EarlyPlayNetworkHandler.register(ResourcePackNetworkHandler::create);
provider.serverStarted(server);
}

Expand All @@ -78,40 +93,28 @@ public static void generateAndCall(MinecraftServer server, Consumer<Text> messag

@Override
public void onInitialize() {
ResourcePackDataProvider.register(new Identifier("polymer", "http_server"), WebServerProvider::new);
ResourcePackDataProvider.register(new Identifier("polymer", "automatic"), NettyProvider::new);
ResourcePackDataProvider.register(new Identifier("polymer", "auto"), NettyProvider::new);
ResourcePackDataProvider.register(new Identifier("polymer", "netty"), NettyProvider::new);
ResourcePackDataProvider.register(new Identifier("polymer", "same_port"), NettyProvider::new);
ResourcePackDataProvider.register(new Identifier("polymer", "http_server"), StandaloneWebServerProvider::new);
ResourcePackDataProvider.register(new Identifier("polymer", "standalone"), StandaloneWebServerProvider::new);
ResourcePackDataProvider.register(new Identifier("polymer", "empty"), EmptyProvider::new);

var config = CommonImpl.loadConfig("auto-host", AutoHostConfig.class);
AutoHost.config = config;

if (!config.enabled) {
return;
}

try {
AutoHost.message = Text.Serialization.fromJsonTree(AutoHost.config.message);
} catch (Exception e) {
AutoHost.message = null;
}

try {
AutoHost.disconnectMessage = Text.Serialization.fromJsonTree(AutoHost.config.disconnectMessage);
} catch (Exception e) {
AutoHost.disconnectMessage = Text.literal("This server requires resource pack enabled to play!");
}

CommonImplUtils.registerDevCommands((c) -> {
c.then(literal("reload_resourcepack").executes(context -> {
if (provider.isReady()) {
for (var x : provider.getProperties()) {
for (var x : provider.getProperties(((CommonNetworkHandlerExt) context.getSource().getPlayerOrThrow().networkHandler).polymerCommon$getConnection()
)) {
context.getSource().getPlayerOrThrow().networkHandler.sendPacket(new ResourcePackSendS2CPacket(x.id(), x.url(), x.hash(), AutoHost.config.require || PolymerResourcePackUtils.isRequired(), AutoHost.message));
}
}
return 0;
}));
c.then(literal("rebuild_reload_rp").executes(context -> {
generateAndCall(context.getSource().getServer(), context.getSource()::sendMessage, () -> {
for (var x : provider.getProperties()) {
for (var x : provider.getProperties(((CommonNetworkHandlerExt) context.getSource().getPlayer().networkHandler).polymerCommon$getConnection()
)) {
context.getSource().getPlayer().networkHandler.sendPacket(new ResourcePackSendS2CPacket(x.id(), x.url(), x.hash(), AutoHost.config.require || PolymerResourcePackUtils.isRequired(), AutoHost.message));
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package eu.pb4.polymer.autohost.impl;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.annotations.SerializedName;
import eu.pb4.polymer.autohost.impl.providers.WebServerProvider;
import eu.pb4.polymer.common.impl.CommonImpl;
import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;

public class AutoHostConfig {
public String _c1 = "Enables Polymer's ResourcePack Auto Hosting";
@SerializedName("enabled")
public boolean enabled = CommonImpl.DEV_ENV;
public boolean enabled = CommonImpl.DEV_ENV || PolymerResourcePackUtils.isRequired();
public String _c2 = "Marks resource pack as required";
@SerializedName("required")
public boolean require = false;
public String _c3 = "Type of resource pack provider. Default: 'polymer:http_server'";
public String type = "polymer:http_server";
public String type = "polymer:automatic";
public String _c4 = "Configuration of type, see provider's source for more details";
@SerializedName("settings")
public JsonElement providerSettings = new WebServerProvider.Config().toJson();
public JsonElement providerSettings = new JsonObject();

public String _c5 = "Message sent to clients before pack is loaded";
public JsonElement message = new JsonPrimitive("This server uses resource pack to enhance gameplay with custom textures and models. It might be unplayable without them.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package eu.pb4.polymer.autohost.impl;

import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.c2s.common.ResourcePackStatusC2SPacket;
import net.minecraft.network.packet.s2c.common.ResourcePackSendS2CPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerConfigurationNetworkHandler;
import net.minecraft.server.network.ServerPlayerConfigurationTask;
import net.minecraft.text.Text;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;

public class AutoHostTask implements ServerPlayerConfigurationTask {
public static final Key KEY = new Key("polymer:autohost/send_packs");
private final Collection<MinecraftServer.ServerResourcePackProperties> packs;

private final Set<UUID> requiredPacks = new HashSet<>();
private final Set<UUID> waitingFor = new HashSet<>();

public AutoHostTask(Collection<MinecraftServer.ServerResourcePackProperties> properties) {
this.packs = properties;
for (var pack : packs) {
if (pack.isRequired()) {
requiredPacks.add(pack.id());
}
waitingFor.add(pack.id());
}
}

@Override
public void sendPacket(Consumer<Packet<?>> sender) {
for (var pack : packs) {
sender.accept(new ResourcePackSendS2CPacket(pack.id(), pack.url(), pack.hash(), pack.isRequired(), pack.prompt()));
}
}

@Override
public Key getKey() {
return KEY;
}

public boolean onStatus(ServerConfigurationNetworkHandler handler, UUID id, ResourcePackStatusC2SPacket.Status status) {
switch (status) {
case DECLINED, FAILED_RELOAD, FAILED_DOWNLOAD, INVALID_URL -> {
if (this.requiredPacks.contains(id)) {
handler.disconnect(Text.translatable("multiplayer.requiredTexturePrompt.disconnect"));
}
}
}

if (status.hasFinished()) {
this.waitingFor.remove(id);
}

return this.waitingFor.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package eu.pb4.polymer.autohost.impl;

public interface ClientConnectionExt {
void polymerAutoHost$setAddress(String address, int port);

String polymerAutoHost$getAddress();
int polymerAutoHost$getPort();

default String polymerAutoHost$getFullAddress() {
return polymerAutoHost$getAddress() + ":" + polymerAutoHost$getPort();
}
}
Loading

0 comments on commit 9a08324

Please sign in to comment.