Jump to content

[1.7.2] OnBlockStartBreak Issue :( adding custom exp when pick breaks block


Recommended Posts

Posted

im trying to make my custom pickaxe do something upon breaking a block, i know how to do it from the blocks end but it must be done from the pickaxe class for the sake of my mod.

 

ive tried

public boolean onBlockStartBreak(ItemStack stack, int x, int y, int z, EntityPlayer player)
 {
	 ExperienceStorage.get(player).AddExperience(10);

	 return true;

 	}

 

which does in fact add experience to the game but crashes it in the process, it gives the impression something is wrong with the add experience method in my other class with the crash report -

-- Head --
Stacktrace:
at com.MCR.MinecraftReloaded.Lib.ExperienceStorage.AddExperience(ExperienceStorage.java:75)
at com.MCR.MinecraftReloaded.Tools.ModItemCastrealmPickaxe.onBlockStartBreak(ModItemCastrealmPickaxe.java:52)
at net.minecraft.client.multiplayer.PlayerControllerMP.onPlayerDestroyBlock(PlayerControllerMP.java:132)
at net.minecraft.client.multiplayer.PlayerControllerMP.onPlayerDamageBlock(PlayerControllerMP.java:292)
at net.minecraft.client.Minecraft.func_147115_a(Minecraft.java:1408)

 

but ive added experience with "ExperienceStorage.get(player).AddExperience(10);" plenty of times before in things like respawn events

 }else if(event.entity.worldObj.difficultySetting == EnumDifficulty.NORMAL){
                    if(event.entity instanceof EntityPlayer){
                        EntityPlayer player = (EntityPlayer)event.entity;

                        if(LevelStorage.get(player).GetLevel() > 0)
                        	LevelStorage.get(player).SetLevel(LevelStorage.get(player).GetLevel() - 25);

                    }

 

block break events (in the block class itself)

	/**
 * Called when the block is attempted to be harvested
 */
public void onBlockHarvested(World world, int x, int y, int z, int meta, EntityPlayer player)
{
	ExperienceStorage.get(player).AddExperience(10);
}

 

and playerupdate event

@SubscribeEvent
public void onPlayerUpdate(LivingUpdateEvent event)
{
	if(event.entityLiving instanceof EntityPlayer)
	{
		EntityPlayer player = (EntityPlayer) event.entityLiving;
		if(ExperienceStorage.get(player).GetExperience() > 50)
		{
    		LevelStorage.get(player).AddLevel(1);
    	    ExperienceStorage.get(player).SetExperience(0);
		}
	}

}

 

all of which performed fine.... :/ so it makes me believe that it isnt something to do with my add experience method which is

    public void AddLevel(int Amount)
    {
    	Experience += (Amount * (ExperienceUtils.Multiplier));

    	MinecraftReloaded.packetPipeline.sendTo(new SyncPlayerPropsPacket(player), (EntityPlayerMP) player);
    }

 

ive searched the Items.class for other ways to do it and tried millions of long shots but no luck :/ maybe im not doing something right you can see?

 

to make this clear it had to only add experience when the pickaxe breaks blocks that are minable by pick axes, it cant be an event or method that does it for every block in the game as its part of a leveling system this item in particular increases your mining skill... THANKS IN ADVANCE TO ANYONE WILLING TO SPARE THEIR TIME TO HELP IL BE SURE TO 1+ any helpful posts :)

Posted

Also tried things like

public boolean onBlockStartBreak(ItemStack stack, int x, int y, int z, EntityPlayer player)

{

if(player.inventory.getCurrentItem().getItem() instanceof ModItemCastrealmPickaxe)

        {

ExperienceStorage.get(player).AddExperience(10);

        }

 

return true;

 

}

Posted

Do you want it only for your custom pickaxe, or any pickaxe breaking any pickaxe-breakable block? If it's the latter, then you do want to use an event: BreakEvent.

 

