use of org.spongepowered.api.world.server.ServerWorld in project SpongeCommon by SpongePowered.
the class SpongeEntitySnapshot method restore.
@Override
public Optional<Entity> restore() {
if (this.entityReference != null) {
final Entity entity = this.entityReference.get();
if (entity != null) {
return Optional.of(entity);
}
}
final Optional<ServerWorld> world = Sponge.server().worldManager().world(this.worldKey);
if (!world.isPresent()) {
return Optional.empty();
}
if (this.uniqueId != null) {
final Optional<Entity> entity = world.get().entity(this.uniqueId);
if (entity.isPresent()) {
return entity;
}
}
try (final StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
frame.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.PLUGIN);
final Entity newEntity = world.get().createEntity(this.type(), this.position);
if (newEntity != null) {
final net.minecraft.world.entity.Entity nmsEntity = (net.minecraft.world.entity.Entity) newEntity;
if (this.compound != null) {
nmsEntity.load(this.compound);
}
final boolean spawnResult = world.get().spawnEntity((Entity) nmsEntity);
if (spawnResult) {
return Optional.of((Entity) nmsEntity);
}
}
}
return Optional.empty();
}
use of org.spongepowered.api.world.server.ServerWorld in project SpongeCommon by SpongePowered.
the class EntityUtil method performPostChangePlayerWorldLogic.
public static void performPostChangePlayerWorldLogic(final ServerPlayer player, final ServerLevel fromWorld, final ServerLevel originalToWorld, final ServerLevel toWorld, final boolean isPortal) {
// Sponge Start - Send any platform dimension data
((ServerPlayerBridge) player).bridge$sendDimensionData(player.connection.connection, toWorld.dimensionType(), toWorld.dimension());
// Sponge End
final LevelData worldinfo = toWorld.getLevelData();
// We send dimension change for portals before loading chunks
if (!isPortal) {
// Sponge Start - Allow the platform to handle how dimension changes are sent down
((ServerPlayerBridge) player).bridge$sendChangeDimension(toWorld.dimensionType(), toWorld.dimension(), BiomeManager.obfuscateSeed(toWorld.getSeed()), player.gameMode.getGameModeForPlayer(), player.gameMode.getPreviousGameModeForPlayer(), toWorld.isDebug(), toWorld.isFlat(), true);
}
// Sponge End
player.connection.send(new ClientboundChangeDifficultyPacket(worldinfo.getDifficulty(), worldinfo.isDifficultyLocked()));
final PlayerList playerlist = player.getServer().getPlayerList();
playerlist.sendPlayerPermissionLevel(player);
// Sponge Start - Have the platform handle removing the entity from the world. Move this to after the event call so
// that we do not remove the player from the world unless we really have teleported..
((PlatformServerLevelBridge) fromWorld).bridge$removeEntity(player, true);
((PlatformEntityBridge) player).bridge$revive();
// Sponge End
player.setLevel(toWorld);
toWorld.addDuringPortalTeleport(player);
if (isPortal) {
((ServerPlayerAccessor) player).invoker$triggerDimensionChangeTriggers(toWorld);
}
player.gameMode.setLevel(toWorld);
player.connection.send(new ClientboundPlayerAbilitiesPacket(player.abilities));
playerlist.sendLevelInfo(player, toWorld);
playerlist.sendAllPlayerInfo(player);
for (final MobEffectInstance effectinstance : player.getActiveEffects()) {
player.connection.send(new ClientboundUpdateMobEffectPacket(player.getId(), effectinstance));
}
if (isPortal) {
player.connection.send(new ClientboundLevelEventPacket(1032, BlockPos.ZERO, 0, false));
}
((ServerLevelBridge) fromWorld).bridge$getBossBarManager().onPlayerDisconnect(player);
((ServerLevelBridge) toWorld).bridge$getBossBarManager().onPlayerDisconnect(player);
((ServerPlayerAccessor) player).accessor$lastSentExp(-1);
((ServerPlayerAccessor) player).accessor$lastSentHealth(-1.0f);
((ServerPlayerAccessor) player).accessor$lastSentFood(-1);
if (!isPortal) {
player.connection.teleport(player.getX(), player.getY(), player.getZ(), player.yRot, player.xRot);
player.connection.resetPosition();
}
if (player.containerMenu != player.inventoryMenu) {
player.closeContainer();
}
// Sponge Start - Call event
Sponge.eventManager().post(SpongeEventFactory.createChangeEntityWorldEventPost(PhaseTracker.getCauseStackManager().currentCause(), (org.spongepowered.api.entity.Entity) player, (ServerWorld) fromWorld, (ServerWorld) originalToWorld, (ServerWorld) toWorld));
// Sponge End
}
use of org.spongepowered.api.world.server.ServerWorld in project SpongeCommon by SpongePowered.
the class CompassItemData method register.
static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(ItemStack.class).create(Keys.LODESTONE).get(stack -> {
if (CompassItem.isLodestoneCompass(stack)) {
final CompoundTag tag = stack.getOrCreateTag();
final Optional<ResourceKey<Level>> dimension = CompassItem.getLodestoneDimension(tag);
if (dimension.isPresent()) {
return ServerLocation.of((ServerWorld) SpongeCommon.server().getLevel(dimension.get()), VecHelper.toVector3d(NbtUtils.readBlockPos(tag.getCompound("LodestonePos"))));
}
}
return null;
}).set((stack, location) -> {
final CompoundTag tag = stack.getOrCreateTag();
tag.put("LodestonePos", NbtUtils.writeBlockPos(VecHelper.toBlockPos(location)));
Level.RESOURCE_KEY_CODEC.encodeStart(NbtOps.INSTANCE, ((net.minecraft.server.level.ServerLevel) location.world()).dimension()).resultOrPartial(SpongeCommon.logger()::error).ifPresent(dimension -> tag.put("LodestoneDimension", dimension));
tag.putBoolean("LodestoneTracked", true);
}).delete(stack -> {
final CompoundTag tag = stack.getTag();
if (tag != null) {
tag.remove("LodestoneDimension");
tag.remove("LodestonePos");
tag.remove("LodestoneTracked");
}
});
}
use of org.spongepowered.api.world.server.ServerWorld in project SpongeCommon by SpongePowered.
the class SpongePlaceholderContextBuilder method associatedObject.
@Override
@SuppressWarnings("unchecked")
public PlaceholderContext.Builder associatedObject(@Nullable final Object associatedObject) {
if (associatedObject == null) {
this.associatedObjectSupplier = null;
} else if (associatedObject instanceof Supplier) {
return this.associatedObject((Supplier<Object>) associatedObject);
} else if (associatedObject instanceof SystemSubject) {
this.associatedObjectSupplier = Sponge::systemSubject;
} else if (associatedObject instanceof Server) {
this.associatedObjectSupplier = Sponge::server;
} else if (associatedObject instanceof Player) {
return this.associatedObject((Player) associatedObject);
} else if (associatedObject instanceof ServerWorld) {
final ResourceKey key = ((ServerWorld) associatedObject).key();
this.associatedObjectSupplier = () -> SpongeCommon.game().server().worldManager().world(key).orElse(null);
} else if (associatedObject instanceof Entity) {
final Entity entity = ((Entity) associatedObject);
final ResourceKey key = entity.serverLocation().world().key();
final UUID entityUuid = ((Entity) associatedObject).uniqueId();
this.associatedObjectSupplier = () -> SpongeCommon.game().server().worldManager().world(key).flatMap(x -> x.entity(entityUuid)).orElse(null);
} else {
// We create a weak reference here so we don't hold on to game objects.
final WeakReference<Object> objectWeakReference = new WeakReference<>(associatedObject);
this.associatedObjectSupplier = objectWeakReference::get;
}
return this;
}
use of org.spongepowered.api.world.server.ServerWorld in project SpongeCommon by SpongePowered.
the class PlaceBlockPacketState method appendNotifierToBlockEvent.
@Override
public void appendNotifierToBlockEvent(final BasicPacketContext context, final TrackedWorldBridge mixinWorldServer, final BlockPos pos, final TrackableBlockEventDataBridge blockEvent) {
final Player player = PhaseTracker.getCauseStackManager().currentCause().first(Player.class).get();
final BlockState state = ((ServerWorld) mixinWorldServer).block(pos.getX(), pos.getY(), pos.getZ());
final LocatableBlock locatable = new SpongeLocatableBlockBuilder().world((ServerWorld) mixinWorldServer).position(pos.getX(), pos.getY(), pos.getZ()).state(state).build();
blockEvent.bridge$setTickingLocatable(locatable);
blockEvent.bridge$setSourceUserUUID(player.uniqueId());
}
Aggregations