-
Notifications
You must be signed in to change notification settings - Fork 56
Fake Levels
A Level that isn't a world, and a Player that isn't a person.
Plenty of vanilla and modded code demands a Level or a Player when there isn't a real one
around. Rendering a block in a GUI, previewing a structure, running a block's use from a machine,
simulating a placement to see what would happen. A fake level is a real Level subclass backed by
nothing: no chunks, no ticking, no saving, but blocks, block entities and recipes work, so anything
that only reads from the level is happy.
This is one of the more unusual things in the library and it solves a class of problem that's otherwise very annoying.
Levels are cached by id, so ask the manager rather than constructing one:
Level fake = FakeLevelManager.getDefault(realLevel);
// or your own, keyed by id, so several systems don't share one
FakeLevel mine = FakeLevelManager.getClient("my_preview", realLevel, MyFakeLevel::new);Set blocks in it and render or query them like normal:
fake.setBlock(BlockPos.ZERO, Blocks.FURNACE.defaultBlockState(), 3);
BlockEntity be = fake.getBlockEntity(BlockPos.ZERO);FakeServerLevel is the server side variant, for code that hard casts to ServerLevel.
Fake levels have a controllable day time (setDayTimeFraction, setDayTimePerTick), which matters
when you're rendering a preview and don't want it to depend on the real world's time of day.
Call FakeLevelManager.invalidate(id) when you're done with one, or invalidateAll().
Player fake = PlatHelper.getFakeServerPlayer(profile, serverLevel);
// and to check, since other mods' fake players show up too
PlatHelper.isFakePlayer(somePlayer);Used when a machine or block needs to act "as" a player: breaking blocks, using items, triggering
events that require a player. FakeGenericPlayer and FakeLocalPlayer are the implementations.
DummyBlockGetter is the lighter option when all you need is a BlockGetter for a handful of
positions and not a whole level.
Basics Platform Helpers Registration Networking Events Configs Config Screen
Resources Runtime Resource Packs Texture Manipulation Resource Helpers Block Set API
Client Custom Models Item Rendering Rendered Textures Post Shaders GUI Toolkit Colors
World Block and Item Interfaces Additional Item Placements Improved Entities Fake Levels World Data Dispenser Behaviors
Utilities Codec Utilities Misc Helpers Commands
Datapacks Villagers Soft Fluids Map Markers Spawn Boxes Global Datapack Folder