-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add customizable parameters to dialogue layout
- Loading branch information
Showing
24 changed files
with
271 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/java/org/ladysnake/blabber/api/layout/DefaultLayoutParams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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.layout; | ||
|
||
import com.mojang.serialization.Codec; | ||
import net.minecraft.network.PacketByteBuf; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
@ApiStatus.Experimental | ||
public record DefaultLayoutParams() implements DialogueLayout.Params { | ||
public static final DefaultLayoutParams DEFAULT = new DefaultLayoutParams(); | ||
public static final Codec<DefaultLayoutParams> CODEC = Codec.unit(() -> DefaultLayoutParams.DEFAULT); | ||
|
||
public DefaultLayoutParams(PacketByteBuf buf) { | ||
this(); | ||
} | ||
|
||
public static void writeToPacket(PacketByteBuf buf, DefaultLayoutParams params) { | ||
// NO-OP | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/org/ladysnake/blabber/api/layout/DialogueLayout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* 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.layout; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
import org.ladysnake.blabber.impl.common.BlabberRegistrar; | ||
|
||
@ApiStatus.Experimental | ||
public record DialogueLayout<P extends DialogueLayout.Params>(DialogueLayoutType<P> type, P params) { | ||
public static final DialogueLayout<DefaultLayoutParams> DEFAULT = new DialogueLayout<>(BlabberRegistrar.CLASSIC_LAYOUT, DefaultLayoutParams.DEFAULT); | ||
|
||
public interface Params { } | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/org/ladysnake/blabber/api/layout/DialogueLayoutType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* 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.layout; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.network.PacketByteBuf; | ||
import net.minecraft.util.dynamic.Codecs; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.ladysnake.blabber.impl.common.BlabberRegistrar; | ||
|
||
import java.util.function.BiConsumer; | ||
import java.util.function.Function; | ||
|
||
@ApiStatus.Experimental | ||
public class DialogueLayoutType<P extends DialogueLayout.Params> { | ||
public static final Codec<DialogueLayout<?>> CODEC = BlabberRegistrar.LAYOUT_REGISTRY.getCodec().dispatch( | ||
"type", DialogueLayout::type, DialogueLayoutType::getCodec | ||
); | ||
|
||
private final Codec<DialogueLayout<P>> codec; | ||
private final Function<PacketByteBuf, P> read; | ||
private final BiConsumer<PacketByteBuf, P> write; | ||
|
||
public DialogueLayoutType(Codec<P> paramsCodec, P defaultParams, Function<PacketByteBuf, P> read, BiConsumer<PacketByteBuf, P> write) { | ||
this.codec = RecordCodecBuilder.create(instance -> instance.group( | ||
Codecs.createStrictOptionalFieldCodec(paramsCodec, "params", defaultParams).forGetter(DialogueLayout::params) | ||
).apply(instance, p -> new DialogueLayout<>(this, p))); | ||
this.read = read; | ||
this.write = write; | ||
} | ||
|
||
/** | ||
* @return A codec to serialize and deserialize this layout's parameters | ||
*/ | ||
public Codec<DialogueLayout<P>> getCodec() { | ||
return codec; | ||
} | ||
|
||
/** | ||
* Parses this type of DialogueIllustration from a packet. The data within should be everything the client needs to render this | ||
* @param buf the packet's data | ||
* @return a newly parsed DialogueIllustration corresponding to this type | ||
*/ | ||
@ApiStatus.Experimental | ||
public static <P extends DialogueLayout.Params> DialogueLayout<P> readFromPacket(PacketByteBuf buf) { | ||
@SuppressWarnings("unchecked") DialogueLayoutType<P> type = (DialogueLayoutType<P>) buf.readRegistryValue(BlabberRegistrar.LAYOUT_REGISTRY); | ||
assert type != null; | ||
return new DialogueLayout<>(type, type.read.apply(buf)); | ||
} | ||
|
||
/** | ||
* Write the data this illustration needs to be drawn client-side to a packet | ||
* @param buf the packet to write to | ||
* @param toWrite the illustration to write | ||
*/ | ||
@ApiStatus.Experimental | ||
public static <P extends DialogueLayout.Params> void writeToPacket(PacketByteBuf buf, DialogueLayout<P> toWrite) { | ||
buf.writeRegistryValue(BlabberRegistrar.LAYOUT_REGISTRY, toWrite.type()); | ||
toWrite.type().write.accept(buf, toWrite.params()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.