use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.
the class SpongeCommandCauseFactory method create.
@Override
@NonNull
public CommandCause create() {
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
final Cause cause = frame.currentCause();
final CommandSource iCommandSource = cause.first(CommandSource.class).orElseGet(() -> SpongeCommon.game().systemSubject());
final CommandSourceStack commandSource;
if (iCommandSource instanceof CommandSourceProviderBridge) {
// We know about this one so we can create it using the factory method on the source.
commandSource = ((CommandSourceProviderBridge) iCommandSource).bridge$getCommandSource(cause);
} else {
// try to create a command cause from the given ICommandSource, but as Mojang did not see fit to
// put any identifying characteristics on the object, we have to go it alone...
final EventContext context = cause.context();
@Nullable final Locatable locatable = iCommandSource instanceof Locatable ? (Locatable) iCommandSource : null;
final Component displayName;
if (iCommandSource instanceof Entity) {
displayName = ((Entity) iCommandSource).get(Keys.DISPLAY_NAME).map(SpongeAdventure::asVanilla).orElseGet(() -> new TextComponent(iCommandSource instanceof Nameable ? ((Nameable) iCommandSource).name() : iCommandSource.getClass().getSimpleName()));
} else {
displayName = new TextComponent(iCommandSource instanceof Nameable ? ((Nameable) iCommandSource).name() : iCommandSource.getClass().getSimpleName());
}
final String name = displayName.getString();
commandSource = new CommandSourceStack(iCommandSource, context.get(EventContextKeys.LOCATION).map(x -> VecHelper.toVanillaVector3d(x.position())).orElseGet(() -> locatable == null ? Vec3.ZERO : VecHelper.toVanillaVector3d(locatable.location().position())), context.get(EventContextKeys.ROTATION).map(rot -> new Vec2((float) rot.x(), (float) rot.y())).orElse(Vec2.ZERO), context.get(EventContextKeys.LOCATION).map(x -> (ServerLevel) x.world()).orElseGet(() -> locatable == null ? SpongeCommon.server().getLevel(Level.OVERWORLD) : (ServerLevel) locatable.serverLocation().world()), 4, name, displayName, SpongeCommon.server(), iCommandSource instanceof Entity ? (net.minecraft.world.entity.Entity) iCommandSource : null);
}
// We don't want the command source to have altered the cause here (unless there is the special case of the
// server), so we reset it back to what it was (in the ctor of CommandSource, it will add the current source
// to the cause - that's for if the source is created elsewhere, not here)
((CommandSourceStackBridge) commandSource).bridge$setCause(frame.currentCause());
return (CommandCause) commandSource;
}
}
use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.
the class PaintingData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(Painting.class).create(Keys.ART_TYPE).get(h -> (ArtType) h.motive).setAnd((h, v) -> {
if (!h.level.isClientSide) {
final Motive oldArt = h.motive;
h.motive = (Motive) v;
((HangingEntityAccessor) h).invoker$setDirection(h.getDirection());
if (!h.survives()) {
h.motive = oldArt;
((HangingEntityAccessor) h).invoker$setDirection(h.getDirection());
return false;
}
final ChunkMapAccessor chunkManager = (ChunkMapAccessor) ((ServerLevel) h.level).getChunkSource().chunkMap;
final ChunkMap_TrackedEntityAccessor paintingTracker = chunkManager.accessor$entityMap().get(h.getId());
if (paintingTracker == null) {
return true;
}
final List<ServerPlayer> players = new ArrayList<>();
for (final ServerPlayer player : paintingTracker.accessor$seenBy()) {
final ClientboundRemoveEntitiesPacket packet = new ClientboundRemoveEntitiesPacket(h.getId());
player.connection.send(packet);
players.add(player);
}
for (final ServerPlayer player : players) {
SpongeCommon.serverScheduler().submit(Task.builder().plugin(Launch.instance().commonPlugin()).delay(new SpongeTicks(SpongeGameConfigs.getForWorld(h.level).get().entity.painting.respawnDelay)).execute(() -> {
final ClientboundAddPaintingPacket packet = new ClientboundAddPaintingPacket(h);
player.connection.send(packet);
}).build());
}
return true;
}
return true;
});
}
use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.
the class DispenserSourceLogic method launch.
@SuppressWarnings("unchecked")
private <P extends Projectile> Optional<P> launch(final DispenserBlockEntity dispenser, final EntityType<P> projectileType, final Item item) {
final DispenseItemBehavior behavior = DispenserBlockAccessor.accessor$DISPENSER_REGISTRY().get(item);
final ServerLevel world = (ServerLevel) dispenser.getLevel();
behavior.dispense(new BlockSourceImpl(world, dispenser.getBlockPos()), new ItemStack(item));
final List<Entity> entities = world.getEntities((net.minecraft.world.entity.EntityType<?>) projectileType, entity -> true);
if (entities.isEmpty()) {
return Optional.empty();
}
// Hack - get the projectile that was spawned from dispense()
return Optional.of((P) entities.get(entities.size() - 1));
}
use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.
the class TileEntityPipeline method kickOff.
public static Builder kickOff(final ServerLevel world, final BlockPos pos) {
final WeakReference<ServerLevel> worldRef = new WeakReference<>(world);
final LevelChunk chunk = world.getChunkAt(pos);
final WeakReference<LevelChunk> chunkRef = new WeakReference<>(chunk);
final WeakReference<LevelChunkSection> sectionRef = new WeakReference<>(chunk.getSections()[pos.getY() >> 4]);
final Supplier<ServerLevel> worldSupplier = () -> Objects.requireNonNull(worldRef.get(), "ServerWorld de-referenced");
final Supplier<LevelChunk> chunkSupplier = () -> Objects.requireNonNull(chunkRef.get(), "Chunk de-referenced");
final Supplier<LevelChunkSection> chunkSectionSupplier = () -> Objects.requireNonNull(sectionRef.get(), "ChunkSection de-referenced");
return TileEntityPipeline.builder().chunk(chunkSupplier).world(worldSupplier).chunkSection(chunkSectionSupplier);
}
use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.
the class EntityPerformingDropsTransaction method captureState.
@Override
protected void captureState() {
super.captureState();
final Entity entity = this.destroyingEntity;
this.worldSupplier = VolumeStreamUtils.createWeaklyReferencedSupplier((ServerLevel) entity.level, "ServerLevel");
final CompoundTag tag = new CompoundTag();
entity.saveWithoutId(tag);
this.entityTag = tag;
@Nullable final DamageSource lastAttacker;
if (entity instanceof LivingEntity) {
final CombatEntry entry = ((CombatTrackerAccessor) ((LivingEntity) entity).getCombatTracker()).invoker$getMostSignificantFall();
if (entry != null) {
lastAttacker = ((CombatEntryAccessor) entry).accessor$source();
} else {
lastAttacker = null;
}
} else {
lastAttacker = null;
}
final WeakReference<@Nullable DamageSource> ref = new WeakReference<>(lastAttacker);
this.lastAttacker = () -> {
@Nullable final DamageSource damageSource = ref.get();
// Yes, I know, we're effectively
if (damageSource == null) {
return Optional.empty();
}
return Optional.of(damageSource);
};
}
Aggregations