From b2eed81677cbebe24a9961bd0ed434582f8a0a6a Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Sun, 26 Dec 2021 16:48:44 +0300 Subject: [PATCH] Added `Origins` support Closes #15 --- build.gradle | 23 +++- gradle.properties | 1 + .../sync/compat/origins/OriginsCompat.java | 15 +++ .../origins/OriginsShellStateComponent.java | 113 ++++++++++++++++++ src/main/resources/fabric.mod.json | 2 + 5 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 src/main/java/dev/kir/sync/compat/origins/OriginsCompat.java create mode 100644 src/main/java/dev/kir/sync/compat/origins/OriginsShellStateComponent.java diff --git a/build.gradle b/build.gradle index 480cbc8..e26c8c2 100644 --- a/build.gradle +++ b/build.gradle @@ -69,6 +69,25 @@ repositories { name = "CrowdinTranslate" url = "https://minecraft.guntram.de/maven/" } + maven { + name = "JitPack" + url = "https://jitpack.io" + content { + includeGroupByRegex "(io|com)\\.github\\..*" + } + } + maven { + url = "https://maven.jamieswhiteshirt.com/libs-release/" + content { + includeGroup "com.jamieswhiteshirt" + } + } + maven { + url = "https://maven.cafeteria.dev" + content { + includeGroup "net.adriantodt.fabricmc" + } + } } // To change the versions see the gradle.properties file @@ -89,6 +108,7 @@ dependencies { modImplementation "com.terraformersmc:modmenu:${project.modmenu_version}" modImplementation "dev.emi:trinkets:${project.trinkets_version}" modImplementation "io.github.ladysnake:requiem-api:${project.requiem_version}" + modImplementation "com.github.apace100:origins-fabric:${project.origins_version}" // Minecraft 1.17 is slow as heck modRuntime("supercoder79:databreaker:${project.databreaker_version}") { @@ -107,7 +127,8 @@ processResources { "team_reborn_energy": project.tr_energy_version, "modmenu": ">=${project.modmenu_version}", "trinkets": ">=${project.trinkets_version}", - "requiem": ">=${project.requiem_version}" + "requiem": ">=${project.requiem_version}", + "origins": ">=${project.origins_version}", ] filesMatching("fabric.mod.json") { diff --git a/gradle.properties b/gradle.properties index 6d0dfeb..f462fab 100644 --- a/gradle.properties +++ b/gradle.properties @@ -19,6 +19,7 @@ cloth_version=6.1.48 modmenu_version=2.0.14 trinkets_version=3.0.4 requiem_version=2.0.0-alpha.12 +origins_version=1.1.4 smartrecipes_version=0.1.1+1.17 databreaker_version=0.2.7 crowdin_translate_version=1.3+1.17 \ No newline at end of file diff --git a/src/main/java/dev/kir/sync/compat/origins/OriginsCompat.java b/src/main/java/dev/kir/sync/compat/origins/OriginsCompat.java new file mode 100644 index 0000000..306a16f --- /dev/null +++ b/src/main/java/dev/kir/sync/compat/origins/OriginsCompat.java @@ -0,0 +1,15 @@ +package dev.kir.sync.compat.origins; + +import dev.kir.sync.api.shell.ShellStateComponentFactoryRegistry; +import dev.onyxstudios.cca.api.v3.entity.EntityComponentFactoryRegistry; +import dev.onyxstudios.cca.api.v3.entity.EntityComponentInitializer; +import net.fabricmc.loader.api.FabricLoader; + +public class OriginsCompat implements EntityComponentInitializer { + @Override + public void registerEntityComponentFactories(EntityComponentFactoryRegistry registry) { + if (FabricLoader.getInstance().isModLoaded("origins")) { + ShellStateComponentFactoryRegistry.getInstance().register(OriginsShellStateComponent::new, OriginsShellStateComponent::new); + } + } +} diff --git a/src/main/java/dev/kir/sync/compat/origins/OriginsShellStateComponent.java b/src/main/java/dev/kir/sync/compat/origins/OriginsShellStateComponent.java new file mode 100644 index 0000000..4fe32f3 --- /dev/null +++ b/src/main/java/dev/kir/sync/compat/origins/OriginsShellStateComponent.java @@ -0,0 +1,113 @@ +package dev.kir.sync.compat.origins; + +import dev.kir.sync.api.shell.ShellStateComponent; +import io.github.apace100.apoli.component.PowerHolderComponent; +import io.github.apace100.origins.component.OriginComponent; +import io.github.apace100.origins.networking.ModPackets; +import io.github.apace100.origins.origin.Origin; +import io.github.apace100.origins.origin.OriginLayer; +import io.github.apace100.origins.origin.OriginLayers; +import io.github.apace100.origins.registry.ModComponents; +import io.netty.buffer.Unpooled; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtElement; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.network.ServerPlayerEntity; + +class OriginsShellStateComponent extends ShellStateComponent { + private final ServerPlayerEntity player; + private boolean activated; + private NbtCompound originComponentNbt; + private NbtCompound powerHolderComponentNbt; + + public OriginsShellStateComponent() { + this(null, false); + } + + public OriginsShellStateComponent(ServerPlayerEntity player) { + this(player, true); + } + + private OriginsShellStateComponent(ServerPlayerEntity player, boolean activated) { + this.player = player; + this.activated = activated; + } + + @Override + public String getId() { + return "origins"; + } + + public boolean isActivated() { + return this.activated; + } + + public NbtCompound getOriginComponentNbt() { + NbtCompound nbt = this.originComponentNbt; + if (this.player != null) { + nbt = new NbtCompound(); + ModComponents.ORIGIN.get(this.player).writeToNbt(nbt); + } + return nbt == null ? new NbtCompound() : nbt; + } + + public NbtCompound getPowerHolderComponentNbt() { + NbtCompound nbt = this.powerHolderComponentNbt; + if (this.player != null) { + nbt = new NbtCompound(); + PowerHolderComponent.KEY.get(this.player).writeToNbt(nbt); + } + return nbt == null ? new NbtCompound() : nbt; + } + + @Override + public void clone(ShellStateComponent component) { + OriginsShellStateComponent other = component.as(OriginsShellStateComponent.class); + if (other == null) { + return; + } + + this.originComponentNbt = other.getOriginComponentNbt(); + this.powerHolderComponentNbt = other.getPowerHolderComponentNbt(); + this.activated = other.isActivated(); + if (this.player == null) { + return; + } + + OriginComponent originComponent = ModComponents.ORIGIN.get(this.player); + if (this.activated) { + originComponent.readFromNbt(this.originComponentNbt); + PowerHolderComponent powerHolderComponent = PowerHolderComponent.KEY.get(this.player); + powerHolderComponent.readFromNbt(this.powerHolderComponentNbt); + originComponent.sync(); + } else { + for (OriginLayer layer : OriginLayers.getLayers()) { + if(layer.isEnabled()) { + originComponent.setOrigin(layer, Origin.EMPTY); + } + } + originComponent.checkAutoChoosingLayers(this.player, false); + originComponent.sync(); + PacketByteBuf data = new PacketByteBuf(Unpooled.buffer()); + data.writeBoolean(false); + ServerPlayNetworking.send(this.player, ModPackets.OPEN_ORIGIN_SCREEN, data); + this.activated = true; + } + } + + @Override + protected void readComponentNbt(NbtCompound nbt) { + this.originComponentNbt = nbt.contains("origins", NbtElement.COMPOUND_TYPE) ? nbt.getCompound("origins") : new NbtCompound(); + this.powerHolderComponentNbt = nbt.contains("powers", NbtElement.COMPOUND_TYPE) ? nbt.getCompound("powers") : new NbtCompound(); + this.activated = nbt.getBoolean("activated"); + } + + @Override + protected NbtCompound writeComponentNbt(NbtCompound nbt) { + nbt.put("origins", this.getOriginComponentNbt()); + nbt.put("powers", this.getPowerHolderComponentNbt()); + nbt.putBoolean("activated", this.isActivated()); + return nbt; + } +} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index b346854..d578045 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -27,6 +27,7 @@ "dev.kir.sync.compat.modmenu.ModMenuCompat" ], "cardinal-components-entity": [ + "dev.kir.sync.compat.origins.OriginsCompat", "dev.kir.sync.compat.trinkets.TrinketsCompat" ] }, @@ -55,6 +56,7 @@ "suggests": { "modmenu": "${modmenu}", "trinkets": "${trinkets}", + "origins": "${origins}", "requiem": "${requiem}" } }