Skip to content

Commit

Permalink
Improved root generation times. (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
CommandrMoose authored Feb 23, 2024
1 parent 240926f commit bec2a83
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import whocraft.tardis_refined.common.tardis.manager.TardisInteriorManager;
import whocraft.tardis_refined.common.tardis.manager.TardisPilotingManager;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

public class RootedShellBlock extends ShellBaseBlock {

Expand All @@ -47,9 +49,12 @@ public InteractionResult use(BlockState blockState, Level level, BlockPos blockP

if(!player.getMainHandItem().is(Items.SHEARS)) return InteractionResult.FAIL;


this.setUpTardis(blockState, level, blockPos);


if (player != null) {

player.getMainHandItem().hurtAndBreak(1, player, arg2 -> arg2.broadcastBreakEvent(interactionHand));
level.playSound(player, player.blockPosition(), SoundEvents.GROWING_PLANT_CROP, SoundSource.BLOCKS, 1.0f, 1.0f);
level.playSound(player, player.blockPosition(), SoundEvents.SLIME_JUMP, SoundSource.BLOCKS, 1.0f, 1.0f);
Expand All @@ -59,32 +64,28 @@ public InteractionResult use(BlockState blockState, Level level, BlockPos blockP
return InteractionResult.SUCCESS; //Prevents processing the ShellBaseBlockEntity generating another UUID and causing a second dimension to be created
}


private boolean setUpTardis(BlockState blockState, Level level, BlockPos blockPos){
if (level instanceof ServerLevel serverLevel) {
if (level.getBlockEntity(blockPos) instanceof ShellBaseBlockEntity shellBaseBlockEntity) {
if (shellBaseBlockEntity.shouldSetup()){


//Create a Level Key with a randomised UUID
ResourceKey<Level> generatedLevelKey = ResourceKey.create(Registries.DIMENSION, new ResourceLocation(TardisRefined.MODID, UUID.randomUUID().toString()));

//Set the shell with this level
shellBaseBlockEntity.setTardisId(generatedLevelKey);
//Create the Level on demand which will create our capability
ServerLevel interior = DimensionHandler.getOrCreateInterior(level, shellBaseBlockEntity.getTardisId().location());
ServerLevel interior = DimensionHandler.getOrCreateInterior(serverLevel, generatedLevelKey.location());

// Set the UUID on the block entity.
shellBaseBlockEntity.setTardisId(generatedLevelKey);

TardisLevelOperator.get(interior).ifPresent(tardisLevelOperator -> {
TardisInteriorManager intManager = tardisLevelOperator.getInteriorManager();
TardisExteriorManager extManager = tardisLevelOperator.getExteriorManager();
TardisPilotingManager pilotManager = tardisLevelOperator.getPilotingManager();
if (!tardisLevelOperator.hasInitiallyGenerated()) {
intManager.generateDesktop(TardisDesktops.DEFAULT_OVERGROWN_THEME);
Direction direction = blockState.getValue(FACING).getOpposite();
TardisNavLocation navLocation = new TardisNavLocation(blockPos, direction, serverLevel);
extManager.setLastKnownLocation(navLocation);
pilotManager.setTargetLocation(navLocation);
tardisLevelOperator.setInitiallyGenerated(true);
level.setBlock(blockPos, blockState.setValue(OPEN, true), Block.UPDATE_ALL);
tardisLevelOperator.setupInitialCave(serverLevel, blockState, blockPos);
}
});

return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,30 @@
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import whocraft.tardis_refined.api.event.TardisEvents;
import whocraft.tardis_refined.client.TardisClientData;
import whocraft.tardis_refined.common.block.shell.ShellBaseBlock;
import whocraft.tardis_refined.common.blockentity.door.RootShellDoorBlockEntity;
import whocraft.tardis_refined.common.blockentity.door.TardisInternalDoor;
import whocraft.tardis_refined.common.capability.upgrades.UpgradeHandler;
import whocraft.tardis_refined.common.tardis.ExteriorShell;
import whocraft.tardis_refined.common.tardis.TardisArchitectureHandler;
import whocraft.tardis_refined.common.tardis.TardisDesktops;
import whocraft.tardis_refined.common.tardis.TardisNavLocation;
import whocraft.tardis_refined.common.tardis.manager.*;
import whocraft.tardis_refined.common.tardis.themes.DesktopTheme;
import whocraft.tardis_refined.common.util.TardisHelper;
import whocraft.tardis_refined.compat.ModCompatChecker;
import whocraft.tardis_refined.compat.portals.ImmersivePortals;
import whocraft.tardis_refined.constants.NbtConstants;

import java.util.Optional;

import static whocraft.tardis_refined.common.block.RootPlantBlock.FACING;
import static whocraft.tardis_refined.common.block.shell.ShellBaseBlock.OPEN;

public class TardisLevelOperator {

private final Level level;
Expand Down Expand Up @@ -261,6 +269,18 @@ public void setInternalDoor(TardisInternalDoor door) {
this.internalDoor.onSetMainDoor(true);
}

public void setupInitialCave(ServerLevel shellServerLevel, BlockState shellBlockState, BlockPos shellBlockPos) {
this.interiorManager.generateDesktop(TardisDesktops.DEFAULT_OVERGROWN_THEME);
Direction direction = shellBlockState.getValue(ShellBaseBlock.FACING).getOpposite();
TardisNavLocation navLocation = new TardisNavLocation(shellBlockPos, direction, shellServerLevel);
this.exteriorManager.setLastKnownLocation(navLocation);
this.pilotingManager.setTargetLocation(navLocation);
shellServerLevel.setBlock(shellBlockPos, shellBlockState.setValue(OPEN, true), Block.UPDATE_ALL);

this.setInitiallyGenerated(true);
}


public TardisExteriorManager getExteriorManager() {
return this.exteriorManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public float getDestroySpeed(ItemStack itemStack, BlockState blockState) {
public InteractionResult useOn(UseOnContext useOnContext) {
BlockState blockState = useOnContext.getLevel().getBlockState(useOnContext.getClickedPos());

if (blockState.getBlock() == BlockRegistry.GROWTH_STONE.get()) {
if (blockState.getBlock() == BlockRegistry.FOOLS_STONE.get()) {
Player player = useOnContext.getPlayer();
Level level = useOnContext.getLevel();
BlockPos clickedPos = useOnContext.getClickedPos();
Expand All @@ -47,7 +47,7 @@ public InteractionResult useOn(UseOnContext useOnContext) {


private void destroyGrowthBlock(Level level, BlockPos pos) {
if (level.getBlockState(pos).getBlock() == BlockRegistry.GROWTH_STONE.get()) {
if (level.getBlockState(pos).getBlock() == BlockRegistry.FOOLS_STONE.get()) {
level.destroyBlock(pos, true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
import whocraft.tardis_refined.common.blockentity.door.TardisInternalDoor;
import whocraft.tardis_refined.common.capability.TardisLevelOperator;
import whocraft.tardis_refined.common.tardis.themes.DesktopTheme;
import whocraft.tardis_refined.constants.TardisGeneration;
import whocraft.tardis_refined.registry.BlockRegistry;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.*;

// Responsible for all the tedious generation of the desktop;
public class TardisArchitectureHandler {
Expand All @@ -33,29 +31,38 @@ public static void generateDesktop(ServerLevel operator, DesktopTheme theme) {
TardisRefined.LOGGER.debug(String.format("Attempting to generate desktop theme: %s for TARDIS.", theme.getIdentifier()));

// Fill the area out.
BlockPos corner = new BlockPos(DESKTOP_CENTER_POS.getX() - INTERIOR_SIZE, operator.getMinBuildHeight() + 75, DESKTOP_CENTER_POS.getZ() - INTERIOR_SIZE);
BlockPos farCorner = new BlockPos(DESKTOP_CENTER_POS.getX() + INTERIOR_SIZE, operator.getMaxBuildHeight() - 75, DESKTOP_CENTER_POS.getZ() + INTERIOR_SIZE);
BlockPos corner = new BlockPos(TardisGeneration.TARDIS_CENTER_POS.getX() - TardisGeneration.DESKTOP_RADIUS, TardisGeneration.TARDIS_ROOT_GENERATION_MIN_HEIGHT, TardisGeneration.TARDIS_CENTER_POS.getZ() - TardisGeneration.DESKTOP_RADIUS);
BlockPos farCorner = new BlockPos(TardisGeneration.TARDIS_CENTER_POS.getX() + TardisGeneration.DESKTOP_RADIUS, TardisGeneration.TARDIS_ROOT_GENERATION_MAX_HEIGHT, TardisGeneration.TARDIS_CENTER_POS.getZ() + TardisGeneration.DESKTOP_RADIUS);

for (BlockPos pos : BlockPos.betweenClosed(corner, farCorner)) {
operator.removeBlock(pos, false);
}

if (theme != TardisDesktops.DEFAULT_OVERGROWN_THEME) {

for (BlockPos pos : BlockPos.betweenClosed(corner, farCorner)) {
if (operator.getBlockState(pos) != BlockRegistry.FOOLS_STONE.get().defaultBlockState()) {
operator.setBlock(pos, BlockRegistry.FOOLS_STONE.get().defaultBlockState(), Block.UPDATE_ALL);
}

}
}

List<Entity> desktopEntities = operator.getLevel().getEntitiesOfClass(Entity.class, new AABB(corner, farCorner));
desktopEntities.forEach(Entity::discard); //Don't teleport entities to a hard coded coordinate, that causes hanging entity out of world issues. In other cases, if another mod defines that coordinate as a safe area (possible) that will mean the entities never get killed.

Optional<StructureTemplate> structureNBT = operator.getLevel().getStructureManager().get(theme.getStructureLocation());

structureNBT.ifPresent(structure -> {
BlockPos offsetPosition = calculateArcOffset(structure, DESKTOP_CENTER_POS);
structure.placeInWorld(operator.getLevel(), offsetPosition, offsetPosition, new StructurePlaceSettings(), operator.getLevel().random, 3);
structure.placeInWorld(operator.getLevel(), offsetPosition, offsetPosition, new StructurePlaceSettings(), operator.getLevel().random, Block.UPDATE_IMMEDIATE);

// Assign the door from the created structure.
setInteriorDoorFromStructure(structure, operator);
buildAirlockEntranceFromStructure(structure, operator);
});
}



}

public static void buildAirlockEntranceFromStructure(StructureTemplate template, ServerLevel level) {
BlockPos minPos = calculateArcOffset(template, DESKTOP_CENTER_POS);
BlockPos maxPos = new BlockPos(minPos.getX() + template.getSize().getX(),
Expand All @@ -70,7 +77,7 @@ public static void buildAirlockEntranceFromStructure(StructureTemplate template,
Optional<StructureTemplate> structureNBT = level.getLevel().getStructureManager().get(new ResourceLocation(TardisRefined.MODID, "corridors/airlock_entrance"));
structureNBT.ifPresent(structure -> {
BlockPos offsetPosition = new BlockPos(3, 2, 0);
structure.placeInWorld(level.getLevel(), pos.subtract(offsetPosition), pos.subtract(offsetPosition), new StructurePlaceSettings(), level.getLevel().random, 3);
structure.placeInWorld(level.getLevel(), pos.subtract(offsetPosition), pos.subtract(offsetPosition), new StructurePlaceSettings(), level.getLevel().random, 2);

cap.getInteriorManager().setCorridorAirlockCenter(pos.south(2));
level.setBlock(pos, level.getBlockState(pos).setValue(BulkHeadDoorBlock.LOCKED, false), 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import whocraft.tardis_refined.common.block.door.BulkHeadDoorBlock;
Expand Down Expand Up @@ -309,14 +311,17 @@ public void generateDesktop(DesktopTheme theme) {
serverLevel.removeBlock(tardisInternalDoor.getDoorPosition(), false);
}

// Generate Corridors
if (!this.hasGeneratedCorridors) {
TardisArchitectureHandler.generateEssentialCorridors(serverLevel);
this.hasGeneratedCorridors = true;
if (theme != TardisDesktops.DEFAULT_OVERGROWN_THEME) {
// Generate Corridors
if (!this.hasGeneratedCorridors) {
TardisArchitectureHandler.generateEssentialCorridors(serverLevel); // This causes a little lag, could be worth a fix.
this.hasGeneratedCorridors = true;
}
}

// Generate Desktop Interior
TardisArchitectureHandler.generateDesktop(serverLevel, theme);

setCurrentTheme(theme);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.RegistryOps;
import net.minecraft.server.level.WorldGenRegion;
Expand All @@ -24,13 +23,12 @@
import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.level.levelgen.RandomState;
import net.minecraft.world.level.levelgen.blending.Blender;
import net.minecraft.world.level.levelgen.structure.StructureSet;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
import whocraft.tardis_refined.common.world.ChunkGenerators;
import whocraft.tardis_refined.constants.TardisGeneration;
import whocraft.tardis_refined.registry.BlockRegistry;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

Expand Down Expand Up @@ -69,17 +67,7 @@ public void applyCarvers(WorldGenRegion p_223043_, long p_223044_, RandomState p

@Override
public void buildSurface(WorldGenRegion level, StructureManager structureManager, RandomState random, ChunkAccess chunk) {
// var bottom = chunk.getMinBuildHeight() + 70;
// BlockPos cornerPos = new BlockPos(chunk.getPos().getMinBlockX(), bottom, chunk.getPos().getMinBlockZ());
// BlockPos lastCornerPos = new BlockPos(chunk.getPos().getMaxBlockX(), chunk.getMaxBuildHeight() - 75, chunk.getPos().getMaxBlockZ());
// for (BlockPos pos : BlockPos.betweenClosed(cornerPos, lastCornerPos)) {
// if (pos.getY() <= bottom + 5) {
// chunk.setBlockState(pos, Blocks.AIR.defaultBlockState(), false);
// } else {
// chunk.setBlockState(pos, Blocks.AIR.defaultBlockState(), false);
// }
//
// }

}

@Override
Expand All @@ -92,6 +80,20 @@ public int getGenDepth() {

@Override
public CompletableFuture<ChunkAccess> fillFromNoise(Executor executor, Blender p_223210_, RandomState p_223211_, StructureManager p_223212_, ChunkAccess access) {

// Flatworlds appear to use this function instead of the surface.
BlockPos cornerPos = new BlockPos(access.getPos().getMinBlockX(), TardisGeneration.TARDIS_ROOT_GENERATION_MIN_HEIGHT -5, access.getPos().getMinBlockZ());
BlockPos lastCornerPos = new BlockPos(access.getPos().getMaxBlockX(), TardisGeneration.TARDIS_ROOT_GENERATION_MAX_HEIGHT + 5, access.getPos().getMaxBlockZ());
for (BlockPos pos : BlockPos.betweenClosed(cornerPos, lastCornerPos)) {

if (pos.getY() <= TardisGeneration.TARDIS_ROOT_GENERATION_MIN_HEIGHT || pos.getY() > TardisGeneration.TARDIS_ROOT_GENERATION_MAX_HEIGHT ) {
access.setBlockState(pos, Blocks.BEDROCK.defaultBlockState(), false);
} else {

access.setBlockState(pos, BlockRegistry.FOOLS_STONE.get().defaultBlockState(), false);
}
}

return CompletableFuture.completedFuture(access);
}

Expand Down Expand Up @@ -121,6 +123,8 @@ public NoiseColumn getBaseColumn(int p_223028_, int p_223029_, LevelHeightAccess
return new NoiseColumn(0, states);
}



@Override
public void addDebugScreenInfo(List<String> p_223175_, RandomState p_223176_, BlockPos p_223177_) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package whocraft.tardis_refined.constants;

import net.minecraft.core.BlockPos;

public class TardisGeneration {

public static final Integer DESKTOP_RADIUS = 150;
public static final BlockPos TARDIS_CENTER_POS = new BlockPos(0, 100, 0);
public static final Integer TARDIS_ROOT_GENERATION_MAX_HEIGHT = 150;
public static final Integer TARDIS_ROOT_GENERATION_MIN_HEIGHT = 0;

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import whocraft.tardis_refined.common.block.device.*;
import whocraft.tardis_refined.common.block.door.BulkHeadDoorBlock;
import whocraft.tardis_refined.common.block.door.GlobalDoorBlock;
import whocraft.tardis_refined.common.block.door.InternalDoorBlock;
import whocraft.tardis_refined.common.block.door.RootShellDoorBlock;
import whocraft.tardis_refined.common.block.life.ArsEggBlock;
import whocraft.tardis_refined.common.block.life.GrowthStoneBlock;
Expand Down Expand Up @@ -50,8 +49,7 @@ private static <T extends Block> RegistrySupplier<T> register(String id, Supplie
public static final RegistrySupplier<RootShellDoorBlock> ROOT_SHELL_DOOR = register("root_shell_door", () -> new RootShellDoorBlock(BlockBehaviour.Properties.of().noOcclusion().strength(1000, 1000)), false, true);

// Generation Blocks
public static final RegistrySupplier<Block> GROWTH_STONE = register("growth_stone", () -> new GrowthStoneBlock(BlockBehaviour.Properties.of().strength(3)), true, true);
public static final RegistrySupplier<Block> HARDENED_GROWTH_STONE = register("hardened_growth_stone", () -> new Block(BlockBehaviour.Properties.of().strength(10000, 10000)), true, true);
public static final RegistrySupplier<Block> FOOLS_STONE = register("fools_stone", () -> new GrowthStoneBlock(BlockBehaviour.Properties.of().strength(3)), true, true);


// Roots
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bed_works": true,
"has_raids": false,
"has_skylight": false,
"has_ceiling": false,
"has_ceiling": true,
"coordinate_scale": 1,
"ambient_light": 0,
"logical_height": 384,
Expand Down
6 changes: 3 additions & 3 deletions fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ dependencies {

// Immersive Portals Start

modImplementation ("com.github.iPortalTeam.ImmersivePortalsMod:imm_ptl_core:v4.0.3-mc1.20.2")
modImplementation ("com.github.iPortalTeam.ImmersivePortalsMod:q_misc_util:v4.0.3-mc1.20.2")
modImplementation ("com.github.iPortalTeam.ImmersivePortalsMod:build:v4.0.3-mc1.20.2")
modCompileOnlyApi ("com.github.iPortalTeam.ImmersivePortalsMod:imm_ptl_core:v4.0.3-mc1.20.2")
modCompileOnlyApi ("com.github.iPortalTeam.ImmersivePortalsMod:q_misc_util:v4.0.3-mc1.20.2")
modCompileOnlyApi ("com.github.iPortalTeam.ImmersivePortalsMod:build:v4.0.3-mc1.20.2")

/* // If working on Immersive Portals, change modCompileOnlyApi to modImplementation for the following two entries, this will enable Immersive Portals in Dev
modCompileOnlyApi("com.github.iPortalTeam.ImmersivePortalsMod:imm_ptl_core:${rootProject.immersive_portals_version}") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ protected void registerModels() {
blockItem(BlockRegistry.ARS_LEAVES.getId());
blockItem(BlockRegistry.ARS_LEAVES_SLAB.getId());
blockItem(BlockRegistry.LANDING_PAD.getId());
blockItem(BlockRegistry.HARDENED_GROWTH_STONE.getId());
blockItem(BlockRegistry.GROWTH_STONE.getId());
blockItem(BlockRegistry.FOOLS_STONE.getId());
blockItem(BlockRegistry.FLIGHT_DETECTOR.getId());


Expand Down
Loading

0 comments on commit bec2a83

Please sign in to comment.