Skip to content

Commit

Permalink
Added Origins support
Browse files Browse the repository at this point in the history
Closes #15
  • Loading branch information
Kir-Antipov committed Dec 26, 2021
1 parent 23195e1 commit b2eed81
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 1 deletion.
23 changes: 22 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}") {
Expand All @@ -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") {
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions src/main/java/dev/kir/sync/compat/origins/OriginsCompat.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
Expand Down Expand Up @@ -55,6 +56,7 @@
"suggests": {
"modmenu": "${modmenu}",
"trinkets": "${trinkets}",
"origins": "${origins}",
"requiem": "${requiem}"
}
}

0 comments on commit b2eed81

Please sign in to comment.