Add use cooldown support

This commit is contained in:
Eclipse 2024-11-29 16:09:51 +00:00
parent 0922b181f2
commit 435b73df69
No known key found for this signature in database
GPG key ID: 95E6998F82EC938A
2 changed files with 23 additions and 10 deletions

View file

@ -51,12 +51,14 @@ import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponen
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.Equippable;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.FoodProperties;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.UseCooldown;
import org.geysermc.mcprotocollib.protocol.data.game.level.sound.BuiltinSound;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
public class CustomItemRegistryPopulator_v2 {
@ -141,6 +143,11 @@ public class CustomItemRegistryPopulator_v2 {
computeConsumableProperties(consumable, foodProperties == null || foodProperties.isCanAlwaysEat(), itemProperties, componentBuilder);
}
UseCooldown useCooldown = components.get(DataComponentType.USE_COOLDOWN);
if (useCooldown != null) {
computeUseCooldownProperties(useCooldown, componentBuilder);
}
// TODO block item/runtime ID, entity placer, chargeable, throwable, item cooldown, hat hardcoded on java,
computeRenderOffsets(false, customItemDefinition.bedrockOptions(), componentBuilder);
@ -306,6 +313,15 @@ public class CustomItemRegistryPopulator_v2 {
componentBuilder.putCompound("minecraft:food", NbtMap.builder().putBoolean("can_always_eat", canAlwaysEat).build());
}
private static void computeUseCooldownProperties(UseCooldown cooldown, NbtMapBuilder componentBuilder) {
Objects.requireNonNull(cooldown.cooldownGroup(), "Cooldown group can't be null");
componentBuilder.putCompound("minecraft:cooldown", NbtMap.builder()
.putString("category", cooldown.cooldownGroup().asString())
.putFloat("duration", cooldown.seconds())
.build()
);
}
private static void computeRenderOffsets(boolean isHat, CustomItemBedrockOptions bedrockOptions, NbtMapBuilder componentBuilder) {
if (isHat) {
componentBuilder.remove("minecraft:render_offsets");

View file

@ -45,23 +45,20 @@ public class JavaCooldownTranslator extends PacketTranslator<ClientboundCooldown
Key cooldownGroup = packet.getCooldownGroup();
Item item = Registries.JAVA_ITEM_IDENTIFIERS.get(cooldownGroup.asString());
// Not every item, as of 1.19, appears to be server-driven. Just these two.
// Custom items can define an item cooldown using a custom cooldown group, which will be sent to the client if there's not a vanilla cooldown group
String cooldownCategory = cooldownGroup.asString();
// Not every vanilla item, as of 1.19, appears to be server-driven. Just these two.
// Use a map here if it gets too big.
String cooldownCategory;
if (item == Items.GOAT_HORN) {
cooldownCategory = "goat_horn";
} else if (item == Items.SHIELD) {
cooldownCategory = "shield";
} else {
cooldownCategory = null;
}
if (cooldownCategory != null) {
PlayerStartItemCooldownPacket bedrockPacket = new PlayerStartItemCooldownPacket();
bedrockPacket.setItemCategory(cooldownCategory);
bedrockPacket.setCooldownDuration(packet.getCooldownTicks());
session.sendUpstreamPacket(bedrockPacket);
}
PlayerStartItemCooldownPacket bedrockPacket = new PlayerStartItemCooldownPacket();
bedrockPacket.setItemCategory(cooldownCategory);
bedrockPacket.setCooldownDuration(packet.getCooldownTicks());
session.sendUpstreamPacket(bedrockPacket);
session.getWorldCache().setCooldown(cooldownGroup, packet.getCooldownTicks());
}