Skip to content

Commit

Permalink
Fixup item hovers for custom items
Browse files Browse the repository at this point in the history
Temporary(?) workaround for ghost cursor items
  • Loading branch information
SoSeDiK committed Feb 23, 2025
1 parent 8191a49 commit b9878ca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private static void allowServerTranslatablesOnItems() {
private static void lessLimitedCraftingBook() {
allowDamagedItemsInRecipeBook = getBoolean(config, "recipe-matcher.allow-damaged-items", false, "Allow using damaged items when searching for items");
allowEnchantedItemsInRecipeBook = getBoolean(config, "recipe-matcher.allow-enchanted-items", false, "Allow using items with enchantments when searching for items");
allowRenamedItemsInRecipeBook = getBoolean(config, "recipe-matcher.allow-renamed-items", false, "Allow using items with custom names (i.e. renamed in anvil) when searching for items");
allowRenamedItemsInRecipeBook = getBoolean(config, "recipe-matcher.allow-renamed-items", false, "Allow using items with custom names (i.e., renamed in anvil) when searching for items");
}
// Kiterino end - Less limited recipe matcher

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.papermc.paper.datacomponent.DataComponentTypes;
import io.papermc.paper.datacomponent.item.BlockItemDataProperties;
import io.papermc.paper.datacomponent.item.BundleContents;
import io.papermc.paper.datacomponent.item.UseCooldown;
import io.papermc.paper.datacomponent.item.UseRemainder;
import me.sosedik.kiterino.KiterinoConfig;
import me.sosedik.kiterino.inventory.InventorySlotHelper;
Expand All @@ -17,6 +18,7 @@
import me.sosedik.kiterino.modifier.item.context.packet.RecipeBookPacketContext;
import me.sosedik.kiterino.modifier.item.context.packet.SlottedItemPacketContext;
import me.sosedik.kiterino.modifier.item.context.packet.UnknownEntityDataContext;
import net.kyori.adventure.key.Key;
import net.minecraft.advancements.Advancement;
import net.minecraft.advancements.AdvancementHolder;
import net.minecraft.advancements.DisplayInfo;
Expand Down Expand Up @@ -58,6 +60,7 @@
import net.minecraft.world.entity.projectile.windcharge.AbstractWindCharge;
import net.minecraft.world.entity.projectile.windcharge.WindCharge;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.item.crafting.SelectableRecipe;
Expand All @@ -84,6 +87,7 @@
import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.craftbukkit.inventory.CraftRecipe;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

Expand Down Expand Up @@ -144,6 +148,23 @@ public void unregisterModifier(ItemModifier itemModifier) {
// Special nbt cases
org.bukkit.inventory.ItemStack item = contextBox.getItem();
if (!item.isEmpty()) {
// Kiterino start - Implement packet item faker for injected items
Player viewer = contextBox.getViewer();
// Cooldown for custom items
if (viewer != null && viewer.hasCooldown(contextBox.getInitialType())) {
boolean applyCooldown = true;
if (item.hasData(DataComponentTypes.USE_COOLDOWN)) {
UseCooldown useCooldown = item.getData(DataComponentTypes.USE_COOLDOWN);
assert useCooldown != null;
applyCooldown = contextBox.getInitialType().key().equals(useCooldown.cooldownGroup());
}
if (applyCooldown) {
UseCooldown useCooldown = UseCooldown.useCooldown(viewer.getCooldown(contextBox.getInitialType()) / 20F).cooldownGroup(contextBox.getInitialType().getKey()).build();
item.setData(DataComponentTypes.USE_COOLDOWN, useCooldown);
modified = true;
}
}
// Kiterino end - Implement packet item faker for injected items
// Bundles
if (item.hasData(DataComponentTypes.BUNDLE_CONTENTS)) {
BundleContents bundleContents = item.getData(DataComponentTypes.BUNDLE_CONTENTS);
Expand Down Expand Up @@ -311,10 +332,12 @@ private static Packet<?> handle(CraftPlayer player, ClientboundPlaceGhostRecipeP
}

private static Packet<?> handle(CraftPlayer player, ClientboundSetCursorItemPacket packet) {
ItemStack original = packet.contents();
// For some reason, the packet desyncs sometimes for custom items, hence grabbing the real cursor item // TODO remove this…
ItemStack original = /* packet.contents() */ player.getHandle().containerMenu.getCarried();
var context = new SlottedItemPacketContext(packet, InventorySlotHelper.CURSOR);
var contextBox = new ItemContextBox(player, ItemModifierContextType.SET_SLOT, context, original.asBukkitCopy());
ItemStack result = fromBukkit(contextBox, original);
if (result == null && original.is(Items.AIR)) result = ItemStack.EMPTY; // Otherwise, the client is getting kicked with the carried workaround above. Why? Good question! // TODO remove this…
return result == null ? packet : new ClientboundSetCursorItemPacket(result);
}

Expand Down Expand Up @@ -420,10 +443,11 @@ private static Packet<?> handle(CraftPlayer player, ClientboundSetEntityDataPack

private static Packet<?> handle(CraftPlayer player, ClientboundSetPlayerInventoryPacket packet) {
ItemStack original = packet.contents();
var context = new SlottedItemPacketContext(packet, packet.slot());
int slot = packet.slot();
var context = new SlottedItemPacketContext(packet, slot);
var contextBox = new ItemContextBox(player, ItemModifierContextType.SET_SLOT, context, original.asBukkitCopy());
ItemStack result = fromBukkit(contextBox, original);
return result == null ? packet : new ClientboundSetCursorItemPacket(result);
return result == null ? packet : new ClientboundSetPlayerInventoryPacket(slot, result);
}

// Yes, this also sucks!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public boolean hasData(DataComponentType type) {
@Override
public Object constructItemProperties() {
return new Item.Properties()
.setId(ResourceKey.create(Registries.ITEM, this.itemKey))
.component(DataComponents.USE_COOLDOWN, new UseCooldown(0F, Optional.of(this.itemKey))); // Kiterino - Implement packet item faker for injected items
.setId(ResourceKey.create(Registries.ITEM, this.itemKey));
}

// Kiterino start - Data-driven blocks
Expand Down

0 comments on commit b9878ca

Please sign in to comment.