Use Int2ObjectMap for LevelEvents

This commit is contained in:
Camotoy 2022-06-03 20:17:17 -04:00
parent 056e54b8e5
commit 0f46b3bcf0
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
2 changed files with 13 additions and 83 deletions

View file

@ -170,84 +170,17 @@ import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundPo
import com.github.steveice10.mc.protocol.packet.status.clientbound.ClientboundStatusResponsePacket;
import com.github.steveice10.mc.protocol.packet.status.serverbound.ServerboundPingRequestPacket;
import com.github.steveice10.mc.protocol.packet.status.serverbound.ServerboundStatusRequestPacket;
import org.cloudburstmc.protocol.common.util.TypeMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
public class MinecraftCodec {
private static final TypeMap<LevelEvent> LEVEL_EVENTS = TypeMap.builder(LevelEvent.class)
.insert(1000, LevelEvent.BLOCK_DISPENSER_DISPENSE)
.insert(1001, LevelEvent.BLOCK_DISPENSER_FAIL)
.insert(1002, LevelEvent.BLOCK_DISPENSER_LAUNCH)
.insert(1003, LevelEvent.ENTITY_ENDEREYE_LAUNCH)
.insert(1004, LevelEvent.ENTITY_FIREWORK_SHOOT)
.insert(1005, LevelEvent.BLOCK_IRON_DOOR_OPEN)
.insert(1006, LevelEvent.BLOCK_WOODEN_DOOR_OPEN)
.insert(1007, LevelEvent.BLOCK_WOODEN_TRAPDOOR_OPEN)
.insert(1008, LevelEvent.BLOCK_FENCE_GATE_OPEN)
.insert(1009, LevelEvent.BLOCK_FIRE_EXTINGUISH)
.insert(1010, LevelEvent.RECORD)
.insert(1011, LevelEvent.BLOCK_IRON_DOOR_CLOSE)
.insert(1012, LevelEvent.BLOCK_WOODEN_DOOR_CLOSE)
.insert(1013, LevelEvent.BLOCK_WOODEN_TRAPDOOR_CLOSE)
.insert(1014, LevelEvent.BLOCK_FENCE_GATE_CLOSE)
.insert(1015, LevelEvent.ENTITY_GHAST_WARN)
.insert(1016, LevelEvent.ENTITY_GHAST_SHOOT)
.insert(1017, LevelEvent.ENTITY_ENDERDRAGON_SHOOT)
.insert(1018, LevelEvent.ENTITY_BLAZE_SHOOT)
.insert(1019, LevelEvent.ENTITY_ZOMBIE_ATTACK_DOOR_WOOD)
.insert(1020, LevelEvent.ENTITY_ZOMBIE_ATTACK_DOOR_IRON)
.insert(1021, LevelEvent.ENTITY_ZOMBIE_BREAK_DOOR_WOOD)
.insert(1022, LevelEvent.ENTITY_WITHER_BREAK_BLOCK)
.insert(1023, LevelEvent.ENTITY_WITHER_SPAWN)
.insert(1024, LevelEvent.ENTITY_WITHER_SHOOT)
.insert(1025, LevelEvent.ENTITY_BAT_TAKEOFF)
.insert(1026, LevelEvent.ENTITY_ZOMBIE_INFECT)
.insert(1027, LevelEvent.ENTITY_ZOMBIE_VILLAGER_CONVERTED)
.insert(1028, LevelEvent.ENTITY_ENDERDRAGON_DEATH)
.insert(1029, LevelEvent.BLOCK_ANVIL_DESTROY)
.insert(1030, LevelEvent.BLOCK_ANVIL_USE)
.insert(1031, LevelEvent.BLOCK_ANVIL_LAND)
.insert(1032, LevelEvent.BLOCK_PORTAL_TRAVEL)
.insert(1033, LevelEvent.BLOCK_CHORUS_FLOWER_GROW)
.insert(1034, LevelEvent.BLOCK_CHORUS_FLOWER_DEATH)
.insert(1035, LevelEvent.BLOCK_BREWING_STAND_BREW)
.insert(1036, LevelEvent.BLOCK_IRON_TRAPDOOR_CLOSE)
.insert(1037, LevelEvent.BLOCK_IRON_TRAPDOOR_OPEN)
.insert(1038, LevelEvent.BLOCK_END_PORTAL_SPAWN)
.insert(1039, LevelEvent.ENTITY_PHANTOM_BITE)
.insert(1040, LevelEvent.ENTITY_ZOMBIE_CONVERTED_TO_DROWNED)
.insert(1041, LevelEvent.ENTITY_HUSK_CONVERTED_TO_ZOMBIE)
.insert(1042, LevelEvent.BLOCK_GRINDSTONE_USE)
.insert(1043, LevelEvent.ITEM_BOOK_PAGE_TURN)
.insert(1044, LevelEvent.BLOCK_SMITHING_TABLE_USE)
.insert(1045, LevelEvent.POINTED_DRIPSTONE_LAND)
.insert(1046, LevelEvent.DRIP_LAVA_INTO_CAULDRON)
.insert(1047, LevelEvent.DRIP_WATER_INTO_CAULDRON)
.insert(1048, LevelEvent.ENTITY_SKELETON_CONVERTED_TO_STRAY)
.insert(1500, LevelEvent.COMPOSTER)
.insert(1501, LevelEvent.BLOCK_LAVA_EXTINGUISH)
.insert(1502, LevelEvent.BLOCK_REDSTONE_TORCH_BURNOUT)
.insert(1503, LevelEvent.BLOCK_END_PORTAL_FRAME_FILL)
.insert(1504, LevelEvent.DRIPSTONE_DRIP)
.insert(1505, LevelEvent.BONEMEAL_GROW_WITH_SOUND)
.insert(2000, LevelEvent.SMOKE)
.insert(2001, LevelEvent.BREAK_BLOCK)
.insert(2002, LevelEvent.BREAK_SPLASH_POTION)
.insert(2003, LevelEvent.BREAK_EYE_OF_ENDER)
.insert(2004, LevelEvent.MOB_SPAWN)
.insert(2005, LevelEvent.BONEMEAL_GROW)
.insert(2006, LevelEvent.ENDERDRAGON_FIREBALL_EXPLODE)
.insert(2007, LevelEvent.BREAK_SPLASH_POTION2)
.insert(2008, LevelEvent.EXPLOSION)
.insert(2009, LevelEvent.EVAPORATE)
.insert(3000, LevelEvent.END_GATEWAY_SPAWN)
.insert(3001, LevelEvent.ENTITY_ENDERDRAGON_GROWL)
.insert(3002, LevelEvent.ELECTRIC_SPARK)
.insert(3003, LevelEvent.WAX_ON)
.insert(3004, LevelEvent.WAX_OFF)
.insert(3005, LevelEvent.SCRAPE)
.insert(3006, LevelEvent.SCULK_BLOCK_CHARGE)
.insert(3007, LevelEvent.SCULK_SHRIEKER_SHRIEK)
.build();
private static final Int2ObjectMap<LevelEvent> LEVEL_EVENTS = new Int2ObjectOpenHashMap<>();
static {
for (LevelEvent levelEvent : LevelEvent.values()) {
LEVEL_EVENTS.put(levelEvent.getId(), levelEvent);
}
}
public static final PacketCodec CODEC = PacketCodec.builder()
.protocolVersion((1 << 30) | 87)

View file

@ -32,12 +32,9 @@ import com.github.steveice10.packetlib.codec.BasePacketCodecHelper;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.math.vector.Vector3i;
import io.netty.buffer.ByteBuf;
import io.netty.util.collection.IntObjectHashMap;
import io.netty.util.collection.IntObjectMap;
import lombok.AccessLevel;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.Component;
import org.cloudburstmc.protocol.common.util.TypeMap;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
@ -58,7 +55,7 @@ public class MinecraftCodecHelper extends BasePacketCodecHelper {
private static final int POSITION_Y_SHIFT = 0xFFF;
private static final int POSITION_WRITE_SHIFT = 0x3FFFFFF;
private final TypeMap<LevelEvent> levelEvents;
private final Int2ObjectMap<LevelEvent> levelEvents;
public UUID readUUID(ByteBuf buf) {
return new UUID(buf.readLong(), buf.readLong());
@ -548,11 +545,11 @@ public class MinecraftCodecHelper extends BasePacketCodecHelper {
}
public LevelEvent readLevelEvent(ByteBuf buf) {
return this.levelEvents.getType(this.readVarInt(buf));
return this.levelEvents.get(this.readVarInt(buf));
}
public void writeLevelEvent(ByteBuf buf, LevelEvent event) {
this.writeVarInt(buf, this.levelEvents.getId(event));
this.writeVarInt(buf, event.getId());
}
public StatisticCategory readStatisticCategory(ByteBuf buf) {