package net.mattias.cobblesnapshot.event; import…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
package net.mattias.cobblesnapshot.event;
import com.cobblemon.mod.common.entity.pokemon.PokemonEntity; import com.cobblemon.mod.common.pokemon.Pokemon; import net.mattias.cobblesnapshot.item.ModItems; import net.minecraft.ChatFormatting; import net.minecraft.core.component.DataComponents; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.component.CustomData; import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
import java.time.LocalDate; import java.time.format.DateTimeFormatter;
public class CameraUseEvents {
@net.neoforged.bus.api.SubscribeEvent
public static void onEntityRightClick(PlayerInteractEvent.EntityInteractSpecific event) {
// We only want to do this on the server to prevent consuming items twice.
// The server will handle creating the item and synchronizing it to the client.
if (event.getLevel().isClientSide()) return;
if (!(event.getTarget() instanceof PokemonEntity target)) return;
Player player = event.getEntity();
ItemStack camera = player.getItemInHand(event.getHand());
if (!camera.is(ModItems.CAMERA.get())) return;
// Check for film in inventory
int filmSlot = findFirstSlotWithItem(player, ModItems.FILM.get());
if (filmSlot == -1) {
player.sendSystemMessage(Component.literal("You need film to take a snapshot!").withStyle(ChatFormatting.RED));
return;
}
// Get Pokémon data
Pokemon pokemon = target.getPokemon();
// Consume 1 film ONLY AFTER we know the operation is successful
player.getInventory().removeItem(filmSlot, 1);
// --- Create the Snapshot Item ---
ItemStack snapshot = new ItemStack(ModItems.IMAGE_FILM.get());
// Set custom name
snapshot.set(DataComponents.CUSTOM_NAME, Component.literal("📸 Snapshot of " + pokemon.getDisplayName().getString()));
// --- Store Metadata in CUSTOM_DATA ---
CompoundTag tag = new CompoundTag();
tag.putString("TakenBy", player.getName().getString());
tag.putString("TakenOn", LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE));
// Get the Pokémon's sprite location from Cobblemon and store it as a string
ResourceLocation sprite = pokemon.getSprite();
tag.putString("SpritePath", sprite.toString());
// Apply the data to the item stack
snapshot.set(DataComponents.CUSTOM_DATA, CustomData.of(tag));
// Give item to player
if (!player.addItem(snapshot)) {
player.drop(snapshot, false);
}
player.sendSystemMessage(Component.literal("📸 You took a snapshot of " + pokemon.getDisplayName() + "!"));
event.setCanceled(true);
event.setCancellationResult(InteractionResult.SUCCESS);
}
private static int findFirstSlotWithItem(Player player, Item item) {
for (int i = 0; i < player.getInventory().getContainerSize(); i++) {
ItemStack stack = player.getInventory().getItem(i);
if (stack.is(item)) return i;
}
return -1;
}
}
Cannot resolve method 'getSprite' in 'Pokemon'
package net.mattias.cobblesnapshot.item.custom;
import net.minecraft.core.component.DataComponents; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.inventory.tooltip.TooltipComponent; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.item.component.CustomData;
import java.util.List; import java.util.Optional;
public class ImageFilmItem extends Item { public ImageFilmItem(Properties properties) { super(properties); }
// This method adds the text lines to the tooltip
@Override
public void appendHoverText(ItemStack stack, TooltipContext context, List<Component> tooltip, TooltipFlag flag) {
super.appendHoverText(stack, context, tooltip, flag);
CustomData customData = stack.get(DataComponents.CUSTOM_DATA);
if (customData != null) {
CompoundTag tag = customData.copyTag(); // Use copyTag() to get the NBT
if (tag.contains("TakenBy")) {
tooltip.add(Component.literal("Taken by: " + tag.getString("TakenBy")));
}
if (tag.contains("TakenOn")) {
tooltip.add(Component.literal("Taken on: " + tag.getString("TakenOn")));
}
}
}
// This new vanilla method adds an image to the tooltip
@Override
public Optional<TooltipComponent> getTooltipImage(ItemStack stack) {
CustomData customData = stack.get(DataComponents.CUSTOM_DATA);
if (customData != null) {
CompoundTag tag = customData.copyTag();
if (tag.contains("SpritePath")) {
// Read the sprite path we saved earlier
ResourceLocation spriteLocation = ResourceLocation.tryParse(tag.getString("SpritePath"));
if (spriteLocation != null) {
// Return a TooltipImage object. The dimensions can be adjusted.
return Optional.of(new TooltipImage(spriteLocation, 64, 64));
}
}
}
return Optional.empty();
}
} Cannot resolve symbol 'TooltipImage'
Okay, let's fix those two compilation errors.
Cannot resolve method 'getSprite' in 'Pokemon': You are correct, the Pokemon object itself doesn't have a direct getSprite() method that returns the ResourceLocation. The sprite is associated with the species of the Pokémon. You need to get the species first.Cannot resolve symbol 'TooltipImage': The getTooltipImage method you tried overriding was from older versions. In NeoForge 1.21, the method is called getTooltipComponent, and it returns a TooltipComponent. To create a TooltipComponent that specifically displays an image, you use a static factory method provided by TooltipComponent.Here are the corrected files:
CameraUseEvents.javapackage net.mattias.cobblesnapshot.event;
import com.cobblemon.mod.common.entity.pokemon.PokemonEntity;
import com.cobblemon.mod.common.pokemon.Pokemon;
import net.mattias.cobblesnapshot.item.ModItems;
import net.minecraft.ChatFormatting;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.CustomData;
import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class CameraUseEvents {
@net.neoforged.bus.api.SubscribeEvent
public static void onEntityRightClick(PlayerInteractEvent.EntityInteractSpecific event) {
// We only want to do this on the server to prevent consuming items twice.
// The server will handle creating the item and synchronizing it to the client.
if (event.getLevel().isClientSide()) return;
if (!(event.getTarget() instanceof PokemonEntity target)) return;
Player player = event.getEntity();
ItemStack camera = player.getItemInHand(event.getHand());
if (!camera.is(ModItems.CAMERA.get())) return;
// Check for film in inventory
int filmSlot = findFirstSlotWithItem(player, ModItems.FILM.get());
if (filmSlot == -1) {
player.sendSystemMessage(Component.literal("You need film to take a snapshot!").withStyle(ChatFormatting.RED));
return;
}
// Get Pokémon data
Pokemon pokemon = target.getPokemon();
// Consume 1 film ONLY AFTER we know the operation is successful
player.getInventory().removeItem(filmSlot, 1);
// --- Create the Snapshot Item ---
ItemStack snapshot = new ItemStack(ModItems.IMAGE_FILM.get());
// Set custom name
snapshot.set(DataComponents.CUSTOM_NAME, Component.literal("📸 Snapshot of " + pokemon.getDisplayName().getString()));
// --- Store Metadata in CUSTOM_DATA ---
CompoundTag tag = new CompoundTag();
tag.putString("TakenBy", player.getName().getString());
tag.putString("TakenOn", LocalDate.now().format(DateTimeFormatter.ISO_LOCAL_DATE));
// Get the Pokémon's sprite location from Cobblemon Species and store it as a string
// Use pokemon.getSpecies() to get the species, then getSprite() from the species.
ResourceLocation sprite = pokemon.getSpecies().getSprite();
tag.putString("SpritePath", sprite.toString());
// Apply the data to the item stack
snapshot.set(DataComponents.CUSTOM_DATA, CustomData.of(tag));
// Give item to player
if (!player.addItem(snapshot)) {
player.drop(snapshot, false);
}
player.sendSystemMessage(Component.literal("📸 You took a snapshot of " + pokemon.getDisplayName() + "!"));
event.setCanceled(true);
event.setCancellationResult(InteractionResult.SUCCESS);
}
private static int findFirstSlotWithItem(Player player, Item item) {
for (int i = 0; i < player.getInventory().getContainerSize(); i++) {
ItemStack stack = player.getInventory().getItem(i);
if (stack.is(item)) return i;
}
return -1;
}
}
Reasoning for getSprite fix: The Pokemon object represents an instance of a Pokémon (with unique stats, IVs, etc.), while the PokemonSpecies object represents the type of Pokémon (Pikachu, Charizard, etc.). The sprite is a property of the species, not the individual instance.
ImageFilmItem.javapackage net.mattias.cobblesnapshot.item.custom;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.tooltip.TooltipComponent; // Correct import for server-side component
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.component.CustomData;
import net.minecraft.world.item.Item.TooltipContext; // Correct import for TooltipContext
import java.util.List;
import java.util.Optional;
public class ImageFilmItem extends Item {
public ImageFilmItem(Properties properties) {
super(properties);
}
// This method adds the text lines to the tooltip
@Override
public void appendHoverText(ItemStack stack, TooltipContext context, List<Component> tooltip, TooltipFlag flag) {
super.appendHoverText(stack, context, tooltip, flag); // Always call super
CustomData customData = stack.get(DataComponents.CUSTOM_DATA);
if (customData != null) {
CompoundTag tag = customData.copyTag(); // Use copyTag() to get the NBT safely
if (tag.contains("TakenBy")) {
tooltip.add(Component.literal("Taken by: " + tag.getString("TakenBy")).withStyle(TooltipFlag.WIKI_LOOKUP.getChatColor())); // Optional: Style the text
}
if (tag.contains("TakenOn")) {
tooltip.add(Component.literal("Taken on: " + tag.getString("TakenOn")).withStyle(TooltipFlag.WIKI_LOOKUP.getChatColor())); // Optional: Style the text
}
// We don't add the ImagePath to the tooltip text, it's used for the tooltip image
}
}
// This new vanilla method adds an image component to the tooltip
// Method name is getTooltipComponent, not getTooltipImage
@Override
public Optional<TooltipComponent> getTooltipComponent(ItemStack stack) {
CustomData customData = stack.get(DataComponents.CUSTOM_DATA);
if (customData != null) {
CompoundTag tag = customData.copyTag();
if (tag.contains("SpritePath")) {
// Read the sprite path we saved earlier
ResourceLocation spriteLocation = ResourceLocation.tryParse(tag.getString("SpritePath"));
if (spriteLocation != null) {
// Use the static factory method on TooltipComponent to create an image component
// The client will then render this using the client-side TooltipImage class
return Optional.of(TooltipComponent.createImage(spriteLocation, 64, 64)); // Adjust size (64x64) as needed
}
}
}
return Optional.empty();
}
// You can remove this method as getTooltipComponent handles adding the image
// @Override
// public void renderGuiItemDecorations(...) { ... }
// Also remove the blit and GuiItemDecorationRenderer imports and usage.
// The image is now shown in the tooltip, not as an overlay on the item icon itself.
}
Reasoning for TooltipImage fix:
Item for adding a custom component to the tooltip is getTooltipComponent(ItemStack stack).Optional<TooltipComponent>. TooltipComponent is a server-side interface that describes what kind of custom content the tooltip has.TooltipComponent that handle the actual rendering (like net.minecraft.client.gui.screens.inventory.tooltip.TooltipImage).TooltipComponent.createImage(ResourceLocation location, int width, int height). This creates a specific type of TooltipComponent that the client knows how to render as an image using its TooltipImage class.With these changes, your code should compile correctly and the Image Film item will display the Pokémon's sprite at the top of its tooltip when you hover over it, along with the text metadata.