Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I have looked at some other posts but none of them seem to have worked, I have also tried some round-about ways of doing this but again, to no avail. I need to check for the pick having Silk Touch to prevent the mob from spawning when you break the ore with a silk pick.

 

here is my ores code:

 

package me.saxon564.mobores.blocks;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Random;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import me.saxon564.mobores.MobOres;
import me.saxon564.mobores.MobOresReference;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.monster.EntitySilverfish;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;

public class BlockCreeptoniteOre extends Block {

private boolean hasSilkTouch;
private EntityPlayer pPlayer;

public BlockCreeptoniteOre() {

	super(Material.rock);
	setBlockName("creeptoniteOre");
	setBlockTextureName(MobOresReference.MODID + ":" + getUnlocalizedName().substring(5));
	setCreativeTab(MobOres.mobOresTab);
	setHardness(6.5F);
	setResistance(25.0F);
	setLightLevel(0.3F);

}

public void onBlockHarvested(World world, int x, int y, int z, int meta, EntityPlayer player) {

	pPlayer = player;

}

public void onBlockDestroyedByPlayer(World world, int x, int y, int z, int meta)
    {
        if ((!world.isRemote) && (!checkSilk()))
        {
            EntityCreeper entitycreeper = new EntityCreeper(world);
            entitycreeper.setLocationAndAngles((double)x + 0.5D, (double)y, (double)z + 0.5D, 0.0F, 0.0F);
            world.spawnEntityInWorld(entitycreeper);
            entitycreeper.spawnExplosionParticle();
        }

        super.onBlockDestroyedByPlayer(world, x, y, z, 1);
    }

public Item getItemDropped(int par1, Random par2, int par3)
    {
        return MobOres.deadCreeptoniteOre.getItemDropped(par1, par2, par3);
    }

public boolean checkSilk() {
	ItemStack item = pPlayer.inventory.getCurrentItem();
	hasSilkTouch = false;
	if (item != null) {
	   NBTTagList enchants = (NBTTagList) item.stackTagCompound.getTag("StoredEnchantments");
	   for (int i = 0; i < enchants.tagCount(); i++) {
		   NBTTagCompound enchant = ((NBTTagList) enchants).getCompoundTagAt(i);
	      if (enchant.getShort("id") == 33)/*.getTag("id") == 33)*/ {
	         hasSilkTouch = true;
	         break;
	      }
	   }
	   System.out.println(hasSilkTouch);
	}
	return hasSilkTouch;
}

}

 

 

it ALWAYS crashes on this line specifically:

NBTTagList enchants = (NBTTagList) item.stackTagCompound.getTag("StoredEnchantments");

and I have no idea why, since i did take a look at some other bits of code in the game and it uses the same code.

 

Does any one have any idea why it is crashing on that line only?

  • Author

how would you suggest checking if it was null? I used

if (item.stackTagCompound.getTag("StoredEnchantments") != null) {

but it crashed on that check.

 

stacktrace:

 

Stacktrace:
at me.saxon564.mobores.blocks.BlockCreeptoniteOre.onBlockHarvested(BlockCreeptoniteOre.java:50)
at net.minecraft.server.management.ItemInWorldManager.removeBlock(ItemInWorldManager.java:274)
at net.minecraft.server.management.ItemInWorldManager.tryHarvestBlock(ItemInWorldManager.java:328)
at net.minecraft.server.management.ItemInWorldManager.uncheckedTryHarvestBlock(ItemInWorldManager.java:241)
at net.minecraft.network.NetHandlerPlayServer.processPlayerDigging(NetHandlerPlayServer.java:541)
at net.minecraft.network.play.client.C07PacketPlayerDigging.processPacket(C07PacketPlayerDigging.java:64)
at net.minecraft.network.play.client.C07PacketPlayerDigging.processPacket(C07PacketPlayerDigging.java:98)
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:246)

 

  • Author

