2020-09-15 00:52:16 +00:00
|
|
|
/*
|
2022-01-01 19:03:05 +00:00
|
|
|
* Copyright (c) 2019-2022 GeyserMC. http://geysermc.org
|
2020-09-15 00:52:16 +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
|
|
|
|
*/
|
|
|
|
|
2021-11-20 23:29:46 +00:00
|
|
|
package org.geysermc.geyser.translator.protocol.bedrock;
|
2020-09-15 00:52:16 +00:00
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.player.GameMode;
|
|
|
|
import com.nukkitx.protocol.bedrock.packet.EntityPickRequestPacket;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.entity.type.BoatEntity;
|
|
|
|
import org.geysermc.geyser.entity.type.Entity;
|
2022-06-08 12:09:14 +00:00
|
|
|
import org.geysermc.geyser.registry.type.ItemMapping;
|
2021-11-22 19:52:26 +00:00
|
|
|
import org.geysermc.geyser.session.GeyserSession;
|
2021-11-20 23:29:46 +00:00
|
|
|
import org.geysermc.geyser.translator.protocol.PacketTranslator;
|
|
|
|
import org.geysermc.geyser.translator.protocol.Translator;
|
|
|
|
import org.geysermc.geyser.util.InventoryUtils;
|
2020-09-15 00:52:16 +00:00
|
|
|
|
2021-11-18 03:02:38 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
|
2020-09-15 00:52:16 +00:00
|
|
|
/**
|
|
|
|
* Called when the Bedrock user uses the pick block button on an entity
|
|
|
|
*/
|
|
|
|
@Translator(packet = EntityPickRequestPacket.class)
|
|
|
|
public class BedrockEntityPickRequestTranslator extends PacketTranslator<EntityPickRequestPacket> {
|
|
|
|
|
|
|
|
@Override
|
2021-11-22 19:52:26 +00:00
|
|
|
public void translate(GeyserSession session, EntityPickRequestPacket packet) {
|
2020-09-15 00:52:16 +00:00
|
|
|
if (session.getGameMode() != GameMode.CREATIVE) return; // Apparently Java behavior
|
|
|
|
Entity entity = session.getEntityCache().getEntityByGeyserId(packet.getRuntimeEntityId());
|
|
|
|
if (entity == null) return;
|
|
|
|
|
|
|
|
// Get the corresponding item
|
|
|
|
String itemName;
|
2021-11-18 03:02:38 +00:00
|
|
|
switch (entity.getDefinition().entityType()) {
|
2022-05-26 17:11:39 +00:00
|
|
|
case BOAT, CHEST_BOAT -> {
|
2020-09-15 00:52:16 +00:00
|
|
|
// Include type of boat in the name
|
2021-11-18 03:02:38 +00:00
|
|
|
int variant = ((BoatEntity) entity).getVariant();
|
2021-09-10 18:10:56 +00:00
|
|
|
String typeOfBoat = switch (variant) {
|
|
|
|
case 1 -> "spruce";
|
|
|
|
case 2 -> "birch";
|
|
|
|
case 3 -> "jungle";
|
|
|
|
case 4 -> "acacia";
|
|
|
|
case 5 -> "dark_oak";
|
2022-05-26 17:11:39 +00:00
|
|
|
case 6 -> "mangrove";
|
2021-09-10 18:10:56 +00:00
|
|
|
default -> "oak";
|
|
|
|
};
|
2022-05-26 17:11:39 +00:00
|
|
|
itemName = typeOfBoat + "_" + entity.getDefinition().entityType().name().toLowerCase(Locale.ROOT);
|
2021-09-10 18:10:56 +00:00
|
|
|
}
|
|
|
|
case LEASH_KNOT -> itemName = "lead";
|
2021-11-27 04:03:46 +00:00
|
|
|
case CHEST_MINECART, COMMAND_BLOCK_MINECART, FURNACE_MINECART, HOPPER_MINECART, TNT_MINECART ->
|
2021-11-18 03:02:38 +00:00
|
|
|
// The Bedrock identifier matches the item name which moves MINECART to the end of the name
|
|
|
|
// TODO test
|
|
|
|
itemName = entity.getDefinition().identifier();
|
2021-11-27 04:03:46 +00:00
|
|
|
case SPAWNER_MINECART -> itemName = "minecart"; // Turns into a normal minecart
|
2021-09-10 18:10:56 +00:00
|
|
|
//case ITEM_FRAME -> Not an entity in Bedrock Edition
|
|
|
|
//case GLOW_ITEM_FRAME ->
|
|
|
|
case ARMOR_STAND, END_CRYSTAL, MINECART, PAINTING ->
|
|
|
|
// No spawn egg, just an item
|
2021-11-18 03:02:38 +00:00
|
|
|
itemName = entity.getDefinition().entityType().toString().toLowerCase(Locale.ROOT);
|
|
|
|
default -> itemName = entity.getDefinition().entityType().toString().toLowerCase(Locale.ROOT) + "_spawn_egg";
|
2020-09-15 00:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String fullItemName = "minecraft:" + itemName;
|
2021-07-13 01:19:40 +00:00
|
|
|
ItemMapping mapping = session.getItemMappings().getMapping(fullItemName);
|
2020-09-15 00:52:16 +00:00
|
|
|
// Verify it is, indeed, an item
|
2021-07-13 01:19:40 +00:00
|
|
|
if (mapping == null) return;
|
2020-09-15 00:52:16 +00:00
|
|
|
|
2020-12-29 23:09:42 +00:00
|
|
|
InventoryUtils.findOrCreateItem(session, fullItemName);
|
2020-09-15 00:52:16 +00:00
|
|
|
}
|
|
|
|
}
|