Skip to content

Commit

Permalink
add polar bear shearing age component
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDeathlyCow committed Sep 30, 2023
1 parent 90c8329 commit b0790a9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public int getRootedTicks() {
public void setRootedTicks(int rootedTicks) {
if (this.rootedTicks != rootedTicks) {
this.rootedTicks = rootedTicks;
FComponents.ENTITY_COMPONENTS.sync(this);
FComponents.ENTITY_COMPONENTS.sync(this.provider);
}
}

Expand All @@ -42,7 +42,7 @@ public byte getSkateFlags() {
public void setSkateFlags(byte skateFlags) {
if (this.skateFlags != skateFlags) {
this.skateFlags = skateFlags;
FComponents.ENTITY_COMPONENTS.sync(this);
FComponents.ENTITY_COMPONENTS.sync(this.provider);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,61 @@
package com.github.thedeathlycow.frostiful.entity.component;

import com.github.thedeathlycow.frostiful.entity.FShearable;
import com.github.thedeathlycow.frostiful.registry.FComponents;
import dev.onyxstudios.cca.api.v3.component.Component;
import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent;
import net.minecraft.entity.passive.PolarBearEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.server.network.ServerPlayerEntity;

public class PolarBearComponents implements Component, AutoSyncedComponent {

private int lastShearedAge = 0;
private static final String LAST_SHEARED_AGE_KEY = "last_sheared_age";

private int lastShearedAge = -1;

private final PolarBearEntity provider;
private final FShearable shearable;

public PolarBearComponents(PolarBearEntity provider) {
this.provider = provider;
this.shearable = (FShearable) provider;
}

@Override
public void readFromNbt(NbtCompound tag) {
public void writeSyncPacket(PacketByteBuf buf, ServerPlayerEntity recipient) {
buf.writeVarInt(this.lastShearedAge);
}

@Override
public void applySyncPacket(PacketByteBuf buf) {
this.lastShearedAge = buf.readVarInt();
}

@Override
public void readFromNbt(NbtCompound tag) {
if (tag.contains(LAST_SHEARED_AGE_KEY, NbtElement.INT_TYPE)) {
this.lastShearedAge = tag.getInt(LAST_SHEARED_AGE_KEY);
}
}

@Override
public void writeToNbt(NbtCompound tag) {
if (shearable.frostiful$wasSheared()) {
tag.putInt(LAST_SHEARED_AGE_KEY, lastShearedAge);
}
}

public int getLastShearedAge() {
return lastShearedAge;
}

public void setLastShearedAge(int lastShearedAge) {
if (this.lastShearedAge != lastShearedAge) {
this.lastShearedAge = lastShearedAge;
FComponents.POLAR_BEAR_COMPONENTS.sync(this.provider);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.thedeathlycow.frostiful.Frostiful;
import com.github.thedeathlycow.frostiful.entity.FShearable;
import com.github.thedeathlycow.frostiful.registry.FComponents;
import com.github.thedeathlycow.frostiful.util.FLootHelper;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.damage.DamageSources;
Expand Down Expand Up @@ -29,60 +30,8 @@ protected PolarBearShearingMixin(EntityType<? extends AnimalEntity> entityType,
super(entityType, world);
}

private static final String FROSTIFUL$KEY = "Frostiful";
private static final String FROSTIFUL$LAST_SHEARED_AGE_KEY = "LastShearedAge";

private static final int frostiful$SHEAR_COOLDOWN = 20 * 300;


private static final TrackedData<Integer> LAST_SHEARED_AGE = DataTracker.registerData(PolarBearShearingMixin.class, TrackedDataHandlerRegistry.INTEGER);


@Inject(
method = "initDataTracker",
at = @At("TAIL")
)
private void trackFrostData(CallbackInfo ci) {
this.dataTracker.startTracking(LAST_SHEARED_AGE, -1);
}


@Inject(
method = "writeCustomDataToNbt",
at = @At("TAIL")
)
private void saveShearingData(NbtCompound nbt, CallbackInfo ci) {
NbtCompound frostiful = new NbtCompound();

if (this.frostiful$wasSheared()) {
frostiful.putInt(FROSTIFUL$LAST_SHEARED_AGE_KEY, this.dataTracker.get(LAST_SHEARED_AGE));
}

if (!frostiful.isEmpty()) {
nbt.put(FROSTIFUL$KEY, frostiful);
}
}

@Inject(
method = "readCustomDataFromNbt",
at = @At("TAIL")
)
private void readShearingData(NbtCompound nbt, CallbackInfo ci) {

int lastShearedAge = -1;

if (nbt.contains(FROSTIFUL$KEY, NbtElement.COMPOUND_TYPE)) {
NbtCompound frostiful = nbt.getCompound(FROSTIFUL$KEY);

if (frostiful.contains(FROSTIFUL$LAST_SHEARED_AGE_KEY, NbtElement.INT_TYPE)) {
lastShearedAge = frostiful.getInt(FROSTIFUL$LAST_SHEARED_AGE_KEY);
}
}

this.dataTracker.set(LAST_SHEARED_AGE, lastShearedAge);

}

@Override
@Unique
public void frostiful$shear(PlayerEntity player, SoundCategory shearedSoundCategory) {
Expand All @@ -95,7 +44,7 @@ private void readShearingData(NbtCompound nbt, CallbackInfo ci) {
FLootHelper.dropLootFromEntity(this, FShearable.POLAR_BEAR_SHEARING_LOOT_TABLE);
}

this.dataTracker.set(LAST_SHEARED_AGE, this.age);
FComponents.POLAR_BEAR_COMPONENTS.get(this).setLastShearedAge(this.age);
}

@Override
Expand All @@ -109,7 +58,7 @@ private void readShearingData(NbtCompound nbt, CallbackInfo ci) {
@Override
@Unique
public boolean frostiful$wasSheared() {
int lastShearedAge = this.dataTracker.get(LAST_SHEARED_AGE);
int lastShearedAge = FComponents.POLAR_BEAR_COMPONENTS.get(this).getLastShearedAge();

return lastShearedAge >= 0
&& this.age - lastShearedAge <= frostiful$SHEAR_COOLDOWN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import com.github.thedeathlycow.frostiful.Frostiful;
import com.github.thedeathlycow.frostiful.entity.component.LivingEntityComponents;
import com.github.thedeathlycow.frostiful.entity.component.PolarBearComponents;
import dev.onyxstudios.cca.api.v3.component.ComponentKey;
import dev.onyxstudios.cca.api.v3.component.ComponentRegistry;
import dev.onyxstudios.cca.api.v3.entity.EntityComponentFactoryRegistry;
import dev.onyxstudios.cca.api.v3.entity.EntityComponentInitializer;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.data.TrackedData;
import net.minecraft.entity.passive.PolarBearEntity;

public class FComponents implements EntityComponentInitializer {

Expand All @@ -16,6 +18,11 @@ public class FComponents implements EntityComponentInitializer {
LivingEntityComponents.class
);

public static final ComponentKey<PolarBearComponents> POLAR_BEAR_COMPONENTS = ComponentRegistry.getOrCreate(
Frostiful.id("polar_bear"),
PolarBearComponents.class
);

TrackedData f;

@Override
Expand All @@ -25,5 +32,10 @@ public void registerEntityComponentFactories(EntityComponentFactoryRegistry regi
ENTITY_COMPONENTS,
LivingEntityComponents::new
);
registry.registerFor(
PolarBearEntity.class,
POLAR_BEAR_COMPONENTS,
PolarBearComponents::new
);
}
}
11 changes: 10 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
],
"mm:early_risers": [
"com.github.thedeathlycow.frostiful.enchantment.target.FEnchantmentTargetEarlyRiser"
],
"cardinal-components-entity": [
"com.github.thedeathlycow.frostiful.registry.FComponents"
]
},
"mixins": [
Expand All @@ -53,5 +56,11 @@
"suggests": {
"another-mod": "*"
},
"accessWidener": "frostiful.accesswidener"
"accessWidener": "frostiful.accesswidener",
"custom": {
"cardinal-components": [
"frostiful:living_entity",
"frostiful:polar_bear"
]
}
}

0 comments on commit b0790a9

Please sign in to comment.