Skip to content

Commit

Permalink
Split the mod into common and client sourcesets
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed May 20, 2024
1 parent 4e55375 commit e0a177a
Show file tree
Hide file tree
Showing 54 changed files with 681 additions and 368 deletions.
33 changes: 28 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ plugins {
id 'io.github.ladysnake.chenille' version '0.12.2'
}

archivesBaseName = project.archives_base_name
version = project.mod_version
group = project.maven_group

base {
archivesName = project.archives_base_name
}

chenille {
configureTestmod {
withBaseTestRuns()
Expand All @@ -19,8 +22,30 @@ chenille {
javaVersion = project.java_version.toInteger()
}

java {
withSourcesJar()
}

loom {
splitEnvironmentSourceSets()

mods {
"blabber" {
sourceSet sourceSets.main
sourceSet sourceSets.client
}
"babblings" {
sourceSet sourceSets.testmod
}
}
}

sourceSets {
reiDummy
testmod {
compileClasspath += client.compileClasspath
runtimeClasspath += client.runtimeClasspath
}
}

repositories {
Expand All @@ -35,6 +60,8 @@ repositories {
}

dependencies {
testmodImplementation(sourceSets.client.output)

// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
Expand Down Expand Up @@ -83,10 +110,6 @@ processResources {
}
}

java {
withSourcesJar()
}

jar {
from("COPYING.LESSER") {
rename { "LICENSE_${project.archivesBaseName}"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
import org.jetbrains.annotations.Nullable;
import org.joml.Vector2i;
import org.ladysnake.blabber.Blabber;
import org.ladysnake.blabber.api.illustration.DialogueIllustration;
import org.ladysnake.blabber.api.client.illustration.DialogueIllustrationRenderer;
import org.ladysnake.blabber.api.layout.DialogueLayout;
import org.ladysnake.blabber.impl.client.BlabberClient;
import org.ladysnake.blabber.impl.common.DialogueScreenHandler;
import org.ladysnake.blabber.impl.common.illustrations.PositionTransform;
import org.ladysnake.blabber.impl.common.machine.AvailableChoice;
Expand All @@ -45,7 +46,9 @@
import org.lwjgl.glfw.GLFW;

import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

@ApiStatus.Experimental // half internal, expect some things to change
Expand Down Expand Up @@ -112,6 +115,8 @@ public class BlabberDialogueScreen<P extends DialogueLayout.Params> extends Hand
protected int selectedChoice;
protected boolean hoveringChoice;

protected Map<String, DialogueIllustrationRenderer<?>> illustrations = new HashMap<>();

public BlabberDialogueScreen(DialogueScreenHandler handler, PlayerInventory inventory, Text title) {
super(handler, inventory, title);
GameOptions options = MinecraftClient.getInstance().options;
Expand All @@ -130,10 +135,12 @@ protected P params() {
@Override
protected void init() {
super.init();
this.layout();
this.prepareLayout();
this.illustrations.clear();
this.handler.getIllustrations().forEach((key, illustration) -> this.illustrations.put(key, BlabberClient.createRenderer(illustration)));
}

protected void layout() {
protected void prepareLayout() {
this.computeMargins();
this.layoutIllustrationAnchors();
}
Expand Down Expand Up @@ -186,7 +193,7 @@ public boolean keyPressed(int key, int scancode, int modifiers) {
return null;
}

ChoiceResult result = this.handler.makeChoice(selectedChoice);
ChoiceResult result = this.makeChoice(selectedChoice);

switch (result) {
case END_DIALOGUE -> this.client.setScreen(null);
Expand All @@ -203,7 +210,7 @@ public boolean keyPressed(int key, int scancode, int modifiers) {
default -> {
this.selectedChoice = 0;
this.hoveringChoice = false;
this.layout();
this.prepareLayout();
}
}

Expand Down Expand Up @@ -265,10 +272,7 @@ public void render(DrawContext context, int mouseX, int mouseY, float tickDelta)
int y = mainTextMinY;

for (String illustrationName : this.handler.getCurrentIllustrations()) {
DialogueIllustration illustration = this.handler.getIllustration(illustrationName);
if (illustration != null) {
illustration.render(context, this.textRenderer, positionTransform, mouseX, mouseY, tickDelta);
}
this.getIllustrationRenderer(illustrationName).render(context, this.textRenderer, positionTransform, mouseX, mouseY, tickDelta);
}

Text mainText = this.handler.getCurrentText();
Expand All @@ -287,10 +291,7 @@ public void render(DrawContext context, int mouseX, int mouseY, float tickDelta)
positionTransform.setControlPoints(choiceListMinX, y, choiceListMinX + choiceListMaxWidth, y + strHeight);

for (String illustrationName : choice.illustrations()) {
DialogueIllustration illustration = this.handler.getIllustration(illustrationName);
if (illustration != null) {
illustration.render(context, this.textRenderer, positionTransform, mouseX, mouseY, tickDelta);
}
this.getIllustrationRenderer(illustrationName).render(context, this.textRenderer, positionTransform, mouseX, mouseY, tickDelta);
}

if (selected) {
Expand All @@ -313,6 +314,12 @@ public void render(DrawContext context, int mouseX, int mouseY, float tickDelta)
}
}

private DialogueIllustrationRenderer<?> getIllustrationRenderer(String illustrationName) {
DialogueIllustrationRenderer<?> renderer = this.illustrations.get(illustrationName);
if (renderer == null) throw new IllegalArgumentException("Unknown illustration " + illustrationName);
return renderer;
}

protected @NotNull PositionTransform createPositionTransform() {
return new PositionTransform(this.illustrationSlots);
}
Expand Down Expand Up @@ -355,4 +362,11 @@ protected void drawBackground(DrawContext matrices, float delta, int mouseX, int
protected void drawForeground(DrawContext matrices, int mouseX, int mouseY) {
// NO-OP
}

public ChoiceResult makeChoice(int choice) {
int originalChoiceIndex = this.handler.getAvailableChoices().get(choice).originalChoiceIndex();
ChoiceResult result = this.handler.makeChoice(originalChoiceIndex);
BlabberClient.sendDialogueActionMessage(originalChoiceIndex);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Blabber
* Copyright (C) 2022-2024 Ladysnake
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; If not, see <https://www.gnu.org/licenses>.
*/
package org.ladysnake.blabber.api.client.illustration;

import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import org.ladysnake.blabber.api.illustration.DialogueIllustration;
import org.ladysnake.blabber.api.illustration.DialogueIllustrationType;
import org.ladysnake.blabber.impl.client.BlabberClient;
import org.ladysnake.blabber.impl.common.illustrations.PositionTransform;

public abstract class DialogueIllustrationRenderer<I extends DialogueIllustration> {
public static <I extends DialogueIllustration> void register(DialogueIllustrationType<I> type, DialogueIllustrationRenderer.Factory<I> factory) {
BlabberClient.registerIllustrationRenderer(type, factory);
}

protected final I illustration;

public DialogueIllustrationRenderer(I illustration) {
this.illustration = illustration;
}

/**
* Draw this illustration to the screen.
*
* @param context a context to draw in
* @param textRenderer a text renderer
* @param positionTransform an object that gives you real coordinates from illustration-local ones
* @param mouseX the current x mouse position
* @param mouseY the current y mouse position
* @param tickDelta how much time has passed since last frame
*/
public abstract void render(DrawContext context, TextRenderer textRenderer, PositionTransform positionTransform, int mouseX, int mouseY, float tickDelta);

@FunctionalInterface
public interface Factory<I extends DialogueIllustration> {
DialogueIllustrationRenderer<I> create(I illustration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,47 @@
import org.ladysnake.blabber.Blabber;
import org.ladysnake.blabber.api.client.BlabberDialogueScreen;
import org.ladysnake.blabber.api.client.BlabberScreenRegistry;
import org.ladysnake.blabber.api.client.illustration.DialogueIllustrationRenderer;
import org.ladysnake.blabber.api.illustration.DialogueIllustration;
import org.ladysnake.blabber.api.illustration.DialogueIllustrationType;
import org.ladysnake.blabber.api.layout.DialogueLayout;
import org.ladysnake.blabber.api.layout.DialogueLayoutType;
import org.ladysnake.blabber.impl.client.illustrations.FakePlayerIllustrationRenderer;
import org.ladysnake.blabber.impl.client.illustrations.IllustrationCollectionRenderer;
import org.ladysnake.blabber.impl.client.illustrations.ItemIllustrationRenderer;
import org.ladysnake.blabber.impl.client.illustrations.NbtEntityIllustrationRenderer;
import org.ladysnake.blabber.impl.client.illustrations.SelectedEntityIllustrationRenderer;
import org.ladysnake.blabber.impl.client.illustrations.TextureIllustrationRenderer;
import org.ladysnake.blabber.impl.common.BlabberRegistrar;
import org.ladysnake.blabber.impl.common.DialogueRegistry;
import org.ladysnake.blabber.impl.common.DialogueScreenHandler;
import org.ladysnake.blabber.impl.common.illustrations.DialogueIllustrationCollection;
import org.ladysnake.blabber.impl.common.illustrations.DialogueIllustrationItem;
import org.ladysnake.blabber.impl.common.illustrations.DialogueIllustrationTexture;
import org.ladysnake.blabber.impl.common.illustrations.entity.DialogueIllustrationFakePlayer;
import org.ladysnake.blabber.impl.common.illustrations.entity.DialogueIllustrationNbtEntity;
import org.ladysnake.blabber.impl.common.illustrations.entity.DialogueIllustrationSelectorEntity;
import org.ladysnake.blabber.impl.common.packets.ChoiceAvailabilityPacket;
import org.ladysnake.blabber.impl.common.packets.DialogueListPacket;
import org.ladysnake.blabber.impl.common.packets.SelectedDialogueStatePacket;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import static io.netty.buffer.Unpooled.buffer;

public final class BlabberClient implements ClientModInitializer {
private static final Map<DialogueLayoutType<?>, HandledScreens.Provider<?, ?>> screenRegistry = new HashMap<>();
private static final Map<DialogueLayoutType<?>, HandledScreens.Provider<?, ?>> screenRegistry = new LinkedHashMap<>();
private static final Map<DialogueIllustrationType<?>, DialogueIllustrationRenderer.Factory<?>> illustrationRenderers = new LinkedHashMap<>();

@Override
public void onInitializeClient() {
DialogueIllustrationRenderer.register(DialogueIllustrationCollection.TYPE, IllustrationCollectionRenderer::new);
DialogueIllustrationRenderer.register(DialogueIllustrationItem.TYPE, ItemIllustrationRenderer::new);
DialogueIllustrationRenderer.register(DialogueIllustrationNbtEntity.TYPE, NbtEntityIllustrationRenderer::new);
DialogueIllustrationRenderer.register(DialogueIllustrationFakePlayer.TYPE, FakePlayerIllustrationRenderer::new);
DialogueIllustrationRenderer.register(DialogueIllustrationSelectorEntity.TYPE, SelectedEntityIllustrationRenderer::new);
DialogueIllustrationRenderer.register(DialogueIllustrationTexture.TYPE, TextureIllustrationRenderer::new);
BlabberScreenRegistry.register(BlabberRegistrar.CLASSIC_LAYOUT, BlabberDialogueScreen::new);
BlabberScreenRegistry.register(BlabberRegistrar.RPG_LAYOUT, BlabberRpgDialogueScreen::new);
HandledScreens.register(BlabberRegistrar.DIALOGUE_SCREEN_HANDLER, (HandledScreens.Provider<DialogueScreenHandler, BlabberDialogueScreen<?>>) BlabberClient::createDialogueScreen);
Expand All @@ -70,6 +92,15 @@ public static <P extends DialogueLayout.Params> void registerLayoutScreen(
screenRegistry.put(layoutId, screenProvider);
}

public static <I extends DialogueIllustration> void registerIllustrationRenderer(DialogueIllustrationType<I> type, DialogueIllustrationRenderer.Factory<I> rendererFactory) {
illustrationRenderers.put(type, rendererFactory);
}

public static <I extends DialogueIllustration> DialogueIllustrationRenderer<I> createRenderer(I illustration) {
@SuppressWarnings("unchecked") DialogueIllustrationRenderer.Factory<I> renderer = (DialogueIllustrationRenderer.Factory<I>) illustrationRenderers.get(illustration.getType());
return renderer.create(illustration);
}

private static <P extends DialogueLayout.Params> BlabberDialogueScreen<P> createDialogueScreen(DialogueScreenHandler handler, PlayerInventory inventory, Text title) {
@SuppressWarnings("unchecked") DialogueLayoutType<P> layoutType = (DialogueLayoutType<P>) handler.getLayout().type();
@SuppressWarnings("unchecked") HandledScreens.Provider<DialogueScreenHandler, BlabberDialogueScreen<P>> provider =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program; If not, see <https://www.gnu.org/licenses>.
*/
package org.ladysnake.blabber.impl.compat;
package org.ladysnake.blabber.impl.client.compat;

import net.fabricmc.loader.api.FabricLoader;
import org.jetbrains.annotations.Nullable;
Expand All @@ -28,8 +28,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BlabberMixinPlugin implements IMixinConfigPlugin {
private static final String COMPAT_PREFIX = "org.ladysnake.blabber.mixin.compat.";
public class BlabberClientMixinPlugin implements IMixinConfigPlugin {
private static final String COMPAT_PREFIX = "org.ladysnake.blabber.mixin.client.compat.";
private static final Pattern COMPAT_MIXIN_PATTERN = Pattern.compile(Pattern.quote(COMPAT_PREFIX) + "(?<modid>[a-z_]+?)\\..*");
private final FabricLoader loader = FabricLoader.getInstance();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program; If not, see <https://www.gnu.org/licenses>.
*/
package org.ladysnake.blabber.impl.compat;
package org.ladysnake.blabber.impl.client.compat;

import me.shedaniel.rei.api.client.registry.screen.OverlayDecider;
import net.minecraft.client.gui.screen.Screen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program; If not, see <https://www.gnu.org/licenses>.
*/
package org.ladysnake.blabber.impl.compat;
package org.ladysnake.blabber.impl.client.compat;

import me.shedaniel.rei.api.client.plugins.REIClientPlugin;
import me.shedaniel.rei.api.client.registry.screen.ScreenRegistry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@FieldsAreNonnullByDefault
@MethodsReturnNonnullByDefault
@ApiStatus.Internal
package org.ladysnake.blabber.impl.compat;
package org.ladysnake.blabber.impl.client.compat;

import net.minecraft.util.annotation.FieldsAreNonnullByDefault;
import net.minecraft.util.annotation.MethodsReturnNonnullByDefault;
Expand Down
Loading

0 comments on commit e0a177a

Please sign in to comment.