From 9611f3fc7f36de88525390133a9eec9030a776f9 Mon Sep 17 00:00:00 2001 From: sisby-folk Date: Mon, 20 Feb 2023 01:03:32 +1100 Subject: [PATCH] extensible quick-switcher API breakages to make this good soon - but this is a good PoC. --- .../sisby/switchy/client/SwitchyClient.java | 6 + .../api/SwitchyClientModInitializer.java | 6 + .../client/api/SwitchyScreenExtensions.java | 16 +++ .../switchy/client/screen/SwitchScreen.java | 126 ++++++++++++------ compat/build.gradle | 10 +- .../folk/sisby/switchy/SwitchyCompat.java | 1 + .../sisby/switchy/SwitchyCompatClient.java | 33 +++++ .../sisby/switchy/modules/DrogtorCompat.java | 2 +- .../switchy/modules/DrogtorCompatClient.java | 34 +++++ .../switchy/modules/FabricTailorCompat.java | 3 +- .../modules/FabricTailorCompatClient.java | 55 ++++++++ .../sisby/switchy/modules/OriginsCompat.java | 4 +- .../switchy/modules/OriginsCompatClient.java | 53 ++++++++ .../modules/StyledNicknamesCompat.java | 14 +- .../modules/StyledNicknamesCompatClient.java | 30 +++++ compat/src/main/resources/quilt.mod.json | 3 +- .../main/java/folk/sisby/switchy/Switchy.java | 2 +- .../folk/sisby/switchy/api/PresetModule.java | 2 +- gradle.properties | 2 +- settings.gradle | 15 ++- src/main/resources/quilt.mod.json | 3 +- 21 files changed, 358 insertions(+), 62 deletions(-) create mode 100644 client/src/main/java/folk/sisby/switchy/client/api/SwitchyClientModInitializer.java create mode 100644 client/src/main/java/folk/sisby/switchy/client/api/SwitchyScreenExtensions.java create mode 100644 compat/src/main/java/folk/sisby/switchy/SwitchyCompatClient.java create mode 100644 compat/src/main/java/folk/sisby/switchy/modules/DrogtorCompatClient.java create mode 100644 compat/src/main/java/folk/sisby/switchy/modules/FabricTailorCompatClient.java create mode 100644 compat/src/main/java/folk/sisby/switchy/modules/OriginsCompatClient.java create mode 100644 compat/src/main/java/folk/sisby/switchy/modules/StyledNicknamesCompatClient.java diff --git a/client/src/main/java/folk/sisby/switchy/client/SwitchyClient.java b/client/src/main/java/folk/sisby/switchy/client/SwitchyClient.java index 831cb836..8c9133f1 100644 --- a/client/src/main/java/folk/sisby/switchy/client/SwitchyClient.java +++ b/client/src/main/java/folk/sisby/switchy/client/SwitchyClient.java @@ -1,7 +1,9 @@ package folk.sisby.switchy.client; import folk.sisby.switchy.Switchy; +import folk.sisby.switchy.client.api.SwitchyClientModInitializer; import org.quiltmc.loader.api.ModContainer; +import org.quiltmc.loader.api.QuiltLoader; import org.quiltmc.qsl.base.api.entrypoint.client.ClientModInitializer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,6 +20,10 @@ public void onInitializeClient(ModContainer mod) { SwitchyCommandsClient.InitializeCommands(); SwitchyClientNetworking.InitializeReceivers(); SwitchyKeybinds.initializeKeybinds(); + + for(SwitchyClientModInitializer init : QuiltLoader.getEntrypoints(ID, SwitchyClientModInitializer.class)) { + init.onInitialize(); + } } } diff --git a/client/src/main/java/folk/sisby/switchy/client/api/SwitchyClientModInitializer.java b/client/src/main/java/folk/sisby/switchy/client/api/SwitchyClientModInitializer.java new file mode 100644 index 00000000..b05430f2 --- /dev/null +++ b/client/src/main/java/folk/sisby/switchy/client/api/SwitchyClientModInitializer.java @@ -0,0 +1,6 @@ +package folk.sisby.switchy.client.api; + +@FunctionalInterface +public interface SwitchyClientModInitializer { + void onInitialize(); +} diff --git a/client/src/main/java/folk/sisby/switchy/client/api/SwitchyScreenExtensions.java b/client/src/main/java/folk/sisby/switchy/client/api/SwitchyScreenExtensions.java new file mode 100644 index 00000000..f6fb4d1f --- /dev/null +++ b/client/src/main/java/folk/sisby/switchy/client/api/SwitchyScreenExtensions.java @@ -0,0 +1,16 @@ +package folk.sisby.switchy.client.api; + +import folk.sisby.switchy.client.presets.SwitchyDisplayPreset; +import folk.sisby.switchy.client.screen.SwitchScreen.ComponentPosition; +import io.wispforest.owo.ui.core.Component; +import net.minecraft.util.Identifier; + +import java.util.function.Function; + +import static folk.sisby.switchy.client.screen.SwitchScreen.registerPresetDisplayComponent; + +public class SwitchyScreenExtensions { + public static void registerQuickSwitchDisplayComponent(Identifier id, ComponentPosition pos, Function componentFunction) { + registerPresetDisplayComponent(id, pos, componentFunction); + } +} diff --git a/client/src/main/java/folk/sisby/switchy/client/screen/SwitchScreen.java b/client/src/main/java/folk/sisby/switchy/client/screen/SwitchScreen.java index eaa2662d..7ce8069e 100644 --- a/client/src/main/java/folk/sisby/switchy/client/screen/SwitchScreen.java +++ b/client/src/main/java/folk/sisby/switchy/client/screen/SwitchScreen.java @@ -7,9 +7,8 @@ import folk.sisby.switchy.client.presets.SwitchyDisplayPresets; import io.wispforest.owo.ui.base.BaseOwoScreen; import io.wispforest.owo.ui.component.Components; -import io.wispforest.owo.ui.container.Containers; -import io.wispforest.owo.ui.container.FlowLayout; -import io.wispforest.owo.ui.container.HorizontalFlowLayout; +import io.wispforest.owo.ui.component.LabelComponent; +import io.wispforest.owo.ui.container.*; import io.wispforest.owo.ui.core.*; import net.minecraft.client.MinecraftClient; import net.minecraft.text.Text; @@ -21,10 +20,24 @@ public class SwitchScreen extends BaseOwoScreen { public final SwitchyDisplayPresets displayPresets; - private static final Map> components = new HashMap<>(); + public enum ComponentPosition { + SIDE_LEFT, + LEFT, + RIGHT, + SIDE_RIGHT + } + private static final Map> sideLeftComponents = new LinkedHashMap<>(); + private static final Map> leftComponents = new LinkedHashMap<>(); + private static final Map> rightComponents = new LinkedHashMap<>(); + private static final Map> sideRightComponents = new LinkedHashMap<>(); - public static void registerPresetDisplayComponent(Identifier id, Function componentFunction) { - components.put(id, componentFunction); + public static void registerPresetDisplayComponent(Identifier id, ComponentPosition pos, Function componentFunction) { + switch (pos) { + case SIDE_LEFT -> sideLeftComponents.put(id, componentFunction); + case LEFT -> leftComponents.put(id, componentFunction); + case RIGHT -> rightComponents.put(id, componentFunction); + case SIDE_RIGHT -> sideRightComponents.put(id, componentFunction); + } } @@ -38,42 +51,75 @@ public SwitchScreen(SwitchyDisplayPresets displayPresets) { return OwoUIAdapter.create(this, Containers::verticalFlow); } + private Component generatePresetComponent(SwitchyDisplayPreset preset) { + // Main Horizontal Flow Panel + HorizontalFlowLayout horizontalFLow = Containers.horizontalFlow(Sizing.fixed(400), Sizing.content()); + horizontalFLow.padding(Insets.vertical(4).withLeft(10).withRight(10)); + horizontalFLow.gap(2); + horizontalFLow.surface(Surface.DARK_PANEL); + horizontalFLow.verticalAlignment(VerticalAlignment.CENTER); + horizontalFLow.horizontalAlignment(HorizontalAlignment.CENTER); + horizontalFLow.mouseEnter().subscribe(() -> horizontalFLow.surface(Surface.DARK_PANEL.and(Surface.outline(Color.WHITE.argb())))); + horizontalFLow.mouseLeave().subscribe(() -> horizontalFLow.surface(Surface.DARK_PANEL)); + horizontalFLow.mouseDown().subscribe((x, y, button) -> { + SwitchyClientNetworking.sendSwitch(preset.presetName); + return true; + }); + + // Left Side Elements + horizontalFLow.children(sideLeftComponents.values().stream().map((fun) -> fun.apply(preset)).filter(Objects::nonNull).toList()); + + // Main Elements + HorizontalFlowLayout leftRightFlow = Containers.horizontalFlow(Sizing.content(), Sizing.content()); + leftRightFlow.gap(4); + + VerticalFlowLayout leftAlignedFlow = Containers.verticalFlow(Sizing.content(), Sizing.content()); + leftAlignedFlow.horizontalAlignment(HorizontalAlignment.LEFT); + leftAlignedFlow.gap(2); + leftAlignedFlow.children(leftComponents.values().stream().map((fun) -> fun.apply(preset)).filter(Objects::nonNull).toList()); + leftRightFlow.child(leftAlignedFlow); + + VerticalFlowLayout rightAlignedFlow = Containers.verticalFlow(Sizing.content(), Sizing.content()); + rightAlignedFlow.horizontalAlignment(HorizontalAlignment.RIGHT); + rightAlignedFlow.gap(2); + rightAlignedFlow.children(rightComponents.values().stream().map((fun) -> fun.apply(preset)).filter(Objects::nonNull).toList()); + leftRightFlow.child(rightAlignedFlow); + + horizontalFLow.child(leftRightFlow); + + // Right Side Elements + horizontalFLow.children(sideRightComponents.values().stream().map((fun) -> fun.apply(preset)).filter(Objects::nonNull).toList()); + + return horizontalFLow; + } + @Override protected void build(FlowLayout rootComponent) { - rootComponent - .surface(Surface.VANILLA_TRANSLUCENT) - .horizontalAlignment(HorizontalAlignment.CENTER) - .verticalAlignment(VerticalAlignment.CENTER); - - List presets = new ArrayList<>(); - displayPresets.presets.forEach((name, preset) -> { - HorizontalFlowLayout presetFlow = Containers.horizontalFlow(Sizing.content(), Sizing.content()); - presetFlow.children(components.values().stream().map((fun) -> fun.apply(preset)).toList()); - presetFlow.padding(Insets.of(10)); - presetFlow.margins(Insets.vertical(2)); - presetFlow.surface(Surface.DARK_PANEL); - presetFlow.mouseDown().subscribe((x, y, button) -> { - SwitchyClientNetworking.sendSwitch(name); - return true; - }); - presetFlow.mouseEnter().subscribe(() -> { - presetFlow.surface(Surface.DARK_PANEL.and(Surface.outline(Color.WHITE.argb()))); - }); - presetFlow.mouseLeave().subscribe(() -> { - presetFlow.surface(Surface.DARK_PANEL); - }); - presets.add(presetFlow); - }); + rootComponent.surface(Surface.VANILLA_TRANSLUCENT); + rootComponent.horizontalAlignment(HorizontalAlignment.CENTER); + rootComponent.verticalAlignment(VerticalAlignment.CENTER); + + List presetFlows = new ArrayList<>(displayPresets.presets.values().stream().map(this::generatePresetComponent).toList()); + + VerticalFlowLayout presetsLayout = Containers.verticalFlow(Sizing.content(), Sizing.content()); + presetsLayout.padding(Insets.of(6)); + presetsLayout.verticalAlignment(VerticalAlignment.CENTER); + presetsLayout.horizontalAlignment(HorizontalAlignment.CENTER); + presetsLayout.gap(4); + presetsLayout.children(presetFlows); + + ScrollContainer presetsScroll = Containers.verticalScroll(Sizing.content(), Sizing.fill(80), presetsLayout); + presetsScroll.surface(Surface.DARK_PANEL); + presetsScroll.padding(Insets.of(4)); + + LabelComponent screenLabel = Components.label(Text.literal("Switchy Presets")); + + VerticalFlowLayout screenLabelFlow = Containers.verticalFlow(Sizing.content(), Sizing.content()); + screenLabelFlow.horizontalAlignment(HorizontalAlignment.CENTER); + screenLabelFlow.gap(2); + screenLabelFlow.children(List.of(screenLabel, presetsScroll)); - rootComponent.child( - Containers.verticalScroll(Sizing.content(), Sizing.fill(80), - Containers.verticalFlow(Sizing.content(), Sizing.content()) - .children(presets) - .padding(Insets.of(10)) - .verticalAlignment(VerticalAlignment.CENTER) - .horizontalAlignment(HorizontalAlignment.CENTER) - ).surface(Surface.DARK_PANEL) - ); + rootComponent.child(screenLabelFlow); } static { @@ -87,6 +133,6 @@ protected void build(FlowLayout rootComponent) { }); // Add base components - registerPresetDisplayComponent(new Identifier(SwitchyClient.ID, "preset_name"), displayPreset -> Components.label(Text.literal(displayPreset.presetName))); + registerPresetDisplayComponent(new Identifier(SwitchyClient.ID, "preset_name"), ComponentPosition.LEFT, displayPreset -> Components.label(Text.literal(displayPreset.presetName))); } } diff --git a/compat/build.gradle b/compat/build.gradle index 38559e26..008a7105 100644 --- a/compat/build.gradle +++ b/compat/build.gradle @@ -1,15 +1,17 @@ dependencies { implementation(project(path: ':core', configuration: 'namedElements')) + implementation(project(path: ':client', configuration: 'namedElements')) + + // Redo Client Deps (why) + modCompileOnly libs.bundles.client // Optional Compatible Mods modCompileOnly libs.bundles.compat.compile modLocalRuntime libs.bundles.compat.runtime modCompileOnly(libs.origins) { - exclude module: 'AdditionalEntityAttributes' + exclude module: 'apoli' } - modLocalRuntime(libs.origins) { - exclude module: 'calio' + modCompileOnly(libs.apoli) { exclude module: 'AdditionalEntityAttributes' } - } diff --git a/compat/src/main/java/folk/sisby/switchy/SwitchyCompat.java b/compat/src/main/java/folk/sisby/switchy/SwitchyCompat.java index 022c298b..b41c8b5b 100644 --- a/compat/src/main/java/folk/sisby/switchy/SwitchyCompat.java +++ b/compat/src/main/java/folk/sisby/switchy/SwitchyCompat.java @@ -5,6 +5,7 @@ import org.quiltmc.loader.api.QuiltLoader; public class SwitchyCompat implements SwitchyModInitializer { + public static final String ID = "switchy_compat"; @Override public void initializeSwitchyCompat() { diff --git a/compat/src/main/java/folk/sisby/switchy/SwitchyCompatClient.java b/compat/src/main/java/folk/sisby/switchy/SwitchyCompatClient.java new file mode 100644 index 00000000..510d45fe --- /dev/null +++ b/compat/src/main/java/folk/sisby/switchy/SwitchyCompatClient.java @@ -0,0 +1,33 @@ +package folk.sisby.switchy; + +import folk.sisby.switchy.client.api.SwitchyClientModInitializer; +import folk.sisby.switchy.modules.DrogtorCompatClient; +import folk.sisby.switchy.modules.FabricTailorCompatClient; +import folk.sisby.switchy.modules.OriginsCompatClient; +import folk.sisby.switchy.modules.StyledNicknamesCompatClient; +import org.quiltmc.loader.api.QuiltLoader; + +public class SwitchyCompatClient implements SwitchyClientModInitializer { + @Override + public void onInitialize() { + // Basically recreate the functionality of every mod because it might not be on the client (ow) + // We can definitely do better than this for API. writing two NBT parsers makes no sense. + + // Resolution: We need to take a path that allows compat modules to be moved into their respective mods + // In this case, this really does mean adding client-side addon functionality to server-sided mods like styled. + // So yes, you may literally need to install styled nicknames onto your client. Patbox is crying. Everyone is crying. + + // If it's serializable or packetable though, we'd be able to parse The module data into something usable on + // the client side *before* sending it. So that's good. + + // I'd say we probably want a Serializer that handles field storage, and an extension of that interface + // Think: DrogtorServerModule extends DrogtorModuleSerializer implements SwitchyModule (?) + // interface SwitchyModule extends SwitchyModuleSerializer + // interface SwitchyClientModule extends SwitchyModuleSerializer + + DrogtorCompatClient.touch(); + StyledNicknamesCompatClient.touch(); + FabricTailorCompatClient.touch(); + if (QuiltLoader.isModLoaded("origins")) OriginsCompatClient.touch(); + } +} diff --git a/compat/src/main/java/folk/sisby/switchy/modules/DrogtorCompat.java b/compat/src/main/java/folk/sisby/switchy/modules/DrogtorCompat.java index 3634d66d..99de8bb2 100644 --- a/compat/src/main/java/folk/sisby/switchy/modules/DrogtorCompat.java +++ b/compat/src/main/java/folk/sisby/switchy/modules/DrogtorCompat.java @@ -45,7 +45,7 @@ public void applyToPlayer(PlayerEntity player) { } @Override - public NbtCompound toNbt() { + public NbtCompound toNbt(boolean displayOnly) { NbtCompound outNbt = new NbtCompound(); if (this.nickname != null) outNbt.putString(KEY_NICKNAME, this.nickname); if (this.namecolor != null) outNbt.putString(KEY_NAME_COLOR, this.namecolor.getName()); diff --git a/compat/src/main/java/folk/sisby/switchy/modules/DrogtorCompatClient.java b/compat/src/main/java/folk/sisby/switchy/modules/DrogtorCompatClient.java new file mode 100644 index 00000000..516f9416 --- /dev/null +++ b/compat/src/main/java/folk/sisby/switchy/modules/DrogtorCompatClient.java @@ -0,0 +1,34 @@ +package folk.sisby.switchy.modules; + +import folk.sisby.switchy.client.api.SwitchyScreenExtensions; +import folk.sisby.switchy.client.screen.SwitchScreen.ComponentPosition; +import io.wispforest.owo.ui.component.Components; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.text.HoverEvent; +import net.minecraft.text.Style; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; +import net.minecraft.util.Identifier; + +public class DrogtorCompatClient { + public static final Identifier ID = new Identifier("switchy", "drogtor"); + + public static final String KEY_NICKNAME = "nickname"; + public static final String KEY_NAME_COLOR = "nameColor"; + public static final String KEY_BIO = "bio"; + + + public static void touch() {} + + static { + SwitchyScreenExtensions.registerQuickSwitchDisplayComponent(ID, ComponentPosition.RIGHT, displayPreset -> { + if (!displayPreset.modules.containsKey(ID)) return null; + NbtCompound nbt = displayPreset.modules.get(ID); + if (!nbt.contains(KEY_NICKNAME)) return null; + Style style = Style.EMPTY; + if (nbt.contains(KEY_NAME_COLOR)) style = style.withColor(Formatting.byName(nbt.getString(KEY_NAME_COLOR))); + if (nbt.contains(KEY_BIO)) style = style.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.literal(nbt.getString(KEY_BIO)))); + return Components.label(Text.literal(nbt.getString(KEY_NICKNAME)).setStyle(style)); + }); + } +} diff --git a/compat/src/main/java/folk/sisby/switchy/modules/FabricTailorCompat.java b/compat/src/main/java/folk/sisby/switchy/modules/FabricTailorCompat.java index 6dd95060..8d73ce61 100644 --- a/compat/src/main/java/folk/sisby/switchy/modules/FabricTailorCompat.java +++ b/compat/src/main/java/folk/sisby/switchy/modules/FabricTailorCompat.java @@ -35,10 +35,11 @@ public void applyToPlayer(PlayerEntity player) { } @Override - public NbtCompound toNbt() { + public NbtCompound toNbt(boolean displayOnly) { NbtCompound outNbt = new NbtCompound(); if (this.skinValue != null) outNbt.putString(KEY_SKIN_VALUE, this.skinValue); if (this.skinSignature != null) outNbt.putString(KEY_SKIN_SIGNATURE, this.skinSignature); + return outNbt; } diff --git a/compat/src/main/java/folk/sisby/switchy/modules/FabricTailorCompatClient.java b/compat/src/main/java/folk/sisby/switchy/modules/FabricTailorCompatClient.java new file mode 100644 index 00000000..d58fc8ea --- /dev/null +++ b/compat/src/main/java/folk/sisby/switchy/modules/FabricTailorCompatClient.java @@ -0,0 +1,55 @@ +package folk.sisby.switchy.modules; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.mojang.authlib.minecraft.MinecraftProfileTexture; +import com.mojang.authlib.yggdrasil.response.MinecraftTexturesPayload; +import com.mojang.util.UUIDTypeAdapter; +import folk.sisby.switchy.client.api.SwitchyScreenExtensions; +import folk.sisby.switchy.client.screen.SwitchScreen; +import io.wispforest.owo.ui.component.Components; +import io.wispforest.owo.ui.component.EntityComponent; +import io.wispforest.owo.ui.core.Sizing; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.network.AbstractClientPlayerEntity; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.util.Identifier; + +import java.util.Base64; +import java.util.UUID; + +public class FabricTailorCompatClient { + public static final Identifier ID = new Identifier("switchy", "fabric_tailor"); + + public static final String KEY_SKIN_VALUE = "skinValue"; + public static final String KEY_SKIN_SIGNATURE = "skinSignature"; + + public static void touch() {} + + static { + SwitchyScreenExtensions.registerQuickSwitchDisplayComponent(ID, SwitchScreen.ComponentPosition.SIDE_RIGHT, displayPreset -> { + if (!displayPreset.modules.containsKey(ID)) return null; + NbtCompound nbt = displayPreset.modules.get(ID); + if (!nbt.contains(KEY_SKIN_VALUE) || !nbt.contains(KEY_SKIN_SIGNATURE)) return null; + MinecraftClient client = MinecraftClient.getInstance(); + + String value = nbt.getString(KEY_SKIN_VALUE); + Gson gson = new GsonBuilder().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create(); + MinecraftTexturesPayload payload = gson.fromJson(new String(Base64.getDecoder().decode(value)), MinecraftTexturesPayload.class); + MinecraftProfileTexture skinTexture = payload.getTextures().get(MinecraftProfileTexture.Type.SKIN); + + Identifier skinId = client.getSkinProvider().loadSkin(skinTexture, MinecraftProfileTexture.Type.SKIN); + + EntityComponent skinPreview = Components.entity(Sizing.fixed(60), new AbstractClientPlayerEntity(client.world, client.getSession().getProfile(), null) { + @Override + public Identifier getSkinTexture() { + return skinId; + } + }); + + skinPreview.scale(0.5F); + + return skinPreview; + }); + } +} diff --git a/compat/src/main/java/folk/sisby/switchy/modules/OriginsCompat.java b/compat/src/main/java/folk/sisby/switchy/modules/OriginsCompat.java index 64985869..a46320d7 100644 --- a/compat/src/main/java/folk/sisby/switchy/modules/OriginsCompat.java +++ b/compat/src/main/java/folk/sisby/switchy/modules/OriginsCompat.java @@ -52,7 +52,7 @@ private static void setOrigin(PlayerEntity player, OriginLayer layer, Origin ori } @Override - public NbtCompound toNbt() { + public NbtCompound toNbt(boolean displayOnly) { NbtCompound outNbt = new NbtCompound(); // From Origins PlayerOriginComponent NbtList originLayerList = new NbtList(); @@ -60,7 +60,7 @@ public NbtCompound toNbt() { for (Map.Entry entry : origins.entrySet()) { NbtCompound layerTag = new NbtCompound(); layerTag.putString("Layer", entry.getKey().getIdentifier().toString()); - layerTag.putString("Origin", entry.getValue().getIdentifier().toString()); + layerTag.putString("Origin", displayOnly ? entry.getValue().getName().getString() : entry.getValue().getIdentifier().toString()); originLayerList.add(layerTag); } } diff --git a/compat/src/main/java/folk/sisby/switchy/modules/OriginsCompatClient.java b/compat/src/main/java/folk/sisby/switchy/modules/OriginsCompatClient.java new file mode 100644 index 00000000..5199c60e --- /dev/null +++ b/compat/src/main/java/folk/sisby/switchy/modules/OriginsCompatClient.java @@ -0,0 +1,53 @@ +package folk.sisby.switchy.modules; + +import folk.sisby.switchy.client.api.SwitchyScreenExtensions; +import folk.sisby.switchy.client.screen.SwitchScreen; +import io.wispforest.owo.ui.component.Components; +import io.wispforest.owo.ui.container.Containers; +import io.wispforest.owo.ui.container.HorizontalFlowLayout; +import io.wispforest.owo.ui.core.Sizing; +import io.wispforest.owo.ui.core.VerticalAlignment; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtElement; +import net.minecraft.text.Style; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; +import net.minecraft.util.Identifier; + +import java.util.ArrayList; +import java.util.List; + +import static io.github.apace100.origins.registry.ModItems.ORB_OF_ORIGIN; + +public class OriginsCompatClient { + public static final Identifier ID = new Identifier("switchy", "origins"); + + public static final String KEY_ORIGINS_LIST = "OriginLayers"; + + public static void touch() {} + + static { + SwitchyScreenExtensions.registerQuickSwitchDisplayComponent(ID, SwitchScreen.ComponentPosition.RIGHT, displayPreset -> { + if (!displayPreset.modules.containsKey(ID)) return null; + NbtCompound nbt = displayPreset.modules.get(ID); + if (!nbt.contains(KEY_ORIGINS_LIST, NbtElement.LIST_TYPE)) return null; + List originNames = new ArrayList<>(); + nbt.getList(KEY_ORIGINS_LIST, NbtElement.COMPOUND_TYPE).forEach((nbtElement -> { + if (nbtElement instanceof NbtCompound c) { + if (c.contains("Origin", NbtElement.STRING_TYPE)) { + originNames.add(c.getString("Origin")); + } + } + })); + + if (originNames.isEmpty()) return null; + + HorizontalFlowLayout originsFlow = Containers.horizontalFlow(Sizing.content(), Sizing.content()); + originsFlow.verticalAlignment(VerticalAlignment.CENTER); + originsFlow.child(Components.item(ORB_OF_ORIGIN.getDefaultStack())); + originsFlow.child(Components.label(Text.literal(String.join(" | ", originNames)).setStyle(Style.EMPTY.withColor(Formatting.GRAY)))); + + return originsFlow; + }); + } +} diff --git a/compat/src/main/java/folk/sisby/switchy/modules/StyledNicknamesCompat.java b/compat/src/main/java/folk/sisby/switchy/modules/StyledNicknamesCompat.java index f3a70c8f..7ee41992 100644 --- a/compat/src/main/java/folk/sisby/switchy/modules/StyledNicknamesCompat.java +++ b/compat/src/main/java/folk/sisby/switchy/modules/StyledNicknamesCompat.java @@ -1,5 +1,6 @@ package folk.sisby.switchy.modules; +import eu.pb4.placeholders.api.TextParserUtils; import eu.pb4.stylednicknames.NicknameHolder; import folk.sisby.switchy.Switchy; import folk.sisby.switchy.api.ModuleImportable; @@ -7,6 +8,7 @@ import folk.sisby.switchy.api.PresetModuleRegistry; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.nbt.NbtCompound; +import net.minecraft.text.Text; import net.minecraft.util.Identifier; import org.jetbrains.annotations.Nullable; @@ -36,9 +38,17 @@ public void applyToPlayer(PlayerEntity player) { } @Override - public NbtCompound toNbt() { + public NbtCompound toNbt(boolean displayOnly) { NbtCompound outNbt = new NbtCompound(); - if (this.styled_nickname != null) outNbt.putString(KEY_NICKNAME, this.styled_nickname); + if (displayOnly && this.styled_nickname != null) { + Text name = TextParserUtils.formatText(this.styled_nickname); + outNbt.putString(KEY_NICKNAME, name.getString()); + if (name.getStyle().getColor() != null) { + outNbt.putString("nameColor", name.getStyle().getColor().getName()); + } + } else if (this.styled_nickname != null) { + outNbt.putString(KEY_NICKNAME, this.styled_nickname); + } return outNbt; } diff --git a/compat/src/main/java/folk/sisby/switchy/modules/StyledNicknamesCompatClient.java b/compat/src/main/java/folk/sisby/switchy/modules/StyledNicknamesCompatClient.java new file mode 100644 index 00000000..4d1bc1df --- /dev/null +++ b/compat/src/main/java/folk/sisby/switchy/modules/StyledNicknamesCompatClient.java @@ -0,0 +1,30 @@ +package folk.sisby.switchy.modules; + +import folk.sisby.switchy.client.api.SwitchyScreenExtensions; +import folk.sisby.switchy.client.screen.SwitchScreen; +import io.wispforest.owo.ui.component.Components; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.text.Style; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; +import net.minecraft.util.Identifier; + +public class StyledNicknamesCompatClient { + public static final Identifier ID = new Identifier("switchy", "styled_nicknames"); + + public static final String KEY_NICKNAME = "styled_nickname"; + public static final String KEY_COLOR = "nameColor"; + + public static void touch() {} + + static { + SwitchyScreenExtensions.registerQuickSwitchDisplayComponent(ID, SwitchScreen.ComponentPosition.RIGHT, displayPreset -> { + if (!displayPreset.modules.containsKey(ID)) return null; + NbtCompound nbt = displayPreset.modules.get(ID); + if (!nbt.contains(KEY_NICKNAME)) return null; + Style style = Style.EMPTY; + if (!nbt.contains(KEY_COLOR)) style.withColor(Formatting.byName(nbt.getString(KEY_COLOR))); + return Components.label(Text.literal(nbt.getString(KEY_NICKNAME)).setStyle(style)); + }); + } +} diff --git a/compat/src/main/resources/quilt.mod.json b/compat/src/main/resources/quilt.mod.json index cf4a2a05..c26aae6a 100644 --- a/compat/src/main/resources/quilt.mod.json +++ b/compat/src/main/resources/quilt.mod.json @@ -19,7 +19,8 @@ }, "intermediate_mappings": "net.fabricmc:intermediary", "entrypoints": { - "switchy": "folk.sisby.switchy.SwitchyCompat" + "switchy": "folk.sisby.switchy.SwitchyCompat", + "switchy_client": "folk.sisby.switchy.SwitchyCompatClient" }, "depends": [ { diff --git a/core/src/main/java/folk/sisby/switchy/Switchy.java b/core/src/main/java/folk/sisby/switchy/Switchy.java index 13051233..6386ad3a 100644 --- a/core/src/main/java/folk/sisby/switchy/Switchy.java +++ b/core/src/main/java/folk/sisby/switchy/Switchy.java @@ -66,7 +66,7 @@ public void onInitialize(ModContainer mod) { SwitchyEvents.InitializeEvents(); SwitchyNetworking.InitializeReceivers(); - for(SwitchyModInitializer init : QuiltLoader.getEntrypoints("switchy", SwitchyModInitializer.class)) { + for(SwitchyModInitializer init : QuiltLoader.getEntrypoints(ID, SwitchyModInitializer.class)) { init.initializeSwitchyCompat(); } diff --git a/core/src/main/java/folk/sisby/switchy/api/PresetModule.java b/core/src/main/java/folk/sisby/switchy/api/PresetModule.java index 6ae1b7a3..370be635 100644 --- a/core/src/main/java/folk/sisby/switchy/api/PresetModule.java +++ b/core/src/main/java/folk/sisby/switchy/api/PresetModule.java @@ -9,7 +9,7 @@ public interface PresetModule { void applyToPlayer(PlayerEntity player); - NbtCompound toNbt(); + default NbtCompound toNbt() {return toNbt(false);} default NbtCompound toNbt(boolean displayOnly) { return displayOnly ? new NbtCompound() : toNbt(); diff --git a/gradle.properties b/gradle.properties index d267f75c..08e6784e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,6 +3,6 @@ org.gradle.jvmargs = -Xmx1G org.gradle.parallel = true # Mod Properties -version = 1.9.0 +version = 1.9.1 maven_group = folk.sisby archives_base_name = switchy diff --git a/settings.gradle b/settings.gradle index 8199f595..e9f680a2 100644 --- a/settings.gradle +++ b/settings.gradle @@ -71,24 +71,26 @@ dependencyResolutionManagement { // // Compat // // version('drogtor', "1.1.3${mc_major == '1.18' ? '' : "+${mc_major}"}") - version('fabrictailor', "1.9.0") + version('fabrictailor', "2.0.1") version('origins', "v1.7.1") + version('apoli', "v2.6.0") version('pehkui', "3.4.2") version('fabrication', "3.0.2+${mc_major}") version('placeholderApi', "2.0.0-pre.1+${mc}") version('playerDataApi', "0.2.2+${mc}") version('styledNicknames', "1.1.2+${mc_major}") - version('styledPlayerList', "2.2.2+1.19.1") + version('placeholder', "2.0.0-beta.7+${mc_major}") library('drogtor', 'maven.modrinth', 'drogtor').versionRef('drogtor') - library('fabrictailor', 'com.github.samolego', 'FabricTailor').versionRef('fabrictailor') + library('fabrictailor', 'maven.modrinth', 'fabrictailor').versionRef('fabrictailor') library('origins', 'com.github.apace100', 'origins-fabric').versionRef('origins') + library('apoli', 'com.github.apace100', 'apoli').versionRef('apoli') library('pehkui', 'com.github.Virtuoel', 'Pehkui').versionRef('pehkui') library('fabrication', 'maven.modrinth', 'fabrication').versionRef('fabrication') library('placeholderApi', 'eu.pb4', 'placeholder-api').versionRef('placeholderApi') library('playerDataApi', 'eu.pb4', 'player-data-api').versionRef('playerDataApi') library('styledNicknames', 'maven.modrinth', 'styled-nicknames').versionRef('styledNicknames') - library('styledPlayerList', 'maven.modrinth', 'styledplayerlist').versionRef('styledPlayerList') + library('placeholder', 'eu.pb4', 'placeholder-api').versionRef('placeholder') bundle('compat_compile', [ 'qfapi', @@ -97,13 +99,12 @@ dependencyResolutionManagement { 'pehkui', 'fabrication', 'styledNicknames', - 'origins' + 'placeholder' ]) bundle('compat_runtime', [ 'drogtor', 'fabrictailor', - 'pehkui', - 'origins' + 'pehkui' ]) } } diff --git a/src/main/resources/quilt.mod.json b/src/main/resources/quilt.mod.json index e2923530..c1a64bf9 100644 --- a/src/main/resources/quilt.mod.json +++ b/src/main/resources/quilt.mod.json @@ -12,7 +12,8 @@ "MerchantPug": "Contributor/s", "Ampflower": "Contributor/s", "SilverAndro": "Contributor/s", - "Ami": "Contributor/s" + "Ami": "Contributor/s", + "Garden System": "Contributor/s" }, "contact": { "homepage": "https://modrinth.com/mod/switchy",