9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-24 09:29:33 +00:00

重置tick逻辑

This commit is contained in:
XiaoMoMi
2025-09-03 20:33:30 +08:00
parent 36654ea2cd
commit ef2c26f15f
8 changed files with 64 additions and 31 deletions

View File

@@ -22,8 +22,7 @@ public class BukkitCEWorld extends CEWorld {
}
@Override
public void tick() {
super.tick();
public void updateLight() {
HashSet<SectionPos> poses;
synchronized (super.updatedSectionSet) {
poses = new HashSet<>(super.updatedSectionSet);

View File

@@ -1,6 +1,7 @@
package net.momirealms.craftengine.bukkit.world;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.momirealms.craftengine.bukkit.api.CraftEngineBlocks;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
import net.momirealms.craftengine.bukkit.plugin.injector.WorldStorageInjector;
@@ -33,7 +34,6 @@ import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class BukkitWorldManager implements WorldManager, Listener {
@@ -42,12 +42,10 @@ public class BukkitWorldManager implements WorldManager, Listener {
private final Map<UUID, CEWorld> worlds;
private CEWorld[] worldArray = new CEWorld[0];
private final ReentrantReadWriteLock worldMapLock = new ReentrantReadWriteLock();
private SchedulerTask tickTask;
// cache
private UUID lastVisitedUUID;
private CEWorld lastVisitedWorld;
private StorageAdaptor storageAdaptor;
private boolean isTicking = false;
private boolean initialized = false;
public BukkitWorldManager(BukkitCraftEngine plugin) {
@@ -101,20 +99,6 @@ public class BukkitWorldManager implements WorldManager, Listener {
}
public void delayedInit() {
// events and tasks
this.tickTask = this.plugin.scheduler().asyncRepeating(() -> {
try {
if (this.isTicking) {
return;
}
this.isTicking = true;
for (CEWorld world : this.worldArray) {
world.tick();
}
} finally {
this.isTicking = false;
}
}, 50, 50, TimeUnit.MILLISECONDS);
// load loaded chunks
this.worldMapLock.writeLock().lock();
try {
@@ -124,6 +108,7 @@ public class BukkitWorldManager implements WorldManager, Listener {
for (Chunk chunk : world.getLoadedChunks()) {
handleChunkLoad(ceWorld, chunk);
}
ceWorld.setTicking(true);
} catch (Exception e) {
CraftEngine.instance().logger().warn("Error loading world: " + world.getName(), e);
}
@@ -142,11 +127,9 @@ public class BukkitWorldManager implements WorldManager, Listener {
if (this.storageAdaptor instanceof Listener listener) {
HandlerList.unregisterAll(listener);
}
if (this.tickTask != null && !this.tickTask.cancelled()) {
this.tickTask.cancel();
}
for (World world : Bukkit.getWorlds()) {
CEWorld ceWorld = getWorld(world.getUID());
ceWorld.setTicking(false);
for (Chunk chunk : world.getLoadedChunks()) {
handleChunkUnload(ceWorld, chunk);
}
@@ -175,6 +158,7 @@ public class BukkitWorldManager implements WorldManager, Listener {
for (Chunk chunk : ((World) world.platformWorld()).getLoadedChunks()) {
handleChunkLoad(ceWorld, chunk);
}
ceWorld.setTicking(true);
} finally {
this.worldMapLock.writeLock().unlock();
}
@@ -190,6 +174,7 @@ public class BukkitWorldManager implements WorldManager, Listener {
for (Chunk chunk : ((World) world.world().platformWorld()).getLoadedChunks()) {
handleChunkLoad(world, chunk);
}
world.setTicking(true);
} finally {
this.worldMapLock.writeLock().unlock();
}
@@ -229,6 +214,7 @@ public class BukkitWorldManager implements WorldManager, Listener {
} finally {
this.worldMapLock.writeLock().unlock();
}
ceWorld.setTicking(false);
for (Chunk chunk : ((World) world.platformWorld()).getLoadedChunks()) {
handleChunkUnload(ceWorld, chunk);
}

View File

@@ -361,6 +361,8 @@ gui:
light-system:
# Required for custom light-emitting blocks
enable: true
# Async light update
async-update: true
chunk-system:
# With cache system, those frequently load/unload chunks would consume fewer resources on serialization

View File

@@ -40,7 +40,7 @@ public abstract class AbstractFontManager implements FontManager {
private final EmojiParser emojiParser;
private OffsetFont offsetFont;
protected Trie imageTagTrie;
protected Trie networkTagTrie;
protected Trie emojiKeywordTrie;
protected Map<String, ComponentProvider> networkTagMapper;
protected Map<String, Emoji> emojiMapper;
@@ -67,9 +67,14 @@ public abstract class AbstractFontManager implements FontManager {
this.images.clear();
this.illegalChars.clear();
this.emojis.clear();
this.networkTagTrie = null;
this.emojiKeywordTrie = null;
if (this.networkTagMapper != null) {
this.networkTagMapper.clear();
}
if (this.emojiMapper != null) {
this.emojiMapper.clear();
}
}
@Override
@@ -88,7 +93,7 @@ public abstract class AbstractFontManager implements FontManager {
this.registerImageTags();
this.registerShiftTags();
this.registerGlobalTags();
this.buildImageTagTrie();
this.buildNetworkTagTrie();
this.buildEmojiKeywordsTrie();
this.emojiList = new ArrayList<>(this.emojis.values());
this.allEmojiSuggestions = this.emojis.values().stream()
@@ -131,11 +136,11 @@ public abstract class AbstractFontManager implements FontManager {
@Override
public Map<String, ComponentProvider> matchTags(String json) {
if (this.imageTagTrie == null) {
if (this.networkTagTrie == null) {
return Collections.emptyMap();
}
Map<String, ComponentProvider> tags = new HashMap<>();
for (Token token : this.imageTagTrie.tokenize(json)) {
for (Token token : this.networkTagTrie.tokenize(json)) {
if (token.isMatch()) {
tags.put(token.getFragment(), this.networkTagMapper.get(token.getFragment()));
}
@@ -305,8 +310,8 @@ public abstract class AbstractFontManager implements FontManager {
.build();
}
private void buildImageTagTrie() {
this.imageTagTrie = Trie.builder()
private void buildNetworkTagTrie() {
this.networkTagTrie = Trie.builder()
.ignoreOverlaps()
.addKeywords(this.networkTagMapper.keySet())
.build();

View File

@@ -97,6 +97,7 @@ public class Config {
protected Component resource_pack$send$prompt;
protected boolean light_system$force_update_light;
protected boolean light_system$async_update;
protected boolean light_system$enable;
protected int chunk_system$compression_method;
@@ -314,6 +315,7 @@ public class Config {
// light
light_system$force_update_light = config.getBoolean("light-system.force-update-light", false);
light_system$async_update = config.getBoolean("light-system.async-update", true);
light_system$enable = config.getBoolean("light-system.enable", true);
// chunk
@@ -885,6 +887,10 @@ public class Config {
return instance.block$chunk_relighter;
}
public static boolean asyncLightUpdate() {
return instance.light_system$async_update;
}
public void setObf(boolean enable) {
this.resource_pack$protection$obfuscation$enable = enable;
}

View File

@@ -365,6 +365,8 @@ public class AdventureHelper {
return text.replaceText(builder ->
builder.match(Pattern.compile(patternString))
.replacement((result, b) ->
replacements.get(result.group()).apply(context)));
Optional.ofNullable(replacements.get(result.group())).orElseThrow(() -> new IllegalStateException("Could not find tag '" + result.group() + "'")).apply(context)
)
);
}
}

View File

@@ -6,6 +6,8 @@ import net.momirealms.craftengine.core.block.ImmutableBlockState;
import net.momirealms.craftengine.core.block.entity.BlockEntity;
import net.momirealms.craftengine.core.block.entity.tick.TickingBlockEntity;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.config.Config;
import net.momirealms.craftengine.core.plugin.scheduler.SchedulerTask;
import net.momirealms.craftengine.core.world.chunk.CEChunk;
import net.momirealms.craftengine.core.world.chunk.storage.StorageAdaptor;
import net.momirealms.craftengine.core.world.chunk.storage.WorldDataStorage;
@@ -25,6 +27,8 @@ public abstract class CEWorld {
protected final List<TickingBlockEntity> tickingBlockEntities = new ArrayList<>();
protected final List<TickingBlockEntity> pendingTickingBlockEntities = new ArrayList<>();
protected boolean isTickingBlockEntities = false;
protected SchedulerTask syncTickTask;
protected SchedulerTask asyncTickTask;
private CEChunk lastChunk;
private long lastChunkPos;
@@ -45,6 +49,24 @@ public abstract class CEWorld {
this.lastChunkPos = ChunkPos.INVALID_CHUNK_POS;
}
public void setTicking(boolean ticking) {
if (ticking) {
if (this.syncTickTask == null || this.syncTickTask.cancelled()) {
this.syncTickTask = CraftEngine.instance().scheduler().sync().runRepeating(this::syncTick, 1, 1);
}
if (this.asyncTickTask == null || this.asyncTickTask.cancelled()) {
this.asyncTickTask = CraftEngine.instance().scheduler().sync().runAsyncRepeating(this::asyncTick, 1, 1);
}
} else {
if (this.syncTickTask != null && !this.syncTickTask.cancelled()) {
this.syncTickTask.cancel();
}
if (this.asyncTickTask != null && !this.asyncTickTask.cancelled()) {
this.asyncTickTask.cancel();
}
}
}
public String name() {
return this.world.name();
}
@@ -163,10 +185,21 @@ public abstract class CEWorld {
return this.worldHeightAccessor;
}
public void tick() {
public void syncTick() {
this.tickBlockEntities();
if (!Config.asyncLightUpdate()) {
this.updateLight();
}
}
public void asyncTick() {
if (Config.asyncLightUpdate()) {
this.updateLight();
}
}
public abstract void updateLight();
protected void tickBlockEntities() {
this.isTickingBlockEntities = true;
if (!this.pendingTickingBlockEntities.isEmpty()) {

View File

@@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx1G
# Project settings
# Rule: [major update].[feature update].[bug fix]
project_version=0.0.62.8
project_version=0.0.62.9
config_version=45
lang_version=25
project_group=net.momirealms