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