Topic category: Troubleshooting, bugs, and solutions
I am trying to damage items upon right click but it's not working... Here is the code and the gradle log:
Code:
package mod.mcreator;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.common.util.EnumHelper;
import net.minecraft.world.World;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ActionResult;
import net.minecraft.server.MinecraftServer;
import net.minecraft.item.ItemSword;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Item;
import net.minecraft.init.Blocks;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.Minecraft;
import java.util.Set;
import java.util.Random;
import java.util.HashMap;
public class mcreator_voidSword {
public mcreator_voidSword() {
}
public static Item block;
public static Object instance;
public void load(FMLInitializationEvent event) {
ItemStack stack = new ItemStack(block, 1);
if (event.getSide() == Side.CLIENT)
Minecraft.getMinecraft().getRenderItem().getItemModelMesher()
.register(block, 0, new ModelResourceLocation("testenvironmentmod:voidsword", "inventory"));
GameRegistry.addRecipe(stack,
new Object[]{"012", "345", "678", Character.valueOf('0'), mcreator_netherStarBlock.block, Character.valueOf('1'),
mcreator_voidBlock.block, Character.valueOf('2'), mcreator_netherStarBlock.block, Character.valueOf('3'),
mcreator_netherStarBlock.block, Character.valueOf('4'), mcreator_voidBlock.block, Character.valueOf('5'),
mcreator_netherStarBlock.block, Character.valueOf('6'), mcreator_netherStarBlock.block, Character.valueOf('7'),
Blocks.OBSIDIAN, Character.valueOf('8'), mcreator_netherStarBlock.block,});
}
public void generateNether(World world, Random random, int chunkX, int chunkZ) {
}
public void generateSurface(World world, Random random, int chunkX, int chunkZ) {
}
public int addFuel(ItemStack fuel) {
return 0;
}
public void serverLoad(FMLServerStartingEvent event) {
}
public void preInit(FMLPreInitializationEvent event) {
}
public void registerRenderers() {
}
static {
Item.ToolMaterial enumt = EnumHelper.addToolMaterial("VOIDSWORD", 1, 1000, 4F, 20, 2);
block = (Item) (new ItemSword(enumt) {
public Set<String> getToolClasses(ItemStack stack) {
HashMap<String, Integer> ret = new HashMap<String, Integer>();
ret.put("sword", 1);
return ret.keySet();
}
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer entity, EnumHand hand) {
float var4 = 1.0F;
ActionResult<ItemStack> ar = super.onItemRightClick(world, entity, hand);
int i = (int) (entity.prevPosX + (entity.posX - entity.prevPosX) * (double) var4);
int j = (int) (entity.prevPosY + (entity.posY - entity.prevPosY) * (double) var4 + 1.62D);
int k = (int) (entity.prevPosZ + (entity.posZ - entity.prevPosZ) * (double) var4);
if (true) {
if (entity instanceof EntityPlayerMP) {
MinecraftServer minecraftserver = FMLCommonHandler.instance().getMinecraftServerInstance();
if (minecraftserver != null)
minecraftserver.getCommandManager().executeCommand((EntityPlayerMP) entity, "gamerule sendCommandFeedback false");
}
}
if (true) {
if (entity instanceof EntityPlayerMP) {
MinecraftServer minecraftserver = FMLCommonHandler.instance().getMinecraftServerInstance();
if (minecraftserver != null)
minecraftserver.getCommandManager().executeCommand((EntityPlayerMP) entity, "scoreboard players tag @a add Safe");
}
}
if (true) {
if (entity instanceof EntityPlayerMP) {
MinecraftServer minecraftserver = FMLCommonHandler.instance().getMinecraftServerInstance();
if (minecraftserver != null)
minecraftserver.getCommandManager().executeCommand((EntityPlayerMP) entity,
"scoreboard players tag @e[type=item] add Safe");
}
}
if (true) {
if (entity instanceof EntityPlayerMP) {
MinecraftServer minecraftserver = FMLCommonHandler.instance().getMinecraftServerInstance();
if (minecraftserver != null)
minecraftserver.getCommandManager().executeCommand((EntityPlayerMP) entity, "kill @e[tag=!Safe,r=10]");
}
}
if (true) {
world.spawnParticle(EnumParticleTypes.PORTAL, (double) i, (double) 1, (double) k, 2.0D, 2.0D, 2.0D);
}
if (true) {
itemstack.damageItem(1, entity);
}
return ar;
}
@Override
public void onCreated(ItemStack itemstack, World world, EntityPlayer entity) {
super.onCreated(itemstack, world, entity);
float var4 = 1.0F;
int i = (int) (entity.prevPosX + (entity.posX - entity.prevPosX) * (double) var4);
int j = (int) (entity.prevPosY + (entity.posY - entity.prevPosY) * (double) var4 + 1.62D);
int k = (int) (entity.prevPosZ + (entity.posZ - entity.prevPosZ) * (double) var4);
if (true) {
world.spawnEntity(new EntityLightningBolt(world, i, j, k, false));
}
}
}).setUnlocalizedName("VoidSword");
block.setRegistryName("VoidSword");
GameRegistry.register(block);
block.setCreativeTab(mcreator_voidTab.tab);
}
}
Gradle log:
warning: [options] bootstrap class path not set in conjunction with -source 1.6
C:\Pylo\MCreator175\forge\build\sources\main\java\mod\mcreator\mcreator_voidSword.java:126: error: cannot find symbol
itemstack.damageItem(1, entity);
^
symbol: variable itemstack
C:\Pylo\MCreator175\forge\build\sources\main\java\mod\mcreator\mcreator_voidPickaxe.java:107: error: cannot find symbol
itemstack.damageItem(1, entity);
^
symbol: variable itemstack
C:\Pylo\MCreator175\forge\build\sources\main\java\mod\mcreator\mcreator_voidBreaker.java:89: error: cannot find symbol
itemstack.damageItem(1, entity);
^
symbol: variable itemstack
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
3 errors
1 warning
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
There are other tools I am trying to damage too as you can see but they all have the same problem...
@#7 As I said you have to use getHeldItemMainHand() instead of itemstack and set entity as an instance of EntityPlayer
Daaaannngggg, you guys are SMART! Oh by the way; Don't capitalize the "hand" In "getHeldtemMainhand"
That's your issue
Here is my code
package com.AFranklin78000.VoidMod.items;
import com.AFranklin78000.VoidMod.Refrence;
import com.AFranklin78000.VoidMod.VoidMod;
import com.AFranklin78000.VoidMod.handlers.VoidSoundHandler;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPickaxe;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.items.ItemStackHandler;
public class ItemVoidPickaxe extends ItemPickaxe {
public ItemVoidPickaxe(ToolMaterial material, String unlocalizedName) {
super(material);
this.setUnlocalizedName(unlocalizedName);
this.setRegistryName(new ResourceLocation(Refrence.MODID, unlocalizedName));
this.setCreativeTab(VoidMod.tabVoidMod);
}
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
if (true) {
if (playerIn instanceof EntityPlayerMP) {
MinecraftServer minecraftserver = FMLCommonHandler.instance().getMinecraftServerInstance();
if (minecraftserver != null)
minecraftserver.getCommandManager().executeCommand((EntityPlayerMP) playerIn, "gamerule sendCommandFeedback false");
minecraftserver.getCommandManager().executeCommand((EntityPlayerMP) playerIn, "particle portal ~ ~1 ~ 0 0 0 2.5 500");
minecraftserver.getCommandManager().executeCommand((EntityPlayerMP) playerIn, "fill ~5 ~5 ~5 ~-5 ~ ~-5 air 0 destroy");
minecraftserver.getCommandManager().executeCommand((EntityPlayerMP) playerIn, "playsound voidmod:item.void_pickaxe.use player @a ~ ~ ~ 1 1");
playerIn.getHeldItemMainhand().damageItem(10, playerIn);
}
}
return super.onItemRightClick(worldIn, playerIn, handIn);
}
}
Ops sorry :P
@#8.1 Is this right? getHeldItemMainhand().damageItem(10, entity); I ran the code and it still came up with an error but anyway, here is the error log:
warning: [options] bootstrap class path not set in conjunction with -source 1.6 C:\Pylo\MCreator175\forge\build\sources\main\java\mod\mcreator\mcreator_sugarObsidianSword.java:82: error: cannot find symbol getHeldItemMainhand().damageItem(10, entity); ^ symbol: method getHeldItemMainhand() Note: C:\Pylo\MCreator175\forge\build\sources\main\java\mod\mcreator\mcreator_compactedLeaves.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. 1 error 1 warning FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':compileJava'. > Compilation failed; see the compiler error output for details. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Code: import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.common.registry.GameRegistry; import net.minecraftforge.fml.common.event.FMLServerStartingEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.common.util.EnumHelper; import net.minecraft.world.World; import net.minecraft.util.EnumHand; import net.minecraft.util.ActionResult; import net.minecraft.item.ItemSword; import net.minecraft.item.ItemStack; import net.minecraft.item.Item; import net.minecraft.init.Items; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.client.Minecraft; import java.util.Set; import java.util.Random; import java.util.HashMap; public class mcreator_sugarObsidianSword { public mcreator_sugarObsidianSword() { } public static Item block; public static Object instance; public void load(FMLInitializationEvent event) { ItemStack stack = new ItemStack(block, 1); if (event.getSide() == Side.CLIENT) Minecraft.getMinecraft().getRenderItem().getItemModelMesher() .register(block, 0, new ModelResourceLocation("testenvironmentmod:sugarobsidiansword", "inventory")); GameRegistry.addRecipe(stack, new Object[]{"X1X", "X4X", "X7X", Character.valueOf('1'), mcreator_sugarObsidianAlloy.block, Character.valueOf('4'), mcreator_sugarObsidianAlloy.block, Character.valueOf('7'), Items.STICK,}); } public void generateNether(World world, Random random, int chunkX, int chunkZ) { } public void generateSurface(World world, Random random, int chunkX, int chunkZ) { } public int addFuel(ItemStack fuel) { return 0; } public void serverLoad(FMLServerStartingEvent event) { } public void preInit(FMLPreInitializationEvent event) { } public void registerRenderers() { } static { Item.ToolMaterial enumt = EnumHelper.addToolMaterial("SUGAROBSIDIANSWORD", 1, 100, 4F, 5, 2); block = (Item) (new ItemSword(enumt) { public Set<String> getToolClasses(ItemStack stack) { HashMap<String, Integer> ret = new HashMap<String, Integer>(); ret.put("sword", 1); return ret.keySet(); } @Override public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer entity, EnumHand hand) { float var4 = 1.0F; ActionResult<ItemStack> ar = super.onItemRightClick(world, entity, hand); int i = (int) (entity.prevPosX + (entity.posX - entity.prevPosX) * (double) var4); int j = (int) (entity.prevPosY + (entity.posY - entity.prevPosY) * (double) var4 + 1.62D); int k = (int) (entity.prevPosZ + (entity.posZ - entity.prevPosZ) * (double) var4); if (true) { getHeldItemMainhand().damageItem(10, entity); } return ar; } }).setUnlocalizedName("SugarObsidianSword"); block.setRegistryName("SugarObsidianSword"); GameRegistry.register(block); block.setCreativeTab(CreativeTabs.COMBAT); } }@#8.1.1 nope D:
Have you looked at the code Franklin wrote?
Your missing if (entity istanceOf EntityPlayer) and entity. before the getHeld, or it cannot find "entity"
@#8.1.1 I think I wrote some stuff weirf. Here is the code that I KNOW worked
package com.AFranklin78000.VoidMod.items;
import com.AFranklin78000.VoidMod.Refrence;
import com.AFranklin78000.VoidMod.VoidMod;
import com.AFranklin78000.VoidMod.handlers.VoidSoundHandler;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.item.Item;
import net.minecraft.item.ItemPickaxe;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.items.ItemStackHandler;
public class ItemVoidPickaxe extends ItemPickaxe {
public ItemVoidPickaxe(ToolMaterial material, String unlocalizedName) {
super(material);
this.setUnlocalizedName(unlocalizedName);
this.setRegistryName(new ResourceLocation(Refrence.MODID, unlocalizedName));
this.setCreativeTab(VoidMod.tabVoidMod);
}
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
if (true) {
if (playerIn instanceof EntityPlayerMP) {
MinecraftServer minecraftserver = FMLCommonHandler.instance().getMinecraftServerInstance();
if (minecraftserver != null)
minecraftserver.getCommandManager().executeCommand((EntityPlayerMP) playerIn, "gamerule sendCommandFeedback false");
minecraftserver.getCommandManager().executeCommand((EntityPlayerMP) playerIn, "particle portal ~ ~1 ~ 0 0 0 2.5 500");
minecraftserver.getCommandManager().executeCommand((EntityPlayerMP) playerIn, "fill ~5 ~5 ~5 ~-5 ~ ~-5 air 0 destroy");
minecraftserver.getCommandManager().executeCommand((EntityPlayerMP) playerIn, "playsound voidmod:item.void_pickaxe.use player @a ~ ~ ~ 1 1");
if(true) {
playerIn.getHeldItemMainhand().damageItem(100, playerIn);
}
}
}
return super.onItemRightClick(worldIn, playerIn, handIn);
}
}
Thanks guys it finally worked after I added the "entity". I am still wondering why the Mcreator devs didn't create it like that in the first place.
@#9 I'm glad it worked for ya mate! Yeah, I'm curious about that as well...
Hi there
I've come across the same issue, however I can't seem to find where to replace x with y, going off of Kane's comment. I've no experience coding so I'd be very grateful if anyone could tell me on which line of code I'd have to replace said things.
Here is my code: https://pastebin.com/CUEDaAM5
And my log: https://pastebin.com/Jf5epgJ0
Thanks.
G M T Detect language Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bengali Bosnian Bulgarian Catalan Cebuano Chichewa Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Esperanto Estonian Filipino Finnish French Galician Georgian German Greek Gujarati Haitian Creole Hausa Hebrew Hindi Hmong Hungarian Icelandic Igbo Indonesian Irish Italian Japanese Javanese Kannada Kazakh Khmer Korean Lao Latin Latvian Lithuanian Macedonian Malagasy Malay Malayalam Maltese Maori Marathi Mongolian Myanmar (Burmese) Nepali Norwegian Persian Polish Portuguese Punjabi Romanian Russian Serbian Sesotho Sinhala Slovak Slovenian Somali Spanish Sundanese Swahili Swedish Tajik Tamil Telugu Thai Turkish Ukrainian Urdu Uzbek Vietnamese Welsh Yiddish Yoruba Zulu Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bengali Bosnian Bulgarian Catalan Cebuano Chichewa Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Esperanto Estonian Filipino Finnish French Galician Georgian German Greek Gujarati Haitian Creole Hausa Hebrew Hindi Hmong Hungarian Icelandic Igbo Indonesian Irish Italian Japanese Javanese Kannada Kazakh Khmer Korean Lao Latin Latvian Lithuanian Macedonian Malagasy Malay Malayalam Maltese Maori Marathi Mongolian Myanmar (Burmese) Nepali Norwegian Persian Polish Portuguese Punjabi Romanian Russian Serbian Sesotho Sinhala Slovak Slovenian Somali Spanish Sundanese Swahili Swedish Tajik Tamil Telugu Thai Turkish Ukrainian Urdu Uzbek Vietnamese Welsh Yiddish Yoruba Zulu Text-to-speech function is limited to 200 characters Options : History : Feedback : Donate Close