use of org.spongepowered.common.event.tracking.PhaseTracker in project SpongeCommon by SpongePowered.
the class MixinChunkProviderServer method canDenyChunkRequest.
private boolean canDenyChunkRequest() {
if (!SpongeImpl.getServer().isCallingFromMinecraftThread()) {
return true;
}
if (this.forceChunkRequests) {
return false;
}
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final IPhaseState currentState = phaseTracker.getCurrentState();
// States that cannot deny chunks
if (currentState == TickPhase.Tick.PLAYER || currentState == TickPhase.Tick.DIMENSION || currentState == EntityPhase.State.CHANGING_DIMENSION || currentState == EntityPhase.State.LEAVING_DIMENSION) {
return false;
}
// States that can deny chunks
if (currentState == GenerationPhase.State.WORLD_SPAWNER_SPAWNING || currentState == PluginPhase.Listener.PRE_SERVER_TICK_LISTENER || currentState == PluginPhase.Listener.POST_SERVER_TICK_LISTENER) {
return true;
}
// Phases that can deny chunks
if (currentState.getPhase() == TrackingPhases.BLOCK || currentState.getPhase() == TrackingPhases.ENTITY || currentState.getPhase() == TrackingPhases.TICK) {
return true;
}
return false;
}
use of org.spongepowered.common.event.tracking.PhaseTracker in project SpongeCommon by SpongePowered.
the class MixinBlock method onDropBlockAsItemWithChanceReturn.
@Inject(method = "dropBlockAsItemWithChance", at = @At(value = "RETURN"), cancellable = true)
public void onDropBlockAsItemWithChanceReturn(net.minecraft.world.World worldIn, BlockPos pos, IBlockState state, float chance, int fortune, CallbackInfo ci) {
if (!((IMixinWorld) worldIn).isFake()) {
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final IPhaseState<?> currentState = phaseTracker.getCurrentState();
final boolean shouldEnterBlockDropPhase = !phaseTracker.getCurrentContext().isCapturingBlockItemDrops() && !currentState.alreadyCapturingItemSpawns() && !currentState.isWorldGeneration();
if (shouldEnterBlockDropPhase) {
phaseTracker.completePhase(BlockPhase.State.BLOCK_DROP_ITEMS);
}
}
}
use of org.spongepowered.common.event.tracking.PhaseTracker in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method handleCollideBlockEvent.
public static boolean handleCollideBlockEvent(Block block, net.minecraft.world.World world, BlockPos pos, IBlockState state, net.minecraft.entity.Entity entity, Direction direction) {
if (pos.getY() <= 0) {
return false;
}
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(entity);
if (!(entity instanceof EntityPlayer)) {
IMixinEntity spongeEntity = (IMixinEntity) entity;
Optional<User> user = spongeEntity.getCreatorUser();
if (user.isPresent()) {
Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, user.get());
}
}
// TODO: Add target side support
CollideBlockEvent event = SpongeEventFactory.createCollideBlockEvent(Sponge.getCauseStackManager().getCurrentCause(), (BlockState) state, new Location<>((World) world, VecHelper.toVector3d(pos)), direction);
boolean cancelled = SpongeImpl.postEvent(event);
if (!cancelled) {
IMixinEntity spongeEntity = (IMixinEntity) entity;
if (!pos.equals(spongeEntity.getLastCollidedBlockPos())) {
final PhaseData peek = phaseTracker.getCurrentPhaseData();
final Optional<User> notifier = peek.context.getNotifier();
if (notifier.isPresent()) {
IMixinChunk spongeChunk = (IMixinChunk) world.getChunkFromBlockCoords(pos);
spongeChunk.addTrackedBlockPosition(block, pos, notifier.get(), PlayerTracker.Type.NOTIFIER);
}
}
}
return cancelled;
}
}
use of org.spongepowered.common.event.tracking.PhaseTracker in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method callChangeBlockEventModifyLiquidMix.
public static ChangeBlockEvent.Modify callChangeBlockEventModifyLiquidMix(net.minecraft.world.World worldIn, BlockPos pos, IBlockState state, @Nullable Object source) {
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final PhaseData data = phaseTracker.getCurrentPhaseData();
BlockState fromState = BlockUtil.fromNative(worldIn.getBlockState(pos));
BlockState toState = BlockUtil.fromNative(state);
User owner = data.context.getOwner().orElse(null);
User notifier = data.context.getNotifier().orElse(null);
if (source == null) {
// If source is null the source is the block itself
source = LocatableBlock.builder().state(fromState).world(((World) worldIn)).position(pos.getX(), pos.getY(), pos.getZ()).build();
}
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(source);
Sponge.getCauseStackManager().addContext(EventContextKeys.LIQUID_MIX, (World) worldIn);
if (owner != null) {
Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner);
}
if (notifier != null) {
Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier);
}
WorldProperties world = ((World) worldIn).getProperties();
Vector3i position = new Vector3i(pos.getX(), pos.getY(), pos.getZ());
Transaction<BlockSnapshot> transaction = new Transaction<>(BlockSnapshot.builder().blockState(fromState).world(world).position(position).build(), BlockSnapshot.builder().blockState(toState).world(world).position(position).build());
ChangeBlockEvent.Modify event = SpongeEventFactory.createChangeBlockEventModify(Sponge.getCauseStackManager().getCurrentCause(), Collections.singletonList(transaction));
SpongeImpl.postEvent(event);
return event;
}
}
use of org.spongepowered.common.event.tracking.PhaseTracker in project SpongeCommon by SpongePowered.
the class MixinEntityLivingBase method causeTrackDeathUpdate.
@Redirect(method = "onEntityUpdate", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/EntityLivingBase;onDeathUpdate()V"))
private void causeTrackDeathUpdate(EntityLivingBase entityLivingBase) {
if (!entityLivingBase.world.isRemote) {
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame();
PhaseContext<?> context = EntityPhase.State.DEATH_UPDATE.createPhaseContext().source(entityLivingBase).buildAndSwitch()) {
Sponge.getCauseStackManager().pushCause(entityLivingBase);
((IMixinEntityLivingBase) entityLivingBase).onSpongeDeathUpdate();
}
} else {
((IMixinEntityLivingBase) entityLivingBase).onSpongeDeathUpdate();
}
}
Aggregations