Skip to content

Commit

Permalink
Play effect when feeding baby animals (GeyserMC#2146)
Browse files Browse the repository at this point in the history
Java plays a client-side-only effect when feeding animals. This commit abstracts out the feeding code we already have for interactive tag and checks it when right-clicking any animal that is a baby.
  • Loading branch information
Camotoy authored Apr 26, 2021
1 parent 48fcb47 commit 7d80dff
Show file tree
Hide file tree
Showing 24 changed files with 245 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ public class AnimalEntity extends AgeableEntity {
public AnimalEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
super(entityId, geyserId, entityType, position, motion, rotation);
}

/**
* @param javaIdentifierStripped the stripped Java identifier of the item that is potential breeding food. For example,
* <code>wheat</code>.
* @return true if this is a valid item to breed with for this animal.
*/
public boolean canEat(String javaIdentifierStripped) {
// This is what it defaults to. OK.
return javaIdentifierStripped.equals("wheat");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package org.geysermc.connector.entity.living.animal;

import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata;
import com.google.common.collect.ImmutableSet;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.entity.EntityData;
import com.nukkitx.protocol.bedrock.data.entity.EntityEventType;
Expand All @@ -34,7 +35,15 @@
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;

import java.util.Set;

public class BeeEntity extends AnimalEntity {
/**
* A list of all flowers. Used for feeding bees.
*/
private static final Set<String> FLOWERS = ImmutableSet.of("dandelion", "poppy", "blue_orchid", "allium", "azure_bluet",
"red_tulip", "pink_tulip", "white_tulip", "orange_tulip", "cornflower", "lily_of_the_valley", "wither_rose",
"sunflower", "lilac", "rose_bush", "peony");

public BeeEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
super(entityId, geyserId, entityType, position, motion, rotation);
Expand Down Expand Up @@ -63,4 +72,9 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}
super.updateBedrockMetadata(entityMetadata, session);
}

@Override
public boolean canEat(String javaIdentifierStripped) {
return FLOWERS.contains(javaIdentifierStripped);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2019-2021 GeyserMC. http://geysermc.org
*
* 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.connector.entity.living.animal;

import com.nukkitx.math.vector.Vector3f;
import org.geysermc.connector.entity.type.EntityType;

public class ChickenEntity extends AnimalEntity {

public ChickenEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
super(entityId, geyserId, entityType, position, motion, rotation);
}

@Override
public boolean canEat(String javaIdentifierStripped) {
return javaIdentifierStripped.contains("seeds");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}
super.updateBedrockMetadata(entityMetadata, session);
}

@Override
public boolean canEat(String javaIdentifierStripped) {
return javaIdentifierStripped.equals("sweet_berries");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}
super.updateBedrockMetadata(entityMetadata, session);
}

@Override
public boolean canEat(String javaIdentifierStripped) {
return javaIdentifierStripped.equals("crimson_fungus");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}
super.updateBedrockMetadata(entityMetadata, session);
}

@Override
public boolean canEat(String javaIdentifierStripped) {
return javaIdentifierStripped.equals("cod") || javaIdentifierStripped.equals("salmon");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
super.updateBedrockMetadata(entityMetadata, session);
}

@Override
public boolean canEat(String javaIdentifierStripped) {
return javaIdentifierStripped.equals("bamboo");
}

/**
* Update the panda's appearance, and take into consideration the recessive brown and weak traits that only show up
* when both main and hidden genes match
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
super.updateBedrockMetadata(entityMetadata, session);
}

@Override
public boolean canEat(String javaIdentifierStripped) {
return javaIdentifierStripped.equals("carrot") || javaIdentifierStripped.equals("potato") || javaIdentifierStripped.equals("beetroot");
}

@Override
protected float getDefaultMaxHealth() {
return 10f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}
super.updateBedrockMetadata(entityMetadata, session);
}

@Override
public boolean canEat(String javaIdentifierStripped) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
metadata.put(EntityData.VARIANT, variant);
}
}

@Override
public boolean canEat(String javaIdentifierStripped) {
return javaIdentifierStripped.equals("dandelion") || javaIdentifierStripped.equals("carrot") || javaIdentifierStripped.equals("golden_carrot");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,9 @@ public void updateBedrockMetadata(GeyserSession session) {

super.updateBedrockMetadata(session);
}

@Override
public boolean canEat(String javaIdentifierStripped) {
return javaIdentifierStripped.equals("warped_fungus");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}
super.updateBedrockMetadata(entityMetadata, session);
}

@Override
public boolean canEat(String javaIdentifierStripped) {
return javaIdentifierStripped.equals("seagrass");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
package org.geysermc.connector.entity.living.animal.horse;

import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata;
import com.google.common.collect.ImmutableSet;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.entity.EntityData;
import com.nukkitx.protocol.bedrock.data.entity.EntityEventType;
Expand All @@ -38,7 +39,15 @@
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemRegistry;

import java.util.Set;

public class AbstractHorseEntity extends AnimalEntity {
/**
* 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");

public AbstractHorseEntity(long entityId, long geyserId, EntityType entityType, Vector3f position, Vector3f motion, Vector3f rotation) {
super(entityId, geyserId, entityType, position, motion, rotation);
Expand Down Expand Up @@ -101,4 +110,9 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
updateBedrockAttributes(session);
}
}

@Override
public boolean canEat(String javaIdentifierStripped) {
return DONKEY_AND_HORSE_FOODS.contains(javaIdentifierStripped);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}
super.updateBedrockMetadata(entityMetadata, session);
}

@Override
public boolean canEat(String javaIdentifierStripped) {
return javaIdentifierStripped.equals("wheat") || javaIdentifierStripped.equals("hay_block");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}
}
}

@Override
public boolean canEat(String javaIdentifierStripped) {
return javaIdentifierStripped.equals("cod") || javaIdentifierStripped.equals("salmon");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}
super.updateBedrockMetadata(entityMetadata, session);
}

@Override
public boolean canEat(String javaIdentifierStripped) {
return javaIdentifierStripped.contains("seeds") || javaIdentifierStripped.equals("cookie");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,23 @@
package org.geysermc.connector.entity.living.animal.tameable;

import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadata;
import com.google.common.collect.ImmutableSet;
import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.protocol.bedrock.data.entity.EntityData;
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;

import java.util.Set;

public class WolfEntity extends TameableEntity {
/**
* A list of all foods a wolf can eat on Java Edition.
* Used to display interactive tag or particles if needed.
*/
private static final Set<String> WOLF_FOODS = ImmutableSet.of("pufferfish", "tropical_fish", "chicken", "cooked_chicken",
"porkchop", "beef", "rabbit", "cooked_porkchop", "cooked_beef", "rotten_flesh", "mutton", "cooked_mutton",
"cooked_rabbit");

