Check if log is from a tree

Hi!

I would like to check if a log belongs to a tree to implement a tree breaking axe. Any ideas how to do the check? It should prevent destroying a whole house if it is built entirely with logs.

Thanks!

The options I can think of

  • Anytime a tree is generated, give it some kind of datatag (Probably really resource intensive, please don’t do this)
  • Anytime a player places a log, give it some kind of datatag (Better, but still a bit resource intensive)
  • Check if there are leaves nearby. If players build with a combination of leaves and logs this might not always work though. (All vanilla trees except acacia always have leaves on top of the main tree branch, this might help you)
2 Likes

From a ChangeBlockEvent:

			BlockSnapshot leaf = event.getTransactions().get(0).getOriginal();
			if (leaf.getState().getType().equals(BlockTypes.LOG) || leaf.getState().getType().equals(BlockTypes.LEAVES)) {
				if(!(leaf.getCreator().isPresent()))
				{
			restore = taskBuilder.execute(() -> restore(leaf)).async().delayTicks(100).submit(plugin);
				}
			}
		} ```

Its basically creating a block snapshot, and if world gen placed it, no creator would be present... if a player placed it, it would be present. Obviously only trees would have logs... or you can check if the creator is identical to the player trying to break it...
1 Like

I think player grown tree’s have an owner of player.

You are probably right, and I think I might have found that when I last used this code… but even if it gives OP an idea, it is worth sharing.

This is a good idea, also worth checking is the data on the leaves once found. player placed leaves behave differently to natural leaves, and there is blockstates to represent their lack of decaying!

That’s by far the most efficient way of telling.

The other option is just to say screw it, and let them tree-chop man made tree’s that they have permission to.

3 Likes

That’s very good! I looked for such a clever solution.

Thanks to all for the help! :+1:

1 Like