2019-12-01 00:19:03 +00:00
|
|
|
/*
|
2021-01-01 15:10:36 +00:00
|
|
|
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
|
2019-12-01 00:19:03 +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
|
|
|
|
*/
|
|
|
|
|
2020-01-09 02:35:00 +00:00
|
|
|
package org.geysermc.connector.entity.living.animal.horse;
|
2019-12-01 00:19:03 +00:00
|
|
|
|
|
|
|
import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata;
|
2021-04-26 23:59:12 +00:00
|
|
|
import com.google.common.collect.ImmutableSet;
|
2019-12-01 00:19:03 +00:00
|
|
|
import com.nukkitx.math.vector.Vector3f;
|
2020-11-11 18:13:13 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.entity.EntityData;
|
|
|
|
import com.nukkitx.protocol.bedrock.data.entity.EntityEventType;
|
2020-06-23 00:11:09 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
|
2020-12-26 17:20:59 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.data.inventory.ContainerType;
|
2020-11-11 18:13:13 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.EntityEventPacket;
|
2021-07-08 02:44:53 +00:00
|
|
|
import com.nukkitx.protocol.bedrock.packet.UpdateAttributesPacket;
|
|
|
|
import org.geysermc.connector.entity.attribute.GeyserAttributeType;
|
2020-01-09 02:35:00 +00:00
|
|
|
import org.geysermc.connector.entity.living.animal.AnimalEntity;
|
2019-12-01 00:19:03 +00:00
|
|
|
import org.geysermc.connector.entity.type.EntityType;
|
|
|
|
import org.geysermc.connector.network.session.GeyserSession;
|
2021-05-03 01:47:11 +00:00
|
|
|
import org.geysermc.connector.network.translators.item.ItemEntry;
|
2020-11-11 18:13:13 +00:00
|
|
|
import org.geysermc.connector.network.translators.item.ItemRegistry;
|
2019-12-01 00:19:03 +00:00
|
|
|
|
2021-04-26 23:59:12 +00:00
|
|
|
import java.util.Set;
|
|
|
|
|
2019-12-01 00:19:03 +00:00
|
|
|
public class AbstractHorseEntity extends AnimalEntity {
|
2021-04-26 23:59:12 +00:00
|
|
|
/**
|
|
|
|
* A list of all foods a horse/donkey can eat on Java Edition.
|
|
|
|
* Used to display interactive tag if needed.
|
|
|
|
*/
|
|
|
|
private static final Set<String> DONKEY_AND_HORSE_FOODS = ImmutableSet.of("golden_apple", "enchanted_golden_apple",
|
|
|
|
"golden_carrot", "sugar", "apple", "wheat", "hay_block");
|
2019-12-01 00:19:03 +00:00
|
|
|
|
|
|
|
public AbstractHorseEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
|
|
|
|
super(entityId, geyserId, entityType, position, motion, rotation);
|
2020-12-26 17:20:59 +00:00
|
|
|
|
|
|
|
// Specifies the size of the entity's inventory. Required to place slots in the entity.
|
|
|
|
metadata.put(EntityData.CONTAINER_BASE_SIZE, 2);
|
2021-07-08 22:55:28 +00:00
|
|
|
|
|
|
|
metadata.getFlags().setFlag(EntityFlag.WASD_CONTROLLED, true);
|
2019-12-01 00:19:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-07-08 02:44:53 +00:00
|
|
|
public void spawnEntity(GeyserSession session) {
|
|
|
|
super.spawnEntity(session);
|
|
|
|
|
|
|
|
// Add horse jump strength attribute to allow donkeys and mules to jump, if they don't send the attribute themselves.
|
|
|
|
// Confirmed broken without this code by making a new donkey in vanilla 1.17.1
|
|
|
|
// The spawn packet does have an attributes section, but adding the jump strength property there causes the
|
|
|
|
// donkey to jump very high.
|
|
|
|
UpdateAttributesPacket attributesPacket = new UpdateAttributesPacket();
|
|
|
|
attributesPacket.setRuntimeEntityId(geyserId);
|
|
|
|
attributesPacket.getAttributes().add(GeyserAttributeType.HORSE_JUMP_STRENGTH.getAttribute(0.5f, 2));
|
|
|
|
session.sendUpstreamPacket(attributesPacket);
|
|
|
|
}
|
2020-05-23 21:39:17 +00:00
|
|
|
|
2021-07-08 02:44:53 +00:00
|
|
|
@Override
|
|
|
|
public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) {
|
2021-05-20 02:24:11 +00:00
|
|
|
if (entityMetadata.getId() == 17) {
|
2020-03-06 23:29:11 +00:00
|
|
|
byte xd = (byte) entityMetadata.getValue();
|
2021-07-08 22:55:28 +00:00
|
|
|
boolean tamed = (xd & 0x02) == 0x02;
|
|
|
|
boolean saddled = (xd & 0x04) == 0x04;
|
|
|
|
metadata.getFlags().setFlag(EntityFlag.TAMED, tamed);
|
|
|
|
metadata.getFlags().setFlag(EntityFlag.SADDLED, saddled);
|
2020-03-06 23:29:11 +00:00
|
|
|
metadata.getFlags().setFlag(EntityFlag.EATING, (xd & 0x10) == 0x10);
|
2020-05-23 21:39:17 +00:00
|
|
|
metadata.getFlags().setFlag(EntityFlag.STANDING, (xd & 0x20) == 0x20);
|
2020-11-11 18:13:13 +00:00
|
|
|
|
|
|
|
// HorseFlags
|
|
|
|
// Bred 0x10
|
|
|
|
// Eating 0x20
|
|
|
|
// Open mouth 0x80
|
|
|
|
int horseFlags = 0x0;
|
|
|
|
horseFlags = (xd & 0x40) == 0x40 ? horseFlags | 0x80 : horseFlags;
|
|
|
|
|
|
|
|
// Only set eating when we don't have mouth open so a player interaction doesn't trigger the eating animation
|
|
|
|
horseFlags = (xd & 0x10) == 0x10 && (xd & 0x40) != 0x40 ? horseFlags | 0x20 : horseFlags;
|
|
|
|
|
|
|
|
// Set the flags into the display item
|
|
|
|
metadata.put(EntityData.DISPLAY_ITEM, horseFlags);
|
|
|
|
|
|
|
|
// Send the eating particles
|
|
|
|
// We use the wheat metadata as static particles since Java
|
|
|
|
// doesn't send over what item was used to feed the horse
|
|
|
|
if ((xd & 0x40) == 0x40) {
|
|
|
|
EntityEventPacket entityEventPacket = new EntityEventPacket();
|
|
|
|
entityEventPacket.setRuntimeEntityId(geyserId);
|
|
|
|
entityEventPacket.setType(EntityEventType.EATING_ITEM);
|
|
|
|
entityEventPacket.setData(ItemRegistry.WHEAT.getBedrockId() << 16);
|
|
|
|
session.sendUpstreamPacket(entityEventPacket);
|
|
|
|
}
|
2020-12-26 17:20:59 +00:00
|
|
|
|
|
|
|
// Set container type if tamed
|
2021-07-08 22:55:28 +00:00
|
|
|
metadata.put(EntityData.CONTAINER_TYPE, tamed ? (byte) ContainerType.HORSE.getId() : (byte) 0);
|
2020-05-23 21:39:17 +00:00
|
|
|
|
2021-07-08 22:55:28 +00:00
|
|
|
// Shows the jump meter
|
|
|
|
metadata.getFlags().setFlag(EntityFlag.CAN_POWER_JUMP, saddled);
|
|
|
|
}
|
2020-05-23 21:39:17 +00:00
|
|
|
|
2019-12-01 00:19:03 +00:00
|
|
|
super.updateBedrockMetadata(entityMetadata, session);
|
|
|
|
}
|
2021-04-26 23:59:12 +00:00
|
|
|
|
|
|
|
@Override
|
2021-05-03 01:47:11 +00:00
|
|
|
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
|
2021-04-26 23:59:12 +00:00
|
|
|
return DONKEY_AND_HORSE_FOODS.contains(javaIdentifierStripped);
|
|
|
|
}
|
2019-12-01 00:19:03 +00:00
|
|
|
}
|