rename player abilities packet, save base abilities in Player.kt

This commit is contained in:
Bixilon 2021-04-23 14:46:32 +02:00
parent 1ccf8b20f3
commit b8e68126d2
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
9 changed files with 130 additions and 119 deletions

View File

@ -0,0 +1,24 @@
/*
* Minosoft
* Copyright (C) 2021 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.data.player
data class Abilities(
var isInvulnerable: Boolean = false,
var isFlying: Boolean = false,
var canFly: Boolean = false,
var canInstantBuild: Boolean = false,
var flyingSpeed: Float = 1.0f,
var walkingSpeed: Float = 1.0f,
)

View File

@ -32,4 +32,5 @@ class Player(
@Deprecated(message = "Will be replaced with some kind of teleport manager, ...")
var isSpawnConfirmed = false
val baseAbilities = Abilities()
}

View File

@ -1,47 +0,0 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.protocol.packets.c2s.play;
import de.bixilon.minosoft.protocol.packets.c2s.PlayC2SPacket;
import de.bixilon.minosoft.protocol.protocol.PlayOutByteBuffer;
import de.bixilon.minosoft.util.logging.Log;
import static de.bixilon.minosoft.protocol.protocol.ProtocolVersions.V_1_16_PRE4;
public class PlayerAbilitiesC2SPacket implements PlayC2SPacket {
private final boolean flying;
public PlayerAbilitiesC2SPacket(boolean flying) {
this.flying = flying;
}
@Override
public void write(PlayOutByteBuffer buffer) {
byte flags = 0;
if (this.flying) {
flags |= 0b10;
}
buffer.writeByte(flags);
if (buffer.getVersionId() < V_1_16_PRE4) {
// only fly matters, everything else ignored
buffer.writeFloat(0.0F);
buffer.writeFloat(0.0F);
}
}
@Override
public void log() {
Log.protocol(String.format("[OUT] Sending player abilities packet: (flying=%s)", this.flying));
}
}

View File

@ -0,0 +1,38 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.protocol.packets.c2s.play
import de.bixilon.minosoft.protocol.packets.c2s.PlayC2SPacket
import de.bixilon.minosoft.protocol.protocol.PlayOutByteBuffer
import de.bixilon.minosoft.protocol.protocol.ProtocolVersions
import de.bixilon.minosoft.util.logging.Log
class ToggleFlyC2SP(val flying: Boolean) : PlayC2SPacket {
override fun write(buffer: PlayOutByteBuffer) {
var flags = 0
if (flying) {
flags = flags or 2
}
buffer.writeByte(flags)
if (buffer.versionId < ProtocolVersions.V_1_16_PRE4) {
// only fly matters, everything else ignored
buffer.writeFloat(0.0f) // flyingSpeed
buffer.writeFloat(0.0f) // walkingSpeed
}
}
override fun log() {
Log.protocol("[OUT] Sending player fly toggle packet: (flying=$flying)")
}
}

View File

@ -1,68 +0,0 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.protocol.packets.s2c.play;
import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket;
import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer;
import de.bixilon.minosoft.util.BitByte;
import de.bixilon.minosoft.util.logging.Log;
import static de.bixilon.minosoft.protocol.protocol.ProtocolVersions.V_14W03B;
public class PacketPlayerAbilitiesReceiving extends PlayS2CPacket {
private final boolean creative; // is this needed? receiving the gamemode in change Game state
private final boolean flying;
private final boolean canFly;
private final boolean godMode;
private final float flyingSpeed;
private final float walkingSpeed;
public PacketPlayerAbilitiesReceiving(PlayInByteBuffer buffer) {
byte flags = buffer.readByte();
if (buffer.getVersionId() < V_14W03B) { // ToDo
this.creative = BitByte.isBitSet(flags, 0);
this.flying = BitByte.isBitSet(flags, 1);
this.canFly = BitByte.isBitSet(flags, 2);
this.godMode = BitByte.isBitSet(flags, 3);
} else {
this.godMode = BitByte.isBitSet(flags, 0);
this.flying = BitByte.isBitSet(flags, 1);
this.canFly = BitByte.isBitSet(flags, 2);
this.creative = BitByte.isBitSet(flags, 3);
}
this.flyingSpeed = buffer.readFloat();
this.walkingSpeed = buffer.readFloat();
}
@Override
public void log() {
Log.protocol(String.format("[IN] Received player abilities packet: (creative=%s, flying=%s, canFly=%s, godMode=%s, flyingSpeed=%s, walkingSpeed=%s)", this.creative, this.flying, this.canFly, this.godMode, this.flyingSpeed, this.walkingSpeed));
}
public boolean canFly() {
return this.canFly;
}
public boolean isCreative() {
return this.creative;
}
public boolean isGodMode() {
return this.godMode;
}
public boolean isFlying() {
return this.flying;
}
}

View File

@ -0,0 +1,63 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.protocol.packets.s2c.play
import de.bixilon.minosoft.protocol.network.connection.PlayConnection
import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket
import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer
import de.bixilon.minosoft.protocol.protocol.ProtocolVersions
import de.bixilon.minosoft.util.BitByte
import de.bixilon.minosoft.util.logging.Log
class PlayerAbilitiesS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() {
val isInvulnerable: Boolean
val isFlying: Boolean
val canFly: Boolean
val canInstantBuild: Boolean
val flyingSpeed: Float
val walkingSpeed: Float
init {
val flags = buffer.readByte()
if (buffer.versionId < ProtocolVersions.V_14W03B) { // ToDo: Find out correct version
isInvulnerable = BitByte.isBitSet(flags.toInt(), 0)
isFlying = BitByte.isBitSet(flags.toInt(), 1)
canFly = BitByte.isBitSet(flags.toInt(), 2)
canInstantBuild = BitByte.isBitSet(flags.toInt(), 3)
} else {
canInstantBuild = BitByte.isBitSet(flags.toInt(), 0)
isFlying = BitByte.isBitSet(flags.toInt(), 1)
canFly = BitByte.isBitSet(flags.toInt(), 2)
isInvulnerable = BitByte.isBitSet(flags.toInt(), 3)
}
flyingSpeed = buffer.readFloat()
walkingSpeed = buffer.readFloat()
}
override fun log() {
Log.protocol("[IN] Received player abilities: (isInvulnerable=$isInvulnerable, isFlying=$isFlying, canFly=$canFly, canInstantBuild=$canInstantBuild, flyingSpeed=$flyingSpeed, walkingSpeed=$walkingSpeed)")
}
override fun handle(connection: PlayConnection) {
val abilities = connection.player.baseAbilities
abilities.isInvulnerable = isInvulnerable
abilities.isFlying = isFlying
abilities.canFly = canFly
abilities.canInstantBuild = canInstantBuild
abilities.flyingSpeed = flyingSpeed
abilities.walkingSpeed = walkingSpeed
}
}

View File

@ -41,7 +41,7 @@ public class PacketSender {
}
public void setFlyStatus(boolean flying) {
this.connection.sendPacket(new PlayerAbilitiesC2SPacket(flying));
this.connection.sendPacket(new ToggleFlyC2SP(flying));
}
public void sendChatMessage(String message) {

View File

@ -73,7 +73,7 @@ class PacketTypes {
PLAY_STEER_BOAT(SteerBoatC2SPacket::class.java),
PLAY_PICK_ITEM,
PLAY_CRAFTING_RECIPE_REQUEST(CraftingRecipeRequestC2SPacket::class.java),
PLAY_PLAYER_ABILITIES(PlayerAbilitiesC2SPacket::class.java),
PLAY_TOGGLE_FLY(ToggleFlyC2SP::class.java),
PLAY_PLAYER_DIGGING(PlayerDiggingC2SPacket::class.java),
PLAY_ENTITY_ACTION(EntityActionC2SPacket::class.java),
PLAY_STEER_VEHICLE(SteerVehicleC2SPacket::class.java),
@ -196,7 +196,7 @@ class PacketTypes {
PLAY_OPEN_WINDOW({ PacketOpenWindow(it) }),
PLAY_OPEN_SIGN_EDITOR({ PacketOpenSignEditor(it) }),
PLAY_CRAFT_RECIPE_RESPONSE({ PacketCraftRecipeResponse(it) }),
PLAY_PLAYER_ABILITIES({ PacketPlayerAbilitiesReceiving(it) }),
PLAY_PLAYER_ABILITIES({ PlayerAbilitiesS2CP(it) }),
PLAY_COMBAT_EVENT({ CombatEventS2CPFactory.createPacket(it) }),
PLAY_COMBAT_EVENT_END({ EndCombatEventS2CPacket(it) }),
PLAY_COMBAT_EVENT_ENTER({ EnterCombatEventS2CPacket() }),

File diff suppressed because one or more lines are too long