129 lines
4.4 KiB
Java
129 lines
4.4 KiB
Java
package online.astradev.packs;
|
|
|
|
import net.minecraft.network.protocol.Packet;
|
|
import net.minecraft.network.protocol.common.ClientboundResourcePackPopPacket;
|
|
import net.minecraft.network.protocol.common.ClientboundResourcePackPushPacket;
|
|
import net.minecraft.network.protocol.common.ServerboundResourcePackPacket;
|
|
import net.minecraft.server.network.ConfigurationTask;
|
|
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
|
|
|
|
import java.util.Optional;
|
|
import java.util.UUID;
|
|
import java.util.function.Consumer;
|
|
|
|
/** Configuration only completes on SUCCESSFULLY_LOADED, never merely ACCEPTED or DOWNLOADED. */
|
|
public final class PackSession implements ConfigurationTask {
|
|
public static final Type TYPE = new Type("astrapacks:resource_pack");
|
|
private final PackRuntime runtime;
|
|
private final ServerCommonPacketListenerImpl listener;
|
|
private final PackConfig config;
|
|
private final PackBuilder.Pack pack;
|
|
private UUID id;
|
|
private long deadline;
|
|
private int retries;
|
|
private boolean done;
|
|
private boolean failed;
|
|
private boolean started;
|
|
|
|
public PackSession(PackRuntime runtime, ServerCommonPacketListenerImpl listener) {
|
|
this.runtime = runtime;
|
|
this.listener = listener;
|
|
this.config = runtime.config();
|
|
this.pack = runtime.current();
|
|
}
|
|
|
|
@Override
|
|
public Type type() {
|
|
return TYPE;
|
|
}
|
|
|
|
@Override
|
|
public void start(Consumer<Packet<?>> sender) {
|
|
started = true;
|
|
if (pack == null) {
|
|
if (config.delivery.required) disconnect(config.messages.unavailable);
|
|
else done = true;
|
|
return;
|
|
}
|
|
send();
|
|
}
|
|
|
|
private void send() {
|
|
if (id != null) listener.send(new ClientboundResourcePackPopPacket(Optional.of(id)));
|
|
id = UUID.randomUUID();
|
|
deadline = System.nanoTime() + config.delivery.timeoutSeconds * 1_000_000_000L;
|
|
try {
|
|
listener.send(
|
|
new ClientboundResourcePackPushPacket(
|
|
id,
|
|
runtime.url(listener, pack),
|
|
pack.sha1(),
|
|
config.delivery.required,
|
|
config.messages.prompt.isBlank()
|
|
? Optional.empty()
|
|
: Optional.of(PackText.of(config.messages.prompt, player()))));
|
|
} catch (RuntimeException e) {
|
|
AstraPacks.LOG.error("Cannot send resource pack to {}", player(), e);
|
|
fail(config.messages.unavailable);
|
|
}
|
|
}
|
|
|
|
public void response(ServerboundResourcePackPacket packet) {
|
|
if (!started || failed || !packet.id().equals(id)) return;
|
|
if (config.delivery.logStatus)
|
|
AstraPacks.LOG.info(
|
|
"Resource pack {}: {} ({})", player(), packet.action(), pack.sha1());
|
|
if (done) return;
|
|
switch (packet.action()) {
|
|
case SUCCESSFULLY_LOADED -> {
|
|
done = true;
|
|
runtime.loaded(listener, config.messages.loaded);
|
|
}
|
|
case DECLINED -> {
|
|
if (config.delivery.required) disconnect(config.messages.declined);
|
|
else done = true;
|
|
}
|
|
case FAILED_DOWNLOAD, FAILED_RELOAD, INVALID_URL, DISCARDED -> {
|
|
if (retries < config.delivery.retries) {
|
|
retries++;
|
|
send();
|
|
} else {
|
|
fail(config.messages.failed);
|
|
}
|
|
}
|
|
default -> {
|
|
/* Accept/download progress does not extend the bounded deadline. */
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean tick() {
|
|
if (started && !done && !failed && System.nanoTime() >= deadline) {
|
|
fail(config.messages.timeout);
|
|
}
|
|
return done;
|
|
}
|
|
|
|
public void replace() {
|
|
if (id != null) listener.send(new ClientboundResourcePackPopPacket(Optional.of(id)));
|
|
}
|
|
|
|
private String player() {
|
|
return listener.getOwner().name();
|
|
}
|
|
|
|
private void fail(String message) {
|
|
if (config.delivery.required || config.delivery.kickOnFailure) {
|
|
disconnect(message);
|
|
} else {
|
|
done = true;
|
|
}
|
|
}
|
|
|
|
private void disconnect(String message) {
|
|
failed = true;
|
|
listener.disconnect(PackText.of(message, player()));
|
|
}
|
|
}
|