Skip to content

Commit

Permalink
Use tags sent from the server, where possible (GeyserMC#2188)
Browse files Browse the repository at this point in the history
Java Edition has a tags system that allows for some server-side control of certain properties. This PR allows for piglin trading items, wool, and flowers to be determined from the server.
  • Loading branch information
Camotoy authored May 3, 2021
1 parent e74fa6c commit ce000a4
Show file tree
Hide file tree
Showing 33 changed files with 295 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.nukkitx.math.vector.Vector3f;
import org.geysermc.connector.entity.living.AgeableEntity;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemEntry;

public class AnimalEntity extends AgeableEntity {

Expand All @@ -40,7 +42,7 @@ public AnimalEntity(long entityId, long geyserId, EntityType entityType, Vector3
* <code>wheat</code>.
* @return true if this is a valid item to breed with for this animal.
*/
public boolean canEat(String javaIdentifierStripped) {
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
// This is what it defaults to. OK.
return javaIdentifierStripped.equals("wheat");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,16 @@
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;
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import com.nukkitx.protocol.bedrock.packet.EntityEventPacket;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;

import java.util.Set;
import org.geysermc.connector.network.translators.item.ItemEntry;

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 @@ -74,7 +66,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

@Override
public boolean canEat(String javaIdentifierStripped) {
return FLOWERS.contains(javaIdentifierStripped);
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
return session.getTagCache().isFlower(itemEntry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

import com.nukkitx.math.vector.Vector3f;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemEntry;

public class ChickenEntity extends AnimalEntity {

Expand All @@ -35,7 +37,7 @@ public ChickenEntity(long entityId, long geyserId, EntityType entityType, Vector
}

@Override
public boolean canEat(String javaIdentifierStripped) {
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
return javaIdentifierStripped.contains("seeds");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemEntry;

public class FoxEntity extends AnimalEntity {

Expand All @@ -54,7 +55,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

@Override
public boolean canEat(String javaIdentifierStripped) {
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
return javaIdentifierStripped.equals("sweet_berries");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemEntry;
import org.geysermc.connector.utils.DimensionUtils;

public class HoglinEntity extends AnimalEntity {
Expand All @@ -49,7 +50,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

@Override
public boolean canEat(String javaIdentifierStripped) {
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
return javaIdentifierStripped.equals("crimson_fungus");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemEntry;

public class OcelotEntity extends AnimalEntity {

Expand All @@ -46,7 +47,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

@Override
public boolean canEat(String javaIdentifierStripped) {
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
return javaIdentifierStripped.equals("cod") || javaIdentifierStripped.equals("salmon");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.nukkitx.protocol.bedrock.packet.EntityEventPacket;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemEntry;
import org.geysermc.connector.network.translators.item.ItemRegistry;

public class PandaEntity extends AnimalEntity {
Expand Down Expand Up @@ -80,7 +81,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

@Override
public boolean canEat(String javaIdentifierStripped) {
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
return javaIdentifierStripped.equals("bamboo");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemEntry;

public class PigEntity extends AnimalEntity {

Expand All @@ -47,7 +48,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemEntry;

public class PolarBearEntity extends AnimalEntity {

Expand All @@ -46,7 +47,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

@Override
public boolean canEat(String javaIdentifierStripped) {
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemEntry;

public class RabbitEntity extends AnimalEntity {

Expand Down Expand Up @@ -61,7 +62,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

@Override
public boolean canEat(String javaIdentifierStripped) {
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
return javaIdentifierStripped.equals("dandelion") || javaIdentifierStripped.equals("carrot") || javaIdentifierStripped.equals("golden_carrot");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.geysermc.connector.entity.Entity;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemEntry;

public class StriderEntity extends AnimalEntity {

Expand Down Expand Up @@ -87,7 +88,7 @@ public void updateBedrockMetadata(GeyserSession session) {
}

@Override
public boolean canEat(String javaIdentifierStripped) {
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
return javaIdentifierStripped.equals("warped_fungus");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemEntry;

public class TurtleEntity extends AnimalEntity {

Expand All @@ -48,7 +49,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

@Override
public boolean canEat(String javaIdentifierStripped) {
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
return javaIdentifierStripped.equals("seagrass");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.geysermc.connector.entity.living.animal.AnimalEntity;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemEntry;
import org.geysermc.connector.network.translators.item.ItemRegistry;

import java.util.Set;
Expand Down Expand Up @@ -112,7 +113,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

@Override
public boolean canEat(String javaIdentifierStripped) {
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
return DONKEY_AND_HORSE_FOODS.contains(javaIdentifierStripped);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.nukkitx.protocol.bedrock.packet.MobArmorEquipmentPacket;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemEntry;
import org.geysermc.connector.network.translators.item.ItemRegistry;

public class LlamaEntity extends ChestedHorseEntity {
Expand Down Expand Up @@ -77,7 +78,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

@Override
public boolean canEat(String javaIdentifierStripped) {
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
return javaIdentifierStripped.equals("wheat") || javaIdentifierStripped.equals("hay_block");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemEntry;

public class CatEntity extends TameableEntity {

Expand Down Expand Up @@ -85,7 +86,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

@Override
public boolean canEat(String javaIdentifierStripped) {
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
return javaIdentifierStripped.equals("cod") || javaIdentifierStripped.equals("salmon");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.nukkitx.protocol.bedrock.data.entity.EntityData;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemEntry;

public class ParrotEntity extends TameableEntity {

Expand All @@ -47,7 +48,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

@Override
public boolean canEat(String javaIdentifierStripped) {
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
return javaIdentifierStripped.contains("seeds") || javaIdentifierStripped.equals("cookie");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemEntry;

import java.util.Set;

Expand Down Expand Up @@ -87,7 +88,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
}

@Override
public boolean canEat(String javaIdentifierStripped) {
public boolean canEat(GeyserSession session, String javaIdentifierStripped, ItemEntry itemEntry) {
// Cannot be a baby to eat these foods
return WOLF_FOODS.contains(javaIdentifierStripped) && !metadata.getFlags().getFlag(EntityFlag.BABY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession s
@Override
public void updateOffHand(GeyserSession session) {
// Check if the Piglin is holding Gold and set the ADMIRING flag accordingly so its pose updates
boolean changed = metadata.getFlags().setFlag(EntityFlag.ADMIRING, offHand.getId() == ItemRegistry.GOLD.getBedrockId());
boolean changed = metadata.getFlags().setFlag(EntityFlag.ADMIRING, session.getTagCache().shouldPiglinAdmire(ItemRegistry.getItem(this.offHand)));
if (changed) {
super.updateBedrockMetadata(session);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public class GeyserSession implements CommandSender {
private ChunkCache chunkCache;
private EntityCache entityCache;
private EntityEffectCache effectCache;
private final TagCache tagCache;
private WorldCache worldCache;
private WindowCache windowCache;
private final Int2ObjectMap<TeleportCache> teleportMap = new Int2ObjectOpenHashMap<>();
Expand Down Expand Up @@ -452,6 +453,7 @@ public GeyserSession(GeyserConnector connector, BedrockServerSession bedrockServ
this.chunkCache = new ChunkCache(this);
this.entityCache = new EntityCache(this);
this.effectCache = new EntityEffectCache();
this.tagCache = new TagCache();
this.worldCache = new WorldCache(this);
this.windowCache = new WindowCache(this);

Expand Down
Loading

0 comments on commit ce000a4

Please sign in to comment.