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 com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.Tesselator; import net.mattias.cobblesnapshot.item.ModItems; import net.minecraft.ChatFormatting; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.client.renderer.entity.EntityRenderDispatcher; 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.level.Level; import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; 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();
// Capture the image
BufferedImage image = capturePokemonImage(target);
// Save the image
String imagePath = saveImage(image, pokemon.getDisplayName().getString());
// 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());
tag.putString("ImagePath", imagePath);
snapshot.set(DataComponents.CUSTOM_DATA, 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 BufferedImage capturePokemonImage(PokemonEntity pokemon) {
Minecraft minecraft = Minecraft.getInstance();
EntityRenderDispatcher dispatcher = minecraft.getEntityRenderDispatcher();
PoseStack poseStack = new PoseStack();
poseStack.translate(0, 0, -500); // Move the camera back
poseStack.scale(-100.0F, 100.0F, 100.0F); // Scale the image
BufferedImage image = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = image.createGraphics();
RenderSystem.recordRenderCall(() -> {
dispatcher.render(pokemon, 0, 0, 0, 0, 0, poseStack, MultiBufferSource.immediate(Tesselator.getInstance().getBuilder()), 15728880);
});
graphics.dispose();
return image;
}
private static String saveImage(BufferedImage image, String pokemonName) {
String imagePath = "mods/cobblesnapshot/images/" + pokemonName + ".png";
try {
ImageIO.write(image, "png", new File(imagePath));
} catch (IOException e) {
e.printStackTrace();
}
return imagePath;
}
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 'set(DataComponentType, CompoundTag)' Cannot resolve method 'getBuilder' in 'Tesselator'
package net.mattias.cobblesnapshot.item.custom;
import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.texture.TextureManager; 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.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.level.Level;
import java.util.List;
public class ImageFilmItem extends Item { public ImageFilmItem(Properties properties) { super(properties); }
@Override
public void appendHoverText(ItemStack stack, TooltipContext context, List<Component> tooltip, TooltipFlag flag) {
CompoundTag tag = stack.get(DataComponents.CUSTOM_DATA);
if (tag != null) {
String taker = tag.getString("TakenBy");
String date = tag.getString("TakenOn");
String imagePath = tag.getString("ImagePath");
tooltip.add(Component.literal("Taken by: " + taker));
tooltip.add(Component.literal("Taken on: " + date));
tooltip.add(Component.literal("Image Path: " + imagePath));
}
}
@Override
public boolean isFoil(ItemStack stack) {
return true;
}
@Override
public void renderGuiItemDecorations(GuiItemDecorationRenderer renderer, ItemStack stack, int x, int y, List<Component> tooltip) {
super.renderGuiItemDecorations(renderer, stack, x, y, tooltip);
CompoundTag tag = stack.get(DataComponents.CUSTOM_DATA);
if (tag != null) {
String imagePath = tag.getString("ImagePath");
TextureManager textureManager = Minecraft.getInstance().getTextureManager();
ResourceLocation resourceLocation = ResourceLocation.fromNamespaceAndPath("cobblesnapshot", "images/" + imagePath);
textureManager.bindForSetup(resourceLocation);
blit(x, y, 0, 0, 16, 16, 16, 16);
}
}
} Incompatible types. Found: 'net.minecraft.world.item.component.CustomData', required: 'net.minecraft.nbt.CompoundTag' Method does not override method from its superclass Cannot resolve symbol 'GuiItemDecorationRenderer' Cannot resolve method 'renderGuiItemDecorations' in 'Item' Incompatible types. Found: 'net.minecraft.world.item.component.CustomData', required: 'net.minecraft.nbt.CompoundTag' Cannot resolve method 'blit' in 'ImageFilmItem'
Below is an outline of one approach to have your “Image Film” item show an actual image of the photographed Pokémon. (Keep in mind that dynamically capturing an entity’s rendered image and displaying it on an item is an advanced topic. It requires custom off‐screen rendering and a custom item renderer. The example code below is only one possible approach and may need further adjustments to work in your project.)
When the player “takes a picture” (in your event handler), you want to capture a render of the target Pokémon and then store (or reference) that image. In our example we will:
• Render the entity offscreen (or—for simplicity—create a dummy image)
• Save that image to a file (or in memory)
• Write the file path (or an ID) into the item’s custom data
Because using the actual render on the fly is complex (you would normally need to render into a framebuffer), the example below uses a dummy image for demonstration.
In NeoForge 1.21 the “custom data” stored on an item is not a raw CompoundTag but rather wrapped in a type (commonly called a CustomData) obtained via the DataComponents API. In our code you must convert your CompoundTag into the proper type when setting it and then extract the underlying CompoundTag when reading it.
For example, suppose the “CUSTOM_DATA” component expects an instance of CustomData (a helper class wrapping a CompoundTag). (Your mappings or API might differ.) You will write:
// Import the appropriate class (adjust the package as necessary)
import net.minecraft.world.item.component.CustomData;
Then, in your snapshot event handler:
// … after you have created your CompoundTag called tag…
snapshot.set(DataComponents.CUSTOM_DATA, new CustomData(tag));
And when reading from the item (for example, in your item renderer), do something like:
CustomData customData = stack.get(DataComponents.CUSTOM_DATA);
CompoundTag tag = customData != null ? customData.get() : null;
(Adjust based on your actual API – the point is that you cannot set a raw CompoundTag where a CustomData is expected.)
Below is an adapted version of your event handler. Note that for “capturing” an image we now use a dummy image (a magenta square) because setting up an off‐screen framebuffer render is quite advanced.
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.client.Minecraft;
import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
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.level.Level;
import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
import net.minecraft.world.item.component.CustomData;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
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 from inventory
player.getInventory().removeItem(filmSlot, 1);
// Get Pokémon data
Pokemon pokemon = target.getPokemon();
// Capture the image (for demo purposes, we create a dummy image)
BufferedImage image = capturePokemonImage(target);
// Save the image and record its file path
String imagePath = saveImage(image, pokemon.getDisplayName().getString());
// Create image film item
ItemStack snapshot = new ItemStack(ModItems.IMAGE_FILM.get());
// Set a custom display name
snapshot.set(DataComponents.CUSTOM_NAME, Component.literal("📸 Snapshot of " + pokemon.getDisplayName().getString()));
// Prepare custom item data and include the image path
CompoundTag tag = new CompoundTag();
tag.putString("TakenBy", player.getName().getString());
tag.putString("TakenOn", LocalDate.now().toString());
tag.putString("ImagePath", imagePath);
// Wrap the NBT in a CustomData instance (this is the expected type for CUSTOM_DATA)
snapshot.set(DataComponents.CUSTOM_DATA, new CustomData(tag));
// Give the item to the player (or drop it if the inventory is full)
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);
}
// For demonstration: create a dummy image
private static BufferedImage capturePokemonImage(PokemonEntity pokemon) {
// In a real implementation you would use off-screen rendering (via a framebuffer) to capture the rendered entity.
// Here we simply create a 512x512 image filled with a magenta color.
BufferedImage image = new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = image.createGraphics();
graphics.setColor(Color.MAGENTA);
graphics.fillRect(0, 0, 512, 512);
graphics.dispose();
return image;
}
private static String saveImage(BufferedImage image, String pokemonName) {
// Save the image under the mods/cobblesnapshot/images folder.
// (Ensure that folder exists and that the game has permission to write there.)
String imagePath = "mods/cobblesnapshot/images/" + pokemonName + ".png";
try {
ImageIO.write(image, "png", new File(imagePath));
} catch (IOException e) {
e.printStackTrace();
}
return imagePath;
}
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;
}
}
Notes on the above code:
• The call to
snapshot.set(DataComponents.CUSTOM_DATA, new CustomData(tag));
wraps your CompoundTag in the expected type. (Depending on your available mappings, adjust the package or constructor accordingly.)
• The method capturePokemonImage here just makes a dummy image. In your real mod you would set up an offscreen render (using a framebuffer) so you actually capture the rendered entity.
• The saving of the image returns a file path (for example "mods/cobblesnapshot/images/Pikachu.png"). You may want to adjust this (or store a resource location ID) so your client renderer can later load the image.
Unfortunately, you cannot simply override a method on Item to “blit” your image on the item in the inventory. Instead, you must write a custom item renderer (or provide a custom baked model) that uses your saved image.
A rough outline for a custom renderer would be:
ImageFilmItemRenderer) that implements the appropriate renderer interface (for instance, extending BlockEntityWithoutLevelRenderer in modern Minecraft or using your mod loader’s custom item renderer system).For example, a simplified pseudo‐code snippet for your renderer might look like this:
public class ImageFilmItemRenderer extends BlockEntityWithoutLevelRenderer {
public ImageFilmItemRenderer() {
super();
}
@Override
public void renderByItem(ItemStack stack, ItemTransforms.TransformType transformType, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight, int packedOverlay) {
// Retrieve the custom data from the item
CustomData customData = stack.get(DataComponents.CUSTOM_DATA);
if (customData != null) {
CompoundTag tag = customData.get();
String imagePath = tag.getString("ImagePath");
// Construct a resource location – ideally the image was copied into a resource domain or loaded as a dynamic texture.
ResourceLocation textureLocation = new ResourceLocation("cobblesnapshot", "images/" + imagePath);
// Bind the texture and render a quad with it.
Minecraft.getInstance().getTextureManager().bindForSetup(textureLocation);
// You can now render using rendering helpers.
// For example, using GuiComponent.blit (import net.minecraft.client.gui.GuiComponent)
// or rendering a textured quad manually.
}
}
}
You then need to register this renderer in your client setup (the exact method depends on your mod loader/NeoForge registration system).
Event code:
– Captures (or fakes) an image
– Saves the image to a file
– Stores the file path (and other metadata) into custom data on the item
Custom item renderer:
– Reads that custom data
– Loads the image as a texture
– Renders the image in the item’s inventory or in the world
Because these topics involve fairly advanced rendering concepts (off-screen framebuffers, dynamic textures, custom item renderers), you may need to consult additional documentation or examples (such as the official Minecraft rendering code or existing example mods) for the full implementation.
Good luck implementing your live “camera” mod!