use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.
the class SpongeWorldManager method loadWorld.
@Override
public CompletableFuture<org.spongepowered.api.world.server.ServerWorld> loadWorld(final WorldTemplate template) {
final ResourceKey key = Objects.requireNonNull(template, "template").key();
final net.minecraft.resources.ResourceKey<Level> registryKey = SpongeWorldManager.createRegistryKey(key);
if (Level.OVERWORLD.equals(registryKey)) {
FutureUtil.completedWithException(new IllegalArgumentException("The default world cannot be told to load!"));
}
final ServerLevel serverWorld = this.worlds.get(registryKey);
if (serverWorld != null) {
return CompletableFuture.completedFuture((org.spongepowered.api.world.server.ServerWorld) serverWorld);
}
this.saveTemplate(template);
return this.loadWorld0(registryKey, ((SpongeWorldTemplate) template).asDimension(), ((WorldGenSettings) template.generationConfig()));
}
use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.
the class SpongeWorldManager method loadWorld.
@Override
public CompletableFuture<org.spongepowered.api.world.server.ServerWorld> loadWorld(final ResourceKey key) {
final net.minecraft.resources.ResourceKey<Level> registryKey = SpongeWorldManager.createRegistryKey(Objects.requireNonNull(key, "key"));
if (Level.OVERWORLD.equals(registryKey)) {
FutureUtil.completedWithException(new IllegalArgumentException("The default world cannot be told to load!"));
}
final ServerLevel world = this.worlds.get(registryKey);
if (world != null) {
return CompletableFuture.completedFuture((org.spongepowered.api.world.server.ServerWorld) world);
}
return this.loadTemplate(key).thenCompose(r -> {
WorldTemplate loadedTemplate = r.orElse(null);
if (loadedTemplate == null) {
final LevelStem scratch = BootstrapProperties.worldGenSettings.dimensions().get(net.minecraft.resources.ResourceKey.create(net.minecraft.core.Registry.LEVEL_STEM_REGISTRY, (ResourceLocation) (Object) key));
if (scratch != null) {
((ResourceKeyBridge) (Object) scratch).bridge$setKey(key);
loadedTemplate = new SpongeWorldTemplate(scratch);
}
if (loadedTemplate == null) {
return FutureUtil.completedWithException(new IOException(String.format("Failed to load a template for '%s'!", key)));
}
this.saveTemplate(loadedTemplate);
}
return this.loadWorld0(registryKey, ((SpongeWorldTemplate) loadedTemplate).asDimension(), ((WorldGenSettings) loadedTemplate.generationConfig()));
});
}
use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.
the class SpongeWorldManager method loadLevel.
public void loadLevel() {
final PrimaryLevelData defaultLevelData = (PrimaryLevelData) this.server.getWorldData();
final WorldGenSettings defaultGenerationSettings = defaultLevelData.worldGenSettings();
final LevelSettings defaultLevelSettings = ((PrimaryLevelDataAccessor) defaultLevelData).accessor$settings();
final MappedRegistry<LevelStem> templates = defaultGenerationSettings.dimensions();
final boolean multiworldEnabled = this.server.isSingleplayer() || this.server.isNetherEnabled();
if (!multiworldEnabled) {
SpongeCommon.logger().warn("The option 'allow-nether' has been set to 'false' in the server.properties. " + "Multi-World support has been disabled and no worlds besides the default world will be loaded.");
}
for (final RegistryEntry<LevelStem> entry : ((Registry<LevelStem>) (Object) templates).streamEntries().collect(Collectors.toList())) {
final ResourceKey worldKey = entry.key();
final LevelStem template = entry.value();
final LevelStemBridge templateBridge = (LevelStemBridge) (Object) template;
((ResourceKeyBridge) templateBridge).bridge$setKey(worldKey);
final boolean isDefaultWorld = this.isDefaultWorld(worldKey);
if (!isDefaultWorld && !multiworldEnabled) {
continue;
}
final WorldType worldType = (WorldType) template.type();
final ResourceKey worldTypeKey = RegistryTypes.WORLD_TYPE.get().valueKey((WorldType) template.type());
MinecraftServerAccessor.accessor$LOGGER().info("Loading world '{}' ({})", worldKey, worldTypeKey);
if (!isDefaultWorld && !templateBridge.bridge$loadOnStartup()) {
SpongeCommon.logger().warn("World '{}' has been disabled from loading at startup. Skipping...", worldKey);
continue;
}
final String directoryName = this.getDirectoryName(worldKey);
final boolean isVanillaSubLevel = this.isVanillaSubWorld(directoryName);
final LevelStorageSource.LevelStorageAccess storageSource;
if (isDefaultWorld) {
storageSource = ((MinecraftServerAccessor) this.server).accessor$storageSource();
} else {
try {
if (isVanillaSubLevel) {
storageSource = LevelStorageSource.createDefault(this.defaultWorldDirectory).createAccess(directoryName);
} else {
storageSource = LevelStorageSource.createDefault(this.customWorldsDirectory).createAccess(worldKey.namespace() + File.separator + worldKey.value());
}
} catch (final IOException e) {
throw new RuntimeException(String.format("Failed to create level data for world '%s'!", worldKey), e);
}
}
PrimaryLevelData levelData;
final boolean isDebugGeneration;
if (isDefaultWorld) {
levelData = defaultLevelData;
isDebugGeneration = defaultGenerationSettings.isDebug();
} else {
levelData = (PrimaryLevelData) storageSource.getDataTag((DynamicOps<Tag>) BootstrapProperties.worldSettingsAdapter, defaultLevelSettings.getDataPackConfig());
if (levelData == null) {
final LevelSettings levelSettings;
final WorldGenSettings generationSettings;
if (this.server.isDemo()) {
levelSettings = MinecraftServer.DEMO_SETTINGS;
generationSettings = WorldGenSettings.demoSettings(BootstrapProperties.registries);
} else {
levelSettings = new LevelSettings(directoryName, (GameType) (Object) BootstrapProperties.gameMode.get(Sponge.game()), templateBridge.bridge$hardcore().orElse(BootstrapProperties.hardcore), (Difficulty) (Object) BootstrapProperties.difficulty.get(Sponge.game()), templateBridge.bridge$commands().orElse(BootstrapProperties.commands), new GameRules(), defaultLevelData.getDataPackConfig());
generationSettings = ((WorldGenSettingsBridge) defaultLevelData.worldGenSettings()).bridge$copy();
}
isDebugGeneration = generationSettings.isDebug();
((DimensionGeneratorSettingsAccessor) generationSettings).accessor$dimensions(new MappedRegistry<>(net.minecraft.core.Registry.LEVEL_STEM_REGISTRY, Lifecycle.stable()));
levelData = new PrimaryLevelData(levelSettings, generationSettings, Lifecycle.stable());
} else {
isDebugGeneration = levelData.worldGenSettings().isDebug();
}
}
((PrimaryLevelDataBridge) levelData).bridge$populateFromDimension(template);
final InheritableConfigHandle<WorldConfig> configAdapter = SpongeGameConfigs.createWorld(worldTypeKey, worldKey);
((PrimaryLevelDataBridge) levelData).bridge$configAdapter(configAdapter);
levelData.setModdedInfo(this.server.getServerModName(), this.server.getModdedStatus().isPresent());
final long seed = BiomeManager.obfuscateSeed(levelData.worldGenSettings().seed());
final net.minecraft.resources.ResourceKey<Level> registryKey = SpongeWorldManager.createRegistryKey(worldKey);
final ChunkProgressListener chunkStatusListener = ((MinecraftServerAccessor) this.server).accessor$progressListenerFactory().create(11);
final List<CustomSpawner> spawners;
if (isDefaultWorld) {
spawners = ImmutableList.of(new PhantomSpawner(), new PatrolSpawner(), new CatSpawner(), new VillageSiege(), new WanderingTraderSpawner(levelData));
} else {
spawners = ImmutableList.of();
}
final ServerLevel world = new ServerLevel(this.server, ((MinecraftServerAccessor) this.server).accessor$executor(), storageSource, levelData, registryKey, (DimensionType) worldType, chunkStatusListener, template.generator(), isDebugGeneration, seed, spawners, true);
// Ensure that the world border is registered.
world.getWorldBorder().applySettings(levelData.getWorldBorder());
this.worlds.put(registryKey, world);
this.prepareWorld(world, isDebugGeneration);
}
((MinecraftServerAccessor) this.server).invoker$forceDifficulty();
for (final Map.Entry<net.minecraft.resources.ResourceKey<Level>, ServerLevel> entry : this.worlds.entrySet()) {
try {
this.postWorldLoad(entry.getValue(), true).get();
} catch (final InterruptedException | ExecutionException e) {
throw new IllegalStateException(e);
}
}
((SpongeUserManager) Sponge.server().userManager()).init();
((SpongeServer) SpongeCommon.server()).getPlayerDataManager().load();
}
use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.
the class CommandSourceStackMixin method impl$updateCauseOnWithWorld.
/*
* A note on why we're doing this with the cause manually.
*
* When the object is first constructed, we get the cause from the stack manager. However, as the command processor
* works through the nodes, this entire source may get replaced. We want to keep some of the changes in sync,
* but the original cause may have gone by the time the source changes. Really, this command source is the analogue
* of our Cause, NOT our CauseStackManager, so we just need to do `Cause.with(...)` along with their select `with*(...)`
* methods.
*/
@Inject(method = "withLevel", at = @At("RETURN"))
private void impl$updateCauseOnWithWorld(final ServerLevel serverWorld, final CallbackInfoReturnable<CommandSourceStack> cir) {
if (cir.getReturnValue() != (Object) this) {
final ServerLocation location = this.impl$cause.context().get(EventContextKeys.LOCATION).map(x -> ServerLocation.of((org.spongepowered.api.world.server.ServerWorld) serverWorld, x.position())).orElseGet(() -> ServerLocation.of((org.spongepowered.api.world.server.ServerWorld) serverWorld, VecHelper.toVector3d(cir.getReturnValue().getPosition())));
((CommandSourceStackBridge) cir.getReturnValue()).bridge$setCause(this.impl$applyToCause(EventContextKeys.LOCATION, location));
}
}
use of net.minecraft.server.level.ServerLevel in project SpongeCommon by SpongePowered.
the class FishingHookMixin method retrieve.
/**
* @author Aaron1011 - February 6th, 2015
* @author Minecrell - December 24th, 2016 (Updated to Minecraft 1.11.2)
* @author Minecrell - June 14th, 2017 (Rewritten to handle cases where no items are dropped)
* @reason This needs to handle for both cases where a fish and/or an entity is being caught.
*/
@Overwrite
public int retrieve(final ItemStack stack) {
final Player playerEntity = this.shadow$getPlayerOwner();
if (!this.level.isClientSide && playerEntity != null) {
int i = 0;
// Sponge start
final List<Transaction<@NonNull ItemStackSnapshot>> transactions;
if (this.nibble > 0) {
// Moved from below
final LootContext.Builder lootcontext$builder = new LootContext.Builder((ServerLevel) this.level).withParameter(LootContextParams.ORIGIN, this.shadow$position()).withParameter(LootContextParams.TOOL, stack).withParameter(LootContextParams.THIS_ENTITY, (FishingHook) (Object) this).withRandom(this.random).withLuck((float) this.luck + playerEntity.getLuck());
final LootTable lootTable = this.level.getServer().getLootTables().get(BuiltInLootTables.FISHING);
final List<ItemStack> list = lootTable.getRandomItems(lootcontext$builder.create(LootContextParamSets.FISHING));
transactions = list.stream().map(ItemStackUtil::snapshotOf).map(snapshot -> new Transaction<>(snapshot, snapshot)).collect(Collectors.toList());
CriteriaTriggers.FISHING_ROD_HOOKED.trigger((ServerPlayer) playerEntity, stack, (FishingHook) (Object) this, list);
} else {
transactions = new ArrayList<>();
}
PhaseTracker.getCauseStackManager().pushCause(playerEntity);
if (SpongeCommon.post(SpongeEventFactory.createFishingEventStop(PhaseTracker.getCauseStackManager().currentCause(), ((FishingBobber) this), transactions))) {
// Event is cancelled
return 0;
}
if (this.hookedIn != null) {
this.bringInHookedEntity();
CriteriaTriggers.FISHING_ROD_HOOKED.trigger((ServerPlayer) playerEntity, stack, (FishingHook) (Object) this, Collections.emptyList());
this.level.broadcastEntityEvent((FishingHook) (Object) this, (byte) 31);
i = this.hookedIn instanceof ItemEntity ? 3 : 5;
}
// Sponge start - Moved up to event call
if (!transactions.isEmpty()) {
// Use transactions
for (final Transaction<@NonNull ItemStackSnapshot> transaction : transactions) {
if (!transaction.isValid()) {
continue;
}
final ItemStack itemstack = (ItemStack) (Object) transaction.finalReplacement().createStack();
// Sponge end
final ItemEntity entityitem = new ItemEntity(this.level, this.shadow$getX(), this.shadow$getY(), this.shadow$getZ(), itemstack);
final double d0 = playerEntity.getX() - this.shadow$getX();
final double d1 = playerEntity.getY() - this.shadow$getY();
final double d2 = playerEntity.getZ() - this.shadow$getZ();
final double d3 = Mth.sqrt(d0 * d0 + d1 * d1 + d2 * d2);
// double d4 = 0.1D;
entityitem.setDeltaMovement(d0 * 0.1D, d1 * 0.1D + Mth.sqrt(d3) * 0.08D, d2 * 0.1D);
this.level.addFreshEntity(entityitem);
playerEntity.level.addFreshEntity(new ExperienceOrb(playerEntity.level, playerEntity.getX(), playerEntity.getY() + 0.5D, playerEntity.getZ() + 0.5D, this.random.nextInt(6) + 1));
final Item item = itemstack.getItem();
if (item.is(ItemTags.FISHES)) {
playerEntity.awardStat(Stats.FISH_CAUGHT, 1);
}
}
PhaseTracker.getCauseStackManager().popCause();
// Sponge: Don't lower damage if we've also caught an entity
i = Math.max(i, 1);
}
if (this.onGround) {
i = 2;
}
this.shadow$remove();
return i;
} else {
return 0;
}
}
Aggregations