If you check out the BreakEvent code, you'll see how to check if the block is harvestable by the player (i.e. returns true when the player is using the correct tool for the job). You can also check if the player is holding a pickaxe before awarding your xp. These two checks in combination assure you that a) you have a pickaxe and b) the player's pickaxe can harvest the current block that just broke.

 

You may also want to check if the player is silk-harvesting the block; again, look at the BreakEvent's code for an example.

 

In pseudo-code:

@SubscribeEvent
public void onBreakBlock(BreakEvent event) {
if (ForgeHooks.canHarvestBlock(block, player, meta) and player is holding an item and that item is a pickaxe) {
add experience to the player
}
}

Posted

okay ive tried to add this code and returned an error tell me what im missing :/

 

      @SubscribeEvent
      public void onBreakBlock(BreakEvent event, Block block, EntityPlayer player, int meta) {
      if (ForgeHooks.canHarvestBlock(block, player, meta) && player.inventory.getCurrentItem().getItem() instanceof ItemPickaxe) {
    	  LevelStorage.get(player).AddLevel(1);
      }
      
      }

 

ive registered the event bus in the forgepreinit with the other 6 or 7 event busses "MinecraftForge.EVENT_BUS.register(new PickaxeEvent());"

 

and i got this error (game didnt even reach start screen)

 

 

A detailed walkthrough of the error, its code path and all known details is as follows:

---------------------------------------------------------------------------------------

 

-- Head --

Stacktrace:

at cpw.mods.fml.common.eventhandler.EventBus.register(EventBus.java:59)

at com.MCR.MinecraftReloaded.Main.MinecraftReloaded.preInit(MinecraftReloaded.java:184)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at cpw.mods.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:513)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at com.google.common.eventbus.EventHandler.handleEvent(EventHandler.java:74)

at com.google.common.eventbus.SynchronizedEventHandler.handleEvent(SynchronizedEventHandler.java:47)

at com.google.common.eventbus.EventBus.dispatch(EventBus.java:314)

at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:296)

at com.google.common.eventbus.EventBus.post(EventBus.java:267)

at cpw.mods.fml.common.LoadController.sendEventToModContainer(LoadController.java:209)

at cpw.mods.fml.common.LoadController.propogateStateMessage(LoadController.java:188)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at com.google.common.eventbus.EventHandler.handleEvent(EventHandler.java:74)

at com.google.common.eventbus.SynchronizedEventHandler.handleEvent(SynchronizedEventHandler.java:47)

at com.google.common.eventbus.EventBus.dispatch(EventBus.java:314)

at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:296)

at com.google.common.eventbus.EventBus.post(EventBus.java:267)

at cpw.mods.fml.common.LoadController.distributeStateMessage(LoadController.java:119)

at cpw.mods.fml.common.Loader.loadMods(Loader.java:495)

at cpw.mods.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:201)

at net.minecraft.client.Minecraft.startGame(Minecraft.java:522)

 

-- Initialization --

Details:

Stacktrace:

at net.minecraft.client.Minecraft.run(Minecraft.java:892)

at net.minecraft.client.main.Main.main(Main.java:112)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at net.minecraft.launchwrapper.Launch.launch(Launch.java:134)

at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

 

 

 

its something to do with the event bus? should i put the registry somewhere else in my main file? or did i write the event wrong :/

Posted

Event methods can only have a single parameter: the event to which you are subscribing. All other stuff (player, block, etc) must be retrieved some other way, typically they are fields in the event class.

 

In this case, BreakEvent has public fields for the block and metadata, and a getter method to retrieve the player. Ctrl-click on "BreakEvent" to open up the class in Eclipse, or even just expand it in the package explorer to see what variables are available to you and how to get them.

Posted

Thanks ALOT i found that rather than ctrl clicking and going to the event class itself it was easier to type event. and see the parameters in the drop down box :)

 

this is the final event

 

