Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16.5]A few question about capabilities and (unrelated) runtime.


cwJn
 Share

Recommended Posts

So in my mod I'm attaching a capability to any item which is an armour piece, weapon, or entity. To test my code, I made printed messages to console whenever the event fired. I noticed that these events would be firing multiple times per entity/item being affected. Is this something I need to worry about? I'm not very familiar with the intricacies of how capabilities work. On an unrelated note, I'm getting slightly worried about how my mod will cause the game to lag. I have methods that hook into the ItemAttributeModifier and ItemTooltip events, and use my capabilities to operate on the item's attributes and provide a custom tooltip. I'm not iterating through lists or anything, but it seems as though these events fire every tick the player has the item equipped. While this doesn't lag me in my dev environment, will this have larger ramifications when playing on servers with 4 - 8 players?

Link to comment
Share on other sites

1 hour ago, cwJn said:

Is this something I need to worry about?

no the Event is fired for each Item/Player/Entity/Level

 

1 hour ago, cwJn said:

While this doesn't lag me in my dev environment, will this have larger ramifications when playing on servers with 4 - 8 players?

without code, im not 100% sure but i would say yes,
you can post your code (git repo), and i can try to find the code part which causes the lags

Link to comment
Share on other sites

1 hour ago, cwJn said:

I noticed that these events would be firing multiple times per entity/item being affected.

Which events?

1 hour ago, cwJn said:

I have methods that hook into the ItemAttributeModifier and ItemTooltip events, and use my capabilities to operate on the item's attributes and provide a custom tooltip.

You don't necessarily need to use events for this. You can override getAttributeModifiers and appendHoverText in your Item class to do this without events.

Link to comment
Share on other sites

33 minutes ago, diesieben07 said:

You don't necessarily need to use events for this. You can override getAttributeModifiers and appendHoverText in your Item class to do this without events.

I have to use events for this because I have to operate on every single item in the game.

1 hour ago, Luis_ST said:

without code, im not 100% sure but i would say yes,
you can post your code (git repo), and i can try to find the code part which causes the lags

This is the code I'm worried about. I can't think of a better way to do this though.

@Mod.EventBusSubscriber(modid = HardstuckIntegration.modID)
public class ItemEvents {

    private static DecimalFormat df = new DecimalFormat("#.##");

    //this method needs to remove the armour (toughness) and attack damage from items. Also,
    //use the attack damage and armour to convert to extra damage
    @SubscribeEvent(priority = LOWEST)
    public static void removeVanillaAttributes(ItemAttributeModifierEvent event) {
        ItemStack item = event.getItemStack(); //the item
        double damage = event.removeAttribute(ATTACK_DAMAGE).stream().mapToDouble(AttributeModifier::getAmount).sum(); //the sum of the damage attribute
        double armor = event.removeAttribute(ARMOR).stream().mapToDouble(AttributeModifier::getAmount).sum(); //the sum of the armor attribute
        if (armor > 0) { //make sure there is actually armor to convert
            doResistanceCalc(armor, item); //attach capabilities to the item by converting its base stats and taking the config into account
        }
        if (damage > 0) { //make sure there is actually damage to convert
            doDamageTypesCalc(damage, item); //attach capabilities to the item by converting its base stats and taking the config into account
        }
        event.removeAttribute(ARMOR_TOUGHNESS); //remove redundant stat
    }

    private static void doResistanceCalc(double val, ItemStack item) {
        item.getCapability(CapabilityDamageResistance.DAMAGE_RESISTANCE_CAPABILITY).ifPresent(c -> {
            //we only want to change the capability if it hasn't been affected yet. This assumes if an item has a resistance capability, it isn't all 0.
            if (c.getImpactResistance() == 0 && c.getPunctureResistance() == 0 && c.getSlashResistance() == 0 && c.getFireResistance() == 0 && c.getLightningResistance() == 0 && c.getIceResistance() == 0) {
                ArmourItemData data = JSONHandler.getArmourData(item.getItem().getRegistryName()); // get the data from config file
                double[] resistanceConversions = data.getResConversion(); //get item's conversion ratios
                float i = data.getImpactRes(); // get item's base resistances
                float p = data.getPunctureRes();
                float s = data.getSlashRes();
                float f = data.getFireRes();
                float l = data.getLightningRes();
                float ic = data.getIceRes();
                double IRes, PRes, SRes, FRes, LRes, ICRes;
                IRes = (val * resistanceConversions[0]) + i; //add them together.
                PRes = (val * resistanceConversions[1]) + p;
                SRes = (val * resistanceConversions[2]) + s;
                FRes = (val * resistanceConversions[3]) + f;
                LRes = (val * resistanceConversions[4]) + l;
                ICRes = (val * resistanceConversions[5]) + ic;
                c.setImpactResistance((float) IRes);
                c.setPunctureResistance((float) PRes);
                c.setSlashResistance((float) SRes);
                c.setFireResistance((float) FRes);
                c.setLightningResistance((float) LRes);
                c.setIceResistance((float) ICRes);
            }
        });
    }

