Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.8] custom tool effectiveness


Recommended Posts

Hey, me again, probably getting annoying but I have another issue now. I am using a method...

	public boolean isEffective(ItemStack helditem, Block block) {
	if (helditem.getItem() instanceof ItemTool) {
		Set tooltypes = helditem.getItem().getToolClasses(helditem);

		Iterator<String> i = tooltypes.iterator();

		while (i.hasNext()) {
			if (block.isToolEffective(i.next(), block.getDefaultState())) {
				return true;
			}
		}
	}
	return false;
}

 

In order to easily make an adaptable way to only allow blocks to be destroyed if it is the effective tool, however this gives me a problem, my custom tool I have made a custom tool class so I need to add my own 'effectiveon' Set but no guides I can find help me with this, the only one I find says to use the following....

	public static final Set effectiveon = Sets.newHashSet(new Block[] {
	    Blocks.grass, Blocks.dirt, Blocks.sand, Blocks.gravel, 
	    Blocks.snow_layer, Blocks.farmland});

 

however this passes in blocks where check effectiveness checks strings. What shall I do here please?!

Link to post
Share on other sites

The set of blocks is vanilla's way of doing things. Forge overrides this (kinda). The important methods are

- getDigSpeed

- getHarvestLevel

- getToolClasses

- canHarvestBlock

 

and maybe more. These are patched in by forge and allow for great flexibility. Additionally forge then provides the default implementation so that it falls back on the vanilla code.

 

So, if you interact with tools you should always call these, then you will catch all implementations.

Link to post
Share on other sites

this is what I'm doing, I'm catching it via getDigSpeed, but I want to only affect tools that are specially made for the right blocks (e.g. effective on). but to do that I have to either... use getHavestLevel / canHarvestBlock and check through every single block I want to be effective. Or... add my own 'effective blocks' to the tool, then check isToolEffective.

 

My issue is adding effective blocks to my tool, I need to know the correct string to pass into the Set as it checks strings, but all guides I have found passes in Set<Block>, have tried this and doesn't work.

Link to post
Share on other sites

The String is "pickaxe", "axe", etc.

 

I had a look back and it turns out I am asking something that's impossible. I wanted to add a new "ToolType" and add blocks that this tool is effective against. But I think the only way to do this is change vanilla code and no way to do it using forge hooks.

 

Just encase there is please let me know? basically I want my new tool type "rake" and make some blocks effective to "shovel" and "rake"

 

PS: I really think this is impossible with forge hooks only.

Link to post
Share on other sites

That isn't exactly what I mean. I wanted this code...

if (block.getBlock().isToolEffective(s, block)) {
				return true;
			}

To work on a custom tooltype string. This I don't think is possible.

 

Where do you have this code?  What's the context?

What you're doing is entirely possible, but you can't just insert random code into random places and expect it to make sense.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to post
Share on other sites

this is the event...

     @SubscribeEvent
public void onPlayerMineEvent(PlayerEvent.BreakSpeed event) {

	ItemStack helditem = event.entityPlayer.getCurrentEquippedItem();
	Block block = event.state.getBlock();
	IBlockState blockstate = event.state;
	event.newSpeed = -1;

	if (helditem == null) {

		if (block == Blocks.tallgrass || block == Blocks.double_plant) {
			event.newSpeed = 1f;
		}

	} else if (isEffective(helditem, blockstate)) {

		if (helditem.getItem() instanceof WoodenRake) {
			event.newSpeed = event.originalSpeed * 0.2f;
		} else {
			event.newSpeed = event.originalSpeed * 0.33f;
		}
	}
}

 

which calls this method...

	public boolean isEffective(ItemStack helditem, IBlockState block) {

	if (helditem.getItem().isItemTool(helditem)) {

		Set<String> tooltypes = helditem.getItem().getToolClasses(helditem);

		for (String s : tooltypes) {

			if (block.getBlock().isToolEffective(s, block)) {
				return true;
			}
		}
	}
	return false;
}

 

I wan't to (as all programmers should) avoid as many repeats in code as possible, and use this code to make you only able to break blocks with the correct tools only and nothing else (with a few exceptions like tall grass).

Link to post
Share on other sites

a) Forge has already written that exact code (ForgeHooks.isToolEffective).

b) If you want this to work for your custom tool class, override canHarvestBlock and getDigSpeed to ALSO include your own tool class with list of blocks, in addition to the super-calls.

Link to post
Share on other sites

a) Forge has already written that exact code (ForgeHooks.isToolEffective).

b) If you want this to work for your custom tool class, override canHarvestBlock and getDigSpeed to ALSO include your own tool class with list of blocks, in addition to the super-calls.

 

a) I can't seem to get 'world' from playerEvent.BreakSpeed so I had to make my own to not need that parameter.

 

b) I'm still a little confused about how the classes calculate this all, it seems pickaxes are a special, other then that it calls the getHarvestTool method on the block and checks if that is ok? or something like that. it is a bit inconsistent and hard to follow but I'm learning my way through it.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.