Posted November 9, 20214 yr 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?
November 9, 20214 yr 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
November 9, 20214 yr Author 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))); } } }
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.