use of org.spongepowered.common.event.tracking.PhaseData in project SpongeCommon by SpongePowered.
the class MixinWorldServer method updateBlocks.
/**
* @author blood - July 1st, 2016
* @author gabizou - July 1st, 2016 - Update to 1.10 and cause tracking
*
* @reason Added chunk and block tick optimizations, timings, cause tracking, and pre-construction events.
*/
@SuppressWarnings("unchecked")
@Override
@Overwrite
protected void updateBlocks() {
this.playerCheckLight();
if (this.worldInfo.getTerrainType() == WorldType.DEBUG_ALL_BLOCK_STATES) {
Iterator<net.minecraft.world.chunk.Chunk> iterator1 = this.playerChunkMap.getChunkIterator();
while (iterator1.hasNext()) {
iterator1.next().onTick(false);
}
// Sponge: Add return
return;
}
// else // Sponge - Remove unnecessary else
// { //
int i = this.shadow$getGameRules().getInt("randomTickSpeed");
boolean flag = this.isRaining();
boolean flag1 = this.isThundering();
// this.profiler.startSection("pollingChunks"); // Sponge - Don't use the profiler
// Sponge - get the cause tracker
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
// Sponge: Use SpongeImplHooks for Forge
for (Iterator<net.minecraft.world.chunk.Chunk> iterator = // this.profiler.endSection()) // Sponge - don't use the profiler
SpongeImplHooks.getChunkIterator((WorldServer) (Object) this); // this.profiler.endSection()) // Sponge - don't use the profiler
iterator.hasNext(); ) {
// this.profiler.startSection("getChunk"); // Sponge - Don't use the profiler
net.minecraft.world.chunk.Chunk chunk = iterator.next();
final net.minecraft.world.World world = chunk.getWorld();
int j = chunk.x * 16;
int k = chunk.z * 16;
// this.profiler.endStartSection("checkNextLight"); // Sponge - Don't use the profiler
// Sponge - Timings
this.timings.updateBlocksCheckNextLight.startTiming();
chunk.enqueueRelightChecks();
// Sponge - Timings
this.timings.updateBlocksCheckNextLight.stopTiming();
// this.profiler.endStartSection("tickChunk"); // Sponge - Don't use the profiler
// Sponge - Timings
this.timings.updateBlocksChunkTick.startTiming();
chunk.onTick(false);
// Sponge - Timings
this.timings.updateBlocksChunkTick.stopTiming();
// Sponge start - if surrounding neighbors are not loaded, skip
if (!((IMixinChunk) chunk).areNeighborsLoaded()) {
continue;
}
// Sponge end
// this.profiler.endStartSection("thunder"); // Sponge - Don't use the profiler
// Sponge start
this.timings.updateBlocksThunder.startTiming();
// if (this.provider.canDoLightning(chunk) && flag && flag1 && this.rand.nextInt(100000) == 0) // Sponge - Add SpongeImplHooks for forge
if (this.weatherThunderEnabled && SpongeImplHooks.canDoLightning(this.provider, chunk) && flag && flag1 && this.rand.nextInt(100000) == 0) {
try (final PhaseContext<?> context = TickPhase.Tick.WEATHER.createPhaseContext().source(this).buildAndSwitch()) {
// Sponge end
this.updateLCG = this.updateLCG * 3 + 1013904223;
int l = this.updateLCG >> 2;
BlockPos blockpos = this.adjustPosToNearbyEntity(new BlockPos(j + (l & 15), 0, k + (l >> 8 & 15)));
if (this.isRainingAt(blockpos)) {
DifficultyInstance difficultyinstance = this.getDifficultyForLocation(blockpos);
// Sponge - create a transform to be used for events
final Transform<org.spongepowered.api.world.World> transform = new Transform<>(this, VecHelper.toVector3d(blockpos).toDouble());
if (world.getGameRules().getBoolean("doMobSpawning") && this.rand.nextDouble() < (double) difficultyinstance.getAdditionalDifficulty() * 0.01D) {
// Sponge Start - Throw construction events
try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(this.getWeather());
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.WEATHER);
ConstructEntityEvent.Pre constructEntityEvent = SpongeEventFactory.createConstructEntityEventPre(Sponge.getCauseStackManager().getCurrentCause(), EntityTypes.HORSE, transform);
SpongeImpl.postEvent(constructEntityEvent);
if (!constructEntityEvent.isCancelled()) {
// Sponge End
EntitySkeletonHorse entityhorse = new EntitySkeletonHorse((WorldServer) (Object) this);
entityhorse.setTrap(true);
entityhorse.setGrowingAge(0);
entityhorse.setPosition(blockpos.getX(), blockpos.getY(), blockpos.getZ());
this.spawnEntity(entityhorse);
// Sponge Start - Throw a construct event for the lightning
}
ConstructEntityEvent.Pre lightning = SpongeEventFactory.createConstructEntityEventPre(Sponge.getCauseStackManager().getCurrentCause(), EntityTypes.LIGHTNING, transform);
SpongeImpl.postEvent(lightning);
if (!lightning.isCancelled()) {
LightningEvent.Pre lightningPre = SpongeEventFactory.createLightningEventPre(frame.getCurrentCause());
if (!SpongeImpl.postEvent(lightningPre)) {
// Sponge End
this.addWeatherEffect(new EntityLightningBolt(world, (double) blockpos.getX(), (double) blockpos.getY(), (double) blockpos.getZ(), true));
}
}
// Sponge - Brackets.
}
} else {
// Sponge start - Throw construction event for lightningbolts
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(this.getWeather());
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.WEATHER);
ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(Sponge.getCauseStackManager().getCurrentCause(), EntityTypes.LIGHTNING, transform);
SpongeImpl.postEvent(event);
if (!event.isCancelled()) {
LightningEvent.Pre lightningPre = SpongeEventFactory.createLightningEventPre(frame.getCurrentCause());
if (!SpongeImpl.postEvent(lightningPre)) {
// Sponge End
this.addWeatherEffect(new EntityLightningBolt(world, (double) blockpos.getX(), (double) blockpos.getY(), (double) blockpos.getZ(), true));
}
}
// Sponge - Brackets.
}
}
}
}
// Sponge - brackets
// Sponge End
}
// Sponge - Stop thunder timing
this.timings.updateBlocksThunder.stopTiming();
// Sponge - Start thunder timing
this.timings.updateBlocksIceAndSnow.startTiming();
// if (this.rand.nextInt(16) == 0) // Sponge - Rewrite to use our boolean, and forge hook
if (this.weatherIceAndSnowEnabled && SpongeImplHooks.canDoRainSnowIce(this.provider, chunk) && this.rand.nextInt(16) == 0) {
// Sponge Start - Enter weather phase for snow and ice and flooding.
try (final PhaseContext<?> context = TickPhase.Tick.WEATHER.createPhaseContext().source(this).buildAndSwitch()) {
// Sponge End
this.updateLCG = this.updateLCG * 3 + 1013904223;
int j2 = this.updateLCG >> 2;
BlockPos blockpos1 = this.getPrecipitationHeight(new BlockPos(j + (j2 & 15), 0, k + (j2 >> 8 & 15)));
BlockPos blockpos2 = blockpos1.down();
if (this.canBlockFreezeNoWater(blockpos2)) {
this.setBlockState(blockpos2, Blocks.ICE.getDefaultState());
}
if (flag && this.canSnowAt(blockpos1, true)) {
this.setBlockState(blockpos1, Blocks.SNOW_LAYER.getDefaultState());
}
if (flag && this.getBiome(blockpos2).canRain()) {
this.getBlockState(blockpos2).getBlock().fillWithRain((WorldServer) (Object) this, blockpos2);
}
}
// Sponge - brackets
}
// Sponge - Stop ice and snow timing
this.timings.updateBlocksIceAndSnow.stopTiming();
// Sponge - Start random block tick timing
this.timings.updateBlocksRandomTick.startTiming();
if (i > 0) {
for (ExtendedBlockStorage extendedblockstorage : chunk.getBlockStorageArray()) {
if (extendedblockstorage != net.minecraft.world.chunk.Chunk.NULL_BLOCK_STORAGE && extendedblockstorage.needsRandomTick()) {
for (int i1 = 0; i1 < i; ++i1) {
this.updateLCG = this.updateLCG * 3 + 1013904223;
int j1 = this.updateLCG >> 2;
int k1 = j1 & 15;
int l1 = j1 >> 8 & 15;
int i2 = j1 >> 16 & 15;
IBlockState iblockstate = extendedblockstorage.get(k1, i2, l1);
Block block = iblockstate.getBlock();
if (block.getTickRandomly()) {
// Sponge start - capture random tick
// Remove the random tick for cause tracking
// block.randomTick(this, new BlockPos(k1 + j, i2 + extendedblockstorage.getYLocation(), l1 + k), iblockstate, this.rand);
BlockPos pos = new BlockPos(k1 + j, i2 + extendedblockstorage.getYLocation(), l1 + k);
IMixinBlock spongeBlock = (IMixinBlock) block;
spongeBlock.getTimingsHandler().startTiming();
final PhaseData currentTuple = phaseTracker.getCurrentPhaseData();
final IPhaseState phaseState = currentTuple.state;
if (phaseState.alreadyCapturingBlockTicks(currentTuple.context)) {
block.randomTick(world, pos, iblockstate, this.rand);
} else {
TrackingUtil.randomTickBlock(phaseTracker, this, block, pos, iblockstate, this.rand);
}
spongeBlock.getTimingsHandler().stopTiming();
// Sponge end
}
// this.profiler.endSection(); // Sponge - Don't use the profiler
}
}
}
}
}
// Sponge - Stop random block timing
this.timings.updateBlocksRandomTick.stopTiming();
// this.profiler.endSection(); // Sponge - Don't use the profiler
// } // Sponge- Remove unecessary else
}
use of org.spongepowered.common.event.tracking.PhaseData in project SpongeCommon by SpongePowered.
the class MixinWorldServer method setBlock.
@Override
public boolean setBlock(int x, int y, int z, BlockState blockState, BlockChangeFlag flag) {
checkBlockBounds(x, y, z);
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final PhaseData peek = phaseTracker.getCurrentPhaseData();
boolean isWorldGen = peek.state.isWorldGeneration();
boolean handlesOwnCompletion = peek.state.handlesOwnStateCompletion();
if (!isWorldGen) {
checkArgument(flag != null, "BlockChangeFlag cannot be null!");
}
try (PhaseContext<?> context = isWorldGen || handlesOwnCompletion ? null : PluginPhase.State.BLOCK_WORKER.createPhaseContext().buildAndSwitch()) {
return setBlockState(new BlockPos(x, y, z), (IBlockState) blockState, flag);
}
}
use of org.spongepowered.common.event.tracking.PhaseData in project SpongeForge by SpongePowered.
the class SpongeForgeEventFactory method createChangeBlockEventPre.
public static ChangeBlockEvent.Pre createChangeBlockEventPre(BlockEvent.BreakEvent forgeEvent) {
final net.minecraft.world.World world = forgeEvent.getWorld();
if (world.isRemote) {
return null;
}
final BlockPos pos = forgeEvent.getPos();
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final PhaseData data = phaseTracker.getCurrentPhaseData();
User owner = data.context.getOwner().orElse(null);
User notifier = data.context.getNotifier().orElse(null);
EntityPlayer player = forgeEvent.getPlayer();
if (SpongeImplHooks.isFakePlayer(player)) {
Sponge.getCauseStackManager().addContext(EventContextKeys.FAKE_PLAYER, EntityUtil.toPlayer(player));
} else {
Sponge.getCauseStackManager().pushCause(player);
}
if (owner != null) {
Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner);
if (Sponge.getCauseStackManager().getCurrentCause() == null) {
Sponge.getCauseStackManager().pushCause(owner);
}
} else {
Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, (User) player);
if (Sponge.getCauseStackManager().getCurrentCause() == null) {
Sponge.getCauseStackManager().pushCause(player);
}
}
if (notifier != null) {
Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier);
}
Sponge.getCauseStackManager().addContext(EventContextKeys.PLAYER_BREAK, (World) world);
return SpongeEventFactory.createChangeBlockEventPre(Sponge.getCauseStackManager().getCurrentCause(), ImmutableList.of(new Location<>((World) world, pos.getX(), pos.getY(), pos.getZ())));
}
use of org.spongepowered.common.event.tracking.PhaseData in project SpongeForge by SpongePowered.
the class SpongeForgeEventFactory method createChangeBlockEventBreak.
public static ChangeBlockEvent.Break createChangeBlockEventBreak(BlockEvent.BreakEvent forgeEvent) {
final BlockPos pos = forgeEvent.getPos();
final net.minecraft.world.World world = forgeEvent.getWorld();
if (world.isRemote) {
return null;
}
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final PhaseData data = phaseTracker.getCurrentPhaseData();
BlockSnapshot originalSnapshot = ((World) forgeEvent.getWorld()).createSnapshot(pos.getX(), pos.getY(), pos.getZ());
BlockSnapshot finalSnapshot = BlockTypes.AIR.getDefaultState().snapshotFor(new Location<>((World) world, VecHelper.toVector3d(pos)));
ImmutableList<Transaction<BlockSnapshot>> blockSnapshots = new ImmutableList.Builder<Transaction<BlockSnapshot>>().add(new Transaction<>(originalSnapshot, finalSnapshot)).build();
User owner = data.context.getOwner().orElse(null);
User notifier = data.context.getNotifier().orElse(null);
EntityPlayer player = forgeEvent.getPlayer();
if (SpongeImplHooks.isFakePlayer(player)) {
Sponge.getCauseStackManager().addContext(EventContextKeys.FAKE_PLAYER, EntityUtil.toPlayer(player));
} else if (Sponge.getCauseStackManager().getCurrentCause() == null) {
Sponge.getCauseStackManager().pushCause(player);
}
if (owner != null) {
Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner);
if (Sponge.getCauseStackManager().getCurrentCause() == null) {
Sponge.getCauseStackManager().pushCause(owner);
}
} else {
Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, (User) player);
if (Sponge.getCauseStackManager().getCurrentCause() == null) {
Sponge.getCauseStackManager().pushCause(player);
}
}
if (notifier != null) {
Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier);
}
Sponge.getCauseStackManager().addContext(EventContextKeys.PLAYER_BREAK, (World) world);
return SpongeEventFactory.createChangeBlockEventBreak(Sponge.getCauseStackManager().getCurrentCause(), blockSnapshots);
}
use of org.spongepowered.common.event.tracking.PhaseData in project SpongeForge by SpongePowered.
the class MixinItemShears method itemInteractionForEntity.
/**
* @author gabizou - June 21st, 2016
* @reason Rewrites the forge handling of this to properly handle
* when sheared drops are captured by whatever current phase the
* {@link PhaseTracker} is in.
*
* Returns true if the item can be used on the given entity, e.g. shears on sheep.
*/
@Overwrite
@Override
public boolean itemInteractionForEntity(ItemStack itemstack, EntityPlayer player, EntityLivingBase entity, EnumHand hand) {
if (entity.world.isRemote) {
return false;
}
if (entity instanceof IShearable) {
IShearable target = (IShearable) entity;
BlockPos pos = new BlockPos(entity.posX, entity.posY, entity.posZ);
if (target.isShearable(itemstack, entity.world, pos)) {
List<ItemStack> drops = target.onSheared(itemstack, entity.world, pos, EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, itemstack));
// Sponge Start - Handle drops according to the current phase
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final PhaseData currentData = phaseTracker.getCurrentPhaseData();
final IPhaseState<?> currentState = currentData.state;
final PhaseContext<?> phaseContext = currentData.context;
final Random random = EntityUtil.fromNative(entity).getRandom();
final IMixinEntity mixinEntity = EntityUtil.toMixin(entity);
final double posX = entity.posX;
final double posY = entity.posY + 1.0F;
final double posZ = entity.posZ;
final Vector3d position = new Vector3d(posX, posY, posZ);
// Now the real fun begins.
for (ItemStack drop : drops) {
final ItemStack item;
if (!drop.isEmpty()) {
try (final CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
// FIRST we want to throw the DropItemEvent.PRE
final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(drop);
final List<ItemStackSnapshot> original = new ArrayList<>();
original.add(snapshot);
Sponge.getCauseStackManager().pushCause(entity);
final DropItemEvent.Pre dropEvent = SpongeEventFactory.createDropItemEventPre(Sponge.getCauseStackManager().getCurrentCause(), ImmutableList.of(snapshot), original);
if (dropEvent.isCancelled()) {
continue;
}
// SECOND throw the ConstructEntityEvent
Transform<World> suggested = new Transform<>(mixinEntity.getWorld(), position);
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM);
ConstructEntityEvent.Pre event = SpongeEventFactory.createConstructEntityEventPre(Sponge.getCauseStackManager().getCurrentCause(), EntityTypes.ITEM, suggested);
SpongeImpl.postEvent(event);
item = event.isCancelled() ? null : ItemStackUtil.fromSnapshotToNative(dropEvent.getDroppedItems().get(0));
}
} else {
continue;
}
if (item == null) {
continue;
}
if (!item.isEmpty()) {
if (!currentState.ignoresItemPreMerging() && SpongeImpl.getGlobalConfig().getConfig().getOptimizations().doDropsPreMergeItemDrops()) {
if (currentState.tracksEntitySpecificDrops()) {
final Multimap<UUID, ItemDropData> multimap = phaseContext.getCapturedEntityDropSupplier().get();
final Collection<ItemDropData> itemStacks = multimap.get(entity.getUniqueID());
SpongeImplHooks.addItemStackToListForSpawning(itemStacks, ItemDropData.item(item).motion(new Vector3d((random.nextFloat() - random.nextFloat()) * 0.1F, random.nextFloat() * 0.05F, (random.nextFloat() - random.nextFloat()) * 0.1F)).position(new Vector3d(posX, posY, posZ)).build());
continue;
}
final List<ItemDropData> itemStacks = phaseContext.getCapturedItemStackSupplier().get();
SpongeImplHooks.addItemStackToListForSpawning(itemStacks, ItemDropData.item(item).position(new Vector3d(posX, posY, posZ)).motion(new Vector3d((random.nextFloat() - random.nextFloat()) * 0.1F, random.nextFloat() * 0.05F, (random.nextFloat() - random.nextFloat()) * 0.1F)).build());
continue;
}
EntityItem entityitem = new EntityItem(entity.world, posX, posY, posZ, item);
entityitem.setDefaultPickupDelay();
entityitem.motionY += random.nextFloat() * 0.05F;
entityitem.motionX += (random.nextFloat() - random.nextFloat()) * 0.1F;
entityitem.motionZ += (random.nextFloat() - random.nextFloat()) * 0.1F;
// FIFTH - Capture the entity maybe?
if (currentState.doesCaptureEntityDrops()) {
if (currentState.tracksEntitySpecificDrops()) {
// We are capturing per entity drop
phaseContext.getCapturedEntityItemDropSupplier().get().put(entity.getUniqueID(), entityitem);
} else {
// We are adding to a general list - usually for EntityPhase.State.DEATH
phaseContext.getCapturedItemsSupplier().get().add(entityitem);
}
// Return the item, even if it wasn't spawned in the world.
continue;
}
// FINALLY - Spawn the entity in the world if all else didn't fail
entity.world.spawnEntity(entityitem);
}
}
// Sponge End
itemstack.damageItem(1, entity);
}
return true;
}
return false;
}
Aggregations