Skip to content

Commit 0b27652

Browse files
author
CoolLoong
committed
feat: more work
1 parent 35a8cf9 commit 0b27652

16 files changed

+640
-241
lines changed

src/main/java/cn/nukkit/Server.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -923,15 +923,15 @@ public Level remove(Object key) {
923923
this.setPropertyString("level-name", defaultName);
924924
}
925925

926-
if (!this.loadLevel(defaultName)) {
926+
if (!this.loadLevel(defaultName)) {//default world not exist
927927
long seed;
928928
String seedString = String.valueOf(this.getProperty("level-seed", System.currentTimeMillis()));
929929
try {
930930
seed = Long.parseLong(seedString);
931931
} catch (NumberFormatException e) {
932932
seed = seedString.hashCode();
933933
}
934-
this.generateLevel(defaultName, seed == 0 ? System.currentTimeMillis() : seed);
934+
this.generateLevel(defaultName, seed == 0 ? System.currentTimeMillis() : seed);//generate the default world
935935
}
936936

937937
this.setDefaultLevel(this.getLevelByName(defaultName));

src/main/java/cn/nukkit/level/GameRule.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ public enum GameRule {
4646
TNT_EXPLODES("tntExplodes"),
4747
@PowerNukkitOnly EXPERIMENTAL_GAMEPLAY("experimentalGameplay"),
4848
SHOW_TAGS("showTags"),
49-
PLAYERS_SLEEPING_PERCENTAGE("playersSleepingPercentage");
49+
PLAYERS_SLEEPING_PERCENTAGE("playersSleepingPercentage"),
50+
DO_LIMITED_CRAFTING("dolimitedcrafting"),
51+
RESPAWN_BLOCKS_EXPLODE("respawnBlocksExplode"),
52+
SHOW_BORDER_EFFECT("showBorderEffect");
5053

5154
@PowerNukkitOnly
5255
@Since("1.4.0.0-PN")

src/main/java/cn/nukkit/level/GameRules.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cn.nukkit.level;
22

33
import cn.nukkit.api.Since;
4-
import cn.nukkit.nbt.tag.CompoundTag;
4+
import cn.nukkit.nbt.tag.*;
55
import cn.nukkit.utils.BinaryStream;
66
import com.google.common.base.Preconditions;
77
import com.google.common.collect.ImmutableMap;
@@ -59,6 +59,9 @@ public static GameRules getDefault() {
5959
gameRules.gameRules.put(SHOW_TAGS, new Value<>(Type.BOOLEAN, true));
6060
gameRules.gameRules.put(EXPERIMENTAL_GAMEPLAY, new Value<>(Type.BOOLEAN, true));
6161
gameRules.gameRules.put(PLAYERS_SLEEPING_PERCENTAGE, new Value<>(Type.INTEGER, 100));
62+
gameRules.gameRules.put(DO_LIMITED_CRAFTING, new Value<>(Type.BOOLEAN, false));
63+
gameRules.gameRules.put(RESPAWN_BLOCKS_EXPLODE, new Value<>(Type.BOOLEAN, true));
64+
gameRules.gameRules.put(SHOW_BORDER_EFFECT, new Value<>(Type.BOOLEAN, true));
6265

6366
return gameRules;
6467
}
@@ -235,6 +238,21 @@ public Type getType() {
235238
return this.type;
236239
}
237240

241+
public Tag getTag() {
242+
switch (this.type) {
243+
case BOOLEAN -> {
244+
return new ByteTag(getValueAsBoolean() ? 1 : 0);
245+
}
246+
case INTEGER -> {
247+
return new IntTag(getValueAsInteger());
248+
}
249+
case FLOAT -> {
250+
return new FloatTag(getValueAsFloat());
251+
}
252+
}
253+
throw new IllegalArgumentException("unknown type");
254+
}
255+
238256
private boolean getValueAsBoolean() {
239257
if (type != Type.BOOLEAN) {
240258
throw new UnsupportedOperationException("Rule not of type boolean");

src/main/java/cn/nukkit/level/Level.java

+9-13
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@
8282
import java.util.*;
8383
import java.util.concurrent.*;
8484
import java.util.concurrent.atomic.AtomicBoolean;
85-
import java.util.function.*;
85+
import java.util.function.BiFunction;
86+
import java.util.function.BiPredicate;
87+
import java.util.function.Function;
88+
import java.util.function.Predicate;
8689
import java.util.stream.Collectors;
8790
import java.util.stream.IntStream;
8891
import java.util.stream.Stream;
@@ -311,14 +314,7 @@ public int size() {
311314
private Iterator<cn.nukkit.utils.collection.nb.LongObjectEntry<Long>> lastUsingUnloadingIter;
312315

313316
public Level(Server server, String name, String path, Class<? extends LevelProvider> provider) {
314-
this(server, name, path,
315-
() -> {
316-
try {
317-
return (boolean) provider.getMethod("usesChunkSection").invoke(null);
318-
} catch (ReflectiveOperationException e) {
319-
throw new LevelException("usesChunkSection of " + provider + " failed", e);
320-
}
321-
},
317+
this(server, name, path, true,
322318
(level, levelPath) -> {
323319
try {
324320
return provider.getConstructor(Level.class, String.class).newInstance(level, levelPath);
@@ -334,14 +330,14 @@ public Level(Server server, String name, String path, Class<? extends LevelProvi
334330
*/
335331
@Since("1.4.0.0-PN")
336332
Level(Server server, String name, File path, boolean usesChunkSection, LevelProvider provider) {
337-
this(server, name, path.getAbsolutePath() + "/", () -> usesChunkSection, (lvl, p) -> provider);
333+
this(server, name, path.getAbsolutePath() + "/", usesChunkSection, (lvl, p) -> provider);
338334
}
339335

340336
/**
341337
* Easier constructor to create PowerNukkit tests.
342338
*/
343339
@Since("1.4.0.0-PN")
344-
Level(Server server, String name, String path, BooleanSupplier usesChunkSection, BiFunction<Level, String, LevelProvider> provider) {
340+
Level(Server server, String name, String path, boolean usesChunkSection, BiFunction<Level, String, LevelProvider> provider) {
345341
this.levelId = levelIdCounter++;
346342
this.blockMetadata = new BlockMetadataStore(this);
347343
this.server = server;
@@ -1252,8 +1248,8 @@ public void doTick(int currentTick) {
12521248
}), Server.getInstance().computeThreadPool).join();
12531249
for (long id : this.updateEntities.keySetLong()) {
12541250
Entity entity = this.updateEntities.get(id);
1255-
if(entity instanceof EntityIntelligent intelligent) {
1256-
if(intelligent.getBehaviorGroup() == null) {
1251+
if (entity instanceof EntityIntelligent intelligent) {
1252+
if (intelligent.getBehaviorGroup() == null) {
12571253
this.updateEntities.remove(id);
12581254
continue;
12591255
}

src/main/java/cn/nukkit/level/generator/Flat.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
*/
2626
@Log4j2
2727
public class Flat extends Generator {
28-
2928
private final List<Populator> populators = new ArrayList<>();
30-
private final Map<String, Object> options;
3129
private ChunkManager level;
3230
private NukkitRandom random;
3331
private int[][] structure;
@@ -41,8 +39,8 @@ public Flat() {
4139
}
4240

4341
public Flat(Map<String, Object> options) {
42+
super(options);
4443
this.preset = "2;7,2x3,2;1;";
45-
this.options = options;
4644

4745
if (this.options.containsKey("decoration")) {
4846
PopulatorOre ores = new PopulatorOre(BlockID.STONE, new OreType[]{
@@ -74,11 +72,6 @@ public NukkitRandom getRandom() {
7472
return this.random;
7573
}
7674

77-
@Override
78-
public Map<String, Object> getSettings() {
79-
return this.options;
80-
}
81-
8275
@Override
8376
public String getName() {
8477
return "flat";

src/main/java/cn/nukkit/level/generator/Generator.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ public abstract class Generator implements BlockID {
6060
}
6161
}
6262

63+
protected final Map<String, Object> options;
64+
65+
public Generator(Map<String, Object> options) {
66+
this.options = options;
67+
}
68+
6369
public abstract int getId();
6470

6571
@PowerNukkitXOnly
@@ -125,7 +131,6 @@ public void setRandom(NukkitRandom random) {
125131
this.random = random;
126132
}
127133

128-
@Deprecated
129134
public int getDimension() {
130135
return Level.DIMENSION_OVERWORLD;
131136
}
@@ -227,7 +232,9 @@ public void populateStructure(int chunkX, int chunkZ) {
227232
}
228233
}
229234

230-
public abstract Map<String, Object> getSettings();
235+
public Map<String, Object> getSettings() {
236+
return this.options;
237+
}
231238

232239
public abstract String getName();
233240

@@ -248,6 +255,7 @@ public boolean shouldGenerateStructures() {
248255
* 处理需要计算的异步地形生成任务<br/>
249256
* 有特殊需求的地形生成器可以覆写此方法并提供自己的逻辑<br/>
250257
* 默认采用Server类的fjp线程池
258+
*
251259
* @param task 地形生成任务
252260
*/
253261
@PowerNukkitXOnly
@@ -262,6 +270,7 @@ public void handleAsyncChunkPopTask(AsyncTask task) {
262270
* 处理需要计算的异步结构生成任务<br/>
263271
* 有特殊需求的地形生成器可以覆写此方法并提供自己的逻辑<br/>
264272
* 默认采用Server类的fjp线程池
273+
*
265274
* @param task 结构生成任务
266275
*/
267276
@PowerNukkitXOnly

src/main/java/cn/nukkit/level/generator/Nether.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public Nether() {
4646
}
4747

4848
public Nether(Map<String, Object> options) {
49+
super(options);
4950
//Nothing here. Just used for future update.
5051
}
5152

@@ -64,11 +65,6 @@ public String getName() {
6465
return "nether";
6566
}
6667

67-
@Override
68-
public Map<String, Object> getSettings() {
69-
return new HashMap<>();
70-
}
71-
7268
@Override
7369
public ChunkManager getChunkManager() {
7470
return level;

src/main/java/cn/nukkit/level/generator/Normal.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public Normal() {
148148

149149
public Normal(Map<String, Object> options) {
150150
//Nothing here. Just used for future update.
151+
super(options);
151152
}
152153

153154
@Override
@@ -170,11 +171,6 @@ public String getName() {
170171
return "normal";
171172
}
172173

173-
@Override
174-
public Map<String, Object> getSettings() {
175-
return Collections.emptyMap();
176-
}
177-
178174
public Biome pickBiome(int x, int z) {
179175
return this.selector.pickBiome(x, z);
180176
}

src/main/java/cn/nukkit/level/generator/TerraGeneratorWrapper.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ public class TerraGeneratorWrapper extends Generator {
2929
protected static final Map<Integer, TerraGenerator> generators = new Int2ObjectOpenHashMap<>();
3030
//共享的Terra实例
3131
protected volatile TerraGenerator terra;
32-
protected final Map<String, Object> option;
33-
3432

3533
public TerraGeneratorWrapper(Map<String, Object> option) {
36-
this.option = option;
34+
super(option);
3735
}
3836

3937
@Override
@@ -42,7 +40,7 @@ public void init(ChunkManager chunkManager, NukkitRandom random) {
4240
if (this.terra == null) {
4341
synchronized (generators) {
4442
if (this.terra == null) {
45-
this.terra = new TerraGenerator(this.option, getLevel());
43+
this.terra = new TerraGenerator(this.options, getLevel());
4644
generators.put(getLevel().getId(), this.terra);
4745
}
4846
}

src/main/java/cn/nukkit/level/generator/TheEnd.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public TheEnd() {
9494

9595
public TheEnd(Map<String, Object> options) {
9696
// Nothing here. Just used for future update.
97+
super(options);
9798
}
9899

99100
@Override
@@ -111,11 +112,6 @@ public String getName() {
111112
return "the_end";
112113
}
113114

114-
@Override
115-
public Map<String, Object> getSettings() {
116-
return new HashMap<>();
117-
}
118-
119115
@Override
120116
public ChunkManager getChunkManager() {
121117
return this.level;

src/main/java/cn/nukkit/level/newformat/Chunk.java

+28-7
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
import cn.nukkit.entity.Entity;
66
import cn.nukkit.level.DimensionData;
77
import cn.nukkit.level.biome.Biome;
8+
import cn.nukkit.nbt.tag.CompoundTag;
89
import cn.nukkit.utils.collection.nb.Long2ObjectNonBlockingMap;
910
import com.google.common.base.Preconditions;
1011

1112
import java.io.IOException;
13+
import java.util.ArrayList;
14+
import java.util.List;
1215
import java.util.Map;
1316

1417
/**
@@ -19,6 +22,9 @@
1922
public class Chunk implements IChunk {
2023
protected final Map<Long, Entity> entities;
2124
protected final Map<Long, BlockEntity> blockEntities;
25+
//delay load block entity and entity
26+
protected List<CompoundTag> blockEntityNBT;
27+
protected List<CompoundTag> entityNBT;
2228
protected final LevelProvider provider;
2329
protected final ChunkSection[] sections;
2430
protected final short[] heightMap;//256 size
@@ -41,6 +47,8 @@ private Chunk(
4147
this.heightMap = new short[256];
4248
this.entities = new Long2ObjectNonBlockingMap<>();
4349
this.blockEntities = new Long2ObjectNonBlockingMap<>();
50+
this.entityNBT = new ArrayList<>();
51+
this.blockEntityNBT = new ArrayList<>();
4452
}
4553

4654
private Chunk(
@@ -51,7 +59,9 @@ private Chunk(
5159
final ChunkSection[] sections,
5260
final short[] heightMap,
5361
final Map<Long, Entity> entities,
54-
final Map<Long, BlockEntity> blockEntities
62+
final Map<Long, BlockEntity> blockEntities,
63+
final List<CompoundTag> entityNBT,
64+
final List<CompoundTag> blockEntityNBT
5565
) {
5666
this.chunkState = state;
5767
this.x = chunkX;
@@ -61,6 +71,8 @@ private Chunk(
6171
this.heightMap = heightMap;
6272
this.entities = entities;
6373
this.blockEntities = blockEntities;
74+
this.entityNBT = entityNBT;
75+
this.blockEntityNBT = blockEntityNBT;
6476
}
6577

6678
@Override
@@ -380,8 +392,10 @@ public static class Builder implements IChunkBuilder {
380392
cn.nukkit.level.newformat.LevelProvider levelProvider;
381393
ChunkSection[] sections;
382394
short[] heightMap;
383-
Map<Long, Entity> entities;
384-
Map<Long, BlockEntity> blockEntities;
395+
List<CompoundTag> entities;
396+
List<CompoundTag> blockEntities;
397+
List<CompoundTag> blockEntityNBT;
398+
List<CompoundTag> entityNBT;
385399

386400
public Builder chunkX(int chunkX) {
387401
this.chunkX = chunkX;
@@ -424,17 +438,22 @@ public Builder sections(ChunkSection[] sections) {
424438
return this;
425439
}
426440

441+
@Override
442+
public ChunkSection[] getSections() {
443+
return sections;
444+
}
445+
427446
public Builder heightMap(short[] heightMap) {
428447
this.heightMap = heightMap;
429448
return this;
430449
}
431450

432-
public Builder entities(Map<Long, Entity> entities) {
451+
public Builder entities(List<CompoundTag> entities) {
433452
this.entities = entities;
434453
return this;
435454
}
436455

437-
public Builder blockEntities(Map<Long, BlockEntity> blockEntities) {
456+
public Builder blockEntities(List<CompoundTag> blockEntities) {
438457
this.blockEntities = blockEntities;
439458
return this;
440459
}
@@ -444,15 +463,17 @@ public Chunk build() {
444463
if (state == null) state = ChunkState.NEW;
445464
if (sections == null) sections = new ChunkSection[levelProvider.getDimensionData().getChunkSectionCount()];
446465
if (heightMap == null) heightMap = new short[256];
447-
if (entities == null) entities = new Long2ObjectNonBlockingMap<>();
448-
if (blockEntities == null) blockEntities = new Long2ObjectNonBlockingMap<>();
466+
if (entities == null) entities = new ArrayList<>();
467+
if (blockEntities == null) blockEntities = new ArrayList<>();
449468
return new Chunk(
450469
state,
451470
chunkX,
452471
chunkZ,
453472
levelProvider,
454473
sections,
455474
heightMap,
475+
new Long2ObjectNonBlockingMap<>(),
476+
new Long2ObjectNonBlockingMap<>(),
456477
entities,
457478
blockEntities
458479
);

0 commit comments

Comments
 (0)