private byte collarColor;

Expand Down Expand Up @@ -75,4 +85,10 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s

super.updateBedrockMetadata(entityMetadata, session);
}

@Override
public boolean canEat(String javaIdentifierStripped) {
// Cannot be a baby to eat these foods
return WOLF_FOODS.contains(javaIdentifierStripped) && !metadata.getFlags().getFlag(EntityFlag.BABY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
@Getter
public enum EntityType {

CHICKEN(AnimalEntity.class, 10, 0.7f, 0.4f),
CHICKEN(ChickenEntity.class, 10, 0.7f, 0.4f),
COW(AnimalEntity.class, 11, 1.4f, 0.9f),
PIG(PigEntity.class, 12, 0.9f),
SHEEP(SheepEntity.class, 13, 1.3f, 0.9f),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@

package org.geysermc.connector.network.translators.sound;

import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
import com.nukkitx.math.vector.Vector3f;
import org.geysermc.connector.inventory.GeyserItemStack;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemRegistry;

import java.util.Map;

Expand All @@ -47,6 +45,9 @@ public interface BlockSoundInteractionHandler extends SoundInteractionHandler<St
* @param identifier the identifier of the block
*/
static void handleBlockInteraction(GeyserSession session, Vector3f position, String identifier) {
// If we need to get the hand identifier, only get it once and save it to a variable
String handIdentifier = null;

for (Map.Entry<SoundHandler, SoundInteractionHandler<?>> interactionEntry : SoundHandlerRegistry.INTERACTION_HANDLERS.entrySet()) {
if (!(interactionEntry.getValue() instanceof BlockSoundInteractionHandler)) {
continue;
Expand All @@ -66,7 +67,9 @@ static void handleBlockInteraction(GeyserSession session, Vector3f position, Str
if (itemInHand.isEmpty()) {
continue;
}
String handIdentifier = itemInHand.getItemEntry().getJavaIdentifier();
if (handIdentifier == null) {
handIdentifier = itemInHand.getItemEntry().getJavaIdentifier();
}
boolean contains = false;
for (String itemIdentifier : interactionEntry.getKey().items()) {
if (handIdentifier.contains(itemIdentifier)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@

package org.geysermc.connector.network.translators.sound;

import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
import com.nukkitx.math.vector.Vector3f;
import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.inventory.GeyserItemStack;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemRegistry;

import java.util.Map;

Expand All @@ -48,6 +46,9 @@ public interface EntitySoundInteractionHandler extends SoundInteractionHandler<E
* @param entity the entity interacted with
*/
static void handleEntityInteraction(GeyserSession session, Vector3f position, Entity entity) {
// If we need to get the hand identifier, only get it once and save it to a variable
String handIdentifier = null;

for (Map.Entry<SoundHandler, SoundInteractionHandler<?>> interactionEntry : SoundHandlerRegistry.INTERACTION_HANDLERS.entrySet()) {
if (!(interactionEntry.getValue() instanceof EntitySoundInteractionHandler)) {
continue;
Expand All @@ -67,7 +68,10 @@ static void handleEntityInteraction(GeyserSession session, Vector3f position, En
if (itemInHand.isEmpty()) {
continue;
}
String handIdentifier = itemInHand.getItemEntry().getJavaIdentifier();
if (handIdentifier == null) {
// Don't get the identifier unless we need it
handIdentifier = itemInHand.getItemEntry().getJavaIdentifier();
}
boolean contains = false;
for (String itemIdentifier : interactionEntry.getKey().items()) {
if (handIdentifier.contains(itemIdentifier)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
* Leave empty to ignore.
*
* Only applies to interaction handlers that are an
* instance of {@link BlockSoundInteractionHandler}.
* instance of {@link EntitySoundInteractionHandler}.
*
* @return the value the item in the player's hand must contain
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.geysermc.connector.network.translators.sound.SoundHandler;

@SoundHandler(blocks = "comparator")
public class ComparatorSoundInteractHandler implements BlockSoundInteractionHandler {
public class ComparatorSoundInteractionHandler implements BlockSoundInteractionHandler {

@Override
public void handleInteraction(GeyserSession session, Vector3f position, String identifier) {
Expand Down
Loading

0 comments on commit 7d80dff

Please sign in to comment.