-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
153 additions
and
1 deletion.
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
15 changes: 15 additions & 0 deletions
15
src/main/java/dev/kir/sync/compat/origins/OriginsCompat.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,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); | ||
} | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
src/main/java/dev/kir/sync/compat/origins/OriginsShellStateComponent.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,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; | ||
} | ||
} |
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