Skip to content

Commit 9cc88d0

Browse files
authored
Backport some fixes from leveldb branch
1 parent 3da731d commit 9cc88d0

21 files changed

+226
-120
lines changed

src/main/java/cn/nukkit/Achievement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Achievement {
1515
put("buildWorkBench", new Achievement("Benchmarking", "mineWood"));
1616
put("buildPickaxe", new Achievement("Time to Mine!", "buildWorkBench"));
1717
put("buildFurnace", new Achievement("Hot Topic", "buildPickaxe"));
18-
put("acquireIron", new Achievement("Acquire hardware", "buildFurnace"));
18+
put("acquireIron", new Achievement("Acquire Hardware", "buildFurnace"));
1919
put("buildHoe", new Achievement("Time to Farm!", "buildWorkBench"));
2020
put("makeBread", new Achievement("Bake Bread", "buildHoe"));
2121
put("bakeCake", new Achievement("The Lie", "buildHoe"));

src/main/java/cn/nukkit/AdventureSettings.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,18 @@ public boolean get(Type type) {
4848
return value == null ? type.getDefaultValue() : value;
4949
}
5050

51+
/**
52+
* Send adventure settings values to the player
53+
*/
5154
public void update() {
55+
this.update(true);
56+
}
57+
58+
/**
59+
* Send adventure settings values to the player
60+
* @param reset reset in air ticks
61+
*/
62+
void update(boolean reset) {
5263
UpdateAbilitiesPacket packet = new UpdateAbilitiesPacket();
5364
packet.setEntityId(player.getId());
5465
packet.setCommandPermission(player.isOp() ? UpdateAbilitiesPacket.CommandPermission.OPERATOR : UpdateAbilitiesPacket.CommandPermission.NORMAL);
@@ -102,7 +113,9 @@ public void update() {
102113

103114
player.dataPacket(packet);
104115
player.dataPacket(adventurePacket);
105-
player.resetInAirTicks();
116+
if (reset) {
117+
player.resetInAirTicks();
118+
}
106119
}
107120

108121
public enum Type {

src/main/java/cn/nukkit/Player.java

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ public class Player extends EntityHuman implements CommandSender, InventoryHolde
183183
protected boolean removeFormat = true;
184184

185185
protected String username;
186+
private String unverifiedUsername = "";
186187
protected String iusername;
187188
protected String displayName;
188189

@@ -1552,7 +1553,7 @@ protected void handleMovement(Vector3 clientPos) {
15521553
else this.speed.setComponents(0, 0, 0);
15531554
}
15541555

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

17121713
if (!this.isSpectator() && this.speed != null) {
17131714
if (this.onGround) {
1714-
if (this.inAirTicks != 0) {
1715-
this.startAirTicks = 5;
1715+
if (this.isGliding()) {
1716+
this.setGliding(false);
17161717
}
1717-
this.inAirTicks = 0;
1718-
this.highestPosition = this.y;
1718+
1719+
this.resetFallDistance();
17191720
} else {
17201721
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)) {
17211722
double expectedVelocity = (-this.getGravity()) / ((double) this.getDrag()) - ((-this.getGravity()) / ((double) this.getDrag())) * Math.exp(-((double) this.getDrag()) * ((double) (this.inAirTicks - this.startAirTicks)));
@@ -2162,11 +2163,17 @@ public void handleDataPacket(DataPacket packet) {
21622163
this.loginPacketReceived = true;
21632164

21642165
LoginPacket loginPacket = (LoginPacket) packet;
2165-
this.username = TextFormat.clean(loginPacket.username);
2166-
this.displayName = this.username;
2167-
this.iusername = this.username.toLowerCase();
21682166

2169-
this.setDataProperty(new StringEntityData(DATA_NAMETAG, this.username), false);
2167+
this.unverifiedUsername = TextFormat.clean(loginPacket.username);
2168+
2169+
if (loginPacket.skin == null) {
2170+
this.close("", "disconnectionScreen.invalidSkin");
2171+
return;
2172+
}
2173+
2174+
if (this.server.getOnlinePlayers().size() >= this.server.getMaxPlayers() && this.kick(PlayerKickEvent.Reason.SERVER_FULL, "disconnectionScreen.serverFull", false)) {
2175+
return;
2176+
}
21702177

21712178
this.loginChainData = ClientChainData.read(loginPacket);
21722179

@@ -2175,9 +2182,12 @@ public void handleDataPacket(DataPacket packet) {
21752182
break;
21762183
}
21772184

2178-
if (this.server.getOnlinePlayers().size() >= this.server.getMaxPlayers() && this.kick(PlayerKickEvent.Reason.SERVER_FULL, "disconnectionScreen.serverFull", false)) {
2179-
break;
2180-
}
2185+
// Do not set username before the user is authenticated
2186+
this.username = this.unverifiedUsername;
2187+
this.unverifiedUsername = null;
2188+
this.displayName = this.username;
2189+
this.iusername = this.username.toLowerCase();
2190+
this.setDataProperty(new StringEntityData(DATA_NAMETAG, this.username), false);
21812191

21822192
this.randomClientId = loginPacket.clientId;
21832193

@@ -2560,7 +2570,7 @@ public void onCompletion(Server server) {
25602570
}
25612571
this.getServer().getPluginManager().callEvent(playerToggleFlightEvent);
25622572
if (playerToggleFlightEvent.isCancelled()) {
2563-
this.getAdventureSettings().update();
2573+
this.getAdventureSettings().update(false);
25642574
} else {
25652575
this.getAdventureSettings().set(AdventureSettings.Type.FLYING, playerToggleFlightEvent.isFlying());
25662576
}
@@ -2573,13 +2583,13 @@ public void onCompletion(Server server) {
25732583
}
25742584
this.getServer().getPluginManager().callEvent(playerToggleFlightEvent);
25752585
if (playerToggleFlightEvent.isCancelled()) {
2576-
this.getAdventureSettings().update();
2586+
this.getAdventureSettings().update(false);
25772587
} else {
25782588
this.getAdventureSettings().set(AdventureSettings.Type.FLYING, playerToggleFlightEvent.isFlying());
25792589
}
25802590
}
25812591

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

25842594
double distSqrt = clientPosition.distanceSquared(this);
25852595
if (distSqrt == 0.0 && authPacket.getYaw() % 360 == this.yaw && authPacket.getPitch() % 360 == this.pitch) {
@@ -2956,6 +2966,17 @@ public void onCompletion(Server server) {
29562966
pk.wasServerInitiated = false;
29572967
pk.windowId = -1;
29582968
this.dataPacket(pk);
2969+
} else { // Close bugged inventory
2970+
ContainerClosePacket pk = new ContainerClosePacket();
2971+
pk.windowId = containerClosePacket.windowId;
2972+
pk.wasServerInitiated = false;
2973+
this.dataPacket(pk);
2974+
2975+
for (Inventory open : new ArrayList<>(this.windows.keySet())) {
2976+
if (open instanceof ContainerInventory) {
2977+
this.removeWindow(open);
2978+
}
2979+
}
29592980
}
29602981
break;
29612982
case ProtocolInfo.CRAFTING_EVENT_PACKET:
@@ -3002,23 +3023,13 @@ public void onCompletion(Server server) {
30023023
SetPlayerGameTypePacket setPlayerGameTypePacket1 = new SetPlayerGameTypePacket();
30033024
setPlayerGameTypePacket1.gamemode = this.gamemode & 0x01;
30043025
this.dataPacket(setPlayerGameTypePacket1);
3005-
this.getAdventureSettings().update();
3026+
this.getAdventureSettings().update(false);
30063027
break;
30073028
}
30083029
this.setGamemode(setPlayerGameTypePacket.gamemode, true);
30093030
Command.broadcastCommandMessage(this, new TranslationContainer("commands.gamemode.success.self", Server.getGamemodeString(this.gamemode)));
30103031
}
30113032
break;
3012-
case ProtocolInfo.ITEM_FRAME_DROP_ITEM_PACKET:
3013-
ItemFrameDropItemPacket itemFrameDropItemPacket = (ItemFrameDropItemPacket) packet;
3014-
Vector3 vector3 = this.temporalVector.setComponents(itemFrameDropItemPacket.x, itemFrameDropItemPacket.y, itemFrameDropItemPacket.z);
3015-
if (vector3.distanceSquared(this) < 1000) {
3016-
BlockEntity itemFrame = this.level.getBlockEntity(vector3);
3017-
if (itemFrame instanceof BlockEntityItemFrame) {
3018-
((BlockEntityItemFrame) itemFrame).dropItem(this);
3019-
}
3020-
}
3021-
break;
30223033
case ProtocolInfo.MAP_INFO_REQUEST_PACKET:
30233034
MapInfoRequestPacket pk = (MapInfoRequestPacket) packet;
30243035
Item mapItem = null;
@@ -3207,7 +3218,7 @@ public void onCompletion(Server server) {
32073218

32083219
this.setDataFlag(DATA_FLAGS, DATA_FLAG_ACTION, false);
32093220

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

32563267
Item oldItem = i.clone();
32573268

3258-
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) {
3269+
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) {
32593270
if (this.isSurvival()) {
32603271
this.getFoodData().updateFoodExpLevel(0.005);
32613272
if (!i.equals(oldItem) || i.getCount() != oldItem.getCount()) {
@@ -3679,7 +3690,7 @@ private void onBlockBreakComplete(BlockVector3 blockPos, BlockFace face) {
36793690
Item handItem = this.getInventory().getItemInHand();
36803691
Item clone = handItem.clone();
36813692

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

43334344
this.setSprinting(false);
43344345
this.setSneaking(false);
4346+
this.setSwimming(false);
4347+
this.setGliding(false);
43354348

43364349
this.setDataProperty(new ShortEntityData(Player.DATA_AIR, 400), false);
43374350
this.deadTicks = 0;
@@ -4610,6 +4623,8 @@ public void sendPosition(Vector3 pos, double yaw, double pitch, int mode, Player
46104623
if (targets != null) {
46114624
Server.broadcastPacket(targets, pk);
46124625
} else {
4626+
this.clientMovements.clear();
4627+
46134628
this.dataPacket(pk);
46144629
}
46154630
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,9 @@ public void addOnlinePlayer(Player player) {
996996
}
997997

998998
public void removeOnlinePlayer(Player player) {
999+
if (player.getUniqueId() == null) {
1000+
return;
1001+
}
9991002
if (this.playerList.containsKey(player.getUniqueId())) {
10001003
this.playerList.remove(player.getUniqueId());
10011004

src/main/java/cn/nukkit/block/BlockBed.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import cn.nukkit.lang.TranslationContainer;
1010
import cn.nukkit.level.Level;
1111
import cn.nukkit.math.BlockFace;
12-
import cn.nukkit.math.Vector3;
1312
import cn.nukkit.nbt.tag.CompoundTag;
1413
import cn.nukkit.utils.BlockColor;
1514
import cn.nukkit.utils.DyeColor;
@@ -66,18 +65,21 @@ public boolean onActivate(Item item) {
6665

6766
@Override
6867
public boolean onActivate(Item item, Player player) {
69-
7068
if (this.level.getDimension() == Level.DIMENSION_NETHER || this.level.getDimension() == Level.DIMENSION_THE_END) {
7169
CompoundTag tag = EntityPrimedTNT.getDefaultNBT(this).putShort("Fuse", 0);
7270
new EntityPrimedTNT(this.level.getChunk(this.getFloorX() >> 4, this.getFloorZ() >> 4), tag);
7371
return true;
7472
}
7573

74+
if (player == null) {
75+
return false;
76+
}
77+
7678
int time = this.getLevel().getTime() % Level.TIME_FULL;
7779

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

80-
if (player != null && !isNight) {
82+
if (!isNight && !this.getLevel().isThundering()) {
8183
player.sendMessage(new TranslationContainer("tile.bed.noSleep"));
8284
return true;
8385
}
@@ -100,19 +102,16 @@ public boolean onActivate(Item item, Player player) {
100102
} else if (blockWest.getId() == this.getId() && (blockWest.getDamage() & 0x08) == 0x08) {
101103
b = blockWest;
102104
} else {
103-
if (player != null) {
104-
player.sendMessage(new TranslationContainer("tile.bed.notValid"));
105-
}
105+
player.sendMessage(new TranslationContainer("tile.bed.notValid"));
106106

107107
return true;
108108
}
109109
}
110110

111-
if (player != null && !player.sleepOn(b)) {
111+
if (!player.sleepOn(b)) {
112112
player.sendMessage(new TranslationContainer("tile.bed.occupied"));
113113
}
114114

115-
116115
return true;
117116
}
118117

@@ -172,11 +171,11 @@ public boolean onBreak(Item item) {
172171
return true;
173172
}
174173

175-
private void createBlockEntity(Vector3 pos, int color) {
174+
private void createBlockEntity(Block pos, int color) {
176175
CompoundTag nbt = BlockEntity.getDefaultCompound(pos, BlockEntity.BED);
177176
nbt.putByte("color", color);
178177

179-
BlockEntity.createBlockEntity(BlockEntity.BED, this.level.getChunk(pos.getFloorX() >> 4, pos.getFloorZ() >> 4), nbt);
178+
BlockEntity.createBlockEntity(BlockEntity.BED, pos.getChunk(), nbt);
180179
}
181180

182181
@Override

src/main/java/cn/nukkit/block/BlockTallGrass.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import cn.nukkit.Player;
44
import cn.nukkit.item.Item;
5-
import cn.nukkit.item.ItemSeedsWheat;
65
import cn.nukkit.item.ItemTool;
76
import cn.nukkit.level.Level;
87
import cn.nukkit.level.particle.BoneMealParticle;
@@ -123,24 +122,15 @@ public boolean onActivate(Item item, Player player) {
123122

124123
@Override
125124
public Item[] getDrops(Item item) {
126-
boolean dropSeeds = ThreadLocalRandom.current().nextInt(10) == 0;
127125
if (item.isShears()) {
128-
//todo enchantment
129-
if (dropSeeds) {
130-
return new Item[]{
131-
new ItemSeedsWheat(),
132-
Item.get(Item.TALL_GRASS, this.getDamage(), 1)
133-
};
134-
} else {
135-
return new Item[]{
136-
Item.get(Item.TALL_GRASS, this.getDamage(), 1)
137-
};
138-
}
126+
return new Item[]{
127+
Item.get(Item.TALL_GRASS, this.getDamage() & 2, 1)
128+
};
139129
}
140130

141-
if (dropSeeds) {
131+
if (ThreadLocalRandom.current().nextInt(10) == 0) {
142132
return new Item[]{
143-
new ItemSeedsWheat()
133+
Item.get(Item.WHEAT_SEEDS)
144134
};
145135
} else {
146136
return new Item[0];

0 commit comments

Comments
 (0)