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

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?!

  • Author

According to block.getBlock().isToolEffective(), it requires a string (and is working using strings) but any guide I have seen shows ItemTool() constructor passing in a block set, and it is not working in my method.

  • Author

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.

  • Author

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.

  • Author

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.

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.

  • Author

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).

  • Author

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.

  • Author

Ok it seems I can't do this from 'Block.isToolEffective()'. I'll try just doing getStrVsBlock and see if it is > 1.0f

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

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.