Skip to content

Commit

Permalink
Backport some fixes from leveldb branch
Browse files Browse the repository at this point in the history
  • Loading branch information
PetteriM1 authored Aug 13, 2024
1 parent 3da731d commit 9cc88d0
Show file tree
Hide file tree
Showing 21 changed files with 226 additions and 120 deletions.
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/Achievement.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Achievement {
put("buildWorkBench", new Achievement("Benchmarking", "mineWood"));
put("buildPickaxe", new Achievement("Time to Mine!", "buildWorkBench"));
put("buildFurnace", new Achievement("Hot Topic", "buildPickaxe"));
put("acquireIron", new Achievement("Acquire hardware", "buildFurnace"));
put("acquireIron", new Achievement("Acquire Hardware", "buildFurnace"));
put("buildHoe", new Achievement("Time to Farm!", "buildWorkBench"));
put("makeBread", new Achievement("Bake Bread", "buildHoe"));
put("bakeCake", new Achievement("The Lie", "buildHoe"));
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/cn/nukkit/AdventureSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,18 @@ public boolean get(Type type) {
return value == null ? type.getDefaultValue() : value;
}

/**
* Send adventure settings values to the player
*/
public void update() {
this.update(true);
}

/**
* Send adventure settings values to the player
* @param reset reset in air ticks
*/
void update(boolean reset) {
UpdateAbilitiesPacket packet = new UpdateAbilitiesPacket();
packet.setEntityId(player.getId());
packet.setCommandPermission(player.isOp() ? UpdateAbilitiesPacket.CommandPermission.OPERATOR : UpdateAbilitiesPacket.CommandPermission.NORMAL);
Expand Down Expand Up @@ -102,7 +113,9 @@ public void update() {

player.dataPacket(packet);
player.dataPacket(adventurePacket);
player.resetInAirTicks();
if (reset) {
player.resetInAirTicks();
}
}

public enum Type {
Expand Down
75 changes: 45 additions & 30 deletions src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public class Player extends EntityHuman implements CommandSender, InventoryHolde
protected boolean removeFormat = true;

protected String username;
private String unverifiedUsername = "";
protected String iusername;
protected String displayName;

Expand Down Expand Up @@ -1552,7 +1553,7 @@ protected void handleMovement(Vector3 clientPos) {
else this.speed.setComponents(0, 0, 0);
}

if ((this.isFoodEnabled() || this.getServer().getDifficulty() == 0) && distance >= 0.05) {
if ((this.isFoodEnabled() || this.getServer().getDifficulty() == 0) && distance >= 0.05 && this.riding == null) {
double jump = 0;
double swimming = this.isInsideOfWater() ? 0.015 * distance : 0;
double distance2 = distance;
Expand Down Expand Up @@ -1711,11 +1712,11 @@ public boolean onUpdate(int currentTick) {

if (!this.isSpectator() && this.speed != null) {
if (this.onGround) {
if (this.inAirTicks != 0) {
this.startAirTicks = 5;
if (this.isGliding()) {
this.setGliding(false);
}
this.inAirTicks = 0;
this.highestPosition = this.y;

this.resetFallDistance();
} else {
if (this.checkMovement && !this.isGliding() && !server.getAllowFlight() && !this.getAdventureSettings().get(Type.ALLOW_FLIGHT) && this.inAirTicks > 20 && !this.isSleeping() && !this.isImmobile() && !this.isSwimming() && this.riding == null && !this.hasEffect(Effect.LEVITATION) && !this.hasEffect(Effect.SLOW_FALLING)) {
double expectedVelocity = (-this.getGravity()) / ((double) this.getDrag()) - ((-this.getGravity()) / ((double) this.getDrag())) * Math.exp(-((double) this.getDrag()) * ((double) (this.inAirTicks - this.startAirTicks)));
Expand Down Expand Up @@ -2162,11 +2163,17 @@ public void handleDataPacket(DataPacket packet) {
this.loginPacketReceived = true;

LoginPacket loginPacket = (LoginPacket) packet;
this.username = TextFormat.clean(loginPacket.username);
this.displayName = this.username;
this.iusername = this.username.toLowerCase();

this.setDataProperty(new StringEntityData(DATA_NAMETAG, this.username), false);
this.unverifiedUsername = TextFormat.clean(loginPacket.username);

if (loginPacket.skin == null) {
this.close("", "disconnectionScreen.invalidSkin");
return;
}

if (this.server.getOnlinePlayers().size() >= this.server.getMaxPlayers() && this.kick(PlayerKickEvent.Reason.SERVER_FULL, "disconnectionScreen.serverFull", false)) {
return;
}

this.loginChainData = ClientChainData.read(loginPacket);

Expand All @@ -2175,9 +2182,12 @@ public void handleDataPacket(DataPacket packet) {
break;
}

if (this.server.getOnlinePlayers().size() >= this.server.getMaxPlayers() && this.kick(PlayerKickEvent.Reason.SERVER_FULL, "disconnectionScreen.serverFull", false)) {
break;
}
// Do not set username before the user is authenticated
this.username = this.unverifiedUsername;
this.unverifiedUsername = null;
this.displayName = this.username;
this.iusername = this.username.toLowerCase();
this.setDataProperty(new StringEntityData(DATA_NAMETAG, this.username), false);

this.randomClientId = loginPacket.clientId;

Expand Down Expand Up @@ -2560,7 +2570,7 @@ public void onCompletion(Server server) {
}
this.getServer().getPluginManager().callEvent(playerToggleFlightEvent);
if (playerToggleFlightEvent.isCancelled()) {
this.getAdventureSettings().update();
this.getAdventureSettings().update(false);
} else {
this.getAdventureSettings().set(AdventureSettings.Type.FLYING, playerToggleFlightEvent.isFlying());
}
Expand All @@ -2573,13 +2583,13 @@ public void onCompletion(Server server) {
}
this.getServer().getPluginManager().callEvent(playerToggleFlightEvent);
if (playerToggleFlightEvent.isCancelled()) {
this.getAdventureSettings().update();
this.getAdventureSettings().update(false);
} else {
this.getAdventureSettings().set(AdventureSettings.Type.FLYING, playerToggleFlightEvent.isFlying());
}
}

Vector3 clientPosition = authPacket.getPosition().subtract(0, this.getEyeHeight(), 0).asVector3();
Vector3 clientPosition = authPacket.getPosition().subtract(0, this.riding == null ? this.getBaseOffset() : this.riding.getMountedOffset(this).getY(), 0).asVector3();

double distSqrt = clientPosition.distanceSquared(this);
if (distSqrt == 0.0 && authPacket.getYaw() % 360 == this.yaw && authPacket.getPitch() % 360 == this.pitch) {
Expand Down Expand Up @@ -2956,6 +2966,17 @@ public void onCompletion(Server server) {
pk.wasServerInitiated = false;
pk.windowId = -1;
this.dataPacket(pk);
} else { // Close bugged inventory
ContainerClosePacket pk = new ContainerClosePacket();
pk.windowId = containerClosePacket.windowId;
pk.wasServerInitiated = false;
this.dataPacket(pk);

for (Inventory open : new ArrayList<>(this.windows.keySet())) {
if (open instanceof ContainerInventory) {
this.removeWindow(open);
}
}
}
break;
case ProtocolInfo.CRAFTING_EVENT_PACKET:
Expand Down Expand Up @@ -3002,23 +3023,13 @@ public void onCompletion(Server server) {
SetPlayerGameTypePacket setPlayerGameTypePacket1 = new SetPlayerGameTypePacket();
setPlayerGameTypePacket1.gamemode = this.gamemode & 0x01;
this.dataPacket(setPlayerGameTypePacket1);
this.getAdventureSettings().update();
this.getAdventureSettings().update(false);
break;
}
this.setGamemode(setPlayerGameTypePacket.gamemode, true);
Command.broadcastCommandMessage(this, new TranslationContainer("commands.gamemode.success.self", Server.getGamemodeString(this.gamemode)));
}
break;
case ProtocolInfo.ITEM_FRAME_DROP_ITEM_PACKET:
ItemFrameDropItemPacket itemFrameDropItemPacket = (ItemFrameDropItemPacket) packet;
Vector3 vector3 = this.temporalVector.setComponents(itemFrameDropItemPacket.x, itemFrameDropItemPacket.y, itemFrameDropItemPacket.z);
if (vector3.distanceSquared(this) < 1000) {
BlockEntity itemFrame = this.level.getBlockEntity(vector3);
if (itemFrame instanceof BlockEntityItemFrame) {
((BlockEntityItemFrame) itemFrame).dropItem(this);
}
}
break;
case ProtocolInfo.MAP_INFO_REQUEST_PACKET:
MapInfoRequestPacket pk = (MapInfoRequestPacket) packet;
Item mapItem = null;
Expand Down Expand Up @@ -3207,7 +3218,7 @@ public void onCompletion(Server server) {

this.setDataFlag(DATA_FLAGS, DATA_FLAG_ACTION, false);

if (this.canInteract(blockVector.add(0.5, 0.5, 0.5), this.isCreative() ? 13 : 7)) {
if (this.canInteract(blockVector.add(0.5, 0.5, 0.5), this.isCreative() ? 14 : 8)) {
if (this.isCreative()) {
Item i = inventory.getItemInHand();
if (this.level.useItemOn(blockVector.asVector3(), i, face, useItemData.clickPos.x, useItemData.clickPos.y, useItemData.clickPos.z, this) != null) {
Expand Down Expand Up @@ -3255,7 +3266,7 @@ public void onCompletion(Server server) {

Item oldItem = i.clone();

if (this.canInteract(blockVector.add(0.5, 0.5, 0.5), this.isCreative() ? 13 : 7) && (i = this.level.useBreakOn(blockVector.asVector3(), face, i, this, true)) != null) {
if (this.canInteract(blockVector.add(0.5, 0.5, 0.5), this.isCreative() ? 14 : 8) && (i = this.level.useBreakOn(blockVector.asVector3(), face, i, this, true)) != null) {
if (this.isSurvival()) {
this.getFoodData().updateFoodExpLevel(0.005);
if (!i.equals(oldItem) || i.getCount() != oldItem.getCount()) {
Expand Down Expand Up @@ -3679,7 +3690,7 @@ private void onBlockBreakComplete(BlockVector3 blockPos, BlockFace face) {
Item handItem = this.getInventory().getItemInHand();
Item clone = handItem.clone();

boolean canInteract = this.canInteract(blockPos.add(0.5, 0.5, 0.5), this.isCreative() ? 13 : 7);
boolean canInteract = this.canInteract(blockPos.add(0.5, 0.5, 0.5), this.isCreative() ? 14 : 8);
if (canInteract) {
handItem = this.level.useBreakOn(blockPos.asVector3(), face, handItem, this, true);
if (handItem == null) {
Expand Down Expand Up @@ -4039,7 +4050,7 @@ public void close(TextContainer message, String reason, boolean notify) {
this.server.getPluginManager().unsubscribeFromPermission(Server.BROADCAST_CHANNEL_USERS, this);
this.spawned = false;
this.server.getLogger().info(this.getServer().getLanguage().translateString("nukkit.player.logOut",
TextFormat.AQUA + (this.getName() == null ? "" : this.getName()) + TextFormat.WHITE,
TextFormat.AQUA + (this.getName() == null ? this.unverifiedUsername : this.getName()) + TextFormat.WHITE,
this.getAddress(),
String.valueOf(this.getPort()),
this.getServer().getLanguage().translateString(reason)));
Expand Down Expand Up @@ -4332,6 +4343,8 @@ protected void respawn() {

this.setSprinting(false);
this.setSneaking(false);
this.setSwimming(false);
this.setGliding(false);

this.setDataProperty(new ShortEntityData(Player.DATA_AIR, 400), false);
this.deadTicks = 0;
Expand Down Expand Up @@ -4610,6 +4623,8 @@ public void sendPosition(Vector3 pos, double yaw, double pitch, int mode, Player
if (targets != null) {
Server.broadcastPacket(targets, pk);
} else {
this.clientMovements.clear();

this.dataPacket(pk);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/cn/nukkit/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,9 @@ public void addOnlinePlayer(Player player) {
}

public void removeOnlinePlayer(Player player) {
if (player.getUniqueId() == null) {
return;
}
if (this.playerList.containsKey(player.getUniqueId())) {
this.playerList.remove(player.getUniqueId());

Expand Down
19 changes: 9 additions & 10 deletions src/main/java/cn/nukkit/block/BlockBed.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import cn.nukkit.lang.TranslationContainer;
import cn.nukkit.level.Level;
import cn.nukkit.math.BlockFace;
import cn.nukkit.math.Vector3;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.utils.BlockColor;
import cn.nukkit.utils.DyeColor;
Expand Down Expand Up @@ -66,18 +65,21 @@ public boolean onActivate(Item item) {

@Override
public boolean onActivate(Item item, Player player) {

if (this.level.getDimension() == Level.DIMENSION_NETHER || this.level.getDimension() == Level.DIMENSION_THE_END) {
CompoundTag tag = EntityPrimedTNT.getDefaultNBT(this).putShort("Fuse", 0);
new EntityPrimedTNT(this.level.getChunk(this.getFloorX() >> 4, this.getFloorZ() >> 4), tag);
return true;
}

if (player == null) {
return false;
}

int time = this.getLevel().getTime() % Level.TIME_FULL;

boolean isNight = (time >= Level.TIME_NIGHT && time < Level.TIME_SUNRISE);

if (player != null && !isNight) {
if (!isNight && !this.getLevel().isThundering()) {
player.sendMessage(new TranslationContainer("tile.bed.noSleep"));
return true;
}
Expand All @@ -100,19 +102,16 @@ public boolean onActivate(Item item, Player player) {
} else if (blockWest.getId() == this.getId() && (blockWest.getDamage() & 0x08) == 0x08) {
b = blockWest;
} else {
if (player != null) {
player.sendMessage(new TranslationContainer("tile.bed.notValid"));
}
player.sendMessage(new TranslationContainer("tile.bed.notValid"));

return true;
}
}

if (player != null && !player.sleepOn(b)) {
if (!player.sleepOn(b)) {
player.sendMessage(new TranslationContainer("tile.bed.occupied"));
}


return true;
}

Expand Down Expand Up @@ -172,11 +171,11 @@ public boolean onBreak(Item item) {
return true;
}

private void createBlockEntity(Vector3 pos, int color) {
private void createBlockEntity(Block pos, int color) {
CompoundTag nbt = BlockEntity.getDefaultCompound(pos, BlockEntity.BED);
nbt.putByte("color", color);

BlockEntity.createBlockEntity(BlockEntity.BED, this.level.getChunk(pos.getFloorX() >> 4, pos.getFloorZ() >> 4), nbt);
BlockEntity.createBlockEntity(BlockEntity.BED, pos.getChunk(), nbt);
}

@Override
Expand Down
20 changes: 5 additions & 15 deletions src/main/java/cn/nukkit/block/BlockTallGrass.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import cn.nukkit.Player;
import cn.nukkit.item.Item;
import cn.nukkit.item.ItemSeedsWheat;
import cn.nukkit.item.ItemTool;
import cn.nukkit.level.Level;
import cn.nukkit.level.particle.BoneMealParticle;
Expand Down Expand Up @@ -123,24 +122,15 @@ public boolean onActivate(Item item, Player player) {

@Override
public Item[] getDrops(Item item) {
boolean dropSeeds = ThreadLocalRandom.current().nextInt(10) == 0;
if (item.isShears()) {
//todo enchantment
if (dropSeeds) {
return new Item[]{
new ItemSeedsWheat(),
Item.get(Item.TALL_GRASS, this.getDamage(), 1)
};
} else {
return new Item[]{
Item.get(Item.TALL_GRASS, this.getDamage(), 1)
};
}
return new Item[]{
Item.get(Item.TALL_GRASS, this.getDamage() & 2, 1)
};
}

if (dropSeeds) {
if (ThreadLocalRandom.current().nextInt(10) == 0) {
return new Item[]{
new ItemSeedsWheat()
Item.get(Item.WHEAT_SEEDS)
};
} else {
return new Item[0];
Expand Down
Loading

0 comments on commit 9cc88d0

Please sign in to comment.