Jump to content

Recommended Posts

Posted

In 1.12.2 I was able to use messages between the client and server (https://github.com/ImNotJahan/danmachi-mod/tree/master/v1.12.2/src/main/java/imnotjahan/mod/danmachi/network) I can't figure out how to get any of that to work for 1.16.5 though. This is all my capability code

IStatus

Spoiler

package imnotjahan.mod.danmachi.capabilities;

import java.util.Set;

public interface IStatus
{
    void giveFalna();
    boolean getFalna();

    String getFamilia();

    void setFamilia(String familiaName);

    /**
     * @param points how much the stat should increase by,
     *               is cut in half n - 1 times, n being level.
     *               doesn't cut in half if your adding excelia
     * @param id the id of the stat your increasing
     */
    void increase(int points, int id);

    /**
     * @param id the id of the stat your changing
     * @param amount the amount your setting the stat to
     */
    void set(int id, int amount);

    /**
     * @param id the id of the stat your changing
     * @param amount the amount your setting the stat to
     */
    void setP(int id, int amount);

    /**
     * @param id the id of the stat your grabbing
     */
    int get(int id);

    /**
     * @param id the id of the stat your grabbing
     */
    int getP(int id);

    int updateStatus();

    int getLevel();

    void grantAbility(Status.Ability ability);
    boolean grantSkill();
    boolean grantMagic();

    void setAbilities(Status.Ability[] abilities);
    void setSkills(Status.Skill[] skills);
    void setSpells(Status.Magic[] spells);

    Set<Status.Ability> levelUp();

    boolean canLevelUp();
    void setCanLevelUp(boolean canLevelUp);

    Status.Ability[] getAbilities();
    Status.Skill[] getSkills();
    Status.Magic[] getSpells();
}

 

StatusStorage

Spoiler

package imnotjahan.mod.danmachi.capabilities;

import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;

public class StatusStorage implements Capability.IStorage<IStatus>
{
    @Override
    public INBT writeNBT(Capability<IStatus> capability, IStatus instance, Direction side)
    {
        if(Status.capSide == side)
        {
            CompoundNBT status = new CompoundNBT();
            status.putInt("hasFalna", instance.get(0));
            status.putInt("strength", instance.get(1));
            status.putInt("endurance", instance.get(2));
            status.putInt("dexterity", instance.get(3));
            status.putInt("agility", instance.get(4));
            status.putInt("magic", instance.get(5));
            status.putInt("level", instance.get(6));
            status.putInt("excelia", instance.get(7));

            status.putInt("strengthP", instance.getP(0));
            status.putInt("enduranceP", instance.getP(1));
            status.putInt("dexterityP", instance.getP(2));
            status.putInt("agilityP", instance.getP(3));
            status.putInt("magicP", instance.getP(4));
            status.putInt("exceliaP", instance.getP(5));

            status.putString("familia", instance.getFamilia());

            Status.Skill[] instanceSkills = instance.getSkills();
            int[] skills = new int[instanceSkills.length];

            for(int k = 0; k < skills.length; k++)
            {
                skills[k] = instanceSkills[k].toInt();
            }

            status.putIntArray("skills", skills);

            Status.Ability[] instanceAbilities = instance.getAbilities();
            int[] abilities = new int[instanceAbilities.length];
            int[] abilityStats = new int[instanceAbilities.length];

            for(int k = 0; k < abilities.length; k++)
            {
                abilities[k] = instanceAbilities[k].toInt();
                abilityStats[k] = instanceAbilities[k].getStat();
            }

            status.putIntArray("abilities", abilities);
            status.putIntArray("abilityStats", abilityStats);

            return status;
        }

        return new CompoundNBT();
    }

    @Override
    public void readNBT(Capability<IStatus> capability, IStatus instance, Direction side, INBT nbt)
    {
        if(Status.capSide == side)
        {
            if(nbt instanceof CompoundNBT)
            {
                CompoundNBT tag = (CompoundNBT)nbt;

                instance.set(0, tag.getInt("hasFalna"));
                instance.set(1, tag.getInt("strength"));
                instance.set(2, tag.getInt("endurance"));
                instance.set(3, tag.getInt("dexterity"));
                instance.set(4, tag.getInt("agility"));
                instance.set(5, tag.getInt("magic"));
                instance.set(6, tag.getInt("level"));
                instance.set(7, tag.getInt("excelia"));

                instance.setP(0, tag.getInt("strengthP"));
                instance.setP(1, tag.getInt("enduranceP"));
                instance.setP(2, tag.getInt("dexterityP"));
                instance.setP(3, tag.getInt("agilityP"));
                instance.setP(4, tag.getInt("magicP"));
                instance.setP(5, tag.getInt("exceliaP"));

                instance.setFamilia(tag.getString("familia"));

                int[] oldSkills = tag.getIntArray("skills");
                Status.Skill[] skills = new Status.Skill[oldSkills.length];

                for(int k = 0; k < oldSkills.length; k++)
                {
                    skills[k] = Status.Skill.values()[oldSkills[k]];
                }

                instance.setSkills(skills);

                int[] oldAbilities = tag.getIntArray("abilities");
                int[] oldAbilityStats = tag.getIntArray("abilityStats");
                Status.Ability[] abilities = new Status.Ability[oldAbilities.length];

                for(int k = 0; k < oldAbilities.length; k++)
                {
                    abilities[k] = Status.Ability.values()[oldAbilities[k]];
                    abilities[k].setStat(oldAbilityStats[k]);
                }

                instance.setAbilities(abilities);
            }
        }
    }
}

 

Status

Spoiler

package imnotjahan.mod.danmachi.capabilities;
import net.minecraft.util.Direction;

import java.util.*;

public class Status implements IStatus
{
    public static final Direction capSide = Direction.UP;

    public enum Ability
    {
        AbnormalResistance(0),
        Blacksmith(1),
        Mage(2),
        Mystery(3),
        Hunter(4),
        SpiritHealing(5),
        Luck(6);

        private int stat;
        private final int id;
        Ability(int id)
        {
            this.stat = 0;
            this.id = id;
        }

        public int getStat()
        {
            return stat;
        }

        public void increaseStat(int amount)
        {
            stat += amount;
        }

        public void setStat(int amount)
        {
            stat = amount;
        }

        public int toInt()
        {
            return id;
        }

        @Override
        public String toString()
        {
            String name = super.toString();

            name = name.replaceAll("\\d+", "").replaceAll("(.)([A-Z])", "$1_$2").toLowerCase();

            return name;
        }

        private static final List<Ability> VALUES = Collections.unmodifiableList(Arrays.asList(values()));
        private static final Random RANDOM = new Random();
        private static final int SIZE = VALUES.size();

        public static Ability randomAbility()
        {
            return VALUES.get(RANDOM.nextInt(SIZE));
        }
    }

    public enum Skill
    {
        AeroMana(0, 10),
        AilMacMidna(1, 10),
        Avenger(2, 5),
        MindLoad(3, 6);

        private final int index;
        private final int rarity;
        Skill(int index, int rarity)
        {
            this.index = index;
            this.rarity = rarity;
        }

        public int toInt()
        {
            return index;
        }

        private static final List<Skill> VALUES = Collections.unmodifiableList(Arrays.asList(values()));
        private static final int SIZE = VALUES.size();
        private static final Random RANDOM = new Random();

        public static Skill randomSkill()
        {
            Skill skill = Skill.AeroMana;
            Boolean gotOne = false;
            while(!gotOne)
            {
                skill = VALUES.get(RANDOM.nextInt(SIZE));
                if(Math.random() * 10 < skill.rarity)
                {
                    gotOne = true;
                }
            }

            return skill;
        }

        @Override
        public String toString()
        {
            String name = super.toString();

            name = name.replaceAll("\\d+", "").replaceAll("(.)([A-Z])", "$1_$2").toLowerCase();

            return name;
        }
    }

    public enum Magic
    {
        Firebolt(0);

        private final int index;
        Magic(int index)
        {
            this.index = index;
        }

        public int toInt()
        {
            return index;
        }

        private static final List<Magic> VALUES = Collections.unmodifiableList(Arrays.asList(values()));
        private static final int SIZE = VALUES.size();
        private static final Random RANDOM = new Random();

        public static Magic randomSkill()
        {
            return VALUES.get(RANDOM.nextInt(SIZE));
        }

        @Override
        public String toString()
        {
            String name = super.toString();

            name = name.replaceAll("\\d+", "").replaceAll("(.)([A-Z])", "$1_$2").toLowerCase();

            return name;
        }
    }

    private int hasFalna = 0;
    private int strength = 0;
    private int endurance = 0;
    private int dexterity = 0;
    private int agility = 0;
    private int magic = 0;
    private int level = 1;
    private int excelia = 0;
    private int mind = 100;

    private String familia = "";
    private List<Skill> skills = new ArrayList<>();
    private List<Ability> abilities = new ArrayList<>();
    private List<Magic> spells = new ArrayList<>();

    private int strengthP = 0;
    private int enduranceP = 0;
    private int dexterityP = 0;
    private int agilityP = 0;
    private int magicP = 0;
    private int exceliaP = 0;

    public boolean canLevelUp = false;

    @Override
    public void increase(int points, int id)
    {
        if(id != 7)
        {
            for(int k = 0; k < level - 1; k++)
            {
                points /= 2;
            }
        }

        points *= 1; //ModConfig.statMultiplier

        switch(id)
        {
            case 0:
                this.hasFalna += points;
                break;

            case 1:
                this.strengthP += points;
                break;

            case 2:
                this.enduranceP += points;
                break;

            case 3:
                this.dexterityP += points;
                break;

            case 4:
                this.agilityP += points;
                break;

            case 5:
                this.magicP += points;
                break;

            case 6:
                this.level += points;
                break;

            case 7:
                this.exceliaP += points;
                break;
        }
    }

    @Override
    public void set(int id, int stat)
    {
        switch(id)
        {
            case 0:
                this.hasFalna = stat;
                break;

            case 1:
                this.strength = stat;
                break;

            case 2:
                this.endurance = stat;
                break;

            case 3:
                this.dexterity = stat;
                break;

            case 4:
                this.agility = stat;
                break;

            case 5:
                this.magic = stat;
                break;

            case 6:
                this.level = stat;
                break;

            case 7:
                this.excelia = stat;
                break;
        }
    }

    @Override
    public void setP(int id, int stat)
    {
        switch(id)
        {
            case 0:
                this.strengthP = stat;
                break;

            case 1:
                this.enduranceP = stat;
                break;

            case 2:
                this.dexterityP = stat;
                break;

            case 3:
                this.agilityP = stat;
                break;

            case 4:
                this.magicP = stat;
                break;

            case 5:
                this.exceliaP = stat;
                break;
        }
    }

    @Override
    public int get(int id)
    {
        switch(id)
        {
            case 0:
                return this.hasFalna;

            case 1:
                return this.strength;

            case 2:
                return this.endurance;

            case 3:
                return this.dexterity;

            case 4:
                return this.agility;

            case 5:
                return this.magic;

            case 6:
                return this.level;

            case 7:
                return this.excelia;
        }

        return 0;
    }

    @Override
    public int getP(int id)
    {
        switch (id)
        {
            case 0:
                return this.strengthP;

            case 1:
                return this.enduranceP;

            case 2:
                return this.dexterityP;

            case 3:
                return this.agilityP;

            case 4:
                return this.magicP;

            case 5:
                return this.exceliaP;
        }

        return 0;
    }

    @Override
    public void giveFalna()
    {
        hasFalna = 1;
    }

    @Override
    public boolean getFalna()
    {
        return (hasFalna == 1);
    }

    @Override
    public String getFamilia()
    {
        return familia;
    }

    @Override
    public void setFamilia(String familia)
    {
        this.familia = familia;
    }

    @Override
    public int getLevel()
    {
        int level = this.level;
        if(excelia / (300 * level) >= 1 && strength > 400 ||
                endurance > 400 ||
                dexterity > 400 ||
                agility > 400 ||
                magicP > 400)
        {
            canLevelUp = true;
            level++;
        }

        return level;
    }

    @Override
    public Set<Ability> levelUp()
    {
        level += 1;
        excelia = 0;

        strength = 0;
        endurance = 0;
        dexterity = 0;
        agility = 0;
        magic = 0;

        canLevelUp = false;

        Set<Ability> abilities = new HashSet<>();
        for(int k = 0; k < 3; k++)
        {
            abilities.add(Ability.randomAbility());
        }

        return abilities;
    }

    @Override
    public int updateStatus()
    {
        strength += strengthP;
        endurance += enduranceP;
        dexterity += dexterityP;
        agility += agilityP;
        magic += magicP;
        excelia += exceliaP;
        level = getLevel();

        int increase = exceliaP;

        strengthP = 0;
        enduranceP = 0;
        dexterityP = 0;
        agilityP = 0;
        magicP = 0;
        exceliaP = 0;

        return increase;
    }

    @Override
    public void grantAbility(Ability ability)
    {
        abilities.add(ability);
    }

    @Override
    public boolean grantSkill()
    {
        Skill chosenSkill = Skill.randomSkill();
        if(!skills.contains(chosenSkill))
        {
            skills.add(chosenSkill);
            return true;
        }

        return false;
    }

    @Override
    public boolean grantMagic()
    {
        Magic chosenMagic = Magic.randomSkill();
        if(!spells.contains(chosenMagic))
        {
            spells.add(chosenMagic);
            return true;
        }

        return false;
    }

    @Override
    public void setAbilities(Ability[] abilities)
    {
        for(int k = 0; k < abilities.length; k++)
        {
            this.abilities.add(k, abilities[k]);
        }
    }

    @Override
    public void setSkills(Skill[] skills)
    {
        for(int k = 0; k < skills.length; k++)
        {
            this.skills.add(k, skills[k]);
        }
    }

    @Override
    public void setSpells(Magic[] spells)
    {
        for(int k = 0; k < spells.length; k++)
        {
            this.spells.add(k, spells[k]);
        }
    }

    @Override
    public Ability[] getAbilities()
    {
        Ability[] abilityArray = new Ability[abilities.toArray().length];

        for(int k = 0; k < abilityArray.length; k++)
        {
            abilityArray[k] = abilities.get(k);
        }

        return abilityArray;
    }

    @Override
    public Skill[] getSkills()
    {
        Skill[] skillArray = new Skill[skills.toArray().length];

        for(int k = 0; k < skillArray.length; k++)
        {
            skillArray[k] = skills.get(k);
        }

        return skillArray;
    }

    @Override
    public Magic[] getSpells()
    {
        Magic[] spellArray = new Magic[spells.toArray().length];

        for(int k = 0; k < spellArray.length; k++)
        {
            spellArray[k] = spells.get(k);
        }

        return spellArray;
    }

    @Override
    public boolean canLevelUp()
    {
        return canLevelUp;
    }

    @Override
    public void setCanLevelUp(boolean canLevelUp)
    {
        this.canLevelUp = canLevelUp;
    }
}

 

StatusProvider

Spoiler

package imnotjahan.mod.danmachi.capabilities;

import net.minecraft.nbt.INBT;
import net.minecraft.util.Direction;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;

public class StatusProvider implements ICapabilitySerializable<INBT>
{
    @CapabilityInject(IStatus.class)
    public static final Capability<IStatus> STATUS_CAP = null;

    private IStatus instance = STATUS_CAP.getDefaultInstance();

    public boolean hasCapability(Capability<?> capability, Direction facing)
    {
        return capability == STATUS_CAP && facing == Status.capSide;
    }

    @Override
    public <IStatus> LazyOptional<IStatus> getCapability(Capability<IStatus> capability, Direction facing)
    {
        return hasCapability(capability, facing) ? (LazyOptional<IStatus>)this.instance : null;
    }

    @Override
    public INBT serializeNBT()
    {
        return STATUS_CAP.getStorage().writeNBT(STATUS_CAP, this.instance, Status.capSide);
    }

    @Override
    public void deserializeNBT(INBT nbt)
    {
        STATUS_CAP.getStorage().readNBT(STATUS_CAP, this.instance, Status.capSide, nbt);
    }
}

 

Where I'm accessing the capability:

StatusGui

Spoiler

package imnotjahan.mod.danmachi.gui.container;

import com.mojang.blaze3d.matrix.MatrixStack;
import imnotjahan.mod.danmachi.Reference;
import imnotjahan.mod.danmachi.capabilities.IStatus;
import imnotjahan.mod.danmachi.capabilities.Status;
import imnotjahan.mod.danmachi.capabilities.StatusProvider;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.common.util.LazyOptional;

public final class StatusGui extends Screen
{
    LazyOptional<IStatus> lazyStatus;
    IStatus status;

    public StatusGui()
    {
        super(new StringTextComponent("StatusGui"));
    }

    @Override
    public void render(MatrixStack stack, int mouseX, int mouseY, float tick)
    {
        this.renderBackground(stack);

        if(minecraft != null)
        {
            lazyStatus = minecraft.player.getCapability(StatusProvider.STATUS_CAP, Status.capSide);
            status = lazyStatus.orElse(new Status());
        }

        int level = status.get(6);
        int strength = status.get(1);
        int endurance = status.get(2);
        int dexterity = status.get(3);
        int agility = status.get(4);
        int magic = status.get(5);

        minecraft.getTextureManager().bind(new ResourceLocation(String.format("%s:textures/gui/falnas/%s.png",
                Reference.MODID, "hestia")));
        blit(stack, 0, 0, 0, 0, width,height, width, height);

        drawCenteredString(stack, font, new StringTextComponent("Level " + level),
                width / 2, 20, 0xFFFFFF);
        drawCenteredString(stack, font, new StringTextComponent("Strength: " + strength),
                width / 2, 40, 0xFFFFFF);
        drawCenteredString(stack, font, new StringTextComponent("Endurance: " + endurance),
                width / 2, 60, 0xFFFFFF);
        drawCenteredString(stack, font, new StringTextComponent("Dexterity: " + dexterity),
                width / 2, 80, 0xFFFFFF);
        drawCenteredString(stack, font, new StringTextComponent("Agility: " + agility),
                width / 2, 100, 0xFFFFFF);
        drawCenteredString(stack, font, new StringTextComponent("Magic: " + magic),
                width / 2, 120, 0xFFFFFF);

        super.render(stack, mouseX, mouseY, tick);
    }
}

 

MonsterBase (Inside die method)

Spoiler

package imnotjahan.mod.danmachi.entities.templates;

import imnotjahan.mod.danmachi.capabilities.IStatus;
import imnotjahan.mod.danmachi.capabilities.Status;
import imnotjahan.mod.danmachi.capabilities.StatusProvider;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.monster.ZombieEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.DamageSource;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;

public class MonsterBase extends ZombieEntity
{
    String name = "";

    public MonsterBase(EntityType<? extends ZombieEntity> entityType, World world, String name)
    {
        super(entityType, world);
        this.name = name;
    }

    @Override
    protected boolean isSunSensitive()
    {
        return false;
    }

    @Override
    protected ResourceLocation getDefaultLootTable()
    {
        return super.getDefaultLootTable();
    }

    @Override
    public void die(DamageSource cause)
    {
        super.die(cause);

        if(cause.getEntity() instanceof PlayerEntity)
        {
            PlayerEntity player = (PlayerEntity) cause.getEntity();
            IStatus status = player.getCapability(StatusProvider.STATUS_CAP, Status.capSide)
                    .orElse(new Status());
            if (status.getFalna())
            {
                for (int k = 0; k < 5; k++)
                {
                    status.increase(5, k + 1);
                }

                status.increase(5, 7);
            } else
            {
                return;
            }

        }
    }

    @Override
    public boolean isBaby()
    {
        return false;
    }

    @Override
    public boolean canBeLeashed(PlayerEntity player)
    {
        return true;
    }

    /*@Override
    protected void dropLoot(boolean wasRecentlyHit, int lootingModifier, DamageSource source)
    {
        super.dropLoot(wasRecentlyHit, lootingModifier, source);

        if(ModConfig.dropMagicCrystals)
        {
            ResourceLocation resource = LootTableHandler.magicStones.get(name);
            LootTable loottable = this.world.getLootTableManager().getLootTableFromLocation(resource);

            LootContext.Builder lootcontext$builder = (new LootContext.Builder((WorldServer)this.world)).withLootedEntity(this).withDamageSource(source);

            if (wasRecentlyHit && this.attackingPlayer != null)
            {
                lootcontext$builder = lootcontext$builder.withPlayer(this.attackingPlayer).withLuck(this.attackingPlayer.getLuck());
            }

            ItemStack stack = loottable.generateLootForPools(this.rand, lootcontext$builder.build()).get(0);
            entityDropItem(stack, 0);
        }
    }*/
}

 

 

Posted (edited)
On 9/3/2021 at 12:35 AM, diesieben07 said:

This is invalid.

  1. instance is not a LazyOptional, this cast will always fail.

Bit off-course from the original question, but I've changed my getCapability code to this

@Override
public <T> LazyOptional<T> getCapability(Capability<T> capability, Direction side)
{
	return capability == STATUS_CAP ? lazyStatus.cast() : LazyOptional.empty();
}

based off of what I found in the forge docs on capabilities, but capability is now never equaling STATUS_CAP, so I'm not sure what's going wrong

Here's what the STATUS_CAP code looks like:

    @CapabilityInject(IStatus.class)
    public static final Capability<IStatus> STATUS_CAP = null;

And also the lazyStatus incase that's important

private static final LazyOptional<IStatus> lazyStatus = LazyOptional.of(Status::new);

 

Edit: It also looks like the getCapability function isn't used at all? At least that's what I've gotten from placing random System.out.println's everywhere. I'm guessing I'm not registering the capability correctly 

    @SubscribeEvent
    public void attachCapability(AttachCapabilitiesEvent<Entity> event) 
    {
        if (!(event.getObject() instanceof PlayerEntity)) return;

        event.addCapability(new ResourceLocation(Reference.MODID, "status"),
                new StatusProvider());
    }

but this seems to be what the docs say to do so I'm not sure if anything's wrong with it

Edited by ImNotJahan
Posted

It wasn't!

This isn't really a good thing but now I figured out that it shouldn't be on the mod bus, and so now it's attaching correctly and the getCapability function is being ran, and everything else seems to be working even without networking (for now at least) so thank you very very much

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.