Geyser/core/src/main/java/org/geysermc/geyser/translator/protocol/java/level/JavaMapItemDataTranslator.java

92 lines
4.2 KiB
Java
Raw Normal View History

2020-04-04 20:23:02 +00:00
/*
2022-01-01 19:03:05 +00:00
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
2020-04-04 20:23:02 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.translator.protocol.java.level;
2020-04-04 20:23:02 +00:00
import com.github.steveice10.mc.protocol.data.game.level.map.MapData;
import com.github.steveice10.mc.protocol.data.game.level.map.MapIcon;
import com.github.steveice10.mc.protocol.packet.ingame.clientbound.level.ClientboundMapItemDataPacket;
2022-10-30 00:23:21 +00:00
import org.cloudburstmc.math.vector.Vector3i;
import org.cloudburstmc.protocol.bedrock.data.MapDecoration;
import org.cloudburstmc.protocol.bedrock.data.MapTrackedObject;
2022-06-08 12:09:14 +00:00
import org.geysermc.geyser.level.BedrockMapIcon;
import org.geysermc.geyser.level.MapColor;
2021-11-22 19:52:26 +00:00
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.translator.protocol.PacketTranslator;
import org.geysermc.geyser.translator.protocol.Translator;
import org.geysermc.geyser.util.DimensionUtils;
2020-04-04 20:23:02 +00:00
@Translator(packet = ClientboundMapItemDataPacket.class)
public class JavaMapItemDataTranslator extends PacketTranslator<ClientboundMapItemDataPacket> {
2020-04-04 20:23:02 +00:00
@Override
2021-11-22 19:52:26 +00:00
public void translate(GeyserSession session, ClientboundMapItemDataPacket packet) {
2022-10-30 00:23:21 +00:00
org.cloudburstmc.protocol.bedrock.packet.ClientboundMapItemDataPacket mapItemDataPacket = new org.cloudburstmc.protocol.bedrock.packet.ClientboundMapItemDataPacket();
2020-04-04 20:23:02 +00:00
mapItemDataPacket.setUniqueMapId(packet.getMapId());
mapItemDataPacket.setDimensionId(DimensionUtils.javaToBedrock(session.getDimension()));
2020-04-04 20:23:02 +00:00
mapItemDataPacket.setLocked(packet.isLocked());
mapItemDataPacket.setOrigin(Vector3i.ZERO); // Required since 1.19.20
2020-04-04 20:23:02 +00:00
mapItemDataPacket.setScale(packet.getScale());
2022-11-30 17:09:21 +00:00
// Required as of 1.19.50
mapItemDataPacket.getTrackedEntityIds().add(packet.getMapId());
2020-04-04 20:23:02 +00:00
MapData data = packet.getData();
if (data != null) {
mapItemDataPacket.setXOffset(data.getX());
mapItemDataPacket.setYOffset(data.getY());
mapItemDataPacket.setWidth(data.getColumns());
mapItemDataPacket.setHeight(data.getRows());
2021-09-27 00:54:03 +00:00
// Every int entry is an ARGB color
2020-04-04 20:23:02 +00:00
int[] colors = new int[data.getData().length];
int idx = 0;
for (byte colorId : data.getData()) {
colors[idx++] = MapColor.fromId(colorId & 0xFF).getARGB();
2020-04-04 20:23:02 +00:00
}
mapItemDataPacket.setColors(colors);
}
// Bedrock needs an entity id to display an icon
int id = 0;
for (MapIcon icon : packet.getIcons()) {
BedrockMapIcon bedrockMapIcon = BedrockMapIcon.fromType(icon.getIconType());
mapItemDataPacket.getTrackedObjects().add(new MapTrackedObject(id));
mapItemDataPacket.getDecorations().add(new MapDecoration(bedrockMapIcon.getIconID(), icon.getIconRotation(), icon.getCenterX(), icon.getCenterZ(), "", bedrockMapIcon.toARGB()));
id++;
}
2024-04-17 21:53:56 +00:00
// Client will ignore if sent too early
if (session.isSentSpawnPacket()) {
session.sendUpstreamPacket(mapItemDataPacket);
} else {
session.getUpstream().queuePostStartGamePacket(mapItemDataPacket);
}
2020-04-04 20:23:02 +00:00
}
}