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.minecraft.ChatFormatting; import net.minecraft.core.component.DataComponents; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.world.InteractionHand; 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.level.Level; import net.mattias.cobblesnapshot.item.ModItems; import net.minecraft.core.component.TypedData; import net.minecraft.core.component.SimpleComponentTypes; import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
import java.time.LocalDate;
public class CameraUseEvents {
@net.neoforged.bus.api.SubscribeEvent
public static void onEntityRightClick(PlayerInteractEvent.EntityInteractSpecific event) {
if (!(event.getTarget() instanceof PokemonEntity target)) return;
Player player = event.getEntity();
Level level = player.level();
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;
}
// Remove 1 film
player.getInventory().removeItem(filmSlot, 1);
// Get Pokémon data
Pokemon pokemon = target.getPokemon();
// Create image film item
ItemStack snapshot = new ItemStack(ModItems.IMAGE_FILM.get());
// Set custom name via DataComponents
snapshot.set(DataComponents.CUSTOM_NAME, Component.literal("📸 Snapshot of " + pokemon.getDisplayName().getString()));
// Set lore data using CUSTOM_DATA
CompoundTag tag = new CompoundTag();
tag.putString("TakenBy", player.getName().getString());
tag.putString("TakenOn", LocalDate.now().toString());
snapshot.set(DataComponents.CUSTOM_DATA, SimpleComponentTypes.CUSTOM_DATA_TYPE.value().parse(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 symbol 'TypedData' Cannot resolve symbol 'SimpleComponentTypes' Cannot resolve symbol 'SimpleComponentTypes'
Okay, it looks like the previous attempt still had some slight inaccuracies regarding how CustomData is handled in NeoForge 1.21. Let's correct those points.
The errors "Cannot resolve symbol 'TypedData'" and "Cannot resolve symbol 'SimpleComponentTypes'" indicate that these classes or specific ways of using them are not correct for setting the CUSTOM_DATA.
Here's the refined code:
CameraUseEvents.javaPlease look through this github https://github.com/henkelmax/camera/tree/master
I want the "Image Film" to display an actual image of what I just took a picture of
Okay, let's analyze the provided henkelmax/camera repository and compare it to your goal.
The henkelmax/camera mod implements the feature you want: it saves a "photo" of an entity and displays that photo on a physical item (the PhotoItem).
Here's how it achieves this, and what you need to do in your cobblesnapshot mod:
Understanding the henkelmax/camera Approach:
Saving the "Photo":
PhotoDataStorage class which is a WorldPersistentState (data saved with the world).PhotoDataStorage along with a unique ID (photo_id). It doesn't actually render the entity to an image file at this point.photo_id into the PhotoItem's NBT (photo_id tag).Displaying the "Photo" (Client-Side):
BlockEntityWithoutLevelRenderer (BEWLR - a special renderer for items that aren't normal blocks/block entities) for the PhotoItem.PhotoItemRenderer is asked to render the item:
photo_id from the item's NBT.PhotoDataStorage).What You Need to Do in Your cobblesnapshot Mod:
This is a significant undertaking and moves from simple data storage (NBT/DataComponents) to complex client-side rendering.
Modify Data Storage (CameraUseEvents):
PokemonEntity to be able to it on the client.package net.mattias.cobblesnapshot.event;
import com.cobblemon.mod.common.entity.pokemon.PokemonEntity;
import com.cobblemon.mod.common.pokemon.Pokemon;
import net.minecraft.ChatFormatting;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
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.level.Level;
import net.mattias.cobblesnapshot.item.ModItems;
// Remove these imports - they are not used for setting CUSTOM_DATA this way
// import net.minecraft.core.component.TypedData;
// import net.minecraft.core.component.SimpleComponentTypes;
import net.minecraft.world.item.component.CustomData; // Import CustomData
import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
import java.time.LocalDate;
public class CameraUseEvents {
@net.neoforged.bus.api.SubscribeEvent
public static void onEntityRightClick(PlayerInteractEvent.EntityInteractSpecific event) {
if (!(event.getTarget() instanceof PokemonEntity target)) return;
Player player = event.getEntity();
Level level = player.level();
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;
}
// Remove 1 film
player.getInventory().removeItem(filmSlot, 1);
// Get Pokémon data
Pokemon pokemon = target.getPokemon();
// Create image film item
ItemStack snapshot = new ItemStack(ModItems.IMAGE_FILM.get());
// Set custom name via DataComponents
snapshot.set(DataComponents.CUSTOM_NAME, Component.literal("📸 Snapshot of " + pokemon.getDisplayName().getString()));
// Set lore data using CUSTOM_DATA
CompoundTag tag = new CompoundTag();
tag.putString("TakenBy", player.getName().getString());
tag.putString("TakenOn", LocalDate.now().toString());
// Use CustomData.of(tag) to create the component value
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;
}
}
ImageFilmItem.javapackage net.mattias.cobblesnapshot.item.custom;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.level.Level;
import net.minecraft.world.item.TooltipContext; // Import TooltipContext
import net.minecraft.world.item.component.CustomData; // Import CustomData
import java.util.List;
public class ImageFilmItem extends Item {
public ImageFilmItem(Properties properties) {
super(properties);
}
// Correct method signature for 1.21
@Override
public void appendHoverText(ItemStack stack, TooltipContext context, List<Component> tooltip, TooltipFlag flag) {
// Get the CUSTOM_DATA component
CustomData customData = stack.get(DataComponents.CUSTOM_DATA);
if (customData != null) {
// Get the CompoundTag from the CustomData. Use copyTag() for safety.
CompoundTag tag = customData.copyTag();
String taker = tag.getString("TakenBy");
String date = tag.getString("TakenOn");
tooltip.add(Component.literal("Taken by: " + taker));
tooltip.add(Component.literal("Taken on: " + date));
}
}
}
DataComponents.TAG does not exist: In 1.21, arbitrary NBT data is stored under the DataComponents.CUSTOM_DATA component. There is no DataComponents.TAG.CUSTOM_DATA: The value type for DataComponents.CUSTOM_DATA is net.minecraft.world.item.component.CustomData. To create a CustomData object from a CompoundTag, you use the static factory method CustomData.of(CompoundTag tag).CUSTOM_DATA: To retrieve the data, you get the CustomData component (stack.get(DataComponents.CUSTOM_DATA)). If the component exists, you then extract the CompoundTag from the CustomData object, typically using customData.copyTag() to get a mutable copy of the underlying tag.appendHoverText Signature: The appendHoverText method in 1.21 requires a TooltipContext parameter after the ItemStack. I've added the import and the parameter.These changes should now correctly use the Data Components system as intended in NeoForge 1.21 for storing and retrieving your custom NBT data ("TakenBy", "TakenOn") on the ImageFilmItem.
"cobblemon:bulbasaur").CompoundTag that you put into DataComponents.CUSTOM_DATA.// ... inside onEntityRightClick after getting Pokemon pokemon ...
// Create image film item
ItemStack snapshot = new ItemStack(ModItems.IMAGE_FILM.get());
// Set custom name via DataComponents
snapshot.set(DataComponents.CUSTOM_NAME, Component.literal("📸 Snapshot of " + pokemon.getDisplayName().getString()));
// Set lore data AND POKEMON DATA using CUSTOM_DATA
CompoundTag tag = new CompoundTag();
tag.putString("TakenBy", player.getName().getString());
tag.putString("TakenOn", LocalDate.now().toString());
// --- Add Pokemon specific data ---
tag.putString("PokemonSpecies", pokemon.getSpecies().getId());
tag.putString("PokemonForm", pokemon.getForm());
tag.putBoolean("PokemonShiny", pokemon.isShiny());
tag.putString("PokemonSkin", pokemon.getSkin().getId().toString()); // Store Skin ID
// Add other relevant data like pose, scale if needed for rendering
snapshot.set(DataComponents.CUSTOM_DATA, CustomData.of(tag));
// ... rest of the method ...
Create a Client-Side Package and Setup:
net.mattias.cobblesnapshot.client.net.mattias.cobblesnapshot.client) annotated with @Mod.EventBusSubscriber(modid = CobbleSnapshot.MODID, value = Dist.CLIENT).Create a Custom Item Renderer (ImageFilmItemRenderer.java):
BlockEntityWithoutLevelRenderer.render method: void render(ItemStack stack, ItemDisplayContext displayContext, PoseStack poseStack, MultiBufferSource buffer, int packedLight, int packedOverlay);CustomData from the ItemStack.PokemonSpecies, PokemonShiny, etc.) from the CompoundTag inside the CustomData.Pokemon object based on the species, form, shiny, and skin. You might need to create a dummy Pokemon instance just for rendering.PokemonRenderer from Cobblemon's rendering system.PoseStack to translate, rotate, and scale the Pokémon render so it appears correctly positioned and sized on your ImageFilmItem model.PokemonRenderer's rendering method.// net.mattias.cobblesnapshot.client.renderer.ImageFilmItemRenderer.java
package net.mattias.cobblesnapshot.client.renderer;
import com.cobblemon.mod.client.pokemon.PokemonRenderer; // You'll need this or similar
import com.cobblemon.mod.common.pokemon.Pokemon; // To create a dummy Pokemon for rendering
// You might need other Cobblemon rendering imports
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.item.component.CustomData;
// You might need a constructor if you need access to the BlockEntityRenderDispatcher or similar
public class ImageFilmItemRenderer extends BlockEntityWithoutLevelRenderer { // Or possibly BER for items
public ImageFilmItemRenderer() {
// Potentially pass RenderDispatcher or other context if needed
super(null, null); // Check required super constructor parameters in 1.21
}
@Override
public void render(ItemStack stack, ItemDisplayContext displayContext, PoseStack poseStack, MultiBufferSource buffer, int packedLight, int packedOverlay) {
CustomData customData = stack.get(DataComponents.CUSTOM_DATA);
if (customData != null) {
CompoundTag tag = customData.copyTag();
String speciesId = tag.getString("PokemonSpecies");
String form = tag.getString("PokemonForm");
boolean isShiny = tag.getBoolean("PokemonShiny");
String skinId = tag.getString("PokemonSkin"); // Skin ID
// --- Rendering Logic ---
// This part is highly dependent on Cobblemon's internal client-side APIs.
// You need to:
// 1. Get the correct Pokemon object (species, form, shiny, skin).
// You might create a 'dummy' Pokemon instance just for rendering purposes here client-side.
// 2. Get the appropriate Cobblemon renderer for that Pokemon/entity type.
// 3. Apply transformations (translation, rotation, scale) to the poseStack
// to position the Pokemon render on the item model.
// 4. Call the Cobblemon renderer's render method.
// Example (Illustrative - Actual Cobblemon API usage will differ):
/*
try {
// Attempt to get Pokemon instance (Cobblemon might have utilities for this)
Pokemon dummyPokemon = new Pokemon(speciesId, form, isShiny, skinId); // Placeholder constructor
// Get the renderer (Cobblemon API specific)
PokemonRenderer<Pokemon> pokemonRenderer = CobblemonRenderSystem.getRenderer(dummyPokemon); // Placeholder method
if (pokemonRenderer != null) {
poseStack.pushPose();
// Apply transformations: position, scale, rotation relative to item
// These values will need tweaking to look right on your item model
poseStack.translate(0.5, 0.5, 0.01); // Position on the item face
poseStack.scale(0.2f, 0.2f, 0.2f); // Scale down the Pokemon
// poseStack.mulPose(Axis.YP.rotationDegrees(rotation)); // Maybe rotate
// Render the Pokemon (Cobblemon API specific parameters)
pokemonRenderer.render(dummyPokemon, 0, 0, poseStack, buffer, packedLight);
poseStack.popPose();
}
} catch (Exception e) {
// Handle errors if Pokemon or renderer can't be found
e.printStackTrace();
}
*/
// For testing, you might render a simple texture or shape first
// To verify the renderer is working and position transformations are correct.
}
}
}
Register the Renderer:
ImageFilmItemRenderer for your ImageFilmItem.FMLClientSetupEvent, registerItemDecorations, etc.). The exact method can change with NeoForge versions. You'll likely use something like ItemProperties.register or an event for registering item renderers. Correction: For BEWLR, you typically register it using ItemRenderer.registerBlockEntityWithoutLevelRenderer in the appropriate client setup phase.// net.mattias.cobblesnapshot.client.ClientSetup.java (example)
package net.mattias.cobblesnapshot.client;
import net.mattias.cobblesnapshot.CobbleSnapshot;
import net.mattias.cobblesnapshot.item.ModItems; // Your item class
import net.mattias.cobblesnapshot.client.renderer.ImageFilmItemRenderer; // Your renderer
import net.minecraft.client.renderer.item.ItemProperties; // Needed for general item properties
import net.minecraft.client.renderer.item.ItemRenderer; // Needed for BEWLR registration
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
@Mod.EventBusSubscriber(modid = CobbleSnapshot.MODID, value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD)
public class ClientSetup {
@SubscribeEvent
public static void onClientSetup(FMLClientSetupEvent event) {
event.enqueueWork(() -> {
// Register your BEWLR for the ImageFilm item
ItemRenderer.registerBlockEntityWithoutLevelRenderer(ModItems.IMAGE_FILM.get(), new ImageFilmItemRenderer());
// Register any item properties if you have dynamic textures/models based on state (not needed for basic BEWLR)
// ItemProperties.register(...)
});
}
}
Challenges and Complexity:
PokemonRenderer, texture binding, model loading, etc.) is the most difficult part. This might involve using private or internal APIs which could break in future Cobblemon updates. You'll need to look at Cobblemon's source code or decompile it to understand how they render Pokémon client-side.PoseStack so the Pokémon renders nicely on your item model requires careful trial and error.henkelmax/camera mod's approach of dynamically adding resource packs is very complex and likely unnecessary for just rendering existing Cobblemon assets. Storing the data (species, skin ID) and using Cobblemon's existing rendering resources is much simpler.In summary: Implementing this feature requires a deep dive into client-side rendering, specifically how Cobblemon handles rendering Pokémon, and setting up a custom item renderer (BEWLR) to use that rendering logic. It's significantly more complex than just modifying NBT data. The henkelmax/camera repo provides a good blueprint, but the "render entity onto item" step is the core challenge.