@SubscribeEvent

      public void BreakEvent(BreakEvent event) {

      Block block = (Block)event.block;

    int meta = (int)event.blockMetadata;

    EntityPlayer player = (EntityPlayer)event.getPlayer();

   

 

      if (ForgeHooks.canHarvestBlock(block, player, meta ) && player.inventory.getCurrentItem().getItem() instanceof ItemPickaxe) {

   

      LevelStorage.get(player).AddLevel(1);

     

      }

     

      }

 

 

 

AND it works :D

Posted

ACTUALLY this is one error :/

 

ForgeHooks.canHarvestBlock(block, player, meta ) is noot doing its job, the pickaxe works increasing its level on the normal minable blocks but also increases with stuff like mining wood :/ thats a no no for my mod, any idea what went wrong?

Posted

Some blocks can be harvested by anything, including a player's hands, so those will return true. You can check first to make sure a tool is required to harvest the block, thus ruling out those that can be harvested with anything (or nothing :P)

 

if (block.getMaterial().isToolNotRequired()) {
// don't get xp for this one!
}

// this will crash if the player's not holding anything:
player.inventory.getCurrentItem().getItem()

// be sure to null check first, and I use getHeldItem() because it's shorter to write:
player.getHeldItem() != null  && player.getHeldItem().getItem()

 

Posted

hmm i tried that and it didnt work,

i also tried adding "LevelStorage.get(player).AddLevel(0);" inside the if statement didnt work,

i have however managed to get it to work by checking if the material is wood or not, is there any issues with this way if not awesome :)

if so then how did i use the if statement wrong

 

this is the working way

 	@SubscribeEvent
      public void BreakEvent(BreakEvent event) {
    	  Block block = (Block)event.block;
    	 int meta = (int)event.blockMetadata;
    	 EntityPlayer player = (EntityPlayer)event.getPlayer();
    	 
// Meta meta = (Meta)event.blockMetadata;
    	 if(ForgeHooks.canHarvestBlock(block, player, meta ) && block.getMaterial() == Material.iron || block.getMaterial() == Material.rock && player.getHeldItem() != null  && player.getHeldItem().getItem() instanceof ItemPickaxe) {
    	 
    	  LevelStorage.get(player).AddLevel(1);
      
      }

      
      } 

 

#############################################################

#############################################################

 

this is the way i tried to use the if statement

 

@SubscribeEvent

      public void BreakEvent(BreakEvent event) {

      Block block = (Block)event.block;

    int meta = (int)event.blockMetadata;

    EntityPlayer player = (EntityPlayer)event.getPlayer();

   

// Meta meta = (Meta)event.blockMetadata;

    if(ForgeHooks.canHarvestBlock(block, player, meta ) && player.getHeldItem() != null  && player.getHeldItem().getItem() instanceof ItemPickaxe) {

   

      LevelStorage.get(player).AddLevel(1);

     

      }

    if (block.getMaterial().isToolNotRequired()) {

    //do nothing   

    }

   

    /// or i also tried

    if (block.getMaterial().isToolNotRequired()) {

    LevelStorage.get(player).AddLevel(0);

    }

     

      }

 

 

Posted

Yeah, you didn't use it right :\ Any conditional statements you want to use have to be involved in the logic of the result, otherwise it is totally wasted.

 

Look:

if (can player harvest and player has a pickaxe) {
// here's your result:
add level;
}

if (material doesn't require tool) {
do nothing...
// but your result has already finished...
}

 

Does that make sense? You have to use the if statement as a pre-condition for your result, i.e. put it INSIDE of the other if statement. You could also check if the material does not NOT need a tool, meaning it does need a tool... :P

 

if (can harvest and pickaxe stuff) {
if (!block.getMaterial().isToolNotRequired()) {
// it doesn't not not need a tool: double-negative is positive, so it DOES need a tool
// great! add your xp
}
}

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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