    private static void doDamageTypesCalc(double val, ItemStack item) {
        item.getCapability(CapabilityDamageTypes.DAMAGE_TYPES_CAPABILITY).ifPresent(c -> {
            //we only want to change the capability if it hasn't been affected yet. This assumes if an item has a damage capability, it isn't all 0.
            if (c.getSlashDamage() == 0 && c.getPunctureDamage() == 0 && c.getImpactDamage() == 0 && c.getFireDamage() == 0 && c.getLightningDamage() == 0 && c.getIceDamage() == 0) {
                WeaponItemData data = JSONHandler.getWeaponData(item.getItem().getRegistryName()); // get data from config file
                double[] damageConversions = data.getDmgConversion(); //get item's conversion ratios
                float i = data.getImpactDmg(); //get base damages
                float p = data.getPunctureDmg();
                float s = data.getSlashDmg();
                float f = data.getFireDmg();
                float l = data.getLightningDmg();
                float ic = data.getIceDmg();
                double id, pd, sd, fd, ld, icd;
                id = (val * damageConversions[0]) + i; //add them together
                pd = (val * damageConversions[1]) + p;
                sd = (val * damageConversions[2]) + s;
                fd = (val * damageConversions[3]) + f;
                ld = (val * damageConversions[4]) + l;
                icd = (val * damageConversions[5]) + ic;
                c.setImpactDamage((float) id);
                c.setPunctureDamage((float) pd);
                c.setSlashDamage((float) sd);
                c.setFireDamage((float) fd);
                c.setLightningDamage((float) ld);
                c.setIceDamage((float) icd);
            }
        });
    }