ok, so that seems to have fixed it not getting past that point, now here is the new code:

 

public void onBlockHarvested(World world, int x, int y, int z, int meta,
		EntityPlayer player) {

	ItemStack item = player.inventory.getCurrentItem();
	hasSilkTouch = false;
	if (item != null) {
		if (item.stackTagCompound != null) {
			if (item.stackTagCompound.getTag("ench") != null) {
				NBTTagList enchants = (NBTTagList) item.stackTagCompound
						.getTag("StoredEnchantments");
					for (int i = 0; i < enchants.tagCount(); i++) {
						NBTTagCompound enchant = ((NBTTagList) enchants)
								.getCompoundTagAt(i);
						if (enchant.getTag("id").getId() == 33) {
							hasSilkTouch = true;
							break;
						}
					}
				}
			}
		}
	}

 

 

now the crashing point is herewith a NPE:

for (int i = 0; i < enchants.tagCount(); i++) {

 

i did try throwing that into an if statement to try and check it, but it crashed in the if statement.

Why are you testing this tag:

if (item.stackTagCompound.getTag("ench") != null) 

and then using this other tag as if you know its contents are non-null:

enchants = (NBTTagList) item.stackTagCompound.getTag("StoredEnchantments")

? The "ench" tag is not the same tag as the "StoredEnchantments" tag. Why not test the tag you are actually setting enchants to?

  • Author

i forgot to change "StoredEnchantments" to ench on the second one thats why, but thats not the problem, its not even getting to that second test

 

EDIT: OHHHHH ok, i wasnt thinking when i first wrote this reply. well that fixed the last crash, now i juts need to figure out why it only reads false when checking the enchantment id.

  • Author

Ok, i finally got it working like its suppose to.

 

here is the "final" code:

 

package me.saxon564.mobores.blocks;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Random;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import me.saxon564.mobores.MobOres;
import me.saxon564.mobores.MobOresReference;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.monster.EntitySilverfish;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;

public class BlockCreeptoniteOre extends Block {

private boolean hasSilkTouch;

public BlockCreeptoniteOre() {

	super(Material.rock);
	setBlockName("creeptoniteOre");
	setBlockTextureName(MobOresReference.MODID + ":"
			+ getUnlocalizedName().substring(5));
	setCreativeTab(MobOres.mobOresTab);
	setHardness(6.5F);
	setResistance(25.0F);
	setLightLevel(0.3F);

}

public void onBlockHarvested(World world, int x, int y, int z, int meta,
		EntityPlayer player) {

	ItemStack item = player.inventory.getCurrentItem();
	hasSilkTouch = false;
	if (item != null) {
		if (item.stackTagCompound != null) {
			if (item.stackTagCompound.getTag("ench") != null) {
				NBTTagList enchants = (NBTTagList) item.stackTagCompound
						.getTag("ench");
				for (int i = 0; i < enchants.tagCount(); i++) {
					NBTTagCompound enchant = ((NBTTagList) enchants)
							.getCompoundTagAt(i);
					if (enchant.getInteger("id") == 33) {
						hasSilkTouch = true;
						break;
					}
				}
			}
		}
	}
}

public void onBlockDestroyedByPlayer(World world, int x, int y, int z,
		int meta) {
	if ((!world.isRemote) && (!hasSilkTouch)) {
		EntityCreeper entitycreeper = new EntityCreeper(world);
		entitycreeper.setLocationAndAngles((double) x + 0.5D, (double) y,
				(double) z + 0.5D, 0.0F, 0.0F);
		world.spawnEntityInWorld(entitycreeper);
		entitycreeper.spawnExplosionParticle();
	}

	super.onBlockDestroyedByPlayer(world, x, y, z, 1);
}

public Item getItemDropped(int par1, Random par2, int par3) {
	return MobOres.deadCreeptoniteOre.getItemDropped(par1, par2, par3);
}

}

 

Guest
This topic is now closed to further replies.

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.