Skip to content

Commit

Permalink
Fixes for Virtual Entities, extend Resource Pack generation a bit (co…
Browse files Browse the repository at this point in the history
…py licenses/write more info about mods), allow adding mod ids and custom zips as asset sources, automatically include world/resources.zip
  • Loading branch information
Patbox committed Nov 16, 2023
1 parent a451e88 commit 3e34b52
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,22 @@ public static Path configDir() {
public static <T> T loadConfig(String name, Class<T> clazz) {
try {
var folder = configDir();
if (!folder.toFile().isDirectory()) {
if (folder.toFile().exists()) {
if (!Files.isDirectory(folder)) {
if (Files.exists(folder)) {
Files.deleteIfExists(folder);
}
folder.toFile().mkdirs();
Files.createDirectories(folder);
}
var path = folder.resolve(name + ".json");

if (path.toFile().isFile()) {
String json = IOUtils.toString(new InputStreamReader(new FileInputStream(path.toFile()), StandardCharsets.UTF_8));
var obj = GSON.fromJson(json, clazz);
saveConfig(name, obj);
return obj;

if (obj != null) {
saveConfig(name, obj);
return obj;
}
}
} catch (Exception e) {
LOGGER.warn("Couldn't load config! " + clazz.toString());
Expand All @@ -149,24 +152,24 @@ public static <T> T loadConfig(String name, Class<T> clazz) {
return obj;
} catch (Exception e) {
LOGGER.error("Invalid config class! " + clazz.toString());
return null;
throw new RuntimeException(e);
}
}

public static void saveConfig(String name, Object obj) {
try {
var folder = configDir();
if (!folder.toFile().isDirectory()) {
if (folder.toFile().exists()) {
if (!Files.isDirectory(folder)) {
if (Files.exists(folder)) {
Files.deleteIfExists(folder);
}
folder.toFile().mkdirs();
Files.createDirectories(folder);
}
var path = folder.resolve(name + ".json");

Files.writeString(path, GSON_PRETTY.toJson(obj), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (Exception e) {
LOGGER.warn("Couldn't save config! " + obj.getClass().toString());
LOGGER.warn("Couldn't save config! " + obj.getClass());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public ServerConfigurationNetworkHandlerMixin(MinecraftServer server, ClientConn
@WrapOperation(method = "onReady", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;onPlayerConnect(Lnet/minecraft/network/ClientConnection;Lnet/minecraft/server/network/ServerPlayerEntity;Lnet/minecraft/server/network/ConnectedClientData;)V"))
private void polymerNet$prePlayHandshakeHackfest(PlayerManager manager, ClientConnection connection, ServerPlayerEntity player, ConnectedClientData clientData, Operation<Void> original) {
EarlyPlayConnectionMagic.handle(player, clientData.syncedOptions(), (ServerConfigurationNetworkHandler) (Object) this, player.server, connection, (context) -> {
((ExtClientConnection) connection).polymerNet$wrongPacketConsumer(context.storedPackets()::add);
connection.disableAutoRead();
((ExtClientConnection) connection).polymerNet$wrongPacketConsumer(context.storedPackets()::add);
var attr = ((ExtClientConnection) connection).polymerNet$getChannel().attr(ClientConnection.SERVERBOUND_PROTOCOL_KEY);
attr.set(NetworkState.CONFIGURATION.getHandler(NetworkSide.SERVERBOUND));
connection.setPacketListener(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public abstract class ServerLoginNetworkHandlerMixin implements NetworkHandlerEx
ci.cancel();
var defaultOptions = SyncedClientOptions.createDefault();
EarlyConfigurationConnectionMagic.handle(this.profile, defaultOptions, (ServerLoginNetworkHandler) (Object) this, this.server, connection, (context) -> {
((ExtClientConnection) connection).polymerNet$wrongPacketConsumer(context.storedPackets()::add);
connection.disableAutoRead();
((ExtClientConnection) connection).polymerNet$wrongPacketConsumer(context.storedPackets()::add);
var attr = ((ExtClientConnection) connection).polymerNet$getChannel().attr(ClientConnection.SERVERBOUND_PROTOCOL_KEY);
attr.set(NetworkState.LOGIN.getHandler(NetworkSide.SERVERBOUND));
connection.setPacketListener((ServerLoginNetworkHandler) (Object) this);
Expand All @@ -75,6 +75,7 @@ public abstract class ServerLoginNetworkHandlerMixin implements NetworkHandlerEx
this.polymerNet$overrideOptions = context.options().getValue();
}
this.onEnterConfiguration(packet);
this.connection.enableAutoRead();
if (this.connection.getPacketListener() instanceof ServerConfigurationPacketListener listener) {
for (var packetx : context.storedPackets()) {
try {
Expand All @@ -84,7 +85,6 @@ public abstract class ServerLoginNetworkHandlerMixin implements NetworkHandlerEx
}
}
}
this.connection.enableAutoRead();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;

import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -169,6 +171,43 @@ public static boolean build(Path output, Consumer<String> status) {
builder.copyFromPath(path);
}

try {
for (var field : PolymerResourcePackImpl.INCLUDE_MOD_IDS) {
builder.copyAssets(field);
}
var gamePath = FabricLoader.getInstance().getGameDir();

for (var field : PolymerResourcePackImpl.INCLUDE_ZIPS) {
var zipPath = gamePath.resolve(field);

if (Files.exists(zipPath)) {
try (var fs = FileSystems.newFileSystem(zipPath)) {
builder.copyFromPath(fs.getPath("assets"), "assets/");

for (var root : fs.getRootDirectories()) {
try (var str = Files.list(root)) {
str.forEach(file -> {
try {
var name = file.getFileName().toString();
if (name.toLowerCase(Locale.ROOT).contains("license")
|| name.toLowerCase(Locale.ROOT).contains("licence")) {
builder.addData("licenses/"
+ field.replace("/", "_").replace("\\", "_") + "/" + name, Files.readAllBytes(file));
}
} catch (Throwable ignored) {}
});
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}

} catch (Throwable e) {
e.printStackTrace();
}

if (CompatStatus.POLYMC) {
try {
Files.createDirectories(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@
public interface ResourcePackBuilder {
boolean addData(String path, byte[] data);
boolean copyAssets(String modId);

boolean copyFromPath(Path path, String targetPrefix, boolean override);
default boolean copyFromPath(Path path, String targetPrefix) {
return this.copyFromPath(path, targetPrefix, true);
}
default boolean copyFromPath(Path path) {
return this.copyFromPath(path, true);
return this.copyFromPath(path, "", true);
}
boolean copyFromPath(Path path, boolean override);
default boolean copyFromPath(Path path, boolean override) {
return this.copyFromPath(path, "", override);
}

boolean addCustomModelData(PolymerModelData itemModel);
boolean addArmorModel(PolymerArmorModel model);
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
import eu.pb4.polymer.common.impl.CommonImpl;
import eu.pb4.polymer.common.impl.CompatStatus;

import java.util.ArrayList;
import java.util.List;

public class PolymerResourcePackImpl {
public static final boolean FORCE_REQUIRE;
public static final boolean USE_OFFSET;
public static final int OFFSET_VALUES;
public static final boolean USE_ALT_ARMOR_HANDLER;
public static final List<String> INCLUDE_MOD_IDS;
public static final List<String> INCLUDE_ZIPS;


static {
Expand All @@ -21,6 +26,10 @@ public class PolymerResourcePackImpl {
OFFSET_VALUES = config.offsetValue;

USE_ALT_ARMOR_HANDLER = config.useAlternativeArmorHandler || CompatStatus.REQUIRE_ALT_ARMOR_HANDLER;

INCLUDE_MOD_IDS = config.includeModAssets;

INCLUDE_ZIPS = config.includeZips;
}


Expand All @@ -36,5 +45,12 @@ public static class Config {
public String _c4 = "Enables usage of alternative armor rendering for increased mod compatibility";
@SerializedName("use_alternative_armor_rendering")
public boolean useAlternativeArmorHandler;

public String _c5 = "Included resource packs from mods!";
@SerializedName("include_mod_assets")
public List<String> includeModAssets = new ArrayList<>();
public String _c6 = "Included resource packs from zips!";
@SerializedName("include_zips")
public List<String> includeZips = List.of("world/resources.zip");
}
}
Loading

0 comments on commit 3e34b52

Please sign in to comment.