Skip to content

Instantly share code, notes, and snippets.

@Lanse505
Created January 8, 2021 18:36
Show Gist options
  • Save Lanse505/70a904b629c68ad2e7008238b5628be3 to your computer and use it in GitHub Desktop.
Save Lanse505/70a904b629c68ad2e7008238b5628be3 to your computer and use it in GitHub Desktop.
public ActionResultType resolveRecipe(ItemUseContext context) {
World world = context.getWorld();
BlockPos blockpos = context.getPos();
PlayerEntity player = context.getPlayer();
ItemStack stack = context.getItem();
BlockState targetedState = world.getBlockState(blockpos);
BlockState hook = net.minecraftforge.event.ForgeEventFactory.onToolUse(targetedState, world, blockpos, player, stack, ToolType.SHOVEL);
if (context.getFace() != Direction.DOWN && world.isAirBlock(blockpos.up())) {
if (to != null) {
world.playSound(player, blockpos, SoundEvents.ITEM_SHOVEL_FLATTEN, SoundCategory.BLOCKS, 1.0F, 1.0F);
if (!world.isRemote() && hook != null) {
if (!targetedState.equals(hook)) {
// If the state is changed after firing the forge hook then use the event provided state
world.setBlockState(blockpos, hook, BlockFlags.DEFAULT_AND_RERENDER);
} else {
// If the state is unchanged after firing the forge hook then use the recipe provided state
if (targetedState.hasProperty(BlockStateProperties.AXIS)) {
BlockState state = to.with(BlockStateProperties.AXIS, targetedState.get(BlockStateProperties.AXIS));
world.setBlockState(blockpos, state, BlockFlags.DEFAULT_AND_RERENDER);
} else {
world.setBlockState(blockpos, to, BlockFlags.DEFAULT_AND_RERENDER);
}
}
if (player != null) {
stack.damageItem(1, player, (playerIn) -> {
player.sendBreakAnimation(context.getHand());
});
}
return ActionResultType.SUCCESS;
}
}
}
return ActionResultType.PASS;
}
public ActionResultType resolveRecipe(ItemUseContext context) {
World world = context.getWorld();
BlockPos blockpos = context.getPos();
PlayerEntity playerentity = context.getPlayer();
ItemStack stack = context.getItem();
BlockState targetedState = world.getBlockState(blockpos);
BlockState hook = net.minecraftforge.event.ForgeEventFactory.onToolUse(targetedState, world, blockpos, playerentity, stack, ToolType.HOE);
if (context.getFace() != Direction.DOWN && world.isAirBlock(blockpos.up())) {
if (to != null) {
world.playSound(playerentity, blockpos, SoundEvents.ITEM_HOE_TILL, SoundCategory.BLOCKS, 1.0F, 1.0F);
if (!world.isRemote && hook != null) {
if (!targetedState.equals(hook)) {
// If the state is changed after firing the forge hook then use the event provided state
world.setBlockState(blockpos, hook, BlockFlags.DEFAULT_AND_RERENDER);
} else {
// If the state is unchanged after firing the forge hook then use the recipe provided state
world.setBlockState(blockpos, to, BlockFlags.DEFAULT_AND_RERENDER);
}
if (playerentity != null) {
stack.damageItem(1, playerentity, (player) -> {
player.sendBreakAnimation(context.getHand());
});
}
return ActionResultType.SUCCESS;
}
}
}
return ActionResultType.PASS;
}
public ActionResultType resolveRecipe(ItemUseContext context) {
World world = context.getWorld();
BlockPos blockpos = context.getPos();
PlayerEntity player = context.getPlayer();
ItemStack stack = context.getItem();
BlockState targetedState = world.getBlockState(blockpos);
BlockState hook = net.minecraftforge.event.ForgeEventFactory.onToolUse(targetedState, world, blockpos, player, stack, ToolType.SHOVEL);
if (context.getFace() != Direction.DOWN && world.isAirBlock(blockpos.up())) {
if (to != null) {
world.playSound(player, blockpos, SoundEvents.ITEM_SHOVEL_FLATTEN, SoundCategory.BLOCKS, 1.0F, 1.0F);
if (!world.isRemote() && hook != null) {
if (!targetedState.equals(hook)) {
// If the state is changed after firing the forge hook then use the event provided state
world.setBlockState(blockpos, hook, BlockFlags.DEFAULT_AND_RERENDER);
} else {
// If the state is unchanged after firing the forge hook then use the recipe provided state
world.setBlockState(blockpos, to, BlockFlags.DEFAULT_AND_RERENDER);
}
if (player != null) {
stack.damageItem(1, player, (playerIn) -> {
player.sendBreakAnimation(context.getHand());
});
}
return ActionResultType.SUCCESS;
}
}
}
return ActionResultType.PASS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment