Skip to content

Commit

Permalink
Fixes & updates (#2208)
Browse files Browse the repository at this point in the history
- Fixed players becoming invisible after sleeping
- Fixed server hanging when reading corrupted Zlib input
- Updated enchanted golden apple regeneration amplifier to match vanilla
- Bed/anchor explosions can now create fire
- Bamboo from world generator can generate taller
- Conduits now give conduit power effect
- Froglight now rotates correctly when placed
- Using ThreadLocalRandom
  • Loading branch information
PetteriM1 authored Dec 21, 2024
1 parent d02928d commit 4da37b2
Show file tree
Hide file tree
Showing 76 changed files with 589 additions and 202 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ leveldbjni = { group = "net.daporkchop", name = "leveldb-mcpe-jni", version = "0
snappy = { group = "org.xerial.snappy", name = "snappy-java", version = "1.1.10.7" }
jwt = { group = "com.nimbusds", name = "nimbus-jose-jwt", version = "9.23" }
jopt-simple = { group = "net.sf.jopt-simple", name = "jopt-simple", version = "5.0.4" }
blockstateupdater = { group = "org.cloudburstmc", name = "block-state-updater", version = "1.21.30-SNAPSHOT" }
blockstateupdater = { group = "org.cloudburstmc", name = "block-state-updater", version = "1.21.40-SNAPSHOT" }
lmbda = { group = "org.lanternpowered", name = "lmbda", version = "2.0.0" }
noise = { group = "net.daporkchop.lib", name = "noise", version = "0.5.6-SNAPSHOT" }
lombok = { group = "org.projectlombok", name = "lombok", version = "1.18.36" }
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import java.util.*;
import java.util.Queue;
import java.util.Map.Entry;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
Expand Down Expand Up @@ -1477,7 +1478,7 @@ public void stopSleep() {
this.server.getPluginManager().callEvent(new PlayerBedLeaveEvent(this, this.level.getBlock(this.sleeping)));

this.sleeping = null;
this.setDataProperty(new IntPositionEntityData(DATA_PLAYER_BED_POSITION, 0, 0, 0));
this.removeDataProperty(DATA_PLAYER_BED_POSITION);
this.setDataFlag(DATA_PLAYER_FLAGS, DATA_PLAYER_FLAG_SLEEP, false);

this.level.sleepTicks = 0;
Expand Down Expand Up @@ -2085,7 +2086,7 @@ protected void handleMovement(Vector3 newPos) {
server.getPluginManager().callEvent(waterFrostEvent);
if (!waterFrostEvent.isCancelled()) {
level.setBlockAt((int) block.x, (int) block.y, (int) block.z, Block.ICE_FROSTED, 0);
level.scheduleUpdate(level.getBlock(this.chunk, block.getFloorX(), block.getFloorY(), block.getFloorZ(), true), Utils.random.nextInt(20, 40));
level.scheduleUpdate(level.getBlock(this.chunk, block.getFloorX(), block.getFloorY(), block.getFloorZ(), true), ThreadLocalRandom.current().nextInt(20, 40));
}
}
}
Expand Down Expand Up @@ -6938,7 +6939,7 @@ public boolean pickupEntity(Entity entity, boolean near) {
}

if (!itemsWithMending.isEmpty()) {
int itemToRepair = itemsWithMending.getInt(Utils.random.nextInt(itemsWithMending.size()));
int itemToRepair = itemsWithMending.getInt(ThreadLocalRandom.current().nextInt(itemsWithMending.size()));
boolean isOffhand = itemToRepair == -1;

Item repaired = isOffhand ? offhand : this.inventory.getItem(itemToRepair);
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/cn/nukkit/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -2111,7 +2112,7 @@ public boolean loadLevel(String name) {
* @return generated
*/
public boolean generateLevel(String name) {
return this.generateLevel(name, Utils.random.nextLong());
return this.generateLevel(name, ThreadLocalRandom.current().nextLong());
}

/**
Expand Down Expand Up @@ -2772,6 +2773,9 @@ private static void registerBlockEntities() {
BlockEntity.registerBlockEntity(BlockEntity.BLAST_FURNACE, BlockEntityBlastFurnace.class);
BlockEntity.registerBlockEntity(BlockEntity.SMOKER, BlockEntitySmoker.class);
BlockEntity.registerBlockEntity(BlockEntity.BELL, BlockEntityBell.class);
BlockEntity.registerBlockEntity(BlockEntity.CONDUIT, BlockEntityConduit.class);

// Persistent container, not on vanilla
BlockEntity.registerBlockEntity(BlockEntity.PERSISTENT_CONTAINER, PersistentDataContainerBlockEntity.class);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/cn/nukkit/block/BlockBed.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public boolean onActivate(Item item, Player player) {
}

Explosion explosion = new Explosion(this.add(0.5, 0, 0.5), 5, this);
explosion.setFireSpawnChance(0.3333);
explosion.explodeA();
explosion.explodeB();
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/cn/nukkit/block/BlockBeetroot.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package cn.nukkit.block;

import cn.nukkit.item.Item;
import cn.nukkit.utils.Utils;

import java.util.concurrent.ThreadLocalRandom;

/**
* Created on 2015/11/22 by xtypr.
Expand Down Expand Up @@ -36,7 +37,7 @@ public Item[] getDrops(Item item) {
if (this.getDamage() >= 0x07) {
return new Item[]{
Item.get(Item.BEETROOT, 0, 1),
Item.get(Item.BEETROOT_SEEDS, 0, Utils.random.nextInt(0, 4))
Item.get(Item.BEETROOT_SEEDS, 0, ThreadLocalRandom.current().nextInt(0, 4))
};
} else {
return new Item[]{
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/block/BlockCauldron.java
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ public void clearWithFizz(BlockEntityCauldron cauldron) {
cauldron.clearCustomColor();
this.level.setBlock(this, Block.get(CAULDRON_BLOCK), true);
this.level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_FIZZ);
this.getLevel().addParticle(new SmokeParticle(add(Math.random(), 1.2, Math.random())), null, 8);
this.getLevel().addParticle(new SmokeParticle(add(ThreadLocalRandom.current().nextDouble(), 1.2, ThreadLocalRandom.current().nextDouble())), null, 8);
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/cn/nukkit/block/BlockChorusFlower.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ public int onUpdate(int type) {
}
// Grow Horizontally
} else if (!isFullyAged()) {
for (int i = 0; i < ThreadLocalRandom.current().nextInt(ground ? 5 : 4); i++) {
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int i = 0; i < random.nextInt(ground ? 5 : 4); i++) {
BlockFace face = BlockFace.Plane.HORIZONTAL.random();
Block check = this.getSide(face);
if (check.getId() == AIR && check.down().getId() == AIR && isHorizontalAirExcept(check, face.getOpposite())) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/cn/nukkit/block/BlockCocoa.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import cn.nukkit.utils.Faceable;
import cn.nukkit.utils.Utils;

import java.util.concurrent.ThreadLocalRandom;

/**
* Created by CreeperFace on 27. 10. 2016.
*/
Expand Down Expand Up @@ -111,7 +113,7 @@ public int onUpdate(int type) {
return Level.BLOCK_UPDATE_NORMAL;
}
} else if (type == Level.BLOCK_UPDATE_RANDOM) {
if (Utils.random.nextInt(2) == 1) {
if (ThreadLocalRandom.current().nextInt(2) == 1) {
if (this.getDamage() >> 2 < 2) {
BlockCocoa block = (BlockCocoa) this.clone();
block.setDamage(block.getDamage() + 4);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/cn/nukkit/block/BlockComposter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import cn.nukkit.item.*;
import cn.nukkit.level.Sound;
import cn.nukkit.utils.DyeColor;
import cn.nukkit.utils.Utils;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;

import java.util.concurrent.ThreadLocalRandom;

public class BlockComposter extends BlockTransparentMeta implements ItemID {

private static final Int2IntOpenHashMap ITEMS = new Int2IntOpenHashMap();
Expand Down Expand Up @@ -109,7 +110,7 @@ public boolean onActivate(Item item, Player player) {
return false;
}

boolean success = Utils.random.nextInt(100) < chance;
boolean success = ThreadLocalRandom.current().nextInt(100) < chance;
ComposterFillEvent event = new ComposterFillEvent(this, player, item, chance, success);
this.level.getServer().getPluginManager().callEvent(event);

Expand Down
15 changes: 14 additions & 1 deletion src/main/java/cn/nukkit/block/BlockConduit.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package cn.nukkit.block;

import cn.nukkit.Player;
import cn.nukkit.blockentity.BlockEntity;
import cn.nukkit.item.Item;
import cn.nukkit.item.ItemTool;
import cn.nukkit.math.BlockFace;

public class BlockConduit extends BlockSolidMeta {
public class BlockConduit extends BlockTransparentMeta {

public BlockConduit() {
this(0);
Expand Down Expand Up @@ -56,4 +60,13 @@ public WaterloggingType getWaterloggingType() {
public boolean alwaysDropsOnExplosion() {
return true;
}

@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
if (this.getLevel().setBlock(this, this, true, true)) {
BlockEntity.createBlockEntity(BlockEntity.CONDUIT, this.getChunk(), BlockEntity.getDefaultCompound(this, BlockEntity.CONDUIT));
return true;
}
return false;
}
}
7 changes: 4 additions & 3 deletions src/main/java/cn/nukkit/block/BlockCrops.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import cn.nukkit.level.particle.BoneMealParticle;
import cn.nukkit.math.BlockFace;
import cn.nukkit.utils.BlockColor;
import cn.nukkit.utils.Utils;

import java.util.concurrent.ThreadLocalRandom;

/**
* @author MagicDroidX
Expand Down Expand Up @@ -40,7 +41,7 @@ public boolean onActivate(Item item, Player player) {
if (item.getId() == Item.DYE && item.getDamage() == ItemDye.BONE_MEAL) {
if (this.getDamage() < 7) {
BlockCrops block = (BlockCrops) this.clone();
block.setDamage(block.getDamage() + Utils.random.nextInt(3) + 2);
block.setDamage(block.getDamage() + ThreadLocalRandom.current().nextInt(3) + 2);
if (block.getDamage() > 7) {
block.setDamage(7);
}
Expand Down Expand Up @@ -74,7 +75,7 @@ public int onUpdate(int type) {
return Level.BLOCK_UPDATE_NORMAL;
}
} else if (type == Level.BLOCK_UPDATE_RANDOM) {
if (Utils.random.nextInt(2) == 1) {
if (ThreadLocalRandom.current().nextInt(2) == 1) {
if (this.getDamage() < 0x07) {
BlockCrops block = (BlockCrops) this.clone();
block.setDamage(block.getDamage() + 1);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/cn/nukkit/block/BlockDeadBush.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import cn.nukkit.level.Level;
import cn.nukkit.math.BlockFace;
import cn.nukkit.utils.BlockColor;
import cn.nukkit.utils.Utils;

import java.util.concurrent.ThreadLocalRandom;

/**
* Created on 2015/12/2 by xtypr.
Expand Down Expand Up @@ -68,7 +69,7 @@ public Item[] getDrops(Item item) {
};
} else {
return new Item[]{
Item.get(Item.STICK, 0, Utils.random.nextInt(3))
Item.get(Item.STICK, 0, ThreadLocalRandom.current().nextInt(3))
};
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/cn/nukkit/block/BlockDispenser.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import cn.nukkit.nbt.tag.StringTag;
import cn.nukkit.network.protocol.LevelSoundEventPacket;
import cn.nukkit.utils.Faceable;
import cn.nukkit.utils.Utils;

import java.util.Map.Entry;
import java.util.concurrent.ThreadLocalRandom;

/**
* Created by CreeperFace on 15.4.2017.
Expand Down Expand Up @@ -163,10 +163,11 @@ public void dispense() {
Item target = null;

Inventory inv = ((BlockEntityDispenser) blockEntity).getInventory();
ThreadLocalRandom random = ThreadLocalRandom.current();
for (Entry<Integer, Item> entry : inv.getContents().entrySet()) {
Item item = entry.getValue();

if (!item.isNull() && Utils.random.nextInt(r++) == 0) {
if (!item.isNull() && random.nextInt(r++) == 0) {
target = item;
slot = entry.getKey();
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/cn/nukkit/block/BlockDoublePlant.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import cn.nukkit.level.particle.BoneMealParticle;
import cn.nukkit.math.BlockFace;
import cn.nukkit.utils.BlockColor;
import cn.nukkit.utils.Utils;

import java.util.concurrent.ThreadLocalRandom;

/**
* Created on 2015/11/23 by xtypr.
Expand Down Expand Up @@ -115,7 +116,7 @@ public Item[] getDrops(Item item) {
switch (type) {
case TALL_GRASS:
case LARGE_FERN:
boolean dropSeeds = Utils.random.nextInt(10) == 0;
boolean dropSeeds = ThreadLocalRandom.current().nextInt(10) == 0;
if (item.isShears()) {
if (dropSeeds) {
return new Item[]{
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/cn/nukkit/block/BlockDragonEgg.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import cn.nukkit.level.Level;
import cn.nukkit.network.protocol.LevelEventPacket;
import cn.nukkit.utils.BlockColor;
import cn.nukkit.utils.Utils;

import java.util.concurrent.ThreadLocalRandom;

public class BlockDragonEgg extends BlockFallable {

Expand Down Expand Up @@ -52,8 +53,9 @@ public int onUpdate(int type) {
}

public void teleport() {
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int i = 0; i < 1000; ++i) {
Block to = this.getLevel().getBlock(this.add(Utils.random.nextInt(-16, 16), Utils.random.nextInt(-16, 16), Utils.random.nextInt(-16, 16)));
Block to = this.getLevel().getBlock(this.add(random.nextInt(-16, 16), random.nextInt(-16, 16), random.nextInt(-16, 16)));
if (to.getId() == AIR) {
BlockFromToEvent event = new BlockFromToEvent(this, to);
this.level.getServer().getPluginManager().callEvent(event);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cn/nukkit/block/BlockDropper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import cn.nukkit.nbt.tag.StringTag;
import cn.nukkit.network.protocol.LevelSoundEventPacket;
import cn.nukkit.utils.Faceable;
import cn.nukkit.utils.Utils;

import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
Expand Down Expand Up @@ -138,10 +137,11 @@ public void dispense() {
Item target = null;

Inventory inv = ((BlockEntityDropper) blockEntity).getInventory();
ThreadLocalRandom random = ThreadLocalRandom.current();
for (Map.Entry<Integer, Item> entry : inv.getContents().entrySet()) {
Item item = entry.getValue();

if (!item.isNull() && Utils.random.nextInt(r++) == 0) {
if (!item.isNull() && random.nextInt(r++) == 0) {
target = item;
slot = entry.getKey();
}
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/cn/nukkit/block/BlockFire.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import cn.nukkit.potion.Effect;
import cn.nukkit.potion.Potion;
import cn.nukkit.utils.BlockColor;
import cn.nukkit.utils.Utils;

import java.util.concurrent.ThreadLocalRandom;

/**
* @author MagicDroidX
Expand Down Expand Up @@ -138,21 +139,22 @@ public int onUpdate(int type) {
}

int meta = this.getDamage();
ThreadLocalRandom random = ThreadLocalRandom.current();

if (meta < 15) {
int newMeta = meta + Utils.random.nextInt(3);
int newMeta = meta + random.nextInt(3);
if (newMeta > 15) newMeta = 15;
this.setDamage(newMeta);
this.getLevel().setBlock((int) this.x, (int) this.y, (int) this.z, BlockLayer.NORMAL, this, false, true, false); // No need to send this to client
}

this.getLevel().scheduleUpdate(this, this.tickRate() + Utils.random.nextInt(10));
this.getLevel().scheduleUpdate(this, this.tickRate() + random.nextInt(10));

if (!forever && !this.canNeighborBurn()) {
if (!this.isBlockTopFacingSurfaceSolid(this.down()) || meta > 3) {
this.getLevel().setBlock(this, Block.get(BlockID.AIR), true);
}
} else if (!forever && !(this.down().getBurnAbility() > 0) && meta == 15 && Utils.random.nextInt(4) == 0) {
} else if (!forever && !(this.down().getBurnAbility() > 0) && meta == 15 && random.nextInt(4) == 0) {
this.getLevel().setBlock(this, Block.get(BlockID.AIR), true);
} else {
int o = 0;
Expand Down Expand Up @@ -191,8 +193,8 @@ public int onUpdate(int type) {

//TODO: decrease the t if the rainfall values are high

if (t > 0 && Utils.random.nextInt(k) <= t) {
int damage = meta + (Utils.random.nextInt(5) >> 2);
if (t > 0 && random.nextInt(k) <= t) {
int damage = meta + (random.nextInt(5) >> 2);

if (damage > 15) {
damage = 15;
Expand Down Expand Up @@ -223,9 +225,10 @@ private void tryToCatchBlockOnFire(Block block, int bound, int damage) {
return;
}

if (Utils.random.nextInt(bound) < burnAbility) {
if (Utils.random.nextInt(damage + 10) < 5) {
int meta = damage + (Utils.random.nextInt(5) >> 2);
ThreadLocalRandom random = ThreadLocalRandom.current();
if (random.nextInt(bound) < burnAbility) {
if (random.nextInt(damage + 10) < 5) {
int meta = damage + (random.nextInt(5) >> 2);

if (meta > 15) {
meta = 15;
Expand Down
Loading

0 comments on commit 4da37b2

Please sign in to comment.