Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Commit

Permalink
Change how talents are added and handled (via talent instances)
Browse files Browse the repository at this point in the history
  • Loading branch information
percivalalb committed Oct 31, 2020
1 parent b4ad24b commit fa66fe4
Show file tree
Hide file tree
Showing 38 changed files with 672 additions and 418 deletions.
Expand Up @@ -3,13 +3,14 @@
import com.mojang.blaze3d.matrix.MatrixStack;

import doggytalents.api.inferface.AbstractDogEntity;
import doggytalents.api.registry.TalentInstance;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.entity.layers.LayerRenderer;
import net.minecraft.client.renderer.entity.model.EntityModel;

public interface ITalentRenderer <T extends AbstractDogEntity> {

default void render(LayerRenderer<T, EntityModel<T>> layer, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int packedLightIn, T dog, int level, float limbSwing, float limbSwingAmount, float partialTicks, float ageInTicks, float netHeadYaw, float headPitch) {
default void render(LayerRenderer<T, EntityModel<T>> layer, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int packedLightIn, T dog, TalentInstance inst, float limbSwing, float limbSwingAmount, float partialTicks, float ageInTicks, float netHeadYaw, float headPitch) {

}
}
13 changes: 13 additions & 0 deletions src/api/java/doggytalents/api/feature/IDog.java
@@ -1,12 +1,14 @@
package doggytalents.api.feature;

import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

import doggytalents.api.inferface.AbstractDogEntity;
import doggytalents.api.inferface.IDogFoodHandler;
import doggytalents.api.registry.AccessoryInstance;
import doggytalents.api.registry.Talent;
import doggytalents.api.registry.TalentInstance;
import net.minecraft.entity.LivingEntity;

// TODO: Add javadoc
Expand Down Expand Up @@ -39,6 +41,12 @@ default int getLevel(Supplier<? extends Talent> talentGetter) {
*/
public int getLevel(Talent talentIn);

default Optional<TalentInstance> getTalent(Supplier<? extends Talent> talentGetter) {
return this.getTalent(talentGetter.get());
}

public Optional<TalentInstance> getTalent(Talent talentIn);

public int getDogSize();
public void setDogSize(int size);

Expand All @@ -65,9 +73,14 @@ default int getLevel(Supplier<? extends Talent> talentGetter) {
/**
* Tries to put the object in the map, does nothing if the key already exists
*/
@Deprecated
public <T> void setDataIfEmpty(DataKey<T> key, T value);
@Deprecated
public <T> T getData(DataKey<T> key);
@Deprecated
public <T> T getDataOrGet(DataKey<T> key, Supplier<T> other);
@Deprecated
public <T> T getDataOrDefault(DataKey<T> key, T other);
@Deprecated
public <T> boolean hasData(DataKey<T> key);
}
4 changes: 2 additions & 2 deletions src/api/java/doggytalents/api/inferface/IDogAlteration.java
Expand Up @@ -20,11 +20,11 @@

public interface IDogAlteration {

default void write(AbstractDogEntity dogIn, CompoundNBT compound) {
default void onWrite(AbstractDogEntity dogIn, CompoundNBT compound) {

}

default void read(AbstractDogEntity dogIn, CompoundNBT compound) {
default void onRead(AbstractDogEntity dogIn, CompoundNBT compound) {

}

Expand Down
12 changes: 6 additions & 6 deletions src/api/java/doggytalents/api/registry/Accessory.java
Expand Up @@ -42,12 +42,16 @@ public byte getRenderLayer() {
return AccessoryInstance.RENDER_DEFAULT;
}

public AccessoryInstance getDefault() {
return new AccessoryInstance(this);
}

public AccessoryInstance createInstance(PacketBuffer buf) {
return this.getDefault();
}

public AccessoryInstance getDefault() {
return new AccessoryInstance(this);
public AccessoryInstance read(CompoundNBT compound) {
return this.getDefault();
}

public void write(AccessoryInstance instance, PacketBuffer buf) {
Expand All @@ -58,10 +62,6 @@ public void write(AccessoryInstance instance, CompoundNBT compound) {

}

public AccessoryInstance read(CompoundNBT compound) {
return this.getDefault();
}

public ItemStack getReturnItem(AccessoryInstance instance) {
return this.stack.get();
}
Expand Down
34 changes: 20 additions & 14 deletions src/api/java/doggytalents/api/registry/Talent.java
@@ -1,34 +1,29 @@
package doggytalents.api.registry;

import java.util.function.BiFunction;

import javax.annotation.Nullable;

import doggytalents.api.DoggyTalentsAPI;
import doggytalents.api.inferface.AbstractDogEntity;
import doggytalents.api.inferface.IDogAlteration;
import net.minecraft.util.Util;
import net.minecraftforge.registries.ForgeRegistryEntry;

/**
* @author ProPercivalalb
*/
public abstract class Talent extends ForgeRegistryEntry<Talent> implements IDogAlteration {
public class Talent extends ForgeRegistryEntry<Talent> {

@Nullable
private String translationKey, translationInfoKey;

@Nullable
private final BiFunction<Talent, Integer, TalentInstance> create;

/**
* Called when the talent is added to the dog and anytime the talent map is updated
* @param sup
*/
public void init(AbstractDogEntity dog) {

}

public void set(AbstractDogEntity dog, int level) {

}

public void removed(AbstractDogEntity dog, int preLevel) {

public Talent(BiFunction<Talent, Integer, TalentInstance> sup) {
this.create = sup;
}

public int getMaxLevel() {
Expand Down Expand Up @@ -57,6 +52,17 @@ public String getInfoTranslationKey() {
return this.translationInfoKey;
}

public TalentInstance getDefault(int level) {
if (this.create == null) {
return new TalentInstance(this, level);
}
return this.create.apply(this, level);
}

public TalentInstance getDefault() {
return this.getDefault(1);
}


public boolean hasRenderer() {
return false;
Expand Down
136 changes: 136 additions & 0 deletions src/api/java/doggytalents/api/registry/TalentInstance.java
@@ -0,0 +1,136 @@
package doggytalents.api.registry;

import java.util.Optional;
import java.util.function.Supplier;

import doggytalents.api.DoggyTalentsAPI;
import doggytalents.api.inferface.AbstractDogEntity;
import doggytalents.api.inferface.IDogAlteration;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.IRegistryDelegate;

public class TalentInstance implements IDogAlteration {

protected final IRegistryDelegate<Talent> talentDelegate;

protected int level;

public TalentInstance(Talent talentIn, int levelIn) {
this(talentIn.delegate, levelIn);
}

public TalentInstance(Talent talentIn) {
this(talentIn.delegate, 1);
}

public TalentInstance(IRegistryDelegate<Talent> talentDelegateIn, int levelIn) {
this.talentDelegate = talentDelegateIn;
this.level = levelIn;
}

public Talent getTalent() {
return this.talentDelegate.get();
}

public final int level() {
return this.level;
}

public final void setLevel(int levelIn) {
this.level = levelIn;
}

public boolean of(Supplier<Talent> talentIn) {
return this.of(talentIn.get());
}

public boolean of(Talent talentIn) {
return this.of(talentIn.delegate);
}

public boolean of(IRegistryDelegate<Talent> talentDelegateIn) {
return talentDelegateIn.equals(this.talentDelegate);
}

public TalentInstance copy() {
return this.talentDelegate.get().getDefault(this.level);
}

public void writeToNBT(AbstractDogEntity dogIn, CompoundNBT compound) {
compound.putInt("level", this.level());
}

public void readFromNBT(AbstractDogEntity dogIn, CompoundNBT compound) {
this.setLevel(compound.getInt("level"));
}

public void writeToBuf(PacketBuffer buf) {
buf.writeInt(this.level());
}

public void readFromBuf(PacketBuffer buf) {
this.setLevel(buf.readInt());
}

public final void writeInstance(AbstractDogEntity dogIn, CompoundNBT compound) {
ResourceLocation rl = this.talentDelegate.name();
if (rl != null) {
compound.putString("type", rl.toString());
}

this.writeToNBT(dogIn, compound);
}

public static Optional<TalentInstance> readInstance(AbstractDogEntity dogIn, CompoundNBT compound) {
ResourceLocation rl = ResourceLocation.tryCreate(compound.getString("type"));
if (DoggyTalentsAPI.TALENTS.containsKey(rl)) {
TalentInstance inst = DoggyTalentsAPI.TALENTS.getValue(rl).getDefault();
inst.readFromNBT(dogIn, compound);
return Optional.of(inst);
} else {
DoggyTalentsAPI.LOGGER.warn("Failed to load talent {}", rl);
return Optional.empty();
}
}

@SuppressWarnings("unchecked")
public <T extends TalentInstance> T cast(Class<T> type) {
if (this.getClass().isAssignableFrom(type)) {
return (T) this;
} else {
throw new RuntimeException("Could not cast " + this.getClass().getName() + " to " + type.getName());
}
}

@Override
public String toString() {
return String.format("%s [talent: %s, level: %d]", this.getClass().getSimpleName(), talentDelegate.name(), this.level);
}

/**
* Called when ever this instance is first added to a dog, this is called when
* the level is first set on the dog or when it is loaded from NBT and when the
* talents are synced to the client
*
* @param dogIn The dog
*/
public void init(AbstractDogEntity dogIn) {

}

/**
* Called when the level of the dog changes
* Is not called when the dog is loaded from NBT
*
* @param dogIn The dog
*/
public void set(AbstractDogEntity dog, int levelBefore) {

}

public boolean hasRenderer() {
return this.getTalent().hasRenderer();
}
}
2 changes: 1 addition & 1 deletion src/main/java/doggytalents/DoggySerializers.java
Expand Up @@ -20,7 +20,7 @@ public class DoggySerializers {

public static final DeferredRegister<DataSerializerEntry> SERIALIZERS = DeferredRegister.create(ForgeRegistries.DATA_SERIALIZERS, Constants.MOD_ID);

public static final RegistryObject<DataSerializerEntry> TALENT_LEVEL_SERIALIZER = register2("talent_level_list", TalentListSerializer::new);
public static final RegistryObject<DataSerializerEntry> TALENT_SERIALIZER = register2("talents", TalentListSerializer::new);
public static final RegistryObject<DataSerializerEntry> COLLAR_TYPE_SERIALIZER = register2("collar", CollarSerializer::new);
public static final RegistryObject<DataSerializerEntry> ACCESSORY_SERIALIZER = register2("accessories", AccessorySerializer::new);
public static final RegistryObject<DataSerializerEntry> GENDER_SERIALIZER = register2("gender", GenderSerializer::new);
Expand Down
51 changes: 28 additions & 23 deletions src/main/java/doggytalents/DoggyTalents.java
@@ -1,8 +1,10 @@
package doggytalents;

import java.util.function.BiFunction;
import java.util.function.Supplier;

import doggytalents.api.registry.Talent;
import doggytalents.api.registry.TalentInstance;
import doggytalents.common.lib.Constants;
import doggytalents.common.talent.BedFinderTalent;
import doggytalents.common.talent.BlackPeltTalent;
Expand All @@ -13,7 +15,6 @@
import doggytalents.common.talent.GuardDogTalent;
import doggytalents.common.talent.HappyEaterTalent;
import doggytalents.common.talent.HellHoundTalent;
import doggytalents.common.talent.HunterDogTalent;
import doggytalents.common.talent.PackPuppyTalent;
import doggytalents.common.talent.PestFighterTalent;
import doggytalents.common.talent.PillowPawTalent;
Expand All @@ -32,28 +33,32 @@ public class DoggyTalents {

public static final DeferredRegister<Talent> TALENTS = DeferredRegister.create(Talent.class, Constants.MOD_ID);

public static final RegistryObject<BedFinderTalent> BED_FINDER = register("bed_finder", BedFinderTalent::new);
public static final RegistryObject<BlackPeltTalent> BLACK_PELT = register("black_pelt", BlackPeltTalent::new);
public static final RegistryObject<CreeperSweeperTalent> CREEPER_SWEEPER = register("creeper_sweeper", CreeperSweeperTalent::new);
public static final RegistryObject<DoggyDashTalent> DOGGY_DASH = register("doggy_dash", DoggyDashTalent::new);
public static final RegistryObject<FisherDogTalent> FISHER_DOG = register("fisher_dog", FisherDogTalent::new);
public static final RegistryObject<GuardDogTalent> GUARD_DOG = register("guard_dog", GuardDogTalent::new);
public static final RegistryObject<HappyEaterTalent> HAPPY_EATER = register("happy_eater", HappyEaterTalent::new);
public static final RegistryObject<HellHoundTalent> HELL_HOUND = register("hell_hound", HellHoundTalent::new);
public static final RegistryObject<HunterDogTalent> HUNTER_DOG = register("hunter_dog", HunterDogTalent::new);
public static final RegistryObject<PackPuppyTalent> PACK_PUPPY = register("pack_puppy", PackPuppyTalent::new);
public static final RegistryObject<PestFighterTalent> PEST_FIGHTER = register("pest_fighter", PestFighterTalent::new);
public static final RegistryObject<PillowPawTalent> PILLOW_PAW = register("pillow_paw", PillowPawTalent::new);
public static final RegistryObject<PoisonFangTalent> POISON_FANG = register("poison_fang", PoisonFangTalent::new);
public static final RegistryObject<PuppyEyesTalent> PUPPY_EYES = register("puppy_eyes", PuppyEyesTalent::new);
public static final RegistryObject<QuickHealerTalent> QUICK_HEALER = register("quick_healer", QuickHealerTalent::new);
//public static final RegistryObject<RangedAttacker> RANGED_ATTACKER = register("ranged_attacker", RangedAttacker::new);
public static final RegistryObject<RescueDogTalent> RESCUE_DOG = register("rescue_dog", RescueDogTalent::new);
public static final RegistryObject<RoaringGaleTalent> ROARING_GALE = register("roaring_gale", RoaringGaleTalent::new);
public static final RegistryObject<ShepherdDogTalent> SHEPHERD_DOG = register("shepherd_dog", ShepherdDogTalent::new);
public static final RegistryObject<SwimmerDogTalent> SWIMMER_DOG = register("swimmer_dog", SwimmerDogTalent::new);
public static final RegistryObject<WolfMountTalent> WOLF_MOUNT = register("wolf_mount", WolfMountTalent::new);
public static final RegistryObject<DoggyTorchTalent> DOGGY_TORCH = register("doggy_torch", DoggyTorchTalent::new);
public static final RegistryObject<Talent> BED_FINDER = registerInst("bed_finder", BedFinderTalent::new);
public static final RegistryObject<Talent> BLACK_PELT = registerInst("black_pelt", BlackPeltTalent::new);
public static final RegistryObject<Talent> CREEPER_SWEEPER = registerInst("creeper_sweeper", CreeperSweeperTalent::new);
public static final RegistryObject<Talent> DOGGY_DASH = registerInst("doggy_dash", DoggyDashTalent::new);
public static final RegistryObject<Talent> FISHER_DOG = registerInst("fisher_dog", FisherDogTalent::new);
public static final RegistryObject<Talent> GUARD_DOG = registerInst("guard_dog", GuardDogTalent::new);
public static final RegistryObject<Talent> HAPPY_EATER = registerInst("happy_eater", HappyEaterTalent::new);
public static final RegistryObject<Talent> HELL_HOUND = registerInst("hell_hound", HellHoundTalent::new);
public static final RegistryObject<Talent> HUNTER_DOG = registerInst("hunter_dog", null);
public static final RegistryObject<Talent> PACK_PUPPY = registerInst("pack_puppy", PackPuppyTalent::new);
public static final RegistryObject<Talent> PEST_FIGHTER = registerInst("pest_fighter", PestFighterTalent::new);
public static final RegistryObject<Talent> PILLOW_PAW = registerInst("pillow_paw", PillowPawTalent::new);
public static final RegistryObject<Talent> POISON_FANG = registerInst("poison_fang", PoisonFangTalent::new);
public static final RegistryObject<Talent> PUPPY_EYES = registerInst("puppy_eyes", PuppyEyesTalent::new);
public static final RegistryObject<Talent> QUICK_HEALER = registerInst("quick_healer", QuickHealerTalent::new);
//public static final RegistryObject<Talent> RANGED_ATTACKER = registerInst("ranged_attacker", RangedAttacker::new);
public static final RegistryObject<Talent> RESCUE_DOG = registerInst("rescue_dog", RescueDogTalent::new);
public static final RegistryObject<Talent> ROARING_GALE = registerInst("roaring_gale", RoaringGaleTalent::new);
public static final RegistryObject<Talent> SHEPHERD_DOG = registerInst("shepherd_dog", ShepherdDogTalent::new);
public static final RegistryObject<Talent> SWIMMER_DOG = registerInst("swimmer_dog", SwimmerDogTalent::new);
public static final RegistryObject<Talent> WOLF_MOUNT = registerInst("wolf_mount", WolfMountTalent::new);
public static final RegistryObject<Talent> DOGGY_TORCH = registerInst("doggy_torch", DoggyTorchTalent::new);

private static <T extends Talent> RegistryObject<Talent> registerInst(final String name, final BiFunction<Talent, Integer, TalentInstance> sup) {
return register(name, () -> new Talent(sup));
}

private static <T extends Talent> RegistryObject<T> register(final String name, final Supplier<T> sup) {
return TALENTS.register(name, sup);
Expand Down

0 comments on commit fa66fe4

Please sign in to comment.