    @SubscribeEvent(priority = LOWEST)
    public static void addExtraDamageTooltip(ItemTooltipEvent event) {

        ItemStack item = event.getItemStack();
        IDamageResistance res = item.getCapability(CapabilityDamageResistance.DAMAGE_RESISTANCE_CAPABILITY).orElse(null);
        IDamageTypes dmg = item.getCapability(CapabilityDamageTypes.DAMAGE_TYPES_CAPABILITY).orElse(null);

        if (res != null) {
            //MAKE VARIABLES
            float ir = res.getImpactResistance();
            float pr = res.getPunctureResistance();
            float sr = res.getSlashResistance();
            float fr = res.getFireResistance();
            float lr = res.getLightningResistance();
            float icr = res.getIceResistance();
            IFormattableTextComponent impactText;
            IFormattableTextComponent punctureText;
            IFormattableTextComponent slashText;
            IFormattableTextComponent fireText;
            IFormattableTextComponent lightningText;
            IFormattableTextComponent iceText;
            IFormattableTextComponent resistanceText;
            IFormattableTextComponent irText;
            IFormattableTextComponent prText;
            IFormattableTextComponent srText;
            IFormattableTextComponent frText;
            IFormattableTextComponent lrText;
            IFormattableTextComponent icrText;

            resistanceText = new TranslationTextComponent("----------Damage Resistances----------");
            impactText = new TranslationTextComponent("Impact Resistance: ");
            punctureText = new TranslationTextComponent("Puncture Resistance: ");
            slashText = new TranslationTextComponent("Slash Resistance: ");
            fireText = new TranslationTextComponent("Fire Resistance: ");
            lightningText = new TranslationTextComponent("Lightning Resistance: ");
            iceText = new TranslationTextComponent("Ice Resistance: ");
            irText = new TranslationTextComponent(df.format(ir));
            prText = new TranslationTextComponent(df.format(pr));
            srText = new TranslationTextComponent(df.format(sr));
            frText = new TranslationTextComponent(df.format(fr));
            lrText = new TranslationTextComponent(df.format(lr));
            icrText = new TranslationTextComponent(df.format(icr));

            event.getToolTip().add(TextUtil.withColor(resistanceText, Color.MEDIUMPURPLE).withStyle(TextFormatting.BOLD));
            event.getToolTip().add(TextUtil.withColor(impactText, Color.LIGHTGREEN).append(TextUtil.withColor(irText, Color.LIGHTSTEELBLUE)));
            event.getToolTip().add(TextUtil.withColor(punctureText, Color.LIGHTGREEN).append(TextUtil.withColor(prText, Color.LIGHTSTEELBLUE)));
            event.getToolTip().add(TextUtil.withColor(slashText, Color.LIGHTGREEN).append(TextUtil.withColor(srText, Color.LIGHTSTEELBLUE)));
            event.getToolTip().add(TextUtil.withColor(fireText, Color.ORANGERED).append(TextUtil.withColor(frText, Color.LIGHTSTEELBLUE)));
            event.getToolTip().add(TextUtil.withColor(lightningText, Color.YELLOW).append(TextUtil.withColor(lrText, Color.LIGHTSTEELBLUE)));
            event.getToolTip().add(TextUtil.withColor(iceText, Color.LIGHTBLUE).append(TextUtil.withColor(icrText, Color.LIGHTSTEELBLUE)));
        }
        if (dmg != null) {

            double attackSpeedMainhand = item.getItem().getDefaultInstance().getAttributeModifiers(EquipmentSlotType.MAINHAND).get(ATTACK_SPEED).stream().mapToDouble(AttributeModifier::getAmount).sum();
            double attackSpeedOffhand = item.getItem().getDefaultInstance().getAttributeModifiers(EquipmentSlotType.OFFHAND).get(ATTACK_SPEED).stream().mapToDouble(AttributeModifier::getAmount).sum();
            float id = dmg.getImpactDamage();
            float pd = dmg.getPunctureDamage();
            float sd = dmg.getSlashDamage();
            float fd = dmg.getFireDamage();
            float ld = dmg.getLightningDamage();
            float icd = dmg.getIceDamage();
            IFormattableTextComponent impactText;
            IFormattableTextComponent punctureText;
            IFormattableTextComponent slashText;
            IFormattableTextComponent fireText;
            IFormattableTextComponent lightningText;
            IFormattableTextComponent iceText;
            IFormattableTextComponent damageText;
            IFormattableTextComponent idText;
            IFormattableTextComponent pdText;
            IFormattableTextComponent sdText;
            IFormattableTextComponent fdText;
            IFormattableTextComponent ldText;
            IFormattableTextComponent icdText;

            damageText = new TranslationTextComponent("----------Damage Typing----------");
            impactText = new TranslationTextComponent("Impact Damage: ");
            punctureText = new TranslationTextComponent("Puncture Damage: ");
            slashText = new TranslationTextComponent("Slash Damage: ");
            fireText = new TranslationTextComponent("Fire Damage: ");
            lightningText = new TranslationTextComponent("Lightning Damage: ");
            iceText = new TranslationTextComponent("Ice Damage: ");
            idText = new TranslationTextComponent(df.format(id));
            pdText = new TranslationTextComponent(df.format(pd));
            sdText = new TranslationTextComponent(df.format(sd));
            fdText = new TranslationTextComponent(df.format(fd));
            ldText = new TranslationTextComponent(df.format(ld));
            icdText = new TranslationTextComponent(df.format(icd));

            event.getToolTip().add(TextUtil.withColor(damageText, Color.MEDIUMPURPLE).withStyle(TextFormatting.BOLD));
            event.getToolTip().add(TextUtil.withColor(impactText, Color.LIGHTGREEN).append(TextUtil.withColor(idText, Color.LIGHTSTEELBLUE)));
            event.getToolTip().add(TextUtil.withColor(punctureText, Color.LIGHTGREEN).append(TextUtil.withColor(pdText, Color.LIGHTSTEELBLUE)));
            event.getToolTip().add(TextUtil.withColor(slashText, Color.LIGHTGREEN).append(TextUtil.withColor(sdText, Color.LIGHTSTEELBLUE)));
            event.getToolTip().add(TextUtil.withColor(fireText, Color.ORANGERED).append(TextUtil.withColor(fdText, Color.LIGHTSTEELBLUE)));
            event.getToolTip().add(TextUtil.withColor(lightningText, Color.YELLOW).append(TextUtil.withColor(ldText, Color.LIGHTSTEELBLUE)));
            event.getToolTip().add(TextUtil.withColor(iceText, Color.LIGHTBLUE).append(TextUtil.withColor(icdText, Color.LIGHTSTEELBLUE)));
        }
    }
}

 

