Skip to content

Commit

Permalink
Some minor fixes, fix own block breaking progress not showing on the …
Browse files Browse the repository at this point in the history
…player's end
  • Loading branch information
onebeastchris committed Dec 7, 2024
1 parent a2184e4 commit a41d705
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -685,13 +685,13 @@ public final class EntityDefinitions {
.addTranslator(MetadataType.BOOLEAN, CreakingEntity::setIsTearingDown)
.addTranslator(MetadataType.OPTIONAL_POSITION, CreakingEntity::setHomePos)
.properties(new GeyserEntityProperties.Builder()
.addEnum("minecraft:creaking_state",
.addEnum(CreakingEntity.CREAKING_STATE,
"neutral",
"hostile_observed",
"hostile_unobserved",
"twitching",
"crumbling")
.addInt("minecraft:creaking_swaying_ticks", 0, 6)
.addInt(CreakingEntity.CREAKING_SWAYING_TICKS, 0, 6)
.build())
.build();
CREEPER = EntityDefinition.inherited(CreeperEntity::new, mobEntityBase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
import org.geysermc.geyser.item.type.DyeItem;
import org.geysermc.geyser.item.type.Item;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.session.cache.registry.JavaRegistries;
import org.geysermc.geyser.session.cache.tags.GeyserHolderSet;
import org.geysermc.geyser.session.cache.tags.ItemTag;
import org.geysermc.geyser.session.cache.tags.Tag;
import org.geysermc.geyser.util.InteractionResult;
Expand All @@ -62,7 +60,7 @@

public class WolfEntity extends TameableEntity {
private byte collarColor = 14; // Red - default
private GeyserHolderSet<Item> repairableItems = null;
private HolderSet repairableItems = null;
private boolean isCurseOfBinding = false;

public WolfEntity(GeyserSession session, int entityId, long geyserId, UUID uuid, EntityDefinition<?> definition, Vector3f position, Vector3f motion, float yaw, float pitch, float headYaw) {
Expand Down Expand Up @@ -130,8 +128,7 @@ protected Tag<Item> getFoodTag() {
public void setBody(ItemStack stack) {
super.setBody(stack);
isCurseOfBinding = ItemUtils.hasEffect(session, stack, EnchantmentComponent.PREVENT_ARMOR_CHANGE);
HolderSet set = GeyserItemStack.from(stack).getComponent(DataComponentType.REPAIRABLE);
repairableItems = GeyserHolderSet.convertHolderSet(JavaRegistries.ITEM, set);
repairableItems = GeyserItemStack.from(stack).getComponent(DataComponentType.REPAIRABLE);
}

@Override
Expand Down Expand Up @@ -166,7 +163,7 @@ protected InteractiveTag testMobInteraction(@NonNull Hand hand, @NonNull GeyserI
return InteractiveTag.REMOVE_WOLF_ARMOR;
}
if (getFlag(EntityFlag.SITTING) &&
session.getTagCache().is(repairableItems, itemInHand.asItem()) &&
session.getTagCache().isItem(repairableItems, itemInHand.asItem()) &&
this.body.isValid() && this.body.getTag() != null &&
this.body.getTag().getInt("Damage") > 0) {
return InteractiveTag.REPAIR_WOLF_ARMOR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,21 @@
import java.util.Optional;
import java.util.UUID;

/*
* Relevant bits:
* - LevelSoundEvent2Packet(sound=SPAWN, position=(233.5, 112.295, 4717.5), extraData=-1, identifier=minecraft:creaking, babySound=false, relativeVolumeDisabled=false)
* - [11:29:34:768] [CLIENT BOUND] - LevelSoundEvent2Packet(sound=CREAKING_HEART_SPAWN, position=(233.0, 110.0, 4717.0), extraData=-1, identifier=minecraft:creaking, babySound=false, relativeVolumeDisabled=false)
* - [11:29:34:768] [CLIENT BOUND] - LevelSoundEvent2Packet(sound=CREAKING_HEART_SPAWN, position=(235.0, 113.0, 4722.0), extraData=13734, identifier=, babySound=false, relativeVolumeDisabled=false)
* - [11:29:34:768] [CLIENT BOUND] - LevelEventPacket(type=PARTICLE_MOB_BLOCK_SPAWN, position=(233.0, 110.0, 4717.0), data=769)
*
*/
public class CreakingEntity extends MonsterEntity {

private Vector3i homePosition;

public static final String CREAKING_STATE = "minecraft:creaking_state";
public static final String CREAKING_SWAYING_TICKS = "minecraft:creaking_swaying_ticks";

public CreakingEntity(GeyserSession session, int entityId, long geyserId, UUID uuid, EntityDefinition<?> definition, Vector3f position, Vector3f motion, float yaw, float pitch, float headYaw) {
super(session, entityId, geyserId, uuid, definition, position, motion, yaw, pitch, headYaw);
}
Expand All @@ -58,7 +69,7 @@ protected void initializeMetadata() {

@Override
public void addAdditionalSpawnData(AddEntityPacket addEntityPacket) {
propertyManager.add("minecraft:creaking_state", "neutral");
propertyManager.add(CREAKING_STATE, "neutral");
propertyManager.add("minecraft:creaking_swaying_ticks", 0);
propertyManager.applyIntProperties(addEntityPacket.getProperties().getIntProperties());
}
Expand All @@ -68,11 +79,11 @@ public void setCanMove(EntityMetadata<Boolean,? extends MetadataType<Boolean>> b
setFlag(EntityFlag.BODY_ROTATION_BLOCKED, false);

// unfreeze sound? SoundEvent.UNFREEZE
propertyManager.add("minecraft:creaking_state", "hostile_unobserved");
propertyManager.add(CREAKING_STATE, "hostile_unobserved");
updateBedrockEntityProperties();
} else {
setFlag(EntityFlag.BODY_ROTATION_BLOCKED, true);
propertyManager.add("minecraft:creaking_state", "hostile_observed");
propertyManager.add(CREAKING_STATE, "hostile_observed");
updateBedrockEntityProperties();
}

Expand All @@ -92,15 +103,15 @@ public void setActive(EntityMetadata<Boolean,? extends MetadataType<Boolean>> bo
// setFlag(EntityFlag.HIDDEN_WHEN_INVISIBLE, true);
// setFlag(EntityFlag.BODY_ROTATION_BLOCKED, true);
} else {
propertyManager.add("minecraft:creaking_state", "neutral");
propertyManager.add(CREAKING_STATE, "neutral");
}
GeyserImpl.getInstance().getLogger().warning("set active; " + booleanEntityMetadata.toString());
}

public void setIsTearingDown(EntityMetadata<Boolean,? extends MetadataType<Boolean>> booleanEntityMetadata) {
GeyserImpl.getInstance().getLogger().warning("set isTearingDown; " + booleanEntityMetadata.toString());
if (booleanEntityMetadata.getValue()) {
propertyManager.add("minecraft:creaking_state", "crumbling");
propertyManager.add(CREAKING_STATE, "crumbling");
updateBedrockEntityProperties();
// LevelEventPacket levelEventPacket = new LevelEventPacket();
// levelEventPacket.setType(ParticleType.CREAKING_CRUMBLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@
import org.geysermc.geyser.inventory.item.BedrockEnchantment;
import org.geysermc.geyser.item.Items;
import org.geysermc.geyser.item.enchantment.Enchantment;
import org.geysermc.geyser.item.type.Item;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.session.cache.registry.JavaRegistries;
import org.geysermc.geyser.session.cache.tags.GeyserHolderSet;
import org.geysermc.geyser.translator.inventory.InventoryTranslator;
import org.geysermc.geyser.translator.text.MessageTranslator;
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
Expand Down Expand Up @@ -403,8 +400,7 @@ private boolean isRepairing(GeyserItemStack input, GeyserItemStack material, Gey
return false;
}

GeyserHolderSet<Item> set = GeyserHolderSet.convertHolderSet(JavaRegistries.ITEM, repairable);
return session.getTagCache().is(set, material.asItem());
return session.getTagCache().isItem(repairable, material.asItem());
}

private boolean isRenaming(GeyserSession session, AnvilContainer anvilContainer, boolean bedrock) {
Expand Down
31 changes: 31 additions & 0 deletions core/src/main/java/org/geysermc/geyser/session/cache/TagCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@
import it.unimi.dsi.fastutil.ints.IntArrays;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.kyori.adventure.key.Key;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.GeyserLogger;
import org.geysermc.geyser.inventory.GeyserItemStack;
import org.geysermc.geyser.item.type.Item;
import org.geysermc.geyser.level.block.type.Block;
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.session.cache.registry.JavaRegistries;
import org.geysermc.geyser.session.cache.registry.JavaRegistryKey;
import org.geysermc.geyser.session.cache.tags.GeyserHolderSet;
import org.geysermc.geyser.session.cache.tags.Tag;
import org.geysermc.geyser.util.MinecraftKey;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.HolderSet;
import org.geysermc.mcprotocollib.protocol.packet.common.clientbound.ClientboundUpdateTagsPacket;

import javax.annotation.ParametersAreNonnullByDefault;
Expand Down Expand Up @@ -127,6 +130,34 @@ public <T> boolean is(@Nullable GeyserHolderSet<T> holderSet, @Nullable T object
return contains(holderSet.resolveRaw(this), holderSet.getRegistry().toNetworkId(session, object));
}

/**
* Accessible via the {@link #isItem(HolderSet, Item)} method.
* @return true if the specified network ID is in the given {@link HolderSet} set.
*/
private <T> boolean is(@Nullable HolderSet holderSet, @NonNull JavaRegistryKey<T> registry, int id) {
if (holderSet == null) {
return false;
}

int[] entries = holderSet.resolve(key -> {
if (key.value().startsWith("#")) {
key = Key.key(key.namespace(), key.value().substring(1));
}
return getRaw(new Tag<>(registry, key));
});

return contains(entries, id);
}

public boolean isItem(@Nullable HolderSet holderSet, @NonNull Item item) {
return is(holderSet, JavaRegistries.ITEM, item.javaId());
}

public boolean isBlock(@Nullable HolderSet holderSet, @NonNull Block block) {
return is(holderSet, JavaRegistries.BLOCK, block.javaId());
}


public <T> List<T> get(Tag<T> tag) {
return mapRawArray(session, getRaw(tag), tag.registry());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.geysermc.geyser.session.GeyserSession;
import org.geysermc.geyser.session.cache.TagCache;
import org.geysermc.geyser.session.cache.registry.JavaRegistryKey;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.HolderSet;

import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -88,27 +87,6 @@ public int[] resolveRaw(TagCache tagCache) {
return tagCache.getRaw(Objects.requireNonNull(tag, "HolderSet must have a tag if it doesn't have a list of IDs"));
}

/**
* Reads a MCPL {@link HolderSet} and turns it into a GeyserHolderSet.
* @param registry the registry the HolderSet contains IDs from.
* @param holderSet the HolderSet as the MCPL HolderSet object
*/
public static <T> GeyserHolderSet<T> convertHolderSet(@NonNull JavaRegistryKey<T> registry, @Nullable HolderSet holderSet) {
if (holderSet == null) {
return new GeyserHolderSet<>(registry, IntArrays.EMPTY_ARRAY);
}

if (holderSet.getHolders() != null) {
return new GeyserHolderSet<>(registry, holderSet.getHolders());
}

if (holderSet.getLocation() != null) {
return new GeyserHolderSet<>(registry, new Tag<>(registry, holderSet.getLocation()));
}

throw new IllegalStateException("HolderSet must have a tag or a list of IDs! " + holderSet);
}

/**
* Reads a HolderSet from an object from NBT.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private static void handle(GeyserSession session, PlayerBlockActionData blockAct
LevelEventPacket startBreak = new LevelEventPacket();
startBreak.setType(LevelEvent.BLOCK_START_BREAK);
startBreak.setPosition(vector.toFloat());
double breakTime = BlockUtils.getSessionBreakTime(session, BlockState.of(blockState).block()) * 20; // TODO afdaöwelfunöwoaenf
double breakTime = BlockUtils.getSessionBreakTimeTicks(session, BlockState.of(blockState).block());

// If the block is custom or the breaking item is custom, we must keep track of break time ourselves
GeyserItemStack item = session.getPlayerInventory().getItemInHand();
Expand Down Expand Up @@ -137,7 +137,7 @@ private static void handle(GeyserSession session, PlayerBlockActionData blockAct
Direction direction = Direction.VALUES[blockFace];
spawnBlockBreakParticles(session, direction, vector, breakingBlockState);

double breakTime = BlockUtils.getSessionBreakTime(session, breakingBlockState.block()) * 20;
double breakTime = BlockUtils.getSessionBreakTimeTicks(session, breakingBlockState.block());
// If the block is custom, we must keep track of when it should break ourselves
long blockBreakStartTime = session.getBlockBreakStartTime();
if (blockBreakStartTime != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
@Translator(packet = ClientboundSelectKnownPacks.class)
public class JavaSelectKnownPacksTranslator extends PacketTranslator<ClientboundSelectKnownPacks> {
// todo: dump from client?
private static final Set<String> KNOWN_PACK_IDS = Set.of("core", "winter_drop", "trade_rebalance", "redstone_experiments", "minecart_improvements");
private static final Set<String> KNOWN_PACK_IDS = Set.of("core", "trade_rebalance", "redstone_experiments", "minecart_improvements");

@Override
public void translate(GeyserSession session, ClientboundSelectKnownPacks packet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.cloudburstmc.protocol.bedrock.packet.PlaySoundPacket;
import org.cloudburstmc.protocol.bedrock.packet.SetEntityDataPacket;
import org.cloudburstmc.protocol.bedrock.packet.SetEntityMotionPacket;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.entity.EntityDefinitions;
import org.geysermc.geyser.entity.type.Entity;
import org.geysermc.geyser.entity.type.EvokerFangsEntity;
Expand Down Expand Up @@ -294,6 +295,8 @@ public void translate(GeyserSession session, ClientboundEntityEventPacket packet
creakingEntity.createParticleBeam();
}
break;
default:
GeyserImpl.getInstance().getLogger().debug("unhandled entity event: " + packet);
}

if (entityEventPacket.getType() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ public class JavaBlockDestructionTranslator extends PacketTranslator<Clientbound
@Override
public void translate(GeyserSession session, ClientboundBlockDestructionPacket packet) {
int state = session.getGeyser().getWorldManager().getBlockAt(session, packet.getPosition().getX(), packet.getPosition().getY(), packet.getPosition().getZ());
int breakTime = (int) (65535 / Math.ceil(BlockUtils.getBreakTime(session, BlockState.of(state).block(), ItemMapping.AIR, null, false) * 20));
int breakTime = 12; //(int) (65535 / Math.ceil(BlockUtils.getBreakTime(session, BlockState.of(state).block(), ItemMapping.AIR, null, false)));
// TODO we need to send a "total" time to Bedrock.
// Current plan:
// - start with block destroy time (if applicable)
// - track the time in ticks between stages
// - attempt to "extrapolate" to a value for Bedrock
LevelEventPacket levelEventPacket = new LevelEventPacket();
levelEventPacket.setPosition(packet.getPosition().toFloat());
levelEventPacket.setType(LevelEvent.BLOCK_START_BREAK);
Expand Down
Loading

0 comments on commit a41d705

Please sign in to comment.