Skip to content

Commit

Permalink
Merge pull request #194 from Dragon-Seeker/1.20.2-endec
Browse files Browse the repository at this point in the history
[1.20.2] Endec - Simplier Codec Alternative
  • Loading branch information
gliscowo authored Dec 6, 2023
2 parents e6c7e73 + 6d0c7c1 commit 89fcc07
Show file tree
Hide file tree
Showing 91 changed files with 6,067 additions and 1,131 deletions.
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//file:noinspection GradlePackageVersionRange
plugins {
id 'fabric-loom' version '1.2-SNAPSHOT'
id 'fabric-loom' version '1.4-SNAPSHOT'
id 'maven-publish'
id 'io.github.juuxel.loom-vineflower' version '1.11.0'
}

allprojects {
Expand Down Expand Up @@ -124,7 +123,7 @@ loom {
}

dependencies {
modLocalRuntime("me.shedaniel:RoughlyEnoughItems-fabric:${project.rei_version}")
// modLocalRuntime("me.shedaniel:RoughlyEnoughItems-fabric:${project.rei_version}")
modCompileOnly("me.shedaniel:RoughlyEnoughItems-api-fabric:${project.rei_version}")

modCompileOnly("dev.emi:emi-fabric:${project.emi_version}")
Expand Down
16 changes: 8 additions & 8 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
org.gradle.jvmargs=-Xmx2G
# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_base_version=1.20.2
minecraft_version=1.20.2
yarn_mappings=1.20.2+build.1
loader_version=0.14.22
minecraft_base_version=1.20.3
minecraft_version=1.20.3
yarn_mappings=1.20.3+build.1
loader_version=0.15.0
# Mod Properties
mod_version=0.11.3
mod_version=0.12.0-endec-alpha.15
maven_group=io.wispforest
archives_base_name=owo-lib
# Dependencies
fabric_version=0.89.2+1.20.2
fabric_version=0.91.1+1.20.3

# https://maven.shedaniel.me/me/shedaniel/RoughlyEnoughItems-fabric/
rei_version=13.0.661
Expand All @@ -23,7 +23,7 @@ emi_version=1.0.19+1.20.1
jankson_version=1.2.2

# https://maven.terraformersmc.com/releases/com/terraformersmc/modmenu
modmenu_version=8.0.0
modmenu_version=9.0.0-pre.1

# https://maven.nucleoid.xyz/xyz/nucleoid/server-translations-api/
stapi_version=2.0.0+1.20
stapi_version=2.2.0+1.20.3-rc1
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void launch() {
if (FabricLoader.getInstance().isModLoaded("owo-impl")) return;

try {
if (GraphicsEnvironment.isHeadless() || FORCE_HEADLESS || System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("mac")) {
if (System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("mac") || GraphicsEnvironment.isHeadless() || FORCE_HEADLESS) {
SentinelConsole.run();
} else {
SentinelWindow.open();
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/io/wispforest/owo/Owo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import io.wispforest.owo.client.screens.ScreenInternals;
import io.wispforest.owo.command.debug.OwoDebugCommands;
import io.wispforest.owo.ops.LootOps;
import io.wispforest.owo.text.CustomTextRegistry;
import io.wispforest.owo.text.InsertingTextContent;
import io.wispforest.owo.ui.parsing.UIModelLoader;
import io.wispforest.owo.util.Wisdom;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.resource.ResourceType;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
Expand Down Expand Up @@ -51,7 +49,7 @@ public class Owo implements ModInitializer {
@ApiStatus.Internal
public void onInitialize() {
LootOps.registerListener();
InsertingTextContent.init();
CustomTextRegistry.register(InsertingTextContent.TYPE, "index");
ScreenInternals.init();

ServerLifecycleEvents.SERVER_STARTING.register(server -> SERVER = server);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.wispforest.owo.client.screens;

import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.serialization.endec.ReflectiveEndecBuilder;
import net.minecraft.entity.player.PlayerEntity;
import org.jetbrains.annotations.NotNull;

Expand All @@ -12,12 +14,21 @@ public interface OwoScreenHandler {
* and will automatically synchronize to the client - think {@link net.minecraft.screen.PropertyDelegate}
* but without being restricted to integers
*
* @param klass The class of the property's value
* @param clazz The class of the property's value
* @param endec The endec to use for (de-)serializing the value of this property over the network
* @param initial The value with which to initialize the property
* @return The created property
*/
default <T> SyncedProperty<T> createProperty(Class<T> klass, T initial) {
throw new IllegalStateException("Implemented in ScreenHandlerMixin");
default <T> SyncedProperty<T> createProperty(Class<T> clazz, Endec<T> endec, T initial) {
throw new UnsupportedOperationException("Implemented in ScreenHandlerMixin");
}

/**
* Shorthand for {@link #createProperty(Class, Endec, Object)} which creates the endec
* through {@link ReflectiveEndecBuilder#get(Class)}
*/
default <T> SyncedProperty<T> createProperty(Class<T> clazz, T initial) {
return this.createProperty(clazz, ReflectiveEndecBuilder.get(clazz), initial);
}

/**
Expand All @@ -28,11 +39,20 @@ default <T> SyncedProperty<T> createProperty(Class<T> klass, T initial) {
*
* @param messageClass The class of message to send, must be a record - much like
* packets in an {@link io.wispforest.owo.network.OwoNetChannel}
* @param endec The endec to use for (de-)serializing messages sent over the network
* @param handler The handler to execute when a message of the given class is
* received on the server
*/
default <R extends Record> void addServerboundMessage(Class<R> messageClass, Endec<R> endec, Consumer<R> handler) {
throw new UnsupportedOperationException("Implemented in ScreenHandlerMixin");
}

/**
* Shorthand for {@link #addServerboundMessage(Class, Endec, Consumer)} which creates the endec
* through {@link ReflectiveEndecBuilder#get(Class)}
*/
default <R extends Record> void addServerboundMessage(Class<R> messageClass, Consumer<R> handler) {
throw new IllegalStateException("Implemented in ScreenHandlerMixin");
this.addServerboundMessage(messageClass, ReflectiveEndecBuilder.get(messageClass), handler);
}

/**
Expand All @@ -43,27 +63,36 @@ default <R extends Record> void addServerboundMessage(Class<R> messageClass, Con
*
* @param messageClass The class of message to send, must be a record - much like
* packets in an {@link io.wispforest.owo.network.OwoNetChannel}
* @param endec The endec to use for (de-)serializing messages sent over the network
* @param handler The handler to execute when a message of the given class is
* received on the client
*/
default <R extends Record> void addClientboundMessage(Class<R> messageClass, Endec<R> endec, Consumer<R> handler) {
throw new UnsupportedOperationException("Implemented in ScreenHandlerMixin");
}

/**
* Shorthand for {@link #addClientboundMessage(Class, Endec, Consumer)} which creates the endec
* through {@link ReflectiveEndecBuilder#get(Class)}
*/
default <R extends Record> void addClientboundMessage(Class<R> messageClass, Consumer<R> handler) {
throw new IllegalStateException("Implemented in ScreenHandlerMixin");
this.addClientboundMessage(messageClass, ReflectiveEndecBuilder.get(messageClass), handler);
}

/**
* Send the given message. This message must have been previously
* registered through a call to {@link #addServerboundMessage(Class, Consumer)}
* or {@link #addClientboundMessage(Class, Consumer)} - this also dictates where
* registered through a call to {@link #addServerboundMessage(Class, Endec, Consumer)}
* or {@link #addClientboundMessage(Class, Endec, Consumer)} - this also dictates where
* the message will be sent to
*/
default <R extends Record> void sendMessage(@NotNull R message) {
throw new IllegalStateException("Implemented in ScreenHandlerMixin");
throw new UnsupportedOperationException("Implemented in ScreenHandlerMixin");
}

/**
* @return The player this screen handler is attached to
*/
default PlayerEntity player() {
throw new IllegalStateException("Implemented in ScreenHandlerMixin");
throw new UnsupportedOperationException("Implemented in ScreenHandlerMixin");
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package io.wispforest.owo.client.screens;

import io.wispforest.owo.network.serialization.PacketBufSerializer;
import io.wispforest.owo.serialization.Endec;
import org.jetbrains.annotations.ApiStatus;

import java.util.function.Consumer;

@ApiStatus.Internal
public record ScreenhandlerMessageData<T>(int id, boolean clientbound, PacketBufSerializer<T> serializer, Consumer<T> handler) {}
public record ScreenhandlerMessageData<T>(int id, boolean clientbound, Endec<T> endec, Consumer<T> handler) {}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package io.wispforest.owo.client.screens;

import io.wispforest.owo.network.serialization.PacketBufSerializer;
import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.util.Observable;
import net.minecraft.network.PacketByteBuf;
import org.jetbrains.annotations.ApiStatus;

public class SyncedProperty<T> extends Observable<T> {
private final int index;
private final PacketBufSerializer<T> serializer;
private final Endec<T> endec;
private boolean needsSync;

@ApiStatus.Internal
public SyncedProperty(int index, Class<T> klass, T initial) {
public SyncedProperty(int index, Endec<T> endec, T initial) {
super(initial);

this.index = index;
this.serializer = PacketBufSerializer.get(klass);
this.endec = endec;
}

public int index() {
Expand All @@ -30,13 +30,12 @@ public boolean needsSync() {
@ApiStatus.Internal
public void write(PacketByteBuf buf) {
needsSync = false;

serializer.serializer().accept(buf, value);
buf.write(this.endec, value);
}

@ApiStatus.Internal
public void read(PacketByteBuf buf) {
set(serializer.deserializer().apply(buf));
this.set(buf.read(this.endec));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private static void applyServer(MinecraftServer server, ServerPlayerEntity playe

read(buf, (option, optionBuf) -> {
var config = CLIENT_OPTION_STORAGE.computeIfAbsent(connection, $ -> new HashMap<>()).computeIfAbsent(option.configName(), s -> new HashMap<>());
config.put(option.key(), option.serializer().deserializer().apply(optionBuf));
config.put(option.key(), optionBuf.read(option.endec()));
});
}

Expand Down
15 changes: 8 additions & 7 deletions src/main/java/io/wispforest/owo/config/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import io.wispforest.owo.Owo;
import io.wispforest.owo.config.annotation.RestartRequired;
import io.wispforest.owo.network.serialization.PacketBufSerializer;
import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.serialization.endec.ReflectiveEndecBuilder;
import io.wispforest.owo.util.Observable;
import net.minecraft.network.PacketByteBuf;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -36,7 +37,7 @@ public final class Option<T> {
private final Class<T> clazz;

private final ConfigWrapper.@Nullable Constraint constraint;
private final @Nullable PacketBufSerializer<T> serializer;
private final @Nullable Endec<T> endec;
private final SyncMode syncMode;

/**
Expand Down Expand Up @@ -79,7 +80,7 @@ public Option(String configName,

this.constraint = constraint;
this.syncMode = syncMode;
this.serializer = syncMode.isNone() ? null : (PacketBufSerializer<T>) PacketBufSerializer.getGeneric(this.backingField.field.getGenericType());
this.endec = syncMode.isNone() ? null : (Endec<T>) ReflectiveEndecBuilder.get(this.backingField.field.getGenericType());
}

/**
Expand Down Expand Up @@ -164,7 +165,7 @@ public void observe(Consumer<T> observer) {
* @param buf The packet buffer to write to
*/
void write(PacketByteBuf buf) {
this.serializer.serializer().accept(buf, this.value());
buf.write(this.endec, this.value());
}

/**
Expand All @@ -176,7 +177,7 @@ void write(PacketByteBuf buf) {
* the server's value otherwise
*/
T read(PacketByteBuf buf) {
final var newValue = this.serializer.deserializer().apply(buf);
final var newValue = buf.read(this.endec);

if (!Objects.equals(newValue, this.value()) && this.backingField.hasAnnotation(RestartRequired.class)) {
return newValue;
Expand All @@ -191,8 +192,8 @@ T read(PacketByteBuf buf) {
/**
* @return The serializer for this option's value
*/
PacketBufSerializer<T> serializer() {
return this.serializer;
Endec<T> endec() {
return this.endec;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ItemGroupButtonWidget(int x, int y, int baseU, OwoItemGroup.ButtonDefinit
}

@Override
public void renderButton(DrawContext context, int mouseX, int mouseY, float delta) {
public void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) {
RenderSystem.enableDepthTest();
context.drawTexture(this.definition.texture(), this.getX(), this.getY(), this.baseU, this.isSelected() || this.isSelected ? this.height : 0, this.width, this.height, 64, 64);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.wispforest.owo.mixin;

import io.wispforest.owo.util.pond.MatrixStackTransformer;
import io.wispforest.owo.ui.util.MatrixStackTransformer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.math.MatrixStack;
import org.spongepowered.asm.mixin.Mixin;
Expand Down
30 changes: 19 additions & 11 deletions src/main/java/io/wispforest/owo/mixin/ItemStackMixin.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package io.wispforest.owo.mixin;

import io.wispforest.owo.nbt.NbtCarrier;
import io.wispforest.owo.nbt.NbtKey;
import io.wispforest.owo.serialization.util.MapCarrier;
import io.wispforest.owo.serialization.endec.KeyedEndec;
import io.wispforest.owo.serialization.format.forwarding.ForwardingDeserializer;
import io.wispforest.owo.serialization.format.forwarding.ForwardingSerializer;
import io.wispforest.owo.serialization.format.nbt.NbtDeserializer;
import io.wispforest.owo.serialization.format.nbt.NbtSerializer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

@SuppressWarnings("AddedMixinMembersNamePattern")
@Mixin(ItemStack.class)
public abstract class ItemStackMixin implements NbtCarrier {
public abstract class ItemStackMixin implements MapCarrier {

@Shadow
private @Nullable NbtCompound nbt;
Expand All @@ -19,23 +24,26 @@ public abstract class ItemStackMixin implements NbtCarrier {
public abstract NbtCompound getOrCreateNbt();

@Override
public <T> T get(@NotNull NbtKey<T> key) {
return key.get(this.getOrCreateNbt());
public <T> T getWithErrors(@NotNull KeyedEndec<T> key) {
if (!this.has(key)) return key.defaultValue();
return key.endec()
.decodeFully(e -> ForwardingDeserializer.humanReadable(NbtDeserializer.of(e)), this.nbt.get(key.key()));
}

@Override
public <T> void put(@NotNull NbtKey<T> key, @NotNull T value) {
key.put(this.getOrCreateNbt(), value);
public <T> void put(@NotNull KeyedEndec<T> key, @NotNull T value) {
this.getOrCreateNbt()
.put(key.key(), key.endec().encodeFully(() -> ForwardingSerializer.humanReadable(NbtSerializer.of()), value));
}

@Override
public <T> void delete(@NotNull NbtKey<T> key) {
public <T> void delete(@NotNull KeyedEndec<T> key) {
if (this.nbt == null) return;
key.delete(this.nbt);
this.nbt.remove(key.key());
}

@Override
public <T> boolean has(@NotNull NbtKey<T> key) {
return this.nbt != null && key.isIn(this.nbt);
public <T> boolean has(@NotNull KeyedEndec<T> key) {
return this.nbt != null && this.nbt.contains(key.key());
}
}
Loading

0 comments on commit 89fcc07

Please sign in to comment.