Search in sources :

Example 1 with AbilityRemoveEvent

use of com.lying.variousoddities.api.event.AbilityEvent.AbilityRemoveEvent in project VariousOddities by Lyinginbedmon.

the class Abilities method updateAbilityCache.

public void updateAbilityCache() {
    if (this.entity == null)
        return;
    boolean dirty = false;
    Map<ResourceLocation, Ability> currentAbilities = getCurrentAbilities();
    // If a map name in cachedAbilities doesn't exist in currentAbilities, remove it from cachedAbilities
    List<ResourceLocation> removedAbilities = Lists.newArrayList();
    cachedAbilities.keySet().forEach((mapname) -> {
        if (!currentAbilities.containsKey(mapname))
            removedAbilities.add(mapname);
    });
    if (!removedAbilities.isEmpty())
        dirty = true;
    removedAbilities.forEach((mapname) -> {
        Ability ability = cachedAbilities.get(mapname);
        ability.onAbilityRemoved(this.entity);
        MinecraftForge.EVENT_BUS.post(new AbilityRemoveEvent(this.entity, ability, this));
        if (this.entity.getType() == EntityType.PLAYER)
            PacketHandler.sendTo((ServerPlayerEntity) this.entity, new PacketAbilityRemove(mapname));
        uncacheAbility(mapname);
    });
    List<ResourceLocation> overrides = Lists.newArrayList();
    currentAbilities.forEach((mapname, ability) -> {
        // If the source ID of an ability in currentAbilities doesn't match its counterpart in cachedAbilities, overwrite it in cachedAbilities
        if (!cachedAbilities.containsKey(mapname) || !ability.isTemporary() && !ability.getSourceId().equals(cachedAbilities.get(mapname).getSourceId()))
            overrides.add(mapname);
    });
    if (!overrides.isEmpty())
        dirty = true;
    overrides.forEach((mapname) -> {
        Ability ability = currentAbilities.get(mapname);
        ability.onAbilityAdded(this.entity);
        MinecraftForge.EVENT_BUS.post(new AbilityAddEvent(this.entity, ability, this));
        cacheAbility(ability);
    });
    if (dirty)
        markDirty();
    this.cacheDirty = false;
}
Also used : Ability(com.lying.variousoddities.species.abilities.Ability) IBonusJumpAbility(com.lying.variousoddities.species.abilities.IBonusJumpAbility) ICompoundAbility(com.lying.variousoddities.species.abilities.ICompoundAbility) ResourceLocation(net.minecraft.util.ResourceLocation) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) AbilityRemoveEvent(com.lying.variousoddities.api.event.AbilityEvent.AbilityRemoveEvent) PacketAbilityRemove(com.lying.variousoddities.network.PacketAbilityRemove) AbilityAddEvent(com.lying.variousoddities.api.event.AbilityEvent.AbilityAddEvent)

Example 2 with AbilityRemoveEvent

use of com.lying.variousoddities.api.event.AbilityEvent.AbilityRemoveEvent in project VariousOddities by Lyinginbedmon.

the class Abilities method removeCustomAbility.

public void removeCustomAbility(ResourceLocation mapName) {
    Ability ability = this.customAbilities.get(mapName);
    if (ability != null && this.entity != null) {
        ability.onAbilityRemoved(this.entity);
        MinecraftForge.EVENT_BUS.post(new AbilityRemoveEvent(this.entity, ability, this));
        if (this.entity.getType() == EntityType.PLAYER && !this.entity.getEntityWorld().isRemote)
            PacketHandler.sendTo((ServerPlayerEntity) this.entity, new PacketAbilityRemove(ability.getMapName()));
    }
    this.customAbilities.remove(mapName);
    markForRecache();
    markDirty();
}
Also used : Ability(com.lying.variousoddities.species.abilities.Ability) IBonusJumpAbility(com.lying.variousoddities.species.abilities.IBonusJumpAbility) ICompoundAbility(com.lying.variousoddities.species.abilities.ICompoundAbility) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) AbilityRemoveEvent(com.lying.variousoddities.api.event.AbilityEvent.AbilityRemoveEvent) PacketAbilityRemove(com.lying.variousoddities.network.PacketAbilityRemove)

Example 3 with AbilityRemoveEvent

use of com.lying.variousoddities.api.event.AbilityEvent.AbilityRemoveEvent in project VariousOddities by Lyinginbedmon.

the class PacketAbilityRemove method handle.

public static void handle(PacketAbilityRemove msg, Supplier<NetworkEvent.Context> cxt) {
    NetworkEvent.Context context = cxt.get();
    if (!context.getDirection().getReceptionSide().isServer()) {
        PlayerEntity sender = ((CommonProxy) VariousOddities.proxy).getPlayerEntity(context);
        if (sender != null) {
            LivingData data = LivingData.forEntity(sender);
            if (data != null) {
                Abilities abilities = data.getAbilities();
                Ability ability = abilities.getCachedAbilities().get(msg.mapName);
                if (ability != null) {
                    abilities.uncacheAbility(msg.mapName);
                    ability.onAbilityRemoved(sender);
                    MinecraftForge.EVENT_BUS.post(new AbilityRemoveEvent(sender, ability, abilities));
                }
            }
        }
    }
    context.setPacketHandled(true);
}
Also used : Ability(com.lying.variousoddities.species.abilities.Ability) Abilities(com.lying.variousoddities.capabilities.Abilities) NetworkEvent(net.minecraftforge.fml.network.NetworkEvent) CommonProxy(com.lying.variousoddities.proxy.CommonProxy) LivingData(com.lying.variousoddities.capabilities.LivingData) AbilityRemoveEvent(com.lying.variousoddities.api.event.AbilityEvent.AbilityRemoveEvent) PlayerEntity(net.minecraft.entity.player.PlayerEntity)

Aggregations

AbilityRemoveEvent (com.lying.variousoddities.api.event.AbilityEvent.AbilityRemoveEvent)3 Ability (com.lying.variousoddities.species.abilities.Ability)3 PacketAbilityRemove (com.lying.variousoddities.network.PacketAbilityRemove)2 IBonusJumpAbility (com.lying.variousoddities.species.abilities.IBonusJumpAbility)2 ICompoundAbility (com.lying.variousoddities.species.abilities.ICompoundAbility)2 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)2 AbilityAddEvent (com.lying.variousoddities.api.event.AbilityEvent.AbilityAddEvent)1 Abilities (com.lying.variousoddities.capabilities.Abilities)1 LivingData (com.lying.variousoddities.capabilities.LivingData)1 CommonProxy (com.lying.variousoddities.proxy.CommonProxy)1 PlayerEntity (net.minecraft.entity.player.PlayerEntity)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 NetworkEvent (net.minecraftforge.fml.network.NetworkEvent)1