mirror of
https://github.com/AutismDevelopment/Autism-Client.git
synced 2026-10-28 20:49:48 +00:00
Release the tag update
This commit is contained in:
commit
9e00f40996
977 changed files with 219163 additions and 0 deletions
|
|
@ -0,0 +1,11 @@
|
|||
package autismclient.api.custommenu;
|
||||
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
|
||||
public interface CustomMenuAdapter {
|
||||
String id();
|
||||
|
||||
CustomMenuEvent inspectInbound(Packet<?> packet, String phase);
|
||||
|
||||
CustomMenuSubmitResult submit(CustomMenuSnapshot snapshot, CustomMenuSubmission submission);
|
||||
}
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
package autismclient.api.custommenu;
|
||||
|
||||
import autismclient.AutismClientAddon;
|
||||
import autismclient.addons.AddonManager;
|
||||
import autismclient.util.custommenu.VanillaDialogAdapter;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public final class CustomMenuAdapterRegistry {
|
||||
private record Registered(CustomMenuAdapter adapter, String owner) {}
|
||||
private static final Map<String, Registered> ADAPTERS = new LinkedHashMap<>();
|
||||
|
||||
static {
|
||||
ADAPTERS.put(VanillaDialogAdapter.ID, new Registered(new VanillaDialogAdapter(), "autismclient"));
|
||||
}
|
||||
|
||||
private CustomMenuAdapterRegistry() {}
|
||||
|
||||
public static synchronized boolean register(CustomMenuAdapter adapter) {
|
||||
if (adapter == null || adapter.id() == null || adapter.id().isBlank()) return false;
|
||||
String id = adapter.id().trim().toLowerCase(java.util.Locale.ROOT);
|
||||
if (ADAPTERS.containsKey(id)) return false;
|
||||
ADAPTERS.put(id, new Registered(adapter, AddonManager.currentAddonId()));
|
||||
return true;
|
||||
}
|
||||
|
||||
public static synchronized void unregisterAddon(String addonId) {
|
||||
if (addonId == null || addonId.isBlank()) return;
|
||||
ADAPTERS.entrySet().removeIf(entry -> addonId.equals(entry.getValue().owner()));
|
||||
}
|
||||
|
||||
public static CustomMenuEvent inspect(Packet<?> packet, String phase) {
|
||||
List<Registered> snapshot;
|
||||
synchronized (CustomMenuAdapterRegistry.class) {
|
||||
snapshot = new ArrayList<>(ADAPTERS.values());
|
||||
}
|
||||
for (Registered registered : snapshot) {
|
||||
try {
|
||||
CustomMenuEvent event = registered.adapter().inspectInbound(packet, phase);
|
||||
if (event != null && event.type() != CustomMenuEvent.Type.NONE) return event;
|
||||
} catch (Throwable error) {
|
||||
AutismClientAddon.LOG.warn("[CustomMenus] Adapter '{}' failed while reading a packet", registered.adapter().id(), error);
|
||||
}
|
||||
}
|
||||
return CustomMenuEvent.NONE;
|
||||
}
|
||||
|
||||
public static CustomMenuSubmitResult submit(CustomMenuSnapshot snapshot, CustomMenuSubmission submission) {
|
||||
if (snapshot == null) return CustomMenuSubmitResult.failure("No custom menu is open");
|
||||
Registered registered;
|
||||
synchronized (CustomMenuAdapterRegistry.class) {
|
||||
registered = ADAPTERS.get(snapshot.adapterId().toLowerCase(java.util.Locale.ROOT));
|
||||
}
|
||||
if (registered == null) return CustomMenuSubmitResult.failure("Custom menu adapter is unavailable");
|
||||
try {
|
||||
CustomMenuSubmitResult result = registered.adapter().submit(snapshot, submission);
|
||||
return result == null ? CustomMenuSubmitResult.failure("Custom menu adapter returned no result") : result;
|
||||
} catch (Throwable error) {
|
||||
|
||||
AutismClientAddon.LOG.warn("[CustomMenus] Adapter '{}' failed while submitting a form ({})",
|
||||
registered.adapter().id(), error.getClass().getSimpleName());
|
||||
return CustomMenuSubmitResult.failure("Custom menu adapter failed");
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized List<String> ids() {
|
||||
return List.copyOf(ADAPTERS.keySet());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package autismclient.api.custommenu;
|
||||
|
||||
public record CustomMenuButton(int index, String label, String actionId, Kind kind) {
|
||||
public enum Kind { CUSTOM, COMMAND, DIALOG, URL, CLIPBOARD, OTHER, EMPTY }
|
||||
|
||||
public CustomMenuButton {
|
||||
label = label == null ? "" : label;
|
||||
actionId = actionId == null ? "" : actionId;
|
||||
kind = kind == null ? Kind.OTHER : kind;
|
||||
}
|
||||
|
||||
public boolean serverRelevant() {
|
||||
return kind == Kind.CUSTOM || kind == Kind.COMMAND || kind == Kind.DIALOG;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package autismclient.api.custommenu;
|
||||
|
||||
public record CustomMenuEvent(Type type, CustomMenuSnapshot snapshot) {
|
||||
public enum Type { NONE, OPEN, CLEAR }
|
||||
|
||||
public static final CustomMenuEvent NONE = new CustomMenuEvent(Type.NONE, null);
|
||||
public static final CustomMenuEvent CLEAR = new CustomMenuEvent(Type.CLEAR, null);
|
||||
|
||||
public static CustomMenuEvent open(CustomMenuSnapshot snapshot) {
|
||||
return new CustomMenuEvent(Type.OPEN, snapshot);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package autismclient.api.custommenu;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record CustomMenuInput(
|
||||
int index,
|
||||
String key,
|
||||
String label,
|
||||
Kind kind,
|
||||
String initialValue,
|
||||
int maxLength,
|
||||
double min,
|
||||
double max,
|
||||
double step,
|
||||
List<String> options
|
||||
) {
|
||||
public enum Kind { TEXT, BOOLEAN, NUMBER, OPTION }
|
||||
|
||||
public CustomMenuInput {
|
||||
key = key == null ? "" : key;
|
||||
label = label == null ? "" : label;
|
||||
kind = kind == null ? Kind.TEXT : kind;
|
||||
initialValue = initialValue == null ? "" : initialValue;
|
||||
options = options == null ? List.of() : List.copyOf(options);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package autismclient.api.custommenu;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record CustomMenuSnapshot(
|
||||
String adapterId,
|
||||
String phase,
|
||||
long generation,
|
||||
String title,
|
||||
List<CustomMenuInput> inputs,
|
||||
List<CustomMenuButton> buttons,
|
||||
Object adapterState
|
||||
) {
|
||||
public CustomMenuSnapshot {
|
||||
adapterId = adapterId == null ? "" : adapterId;
|
||||
phase = phase == null ? "" : phase;
|
||||
title = title == null ? "" : title;
|
||||
inputs = inputs == null ? List.of() : List.copyOf(inputs);
|
||||
buttons = buttons == null ? List.of() : List.copyOf(buttons);
|
||||
}
|
||||
|
||||
public CustomMenuSnapshot withConnectionState(String newPhase, long newGeneration) {
|
||||
return new CustomMenuSnapshot(adapterId, newPhase, newGeneration, title, inputs, buttons, adapterState);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package autismclient.api.custommenu;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public record CustomMenuSubmission(Map<String, String> values, CustomMenuButton button) {
|
||||
public CustomMenuSubmission {
|
||||
values = values == null ? Map.of() : Map.copyOf(values);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package autismclient.api.custommenu;
|
||||
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.chat.ClickEvent;
|
||||
import java.util.List;
|
||||
|
||||
public record CustomMenuSubmitResult(
|
||||
boolean success,
|
||||
String error,
|
||||
List<Packet<?>> packets,
|
||||
CustomMenuSnapshot replacement,
|
||||
ClickEvent clientAction
|
||||
) {
|
||||
public CustomMenuSubmitResult {
|
||||
error = error == null ? "" : error;
|
||||
packets = packets == null ? List.of() : List.copyOf(packets);
|
||||
}
|
||||
|
||||
public static CustomMenuSubmitResult failure(String error) {
|
||||
return new CustomMenuSubmitResult(false, error, List.of(), null, null);
|
||||
}
|
||||
|
||||
public static CustomMenuSubmitResult packets(List<Packet<?>> packets) {
|
||||
return new CustomMenuSubmitResult(true, "", packets, null, null);
|
||||
}
|
||||
|
||||
public static CustomMenuSubmitResult replacement(CustomMenuSnapshot replacement, ClickEvent clientAction) {
|
||||
return new CustomMenuSubmitResult(true, "", List.of(), replacement, clientAction);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue