DynRes/src/main/java/io/gitlab/jfronny/dynres/mixin/ServerPropertiesHandlerMixi...

55 lines
2.1 KiB
Java

package io.gitlab.jfronny.dynres.mixin;
import io.gitlab.jfronny.dynres.Cfg;
import io.gitlab.jfronny.dynres.DynRes;
import io.gitlab.jfronny.dynres.Logger;
import io.gitlab.jfronny.dynres.ServerPropertiesHandlerExt;
import io.gitlab.jfronny.libjf.Libjf;
import io.gitlab.jfronny.libweb.extra.LibWeb;
import io.gitlab.jfronny.libweb.extra.WebPaths;
import net.minecraft.server.dedicated.ServerPropertiesHandler;
import org.apache.commons.codec.digest.DigestUtils;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.io.FileInputStream;
import java.io.IOException;
@Mixin(ServerPropertiesHandler.class)
public class ServerPropertiesHandlerMixin implements ServerPropertiesHandlerExt {
@Mutable @Final @Shadow public String resourcePackHash;
@Mutable @Final @Shadow public String resourcePackSha1;
@Mutable @Final @Shadow public String resourcePack;
@Inject(at = @At("RETURN"), method = "<init>(Ljava/util/Properties;)V")
public void init(CallbackInfo info) {
applyChanges(true);
}
@Override
public void applyChanges(boolean print) {
resourcePack = WebPaths.concat(LibWeb.api.getServerRoot(), "resources.zip");
if (print)
Logger.l.info("Pack link: " + resourcePack);
resourcePackSha1 = "";
Libjf.registerConfig("dynres", Cfg.class);
if (Cfg.hashResources) {
try {
FileInputStream fs = new FileInputStream(DynRes.resFile);
resourcePackSha1 = DigestUtils.sha1Hex(fs);
if (print)
Logger.l.info("Set hash to " + resourcePackSha1);
fs.close();
} catch (IOException e) {
Logger.l.error("Failed to get hash, continuing with empty", e);
}
}
resourcePackHash = resourcePackSha1;
}
}