Jump to content

This just won't work no matter how I do it.


EveryBlu

Recommended Posts

I have been wasting several days of my life trying to get this dumb code to work, but it just won't work the way I want it to no matter how I do it.

I am trying to make a system where the player gets certain attribute modifiers based off a data-driven system if they have certain items equipped.

Here's the class: (No this has nothing to do with the vanilla statistics system you can view in the pause menu.)

package everyblu.greateradv.common.events;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import everyblu.greateradv.GreaterAdventuresMod;
import everyblu.greateradv.common.entities.mobs.NetherPaladin;
import everyblu.greateradv.common.init.InitEntityTypes;
import everyblu.greateradv.common.systems.statistics.Statistics;
import everyblu.greateradv.common.systems.statistics.StatisticsManager;
import everyblu.greateradv.common.utils.StatisticsAttributeUtils;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

public class CommonEvents {
    @Mod.EventBusSubscriber(modid = GreaterAdventuresMod.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
    public static class ForgeEvents {
        @SubscribeEvent
        public static void playerPopulateStatisticsAttributes(PlayerEvent.PlayerRespawnEvent event) {
            if (event.isEndConquered()) {return;} // Pretty sure the player keeps their stuff after conquering the end, so skip this event if that happens.

            event.getEntity().getAttribute(Attributes.MAX_HEALTH).addTransientModifier(StatisticsAttributeUtils.getStatisticsModifierForAttribute(Attributes.MAX_HEALTH, 0));
        }

        @SubscribeEvent
        public static void itemStatisticsEquipmentHandler(TickEvent.PlayerTickEvent event) {
            Player player = event.player;

            if (event.phase == TickEvent.Phase.START) {
                float totalMaxHealthBonus = 0;
                float totalKnockbackResistanceBonus = 0;
                float totalArmorBonus = 0;
                float totalArmorToughnessBonus = 0;
                float totalAttackDamageBonus = 0;

                for (ItemStack stack : player.getAllSlots()) {
                    if (stack.isEmpty()) {continue;}
                    Statistics itemStatistics = StatisticsManager.Registry.getItemStatsManagerInstance().getStatisticsForItem(stack.getItem());
                    if (itemStatistics == null) {continue;}

                    if (itemStatistics.maxHealthStat() != null) {totalMaxHealthBonus += itemStatistics.maxHealthStat().getValue();}
                    if (itemStatistics.knockbackResistanceStat() != null) {totalKnockbackResistanceBonus += itemStatistics.knockbackResistanceStat().getValue();}
                    if (itemStatistics.armorStat() != null) {totalArmorBonus += itemStatistics.armorStat().getValue();}
                    if (itemStatistics.armorToughnessStat() != null) {totalArmorToughnessBonus += itemStatistics.armorToughnessStat().getValue();}
                    if (itemStatistics.attackDamageStat() != null) {totalAttackDamageBonus += itemStatistics.attackDamageStat().getValue();}
                }

                Multimap<Attribute,AttributeModifier> oldModifiers = ArrayListMultimap.create();
                oldModifiers.put(Attributes.MAX_HEALTH, player.getAttribute(Attributes.MAX_HEALTH).getModifier(StatisticsAttributeUtils.MAX_HEALTH_UUID));

                Multimap<Attribute, AttributeModifier> newModifiers = ArrayListMultimap.create();
                newModifiers.put(Attributes.MAX_HEALTH, StatisticsAttributeUtils.getStatisticsModifierForAttribute(Attributes.MAX_HEALTH, totalMaxHealthBonus));

                player.getAttributes().removeAttributeModifiers(newModifiers);
                player.getAttributes().addTransientAttributeModifiers(newModifiers);
            }
        }
    }

    @Mod.EventBusSubscriber(modid = GreaterAdventuresMod.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
    public static class ModEvents {
        @SubscribeEvent
        public static void initEntityAttributes(EntityAttributeCreationEvent event) {
            event.put(InitEntityTypes.NETHER_PALADIN.get(), NetherPaladin.createAttributes().build());
        }
    }
}

StatisticsAttributeUtils:

package everyblu.greateradv.common.utils;

import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class StatisticsAttributeUtils {
    public static final UUID MAX_HEALTH_UUID = UUID.fromString("e67a8b8e-5e17-439b-84fc-89b33aaabfbf");
    public static final UUID KNOCKBACK_RESISTANCE_UUID = UUID.fromString("c53f398b-1e6f-47e2-9a37-7b85edc02161");
    public static final UUID ARMOR_UUID = UUID.fromString("9ef4377c-48b2-43df-ab1d-276abbc2331a");
    public static final UUID ARMOR_TOUGHNESS_UUID = UUID.fromString("96f6e24e-227e-48f2-aafc-8e60554d9d97");
    public static final UUID ATTACK_DAMAGE_UUID = UUID.fromString("02075396-74b1-486d-99be-4dfe5d20d6d9");

    private static final Map<Attribute, UUID> ATTRIBUTE_TO_UUID = new HashMap<>();
    static {
        ATTRIBUTE_TO_UUID.put(Attributes.MAX_HEALTH, MAX_HEALTH_UUID);
        ATTRIBUTE_TO_UUID.put(Attributes.KNOCKBACK_RESISTANCE, KNOCKBACK_RESISTANCE_UUID);
        ATTRIBUTE_TO_UUID.put(Attributes.ARMOR, ARMOR_UUID);
        ATTRIBUTE_TO_UUID.put(Attributes.ARMOR_TOUGHNESS, ARMOR_TOUGHNESS_UUID);
        ATTRIBUTE_TO_UUID.put(Attributes.ATTACK_DAMAGE, ATTACK_DAMAGE_UUID);
    }

    public static AttributeModifier getStatisticsModifierForAttribute(Attribute attribute, float amount) {
        UUID uuid = ATTRIBUTE_TO_UUID.get(attribute);

        return new AttributeModifier(uuid, "itemStatistics " + attribute + "Modifier", amount, AttributeModifier.Operation.ADDITION);
    }
}

Let me know if you need to see more code. Any help is highly appreciated.

Edited by EveryBlu
Link to comment
Share on other sites

  • EveryBlu changed the title to This just won't work no matter how I do it.

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.