PacketNBTQueryResponse, use static to initialize packet class mapping

This commit is contained in:
Bixilon 2020-07-09 18:41:42 +02:00
parent a0b1982970
commit dbabc8177a
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 62 additions and 4 deletions

View File

@ -0,0 +1,56 @@
/*
* Codename 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.clientbound.play;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.nbt.tag.CompoundTag;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InPacketBuffer;
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
public class PacketNBTQueryResponse implements ClientboundPacket {
int transactionId;
CompoundTag tag;
@Override
public boolean read(InPacketBuffer buffer) {
switch (buffer.getVersion()) {
case VERSION_1_13_2:
transactionId = buffer.readVarInt();
tag = buffer.readNBT();
return true;
}
return false;
}
@Override
public void log() {
Log.protocol(String.format("Received nbt response (transactionId=%d, nbt=%s)", transactionId, tag.toString()));
}
@Override
public void handle(PacketHandler h) {
h.handle(this);
}
public int getTransactionId() {
return transactionId;
}
public CompoundTag getTag() {
return tag;
}
}

View File

@ -575,4 +575,6 @@ public class PacketHandler {
public void handle(PacketAdvancements pkg) {
}
public void handle(PacketNBTQueryResponse pkg) {
}
}

View File

@ -27,6 +27,9 @@ import java.util.Map;
public abstract class Protocol implements ProtocolInterface {
static final HashMap<Packets.Clientbound, Class<? extends ClientboundPacket>> packetClassMapping = new HashMap<>();
static {
initPacketClassMapping();
}
public final HashMap<Packets.Serverbound, Integer> serverboundPacketMapping;
public final HashMap<Packets.Clientbound, Integer> clientboundPacketMapping;
@ -54,10 +57,6 @@ public abstract class Protocol implements ProtocolInterface {
}
public static Class<? extends ClientboundPacket> getPacketByPacket(Packets.Clientbound p) {
if (packetClassMapping.size() == 0) {
// init
initPacketClassMapping();
}
return packetClassMapping.get(p);
}
@ -146,6 +145,7 @@ public abstract class Protocol implements ProtocolInterface {
packetClassMapping.put(Packets.Clientbound.PLAY_UNLOCK_RECIPES, PacketUnlockRecipes.class);
packetClassMapping.put(Packets.Clientbound.PLAY_SELECT_ADVANCEMENT_TAB, PacketSelectAdvancementTab.class);
packetClassMapping.put(Packets.Clientbound.PLAY_ADVANCEMENTS, PacketAdvancements.class);
packetClassMapping.put(Packets.Clientbound.PLAY_NBT_QUERY_RESPONSE, PacketNBTQueryResponse.class);
}
public static ProtocolVersion getLowestVersionSupported() {