Link to comment
Share on other sites

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
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.

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Im trying to make a moddes server with my friends but every single time we get same error https://pastebin.com/UP6P6YSd Is there a way to fix it? Thanks for solutions
    • Take a break, go for a walk. Then try again. 🙂
    • Where did you see that? Sorry if I am being a bother, it's just that I have been staring at this mod for a long time and I'm probably looking over a lot of things
    • Taking a look at AbstractContainerScreen.renderSlot() I got the following code: Method created in my AbstractContainerScreen class: private void renderCustomSlot(PoseStack poseStack,ItemStack itemstack,int x, int y) { boolean visible = true; boolean hover = false; this.setBlitOffset(100); this.itemRenderer.blitOffset = 100.0F; if (visible) { if (hover) { //Draw a rectangle behind the item fill(poseStack, x, y, x + 16, y + 16, -2130706433); } //Render item icon RenderSystem.enableDepthTest(); this.itemRenderer.renderAndDecorateItem(this.minecraft.player, itemstack, x, y, x + y * this.imageWidth); //Render item string count PoseStack posestack = new PoseStack(); if (itemstack.getCount() != 1) { String s = String.valueOf(itemstack.getCount()); posestack.translate(0.0D, 0.0D, itemRenderer.blitOffset + 200.0F); MultiBufferSource.BufferSource multibuffersource$buffersource = MultiBufferSource.immediate(Tesselator.getInstance().getBuilder()); /* float scale; if (s.length()<3){ scale=1f; }else if (s.length()<4){ scale = 0.75f; }else { scale=0.5f; s = s.substring(0,s.length()-3)+"k"; } float compensatingScale = 1/scale; float stringX = (x + 19f - 2 - font.width(s)*scale)*compensatingScale; float stringY =(y + 6f + 3)*compensatingScale; posestack.scale(scale,scale,scale); */ float stringX = (x + 19f - 2 - font.width(s)); float stringY =(y + 6f + 3); font.drawInBatch(s,stringX,stringY,16777215, true, posestack.last().pose(), multibuffersource$buffersource, false, 0, 15728880); //posestack.scale(compensatingScale,compensatingScale,compensatingScale); multibuffersource$buffersource.endBatch(); } } this.itemRenderer.blitOffset = 0.0F; this.setBlitOffset(0); } Now it works correctly: Thanks to warjort for the tip!
    • Hello, I am currently trying to get a forge server setup so I can play the Pixelmon mod with some friends on a private server and have run into some issues with launching my forge server file, I downloaded the forge 12.2.2-14.23.5.2859 installer, downloaded it into a folder on my desktop, ran the Mc server file, agreed to Eula, made a run.bat file to launch the forge server at 6GBs, and when I try launching I get this issue: Pixelmon Server>java -Xmx6144M -Xms6144M -jar forge-1.12.2-14.23.5.2859-universal.jar --nogui We appear to be missing one or more essential library files. You will need to add them to your server before FML and Forge will run successfully.java.lang.ClassNotFoundException: org.objectweb.asm.Type         at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)         at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)         at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)         at java.base/java.lang.Class.forName0(Native Method)         at java.base/java.lang.Class.forName(Class.java:466)         at net.minecraftforge.fml.relauncher.ServerLaunchWrapper.run(ServerLaunchWrapper.java:53)         at net.minecraftforge.fml.relauncher.ServerLaunchWrapper.main(ServerLaunchWrapper.java:34)   I've tried using another minecraft server jar file from a different source to see if that would solve anything but its still coming up with this issue. Any help is greatly appreciated, thanks for your help in advance.
  • Topics

×
×
  • Create New...

Important Information

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