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;
}
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();
}
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);
}
Aggregations