Search in sources :

Example 1 with CauseStackManager

use of org.spongepowered.api.event.CauseStackManager in project SpongeCommon by SpongePowered.

the class MixinRConThreadClient method rconLogoutCallback.

@Inject(method = "closeSocket", at = @At("HEAD"))
public void rconLogoutCallback(CallbackInfo ci) {
    if (this.loggedIn) {
        SpongeImpl.getScheduler().callSync(() -> {
            final CauseStackManager causeStackManager = Sponge.getCauseStackManager();
            causeStackManager.pushCause(this);
            causeStackManager.pushCause(this.source);
            final RconConnectionEvent.Disconnect event = SpongeEventFactory.createRconConnectionEventDisconnect(causeStackManager.getCurrentCause(), (RconSource) this.source);
            SpongeImpl.postEvent(event);
            causeStackManager.popCauses(2);
            return event;
        });
    }
}
Also used : CauseStackManager(org.spongepowered.api.event.CauseStackManager) RconConnectionEvent(org.spongepowered.api.event.network.rcon.RconConnectionEvent) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 2 with CauseStackManager

use of org.spongepowered.api.event.CauseStackManager in project SpongeAPI by SpongePowered.

the class SimpleServiceManagerTest method mockEventManager.

@Before
public void mockEventManager() throws Exception {
    this.manager = mock(PluginManager.class);
    this.testPluginContainer = mock(PluginContainer.class);
    when(this.testPluginContainer.getId()).thenReturn("TestPlugin");
    when(this.manager.fromInstance(this.testPlugin)).thenReturn(Optional.of(this.testPluginContainer));
    Game game = mock(Game.class);
    TestHooks.setInstance("eventManager", mock(EventManager.class));
    when(game.getEventManager()).thenReturn(mock(EventManager.class));
    CauseStackManager csm = mock(CauseStackManager.class);
    when(game.getCauseStackManager()).thenReturn(csm);
    when(csm.pushCause(null)).thenReturn(csm);
    when(csm.popCause()).thenReturn(null);
    when(csm.getCurrentCause()).thenReturn(Cause.of(EventContext.empty(), this));
    CauseStackManager.StackFrame sf = mock(CauseStackManager.StackFrame.class);
    when(sf.pushCause(null)).thenReturn(sf);
    when(sf.addContext(null, null)).thenReturn(sf);
    when(sf.popCause()).thenReturn(null);
    when(sf.getCurrentCause()).thenReturn(Cause.of(EventContext.empty(), this));
    when(csm.pushCauseFrame()).thenReturn(sf);
    TestHooks.setGame(game);
    TestHooks.setInstance("causeStackManager", csm);
}
Also used : PluginManager(org.spongepowered.api.plugin.PluginManager) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Game(org.spongepowered.api.Game) EventManager(org.spongepowered.api.event.EventManager) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Before(org.junit.Before)

Example 3 with CauseStackManager

use of org.spongepowered.api.event.CauseStackManager in project SpongeCommon by SpongePowered.

the class DamageEventUtil method createArmorModifiers.

public static Optional<DamageFunction> createArmorModifiers(final LivingEntity living, final DamageSource damageSource) {
    if (damageSource.isBypassArmor()) {
        return Optional.empty();
    }
    final DoubleUnaryOperator function = incomingDamage -> -(incomingDamage - CombatRules.getDamageAfterAbsorb((float) incomingDamage, living.getArmorValue(), (float) living.getAttributeValue(Attributes.ARMOR_TOUGHNESS)));
    final DamageFunction armorModifier;
    try (final CauseStackManager.StackFrame frame = ((Server) living.getServer()).causeStackManager().pushCauseFrame()) {
        frame.pushCause(living);
        frame.pushCause(Attributes.ARMOR_TOUGHNESS);
        armorModifier = DamageFunction.of(DamageModifier.builder().cause(frame.currentCause()).type(DamageModifierTypes.ARMOR).build(), function);
    }
    return Optional.of(armorModifier);
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) LivingEntity(net.minecraft.world.entity.LivingEntity) AABB(net.minecraft.world.phys.AABB) LivingEntityAccessor(org.spongepowered.common.accessor.world.entity.LivingEntityAccessor) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) MobType(net.minecraft.world.entity.MobType) EventContext(org.spongepowered.api.event.EventContext) Registry(net.minecraft.core.Registry) ChunkSource(net.minecraft.world.level.chunk.ChunkSource) CreatorTrackedBridge(org.spongepowered.common.bridge.CreatorTrackedBridge) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Map(java.util.Map) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Player(net.minecraft.world.entity.player.Player) List(java.util.List) BlockPos(net.minecraft.core.BlockPos) BlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.BlockDamageSource) Optional(java.util.Optional) ServerLocation(org.spongepowered.api.world.server.ServerLocation) Enchantment(net.minecraft.world.item.enchantment.Enchantment) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) LevelChunkBridge(org.spongepowered.common.bridge.world.level.chunk.LevelChunkBridge) EventContextKeys(org.spongepowered.api.event.EventContextKeys) ServerWorld(org.spongepowered.api.world.server.ServerWorld) BlockState(net.minecraft.world.level.block.state.BlockState) HashMap(java.util.HashMap) DamageModifierTypes(org.spongepowered.api.event.cause.entity.damage.DamageModifierTypes) MobEffects(net.minecraft.world.effect.MobEffects) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) EntityDamageSource(net.minecraft.world.damagesource.EntityDamageSource) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) DamageSource(net.minecraft.world.damagesource.DamageSource) EnchantmentHelper(net.minecraft.world.item.enchantment.EnchantmentHelper) CombatRules(net.minecraft.world.damagesource.CombatRules) Server(org.spongepowered.api.Server) CauseStackManager(org.spongepowered.api.event.CauseStackManager) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) Cause(org.spongepowered.api.event.Cause) Entity(net.minecraft.world.entity.Entity) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DamageModifier(org.spongepowered.api.event.cause.entity.damage.DamageModifier) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) Attributes(net.minecraft.world.entity.ai.attributes.Attributes) Mth(net.minecraft.util.Mth) Collections(java.util.Collections) ListTag(net.minecraft.nbt.ListTag) CauseStackManager(org.spongepowered.api.event.CauseStackManager) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator)

