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'
The errors you're encountering are due to incorrect usage of the new data components system in NeoForge 1.21. Specifically, the symbols TypedData and SimpleComponentTypes are not directly used for setting custom data on items. Instead, you should use the DataComponents.CUSTOM_DATA component directly with a CompoundTag.
Here's the corrected version of your CameraUseEvents class:
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.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, 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;
}
}
Please 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
To display an actual image of what you just took a picture of using the "Image Film" item, you'll need to implement a few additional components:
ImageFilmItem to display the captured image when the item is held or viewed in the inventory.Here's a step-by-step guide to achieve this:
You'll need to capture the image of the Pokémon when the snapshot is taken. This can be done using Minecraft's rendering system.
Store the captured image as a texture. You can save it as a PNG file in the mod's resource directory.
Modify the ImageFilmItem to display the captured image. This involves creating a custom model and texture for the item.
Modify the onEntityRightClick method to capture the image of the Pokémon.
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.EntityRenderDispatcher;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
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;
}
}
TypedData and SimpleComponentTypes: These classes are not used for setting custom NBT data on items. Instead, you directly use DataComponents.CUSTOM_DATA with a CompoundTag.CUSTOM_DATA Directly: The CompoundTag is set directly using snapshot.set(DataComponents.CUSTOM_DATA, tag);.ImageFilmItem Class:package 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.world.item.TooltipContext;
import net.minecraft.world.level.Level;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.core.component.DataComponents;
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");
tooltip.add(Component.literal("Taken by: " + taker));
tooltip.add(Component.literal("Taken on: " + date));
}
}
}
appendHoverText Method Signature: The method now takes TooltipContext instead of Level.CUSTOM_DATA: The CompoundTag is retrieved using stack.get(DataComponents.CUSTOM_DATA).These changes should resolve the compilation errors and ensure your mod is compatible with NeoForge 1.21.
Modify the ImageFilmItem to display the captured image.
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.client.resources.model.ModelManager;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag;
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.world.item.TooltipContext;
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 = new ResourceLocation("cobblesnapshot", "images/" + imagePath);
textureManager.bind(resourceLocation);
blit(x, y, 0, 0, 16, 16, 16, 16);
}
}
}
ImageFilmItem to display the captured image when the item is held or viewed in the inventory.This implementation will allow the "Image Film" item to display an actual image of the Pokémon that was captured.