-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Keep a radius of cached regions loaded #2967
base: master
Are you sure you want to change the base?
Changes from 1 commit
dc8495c
7a5e492
9f1d3ba
f5da562
66ace5a
0465a4e
88ebb6d
5893d20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* This file is part of Baritone. | ||
* | ||
* Baritone is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Baritone is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package baritone.cache; | ||
|
||
import baritone.Baritone; | ||
import baritone.api.cache.ICachedWorld; | ||
import baritone.api.event.events.RenderEvent; | ||
import baritone.api.event.events.TickEvent; | ||
import baritone.api.event.listener.AbstractGameEventListener; | ||
import net.minecraft.client.entity.EntityPlayerSP; | ||
import net.minecraft.entity.Entity; | ||
import net.minecraft.world.World; | ||
|
||
import static baritone.api.utils.Helper.mc; | ||
|
||
/** | ||
* Loads an area around the player and renderViewEntity to the cache. | ||
* The | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think you meant to leave that here. |
||
*/ | ||
public class ChunkLoader implements AbstractGameEventListener { | ||
|
||
private final Baritone baritone; | ||
private final WorldProvider worldProvider; | ||
/** | ||
* The maximum length from the player to the edge of the loaded region. | ||
* A radius of 0 does no caching. A radius of 1 only loads the chunk the player is on. | ||
* A radius of 2 load a 3 by three grid around the player. | ||
*/ | ||
private final int radius; | ||
|
||
public ChunkLoader(Baritone baritone, int radius) { | ||
this.baritone = baritone; | ||
this.worldProvider = baritone.getWorldProvider(); | ||
this.radius = radius; | ||
} | ||
|
||
@Override | ||
public void onTick(TickEvent event) {this.loadChunks();} | ||
|
||
@Override | ||
public void onRenderPass(RenderEvent event) {this.loadChunks();} | ||
|
||
private void loadChunks() { | ||
EntityPlayerSP player = mc.player; | ||
Entity renderViewEntity = mc.getRenderViewEntity(); | ||
// Don't do anything before the player is loaded. | ||
if (player == null || renderViewEntity == null) return; | ||
WorldData worldData = this.worldProvider.getCurrentWorld(); | ||
// Don't do anything if the world doesn't exist. Like in the main menu. | ||
if (worldData == null) return; | ||
loadAroundChunk(player.chunkCoordX, player.chunkCoordZ); | ||
// Only load around the renderViewEntity if it's in a different chunk. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also don't do anything if the position didn't change since last tick? |
||
if (player.chunkCoordX != renderViewEntity.chunkCoordX || player.chunkCoordZ != renderViewEntity.chunkCoordZ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should compare region coords here because you can only load entire regions at once. |
||
loadAroundChunk(renderViewEntity.chunkCoordZ, renderViewEntity.chunkCoordZ); | ||
} | ||
} | ||
|
||
private void loadAroundChunk(int chunkX, int chunkZ) { | ||
CachedWorld cachedWorld = (CachedWorld) this.worldProvider.getCurrentWorld().getCachedWorld(); | ||
// If the radius is zero do nothing. | ||
if (this.radius == 0) return; | ||
World mcWorld = this.baritone.getPlayerContext().world(); | ||
// Load the chunks. | ||
for (int xOffset = -(this.radius - 1); xOffset < this.radius; xOffset++) { | ||
for (int zOffset = -(this.radius - 1); zOffset < this.radius; zOffset++) { | ||
cachedWorld.tryLoadFromDisk((chunkX + xOffset) >> 5, (chunkZ + zOffset) >> 5); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#324 suggested loading 3 by 3 regions around the player, not chunks.
Loading 3 by 3 chunks changes almost nothing, it now just loads the next region 16 blocks before you reach the region border instead of when you reach it.