Example 4 with CauseStackManager

use of org.spongepowered.api.event.CauseStackManager in project SpongeCommon by SpongePowered.

the class RconClientMixin method impl$rconLogoutCallback.

@Inject(method = "closeSocket", at = @At("HEAD"))
private void impl$rconLogoutCallback(final CallbackInfo ci) {
    if (this.authed) {
        SpongeCommon.serverScheduler().execute(() -> {
            final CauseStackManager causeStackManager = PhaseTracker.getCauseStackManager();
            causeStackManager.pushCause(this);
            causeStackManager.pushCause(this.impl$source);
            final RconConnectionEvent.Disconnect event = SpongeEventFactory.createRconConnectionEventDisconnect(causeStackManager.currentCause(), (RconConnection) this.impl$source);
            SpongeCommon.post(event);
            causeStackManager.popCauses(2);
            return event;
        });
    }
}
Also used : CauseStackManager(org.spongepowered.api.event.CauseStackManager) RconConnectionEvent(org.spongepowered.api.event.network.rcon.RconConnectionEvent) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 5 with CauseStackManager

use of org.spongepowered.api.event.CauseStackManager in project SpongeCommon by SpongePowered.

the class ServerLevelMixin method impl$throwBroadcastEvent.

@Inject(method = "levelEvent", at = @At("HEAD"), cancellable = true)
private void impl$throwBroadcastEvent(final Player player, final int eventID, final BlockPos pos, final int dataID, CallbackInfo ci) {
    if (eventID == Constants.WorldEvents.PLAY_RECORD_EVENT && ShouldFire.PLAY_SOUND_EVENT_RECORD) {
        try (final CauseStackManager.StackFrame frame = Sponge.server().causeStackManager().pushCauseFrame()) {
            final BlockEntity tileEntity = this.shadow$getBlockEntity(pos);
            if (tileEntity instanceof JukeboxBlockEntity) {
                final JukeboxBlockEntity jukebox = (JukeboxBlockEntity) tileEntity;
                final ItemStack record = jukebox.getRecord();
                frame.pushCause(jukebox);
                frame.addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(record));
                if (!record.isEmpty()) {
                    final Optional<MusicDisc> recordProperty = ((org.spongepowered.api.item.inventory.ItemStack) (Object) record).get(Keys.MUSIC_DISC);
                    if (!recordProperty.isPresent()) {
                        // Safeguard for https://github.com/SpongePowered/SpongeCommon/issues/2337
                        return;
                    }
                    final MusicDisc recordType = recordProperty.get();
                    final PlaySoundEvent.Record event = SpongeCommonEventFactory.callPlaySoundRecordEvent(frame.currentCause(), jukebox, recordType, dataID);
                    if (event.isCancelled()) {
                        ci.cancel();
                    }
                }
            }
        }
    }
}
Also used : MusicDisc(org.spongepowered.api.effect.sound.music.MusicDisc) CauseStackManager(org.spongepowered.api.event.CauseStackManager) PlaySoundEvent(org.spongepowered.api.event.sound.PlaySoundEvent) JukeboxBlockEntity(net.minecraft.world.level.block.entity.JukeboxBlockEntity) ItemStack(net.minecraft.world.item.ItemStack) JukeboxBlockEntity(net.minecraft.world.level.block.entity.JukeboxBlockEntity) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

CauseStackManager (org.spongepowered.api.event.CauseStackManager)14 HashMap (java.util.HashMap)4 Map (java.util.Map)4 EventContextKeys (org.spongepowered.api.event.EventContextKeys)4 RconConnectionEvent (org.spongepowered.api.event.network.rcon.RconConnectionEvent)4 Inject (org.spongepowered.asm.mixin.injection.Inject)4 IOException (java.io.IOException)3 BlockPos (net.minecraft.core.BlockPos)3 BufferedInputStream (java.io.BufferedInputStream)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 Optional (java.util.Optional)2 ExecutionException (java.util.concurrent.ExecutionException)2 DoubleUnaryOperator (java.util.function.DoubleUnaryOperator)2 Predicate (java.util.function.Predicate)2 Registry (net.minecraft.core.Registry)2 ListTag (net.minecraft.nbt.ListTag)2 Entity (net.minecraft.world.entity.Entity)2