refactor code

This commit is contained in:
2026-08-15 16:08:52 +08:00
parent 2734c91fca
commit 3cb4cc0ab8
13 changed files with 63 additions and 50 deletions
@@ -1,9 +1,9 @@
package io.nanachiyo0721.shiroha.commands.bar;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.nanachiyo0721.shiroha.commands.bar.sub.ConfigEditCommand;
import io.nanachiyo0721.shiroha.commands.bar.sub.ToggleCommand;
import io.nanachiyo0721.shiroha.enums.EnumBarType;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.leavesmc.leaves.command.CommandNode;
@@ -6,12 +6,12 @@ import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.command.brigadier.PaperCommands;
import io.nanachiyo0721.shiroha.commands.bar.BarCommand;
import io.nanachiyo0721.shiroha.enums.EnumBarType;
import io.nanachiyo0721.shiroha.functions.bars.TickableStatusBarList;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.command.brigadier.PaperCommands;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Bukkit;
@@ -3,13 +3,13 @@ package io.nanachiyo0721.shiroha.config;
import com.electronwill.nightconfig.core.UnmodifiableConfig;
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
import com.mojang.logging.LogUtils;
import io.nanachiyo0721.shiroha.config.flags.*;
import io.papermc.paper.threadedregions.RegionizedServer;
import io.nanachiyo0721.shiroha.api.config.ShirohaConfigsInstance;
import io.nanachiyo0721.shiroha.api.config.ConfigDataPair;
import io.nanachiyo0721.shiroha.api.config.ShirohaConfigsInstance;
import io.nanachiyo0721.shiroha.commands.config.ConfigCommand;
import io.nanachiyo0721.shiroha.config.flags.*;
import io.nanachiyo0721.shiroha.enums.EnumConfigCategory;
import io.nanachiyo0721.shiroha.utils.ClassScanUtil;
import io.papermc.paper.threadedregions.RegionizedServer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
@@ -7,9 +7,9 @@ import io.nanachiyo0721.shiroha.config.flags.ConfigClassInfo;
import io.nanachiyo0721.shiroha.config.flags.ConfigInfo;
import io.nanachiyo0721.shiroha.config.flags.DoNotLoad;
import io.nanachiyo0721.shiroha.config.flags.HotReloadUnsupported;
import io.nanachiyo0721.shiroha.data.BufferedLinearRegionFileFlusher;
import io.nanachiyo0721.shiroha.enums.EnumConfigCategory;
import io.nanachiyo0721.shiroha.enums.EnumRegionFormat;
import io.nanachiyo0721.shiroha.data.BufferedLinearRegionFileFlusher;
import net.minecraft.server.MinecraftServer;
import org.jetbrains.annotations.Nullable;
@@ -468,7 +468,7 @@ public class BufferedLinearRegionFile implements io.nanachiyo0721.shiroha.data.R
// since we hold the write lock and any read/write/sync ops is currently blocked all along the close logic, acquiring the locks inside sync is a disaster
try {
this.syncToMasterFile(true, true, true, true);
}catch (IOException ex) {
} catch (IOException ex) {
failure = ex;
}
@@ -477,13 +477,15 @@ public class BufferedLinearRegionFile implements io.nanachiyo0721.shiroha.data.R
this.swapFileChannel.close();
} catch (IOException ex) {
if (failure == null) failure = ex; else failure.addSuppressed(ex);
if (failure == null) failure = ex;
else failure.addSuppressed(ex);
}
try {
this.masterFileParser.closeNoLock();
} catch (IOException e) {
if (failure == null) failure = e; else failure.addSuppressed(e);
if (failure == null) failure = e;
else failure.addSuppressed(e);
}
// finalize
@@ -492,10 +494,10 @@ public class BufferedLinearRegionFile implements io.nanachiyo0721.shiroha.data.R
if (failure != null) {
throw failure;
}
}finally {
} finally {
this.regionObjectLock.writeLock().unlock();
}
}finally {
} finally {
this.masterFileParser.masterFileLock.writeLock().unlock();
}
}
@@ -1,12 +1,12 @@
package io.nanachiyo0721.shiroha.functions.bars.impl;
import ca.spottedleaf.common.time.TickData;
import io.papermc.paper.threadedregions.ThreadedRegionizer;
import io.papermc.paper.threadedregions.TickRegionScheduler;
import io.papermc.paper.threadedregions.TickRegions;
import io.nanachiyo0721.shiroha.config.modules.function.RegionBarConfig;
import io.nanachiyo0721.shiroha.enums.EnumStatusBarDisplay;
import io.nanachiyo0721.shiroha.functions.bars.TickableStatusBar;
import io.papermc.paper.threadedregions.ThreadedRegionizer;
import io.papermc.paper.threadedregions.TickRegionScheduler;
import io.papermc.paper.threadedregions.TickRegions;
import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
@@ -1,12 +1,12 @@
package io.nanachiyo0721.shiroha.functions.bars.impl;
import ca.spottedleaf.common.time.TickData;
import io.papermc.paper.threadedregions.ThreadedRegionizer;
import io.papermc.paper.threadedregions.TickRegionScheduler;
import io.papermc.paper.threadedregions.TickRegions;
import io.nanachiyo0721.shiroha.config.modules.function.TpsBarConfig;
import io.nanachiyo0721.shiroha.enums.EnumStatusBarDisplay;
import io.nanachiyo0721.shiroha.functions.bars.TickableStatusBar;
import io.papermc.paper.threadedregions.ThreadedRegionizer;
import io.papermc.paper.threadedregions.TickRegionScheduler;
import io.papermc.paper.threadedregions.TickRegions;
import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
@@ -164,7 +164,7 @@ public class ReferenceCountingChunkLoader {
RegionizedServer.getInstance().taskQueue.queueChunkTask(this.world, chunkX, chunkZ, () -> {
try {
callback.accept(cached);
}finally {
} finally {
this.decrementReference(increased, coord);
}
}, priority);
@@ -179,7 +179,7 @@ public class ReferenceCountingChunkLoader {
increased.cached = chunk;
callback.accept(chunk);
}finally {
} finally {
this.decrementReference(increased, coord);
}
}
@@ -264,7 +264,7 @@ public class ReferenceCountingChunkLoader {
public boolean addCount() {
int failures = 0;
for (long curr = this.referenceCount.get();;) {
for (long curr = this.referenceCount.get(); ; ) {
for (int i = 0; i < failures; ++i) {
Thread.onSpinWait();
}
@@ -275,7 +275,7 @@ public class ReferenceCountingChunkLoader {
if (curr == (curr = this.referenceCount.compareAndExchange(curr, curr + 1L))) {
int ttlFailures = 0;
for (long currTTL = this.referenceTTL.get();;) {
for (long currTTL = this.referenceTTL.get(); ; ) {
for (int i = 0; i < ttlFailures; i++) {
Thread.onSpinWait();
}
@@ -96,11 +96,11 @@ public class CheckAndCacheBlockChecker {
}
}
public int getChunkSize(){
public int getChunkSize() {
return this.chunkSections2MaybeContainsMatchingBlock.numChunks;
}
public boolean hasUnloadedPossibleChunks(){
public boolean hasUnloadedPossibleChunks() {
return this.unloadedPossibleChunkSections > 0;
}
@@ -129,14 +129,14 @@ public class CheckAndCacheBlockChecker {
return this.chunkSections2MaybeContainsMatchingBlock.getChunkAccess(blockPos);
}
public boolean shouldStop(){
public boolean shouldStop() {
return this.chunkSections2MaybeContainsMatchingBlock.hasNoTrueChunkSections();
}
public boolean checkPosition(BlockPos blockPos) {
if(!this.chunkSections2MaybeContainsMatchingBlock.getChunkSectionBit(blockPos)) return false;
if (!this.chunkSections2MaybeContainsMatchingBlock.getChunkSectionBit(blockPos)) return false;
ChunkAccess chunkAccess = this.chunkSections2MaybeContainsMatchingBlock.getChunkAccess(blockPos);
if(chunkAccess == null) {
if (chunkAccess == null) {
if (!this.shouldChunkLoad) {
return false;
}
@@ -35,12 +35,12 @@ public class CommonBlockSearchesCheckAndCache {
public static Optional<BlockPos> blockPosFindClosestMatch(LevelReader levelReader, LivingEntity livingEntity,
int horizontalRange, int verticalRange,
Predicate<BlockState> blockStatePredicate,
boolean shouldChunkLoad){
boolean shouldChunkLoad) {
BlockPos mobPos = livingEntity.blockPosition();
CheckAndCacheBlockChecker checker = new CheckAndCacheBlockChecker(
mobPos, horizontalRange, verticalRange, levelReader, blockStatePredicate, shouldChunkLoad);
checker.initializeChunks();
if(checker.shouldStop()) {
if (checker.shouldStop()) {
return Optional.empty();
}
return BlockPos.findClosestMatch(mobPos, horizontalRange, verticalRange, checker::checkPosition);
@@ -34,7 +34,7 @@ public class NonPOISearchDistances {
return getVanillaSortOrderInt(getRing(dX, dZ), dX, dZ);
}
public static int getRing(final int dX, final int dZ){
public static int getRing(final int dX, final int dZ) {
return Math.max(Math.abs(dX), Math.abs(dZ));
}
@@ -25,12 +25,15 @@ public class Pos {
public static int getYSize(LevelHeightAccessor view) {
return view.getHeight();
}
public static int getMinY(LevelHeightAccessor view) {
return view.getMinY();
}
public static int getMaxYInclusive(LevelHeightAccessor view) {
return view.getMaxY();
}
public static int getMaxYExclusive(LevelHeightAccessor view) {
return view.getMaxY() + 1;
}
@@ -39,7 +42,7 @@ public class Pos {
return 15 + getMinInSectionCoord(sectionCoord);
}
public static int getMaxYInSectionIndex(LevelHeightAccessor view, int sectionIndex){
public static int getMaxYInSectionIndex(LevelHeightAccessor view, int sectionIndex) {
return getMaxInSectionCoord(SectionYCoord.fromSectionIndex(view, sectionIndex));
}
@@ -66,12 +69,15 @@ public class Pos {
public static int getNumYSections(LevelHeightAccessor view) {
return view.getSectionsCount();
}
public static int getMinYSection(LevelHeightAccessor view) {
return view.getMinSectionY();
}
public static int getMaxYSectionInclusive(LevelHeightAccessor view) {
return view.getMaxSectionY();
}
public static int getMaxYSectionExclusive(LevelHeightAccessor view) {
return view.getMaxSectionY() + 1;
}
@@ -79,6 +85,7 @@ public class Pos {
public static int fromSectionIndex(LevelHeightAccessor view, int sectionCoord) {
return sectionCoord + SectionYCoord.getMinYSection(view);
}
public static int fromBlockCoord(int blockCoord) {
return SectionPos.blockToSectionCoord(blockCoord);
}
@@ -88,12 +95,15 @@ public class Pos {
public static int getNumYSections(LevelHeightAccessor view) {
return view.getSectionsCount();
}
public static int getMinYSectionIndex(LevelHeightAccessor view) {
return 0;
}
public static int getMaxYSectionIndexInclusive(LevelHeightAccessor view) {
return view.getSectionsCount() - 1;
}
public static int getMaxYSectionIndexExclusive(LevelHeightAccessor view) {
return view.getSectionsCount();
}
@@ -102,6 +112,7 @@ public class Pos {
public static int fromSectionCoord(LevelHeightAccessor view, int sectionCoord) {
return sectionCoord - SectionYCoord.getMinYSection(view);
}
public static int fromBlockCoord(LevelHeightAccessor view, int blockCoord) {
return fromSectionCoord(view, SectionPos.blockToSectionCoord(blockCoord));
}
@@ -50,7 +50,7 @@ public class FixedChunkAccessSectionBitBuffer {
this.numSections = yLength * xLength * zLength;
this.chunkSectionBits = new BitSet(numSections);
this.chunkAccesses = new ArrayList<>(Collections.nCopies(xLength * zLength,null));
this.chunkAccesses = new ArrayList<>(Collections.nCopies(xLength * zLength, null));
}
public FixedChunkAccessSectionBitBuffer(BlockPos center, int horizontalRangeInclusive, int verticalRangeInclusive) {
@@ -102,11 +102,11 @@ public class FixedChunkAccessSectionBitBuffer {
return this.getChunkIndex(ChunkPos.getX(chunkPos), ChunkPos.getZ(chunkPos));
}
public ChunkAccess getChunkAccess(long chunkPos){
public ChunkAccess getChunkAccess(long chunkPos) {
return this.chunkAccesses.get(this.getChunkIndex(chunkPos));
}
public ChunkAccess getChunkAccess(BlockPos blockPos){
public ChunkAccess getChunkAccess(BlockPos blockPos) {
return this.getChunkAccess(ChunkPos.pack(blockPos));
}
@@ -118,14 +118,14 @@ public class FixedChunkAccessSectionBitBuffer {
this.setChunkAccess(ChunkPos.pack(blockPos), chunkAccess);
}
public boolean hasNoTrueChunkSections(){
public boolean hasNoTrueChunkSections() {
return this.chunkSectionBits.nextSetBit(0) == -1;
}
public LongIterable getChunkPosInRange() {
return new LongIterable() {
@Override
public @NotNull LongIterator iterator(){
public @NotNull LongIterator iterator() {
return getChunkPosInRangeIterator();
}
};
@@ -141,7 +141,7 @@ public class FixedChunkAccessSectionBitBuffer {
int z = zMin;
@Override
public long nextLong () {
public long nextLong() {
long result = ChunkPos.pack(x, z);
if (z < zMax) {
z++;
@@ -153,7 +153,7 @@ public class FixedChunkAccessSectionBitBuffer {
}
@Override
public boolean hasNext(){
public boolean hasNext() {
return x <= xMax;
}
};
@@ -162,7 +162,7 @@ public class FixedChunkAccessSectionBitBuffer {
public IntIterable getSectionYInRange() {
return new IntIterable() {
@Override
public @NotNull IntIterator iterator(){
public @NotNull IntIterator iterator() {
return getSectionYInRangeIterator();
}
};
@@ -175,12 +175,12 @@ public class FixedChunkAccessSectionBitBuffer {
int y = yMin;
@Override
public int nextInt(){
public int nextInt() {
return y++;
}
@Override
public boolean hasNext(){
public boolean hasNext() {
return y < yLimit;
}
};